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 | a264d96 | 2020-06-04 22:29:18 +0200 | [diff] [blame] | 48 | #include <haproxy/proxy-t.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 | 1e56f92 | 2020-06-04 23:20:13 +0200 | [diff] [blame] | 51 | #include <haproxy/server-t.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 | 9d6bb5a | 2020-02-06 15:55:41 +0100 | [diff] [blame] | 127 | static int hlua_panic_ljmp(lua_State *L) { WILL_LJMP(longjmp(safe_ljmp_env, 1)); } |
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. */ |
| 204 | static struct proxy socket_proxy; |
| 205 | static struct server socket_tcp; |
| 206 | #ifdef USE_OPENSSL |
| 207 | static struct server socket_ssl; |
| 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; |
| 315 | LIST_ADDQ(&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 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 321 | /* If the common state is set, the stack id is 0, otherwise it is the tid + 1 */ |
| 322 | static inline int fcn_ref_to_stack_id(struct hlua_function *fcn) |
| 323 | { |
| 324 | if (fcn->function_ref[0] == -1) |
| 325 | return tid + 1; |
| 326 | return 0; |
| 327 | } |
| 328 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 329 | /* Used to check an Lua function type in the stack. It creates and |
| 330 | * returns a reference of the function. This function throws an |
| 331 | * error if the rgument is not a "function". |
| 332 | */ |
| 333 | __LJMP unsigned int hlua_checkfunction(lua_State *L, int argno) |
| 334 | { |
| 335 | if (!lua_isfunction(L, argno)) { |
Thierry FOURNIER | fd1e955 | 2018-02-23 18:41:18 +0100 | [diff] [blame] | 336 | 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] | 337 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 338 | } |
| 339 | lua_pushvalue(L, argno); |
| 340 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 341 | } |
| 342 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 343 | /* Return the string that is of the top of the stack. */ |
| 344 | const char *hlua_get_top_error_string(lua_State *L) |
| 345 | { |
| 346 | if (lua_gettop(L) < 1) |
| 347 | return "unknown error"; |
| 348 | if (lua_type(L, -1) != LUA_TSTRING) |
| 349 | return "unknown error"; |
| 350 | return lua_tostring(L, -1); |
| 351 | } |
| 352 | |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 353 | __LJMP static const char *hlua_traceback(lua_State *L) |
| 354 | { |
| 355 | lua_Debug ar; |
| 356 | int level = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 357 | struct buffer *msg = get_trash_chunk(); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 358 | int filled = 0; |
| 359 | |
| 360 | while (lua_getstack(L, level++, &ar)) { |
| 361 | |
| 362 | /* Add separator */ |
| 363 | if (filled) |
| 364 | chunk_appendf(msg, ", "); |
| 365 | filled = 1; |
| 366 | |
| 367 | /* Fill fields: |
| 368 | * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what; |
| 369 | * 'l': fills in the field currentline; |
| 370 | * 'n': fills in the field name and namewhat; |
| 371 | * 't': fills in the field istailcall; |
| 372 | */ |
| 373 | lua_getinfo(L, "Slnt", &ar); |
| 374 | |
| 375 | /* Append code localisation */ |
| 376 | if (ar.currentline > 0) |
| 377 | chunk_appendf(msg, "%s:%d ", ar.short_src, ar.currentline); |
| 378 | else |
| 379 | chunk_appendf(msg, "%s ", ar.short_src); |
| 380 | |
| 381 | /* |
| 382 | * Get function name |
| 383 | * |
| 384 | * if namewhat is no empty, name is defined. |
| 385 | * what contains "Lua" for Lua function, "C" for C function, |
| 386 | * or "main" for main code. |
| 387 | */ |
| 388 | if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */ |
| 389 | chunk_appendf(msg, "%s '%s'", ar.namewhat, ar.name); /* use it */ |
| 390 | |
| 391 | else if (*ar.what == 'm') /* "main", the code is not executed in a function */ |
| 392 | chunk_appendf(msg, "main chunk"); |
| 393 | |
| 394 | else if (*ar.what != 'C') /* for Lua functions, use <file:line> */ |
| 395 | chunk_appendf(msg, "C function line %d", ar.linedefined); |
| 396 | |
| 397 | else /* nothing left... */ |
| 398 | chunk_appendf(msg, "?"); |
| 399 | |
| 400 | |
| 401 | /* Display tailed call */ |
| 402 | if (ar.istailcall) |
| 403 | chunk_appendf(msg, " ..."); |
| 404 | } |
| 405 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 406 | return msg->area; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 410 | /* This function check the number of arguments available in the |
| 411 | * stack. If the number of arguments available is not the same |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 412 | * then <nb> an error is thrown. |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 413 | */ |
| 414 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 415 | { |
| 416 | if (lua_gettop(L) == nb) |
| 417 | return; |
| 418 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 419 | } |
| 420 | |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 421 | /* This function pushes an error string prefixed by the file name |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 422 | * and the line number where the error is encountered. |
| 423 | */ |
| 424 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 425 | { |
| 426 | va_list argp; |
| 427 | va_start(argp, fmt); |
| 428 | luaL_where(L, 1); |
| 429 | lua_pushvfstring(L, fmt, argp); |
| 430 | va_end(argp); |
| 431 | lua_concat(L, 2); |
| 432 | return 1; |
| 433 | } |
| 434 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 435 | /* This functions is used with sample fetch and converters. It |
| 436 | * converts the HAProxy configuration argument in a lua stack |
| 437 | * values. |
| 438 | * |
| 439 | * It takes an array of "arg", and each entry of the array is |
| 440 | * converted and pushed in the LUA stack. |
| 441 | */ |
| 442 | static int hlua_arg2lua(lua_State *L, const struct arg *arg) |
| 443 | { |
| 444 | switch (arg->type) { |
| 445 | case ARGT_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 446 | case ARGT_TIME: |
| 447 | case ARGT_SIZE: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 448 | lua_pushinteger(L, arg->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 449 | break; |
| 450 | |
| 451 | case ARGT_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 452 | lua_pushlstring(L, arg->data.str.area, arg->data.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 453 | break; |
| 454 | |
| 455 | case ARGT_IPV4: |
| 456 | case ARGT_IPV6: |
| 457 | case ARGT_MSK4: |
| 458 | case ARGT_MSK6: |
| 459 | case ARGT_FE: |
| 460 | case ARGT_BE: |
| 461 | case ARGT_TAB: |
| 462 | case ARGT_SRV: |
| 463 | case ARGT_USR: |
| 464 | case ARGT_MAP: |
| 465 | default: |
| 466 | lua_pushnil(L); |
| 467 | break; |
| 468 | } |
| 469 | return 1; |
| 470 | } |
| 471 | |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 472 | /* 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] | 473 | * 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] | 474 | * with sample fetch wrappers. The input arguments are given to the |
| 475 | * lua wrapper and converted as arg list by the function. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 476 | */ |
| 477 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg) |
| 478 | { |
| 479 | switch (lua_type(L, ud)) { |
| 480 | |
| 481 | case LUA_TNUMBER: |
| 482 | case LUA_TBOOLEAN: |
| 483 | arg->type = ARGT_SINT; |
| 484 | arg->data.sint = lua_tointeger(L, ud); |
| 485 | break; |
| 486 | |
| 487 | case LUA_TSTRING: |
| 488 | arg->type = ARGT_STR; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 489 | 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] | 490 | /* 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] | 491 | 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] | 492 | arg->data.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 493 | break; |
| 494 | |
| 495 | case LUA_TUSERDATA: |
| 496 | case LUA_TNIL: |
| 497 | case LUA_TTABLE: |
| 498 | case LUA_TFUNCTION: |
| 499 | case LUA_TTHREAD: |
| 500 | case LUA_TLIGHTUSERDATA: |
| 501 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 502 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 503 | break; |
| 504 | } |
| 505 | return 1; |
| 506 | } |
| 507 | |
| 508 | /* the following functions are used to convert a struct sample |
| 509 | * in Lua type. This useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 510 | * fetches or converters. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 511 | */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 512 | static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 513 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 514 | switch (smp->data.type) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 515 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 516 | case SMP_T_BOOL: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 517 | lua_pushinteger(L, smp->data.u.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 518 | break; |
| 519 | |
| 520 | case SMP_T_BIN: |
| 521 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 522 | 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] | 523 | break; |
| 524 | |
| 525 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 526 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 527 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 528 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 529 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 530 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 531 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 532 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 533 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 534 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 535 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 536 | 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] | 537 | break; |
| 538 | default: |
| 539 | lua_pushnil(L); |
| 540 | break; |
| 541 | } |
| 542 | break; |
| 543 | |
| 544 | case SMP_T_IPV4: |
| 545 | case SMP_T_IPV6: |
| 546 | 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] | 547 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 548 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 549 | 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] | 550 | else |
| 551 | lua_pushnil(L); |
| 552 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 553 | default: |
| 554 | lua_pushnil(L); |
| 555 | break; |
| 556 | } |
| 557 | return 1; |
| 558 | } |
| 559 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 560 | /* the following functions are used to convert a struct sample |
| 561 | * in Lua strings. This is useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 562 | * fetches or converters. |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 563 | */ |
| 564 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
| 565 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 566 | switch (smp->data.type) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 567 | |
| 568 | case SMP_T_BIN: |
| 569 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 570 | lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 571 | break; |
| 572 | |
| 573 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 574 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 575 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 576 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 577 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 578 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 579 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 580 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 581 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 582 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 583 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 584 | 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] | 585 | break; |
| 586 | default: |
| 587 | lua_pushstring(L, ""); |
| 588 | break; |
| 589 | } |
| 590 | break; |
| 591 | |
| 592 | case SMP_T_SINT: |
| 593 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 594 | case SMP_T_IPV4: |
| 595 | case SMP_T_IPV6: |
| 596 | 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] | 597 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 598 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 599 | 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] | 600 | else |
| 601 | lua_pushstring(L, ""); |
| 602 | break; |
| 603 | default: |
| 604 | lua_pushstring(L, ""); |
| 605 | break; |
| 606 | } |
| 607 | return 1; |
| 608 | } |
| 609 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 610 | /* the following functions are used to convert an Lua type in a |
| 611 | * struct sample. This is useful to provide data from a converter |
| 612 | * to the LUA code. |
| 613 | */ |
| 614 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 615 | { |
| 616 | switch (lua_type(L, ud)) { |
| 617 | |
| 618 | case LUA_TNUMBER: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 619 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 620 | smp->data.u.sint = lua_tointeger(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 621 | break; |
| 622 | |
| 623 | |
| 624 | case LUA_TBOOLEAN: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 625 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 626 | smp->data.u.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 627 | break; |
| 628 | |
| 629 | case LUA_TSTRING: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 630 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 631 | smp->flags |= SMP_F_CONST; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 632 | 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] | 633 | /* 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] | 634 | 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] | 635 | smp->data.u.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 636 | break; |
| 637 | |
| 638 | case LUA_TUSERDATA: |
| 639 | case LUA_TNIL: |
| 640 | case LUA_TTABLE: |
| 641 | case LUA_TFUNCTION: |
| 642 | case LUA_TTHREAD: |
| 643 | case LUA_TLIGHTUSERDATA: |
Thierry FOURNIER | 93405e1 | 2015-08-26 14:19:03 +0200 | [diff] [blame] | 644 | case LUA_TNONE: |
| 645 | default: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 646 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 647 | smp->data.u.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 648 | break; |
| 649 | } |
| 650 | return 1; |
| 651 | } |
| 652 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 653 | /* This function check the "argp" built by another conversion function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 654 | * 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] | 655 | * 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] | 656 | * |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 657 | * This function assumes that the argp argument contains ARGM_NBARGS + 1 |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 658 | * entries. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 659 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 660 | __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] | 661 | uint64_t mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 662 | { |
| 663 | int min_arg; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 664 | int i, idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 665 | struct proxy *px; |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 666 | struct userlist *ul; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 667 | struct my_regex *reg; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 668 | const char *msg = NULL; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 669 | char *sname, *pname, *err = NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 670 | |
| 671 | idx = 0; |
| 672 | min_arg = ARGM(mask); |
| 673 | mask >>= ARGM_BITS; |
| 674 | |
| 675 | while (1) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 676 | struct buffer tmp = BUF_NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 677 | |
| 678 | /* Check oversize. */ |
| 679 | if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 680 | msg = "Malformed argument mask"; |
| 681 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | /* Check for mandatory arguments. */ |
| 685 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 686 | if (idx < min_arg) { |
| 687 | |
| 688 | /* If miss other argument than the first one, we return an error. */ |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 689 | if (idx > 0) { |
| 690 | msg = "Mandatory argument expected"; |
| 691 | goto error; |
| 692 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 693 | |
| 694 | /* If first argument have a certain type, some default values |
| 695 | * may be used. See the function smp_resolve_args(). |
| 696 | */ |
| 697 | switch (mask & ARGT_MASK) { |
| 698 | |
| 699 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 700 | if (!(p->cap & PR_CAP_FE)) { |
| 701 | msg = "Mandatory argument expected"; |
| 702 | goto error; |
| 703 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 704 | argp[idx].data.prx = p; |
| 705 | argp[idx].type = ARGT_FE; |
| 706 | argp[idx+1].type = ARGT_STOP; |
| 707 | break; |
| 708 | |
| 709 | case ARGT_BE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 710 | if (!(p->cap & PR_CAP_BE)) { |
| 711 | msg = "Mandatory argument expected"; |
| 712 | goto error; |
| 713 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 714 | argp[idx].data.prx = p; |
| 715 | argp[idx].type = ARGT_BE; |
| 716 | argp[idx+1].type = ARGT_STOP; |
| 717 | break; |
| 718 | |
| 719 | case ARGT_TAB: |
| 720 | argp[idx].data.prx = p; |
| 721 | argp[idx].type = ARGT_TAB; |
| 722 | argp[idx+1].type = ARGT_STOP; |
| 723 | break; |
| 724 | |
| 725 | default: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 726 | msg = "Mandatory argument expected"; |
| 727 | goto error; |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 728 | break; |
| 729 | } |
| 730 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 731 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 732 | } |
| 733 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 734 | /* Check for exceed the number of required argument. */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 735 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 736 | argp[idx].type != ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 737 | msg = "Last argument expected"; |
| 738 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 742 | argp[idx].type == ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 743 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 744 | } |
| 745 | |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 746 | /* Convert some argument types. All string in argp[] are for not |
| 747 | * duplicated yet. |
| 748 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 749 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 750 | case ARGT_SINT: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 751 | if (argp[idx].type != ARGT_SINT) { |
| 752 | msg = "integer expected"; |
| 753 | goto error; |
| 754 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 755 | argp[idx].type = ARGT_SINT; |
| 756 | break; |
| 757 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 758 | case ARGT_TIME: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 759 | if (argp[idx].type != ARGT_SINT) { |
| 760 | msg = "integer expected"; |
| 761 | goto error; |
| 762 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 763 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 764 | break; |
| 765 | |
| 766 | case ARGT_SIZE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 767 | if (argp[idx].type != ARGT_SINT) { |
| 768 | msg = "integer expected"; |
| 769 | goto error; |
| 770 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 771 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 772 | break; |
| 773 | |
| 774 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 775 | if (argp[idx].type != ARGT_STR) { |
| 776 | msg = "string expected"; |
| 777 | goto error; |
| 778 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 779 | 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] | 780 | if (!argp[idx].data.prx) { |
| 781 | msg = "frontend doesn't exist"; |
| 782 | goto error; |
| 783 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 784 | argp[idx].type = ARGT_FE; |
| 785 | break; |
| 786 | |
| 787 | case ARGT_BE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 788 | if (argp[idx].type != ARGT_STR) { |
| 789 | msg = "string expected"; |
| 790 | goto error; |
| 791 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 792 | 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] | 793 | if (!argp[idx].data.prx) { |
| 794 | msg = "backend doesn't exist"; |
| 795 | goto error; |
| 796 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 797 | argp[idx].type = ARGT_BE; |
| 798 | break; |
| 799 | |
| 800 | case ARGT_TAB: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 801 | if (argp[idx].type != ARGT_STR) { |
| 802 | msg = "string expected"; |
| 803 | goto error; |
| 804 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 805 | 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] | 806 | if (!argp[idx].data.t) { |
| 807 | msg = "table doesn't exist"; |
| 808 | goto error; |
| 809 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 810 | argp[idx].type = ARGT_TAB; |
| 811 | break; |
| 812 | |
| 813 | case ARGT_SRV: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 814 | if (argp[idx].type != ARGT_STR) { |
| 815 | msg = "string expected"; |
| 816 | goto error; |
| 817 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 818 | sname = strrchr(argp[idx].data.str.area, '/'); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 819 | if (sname) { |
| 820 | *sname++ = '\0'; |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 821 | pname = argp[idx].data.str.area; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 822 | px = proxy_be_by_name(pname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 823 | if (!px) { |
| 824 | msg = "backend doesn't exist"; |
| 825 | goto error; |
| 826 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 827 | } |
| 828 | else { |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 829 | sname = argp[idx].data.str.area; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 830 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 831 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 832 | argp[idx].data.srv = findserver(px, sname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 833 | if (!argp[idx].data.srv) { |
| 834 | msg = "server doesn't exist"; |
| 835 | goto error; |
| 836 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 837 | argp[idx].type = ARGT_SRV; |
| 838 | break; |
| 839 | |
| 840 | case ARGT_IPV4: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 841 | if (argp[idx].type != ARGT_STR) { |
| 842 | msg = "string expected"; |
| 843 | goto error; |
| 844 | } |
| 845 | if (inet_pton(AF_INET, argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 846 | msg = "invalid IPv4 address"; |
| 847 | goto error; |
| 848 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 849 | argp[idx].type = ARGT_IPV4; |
| 850 | break; |
| 851 | |
| 852 | case ARGT_MSK4: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 853 | if (argp[idx].type == ARGT_SINT) |
| 854 | len2mask4(argp[idx].data.sint, &argp[idx].data.ipv4); |
| 855 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 856 | if (!str2mask(argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 857 | msg = "invalid IPv4 mask"; |
| 858 | goto error; |
| 859 | } |
| 860 | } |
| 861 | else { |
| 862 | msg = "integer or string expected"; |
| 863 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 864 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 865 | argp[idx].type = ARGT_MSK4; |
| 866 | break; |
| 867 | |
| 868 | case ARGT_IPV6: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 869 | if (argp[idx].type != ARGT_STR) { |
| 870 | msg = "string expected"; |
| 871 | goto error; |
| 872 | } |
| 873 | if (inet_pton(AF_INET6, argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 874 | msg = "invalid IPv6 address"; |
| 875 | goto error; |
| 876 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 877 | argp[idx].type = ARGT_IPV6; |
| 878 | break; |
| 879 | |
| 880 | case ARGT_MSK6: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 881 | if (argp[idx].type == ARGT_SINT) |
| 882 | len2mask6(argp[idx].data.sint, &argp[idx].data.ipv6); |
| 883 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 884 | if (!str2mask6(argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 885 | msg = "invalid IPv6 mask"; |
| 886 | goto error; |
| 887 | } |
| 888 | } |
| 889 | else { |
| 890 | msg = "integer or string expected"; |
| 891 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 892 | } |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 893 | argp[idx].type = ARGT_MSK6; |
| 894 | break; |
| 895 | |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 896 | case ARGT_REG: |
| 897 | if (argp[idx].type != ARGT_STR) { |
| 898 | msg = "string expected"; |
| 899 | goto error; |
| 900 | } |
| 901 | reg = regex_comp(argp[idx].data.str.area, !(argp[idx].type_flags & ARGF_REG_ICASE), 1, &err); |
| 902 | if (!reg) { |
| 903 | msg = lua_pushfstring(L, "error compiling regex '%s' : '%s'", |
| 904 | argp[idx].data.str.area, err); |
| 905 | free(err); |
| 906 | goto error; |
| 907 | } |
| 908 | argp[idx].type = ARGT_REG; |
| 909 | argp[idx].data.reg = reg; |
| 910 | break; |
| 911 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 912 | case ARGT_USR: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 913 | if (argp[idx].type != ARGT_STR) { |
| 914 | msg = "string expected"; |
| 915 | goto error; |
| 916 | } |
| 917 | if (p->uri_auth && p->uri_auth->userlist && |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 918 | strcmp(p->uri_auth->userlist->name, argp[idx].data.str.area) == 0) |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 919 | ul = p->uri_auth->userlist; |
| 920 | else |
| 921 | ul = auth_find_userlist(argp[idx].data.str.area); |
| 922 | |
| 923 | if (!ul) { |
| 924 | msg = lua_pushfstring(L, "unable to find userlist '%s'", argp[idx].data.str.area); |
| 925 | goto error; |
| 926 | } |
| 927 | argp[idx].type = ARGT_USR; |
| 928 | argp[idx].data.usr = ul; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 929 | break; |
| 930 | |
| 931 | case ARGT_STR: |
| 932 | if (!chunk_dup(&tmp, &argp[idx].data.str)) { |
| 933 | msg = "unable to duplicate string arg"; |
| 934 | goto error; |
| 935 | } |
| 936 | argp[idx].data.str = tmp; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 937 | break; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 938 | |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 939 | case ARGT_MAP: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 940 | msg = "type not yet supported"; |
| 941 | goto error; |
| 942 | break; |
| 943 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 944 | } |
| 945 | |
| 946 | /* Check for type of argument. */ |
| 947 | if ((mask & ARGT_MASK) != argp[idx].type) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 948 | msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 949 | arg_type_names[(mask & ARGT_MASK)], |
| 950 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 951 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 952 | } |
| 953 | |
| 954 | /* Next argument. */ |
| 955 | mask >>= ARGT_BITS; |
| 956 | idx++; |
| 957 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 958 | return 0; |
| 959 | |
| 960 | error: |
| 961 | for (i = 0; i < idx; i++) { |
| 962 | if (argp[i].type == ARGT_STR) |
| 963 | chunk_destroy(&argp[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 964 | else if (argp[i].type == ARGT_REG) |
| 965 | regex_free(argp[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 966 | } |
| 967 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 968 | return 0; /* Never reached */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 969 | } |
| 970 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 971 | /* |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 972 | * The following functions are used to make correspondence between the the |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 973 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 974 | * |
| 975 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 976 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 977 | */ |
| 978 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 979 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 980 | struct hlua **hlua = lua_getextraspace(L); |
| 981 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 982 | } |
| 983 | static inline void hlua_sethlua(struct hlua *hlua) |
| 984 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 985 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 986 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 987 | } |
| 988 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 989 | /* This function is used to send logs. It try to send on screen (stderr) |
| 990 | * and on the default syslog server. |
| 991 | */ |
| 992 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 993 | { |
| 994 | struct tm tm; |
| 995 | char *p; |
| 996 | |
| 997 | /* Cleanup the log message. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 998 | p = trash.area; |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 999 | for (; *msg != '\0'; msg++, p++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1000 | if (p >= trash.area + trash.size - 1) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 1001 | /* Break the message if exceed the buffer size. */ |
| 1002 | *(p-4) = ' '; |
| 1003 | *(p-3) = '.'; |
| 1004 | *(p-2) = '.'; |
| 1005 | *(p-1) = '.'; |
| 1006 | break; |
| 1007 | } |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 1008 | if (isprint((unsigned char)*msg)) |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1009 | *p = *msg; |
| 1010 | else |
| 1011 | *p = '.'; |
| 1012 | } |
| 1013 | *p = '\0'; |
| 1014 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1015 | send_log(px, level, "%s\n", trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1016 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Christopher Faulet | f98d821 | 2020-10-02 18:13:52 +0200 | [diff] [blame] | 1017 | if (level == LOG_DEBUG && !(global.mode & MODE_DEBUG)) |
| 1018 | return; |
| 1019 | |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 1020 | get_localtime(date.tv_sec, &tm); |
| 1021 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1022 | 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] | 1023 | (int)getpid(), trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1024 | fflush(stderr); |
| 1025 | } |
| 1026 | } |
| 1027 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1028 | /* This function just ensure that the yield will be always |
| 1029 | * returned with a timeout and permit to set some flags |
| 1030 | */ |
| 1031 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1032 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1033 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1034 | struct hlua *hlua; |
| 1035 | |
| 1036 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1037 | hlua = hlua_gethlua(L); |
| 1038 | if (!hlua) { |
| 1039 | return; |
| 1040 | } |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1041 | |
| 1042 | /* Set the wake timeout. If timeout is required, we set |
| 1043 | * the expiration time. |
| 1044 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1045 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1046 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 1047 | hlua->flags |= flags; |
| 1048 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1049 | /* Process the yield. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1050 | MAY_LJMP(lua_yieldk(L, nresults, ctx, k)); |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1051 | } |
| 1052 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1053 | /* This function initialises the Lua environment stored in the stream. |
| 1054 | * 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] | 1055 | * 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] | 1056 | * |
| 1057 | * This function is particular. it initialises a new Lua thread. If the |
| 1058 | * initialisation fails (example: out of memory error), the lua function |
| 1059 | * throws an error (longjmp). |
| 1060 | * |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1061 | * 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] | 1062 | * environment, so we must not initialise it. While the support of |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1063 | * threads appear, the safe environment set a lock to ensure only one |
| 1064 | * Lua execution at a time. If we initialize safe environment in another |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1065 | * safe environment, we have a dead lock. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1066 | * |
| 1067 | * set "already_safe" true if the context is initialized form safe |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1068 | * Lua function. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1069 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1070 | * This function manipulates two Lua stacks: the main and the thread. Only |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 1071 | * the main stack can fail. The thread is not manipulated. This function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1072 | * MUST NOT manipulate the created thread stack state, because it is not |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1073 | * protected against errors thrown by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1074 | */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1075 | 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] | 1076 | { |
| 1077 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1078 | lua->flags = 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1079 | lua->gc_count = 0; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1080 | lua->wake_time = TICK_ETERNITY; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1081 | lua->state_id = state_id; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1082 | LIST_INIT(&lua->com); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1083 | if (!already_safe) { |
| 1084 | if (!SET_SAFE_LJMP_PARENT(lua)) { |
| 1085 | lua->Tref = LUA_REFNIL; |
| 1086 | return 0; |
| 1087 | } |
| 1088 | } |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1089 | lua->T = lua_newthread(hlua_states[state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1090 | if (!lua->T) { |
| 1091 | lua->Tref = LUA_REFNIL; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1092 | if (!already_safe) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1093 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1094 | return 0; |
| 1095 | } |
| 1096 | hlua_sethlua(lua); |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1097 | lua->Tref = luaL_ref(hlua_states[state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1098 | lua->task = task; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1099 | if (!already_safe) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1100 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1101 | return 1; |
| 1102 | } |
| 1103 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1104 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1105 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 1106 | * is not freed. |
| 1107 | */ |
| 1108 | void hlua_ctx_destroy(struct hlua *lua) |
| 1109 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1110 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 1111 | return; |
| 1112 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1113 | if (!lua->T) |
| 1114 | goto end; |
| 1115 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1116 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1117 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1118 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1119 | if (!SET_SAFE_LJMP(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1120 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1121 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1122 | RESET_SAFE_LJMP(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1123 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1124 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1125 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1126 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1127 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1128 | /* Forces a garbage collecting process. If the Lua program is finished |
| 1129 | * without error, we run the GC on the thread pointer. Its freed all |
| 1130 | * the unused memory. |
| 1131 | * If the thread is finnish with an error or is currently yielded, |
| 1132 | * it seems that the GC applied on the thread doesn't clean anything, |
| 1133 | * so e run the GC on the main thread. |
| 1134 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 1135 | * the garbage collection. |
| 1136 | */ |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1137 | if (lua->gc_count) { |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1138 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1139 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1140 | lua_gc(hlua_states[lua->state_id], LUA_GCCOLLECT, 0); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1141 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1142 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1143 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 1144 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1145 | |
| 1146 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1147 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1148 | } |
| 1149 | |
| 1150 | /* This function is used to restore the Lua context when a coroutine |
| 1151 | * fails. This function copy the common memory between old coroutine |
| 1152 | * and the new coroutine. The old coroutine is destroyed, and its |
| 1153 | * replaced by the new coroutine. |
| 1154 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 1155 | * as string error message and it is copied in the new stack. |
| 1156 | */ |
| 1157 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 1158 | { |
| 1159 | lua_State *T; |
| 1160 | int new_ref; |
| 1161 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1162 | /* New Lua coroutine. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1163 | T = lua_newthread(hlua_states[lua->state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1164 | if (!T) |
| 1165 | return 0; |
| 1166 | |
| 1167 | /* Copy last error message. */ |
| 1168 | if (keep_msg) |
| 1169 | lua_xmove(lua->T, T, 1); |
| 1170 | |
| 1171 | /* Copy data between the coroutines. */ |
| 1172 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1173 | lua_xmove(lua->T, T, 1); |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 1174 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */ |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1175 | |
| 1176 | /* Destroy old data. */ |
| 1177 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1178 | |
| 1179 | /* The thread is garbage collected by Lua. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1180 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1181 | |
| 1182 | /* Fill the struct with the new coroutine values. */ |
| 1183 | lua->Mref = new_ref; |
| 1184 | lua->T = T; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1185 | lua->Tref = luaL_ref(hlua_states[lua->state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1186 | |
| 1187 | /* Set context. */ |
| 1188 | hlua_sethlua(lua); |
| 1189 | |
| 1190 | return 1; |
| 1191 | } |
| 1192 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1193 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1194 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1195 | struct hlua *hlua; |
| 1196 | |
| 1197 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1198 | hlua = hlua_gethlua(L); |
| 1199 | if (!hlua) |
| 1200 | return; |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1201 | |
| 1202 | /* Lua cannot yield when its returning from a function, |
| 1203 | * so, we can fix the interrupt hook to 1 instruction, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1204 | * expecting that the function is finished. |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1205 | */ |
| 1206 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1207 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1208 | return; |
| 1209 | } |
| 1210 | |
| 1211 | /* restore the interrupt condition. */ |
| 1212 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1213 | |
| 1214 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1215 | * If the state is not yieldable, trying yield causes an error. |
| 1216 | */ |
| 1217 | if (lua_isyieldable(L)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1218 | MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1219 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 1220 | /* If we cannot yield, update the clock and check the timeout. */ |
| 1221 | tv_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1222 | hlua->run_time += now_ms - hlua->start_time; |
| 1223 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1224 | lua_pushfstring(L, "execution timeout"); |
| 1225 | WILL_LJMP(lua_error(L)); |
| 1226 | } |
| 1227 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1228 | /* Update the start time. */ |
| 1229 | hlua->start_time = now_ms; |
| 1230 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1231 | /* Try to interrupt the process at the end of the current |
| 1232 | * unyieldable function. |
| 1233 | */ |
| 1234 | 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] | 1235 | } |
| 1236 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1237 | /* This function start or resumes the Lua stack execution. If the flag |
| 1238 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1239 | * The function return an error. |
| 1240 | * |
| 1241 | * The function can returns 4 values: |
| 1242 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1243 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1244 | * task wakeup. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1245 | * - 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] | 1246 | * the top of the stack. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1247 | * - HLUA_E_ERR : An error has occurred without error message. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1248 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1249 | * 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] | 1250 | * LUA code. |
| 1251 | */ |
| 1252 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1253 | { |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1254 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 1255 | int nres; |
| 1256 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1257 | int ret; |
| 1258 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1259 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1260 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1261 | /* Initialise run time counter. */ |
| 1262 | if (!HLUA_IS_RUNNING(lua)) |
| 1263 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1264 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1265 | /* Lock the whole Lua execution. This lock must be before the |
| 1266 | * label "resume_execution". |
| 1267 | */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1268 | if (lua->state_id == 0) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1269 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1270 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1271 | resume_execution: |
| 1272 | |
| 1273 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1274 | * instructions. it is used for preventing infinite loops. |
| 1275 | */ |
| 1276 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1277 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1278 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1279 | HLUA_SET_RUN(lua); |
| 1280 | HLUA_CLR_CTRLYIELD(lua); |
| 1281 | HLUA_CLR_WAKERESWR(lua); |
| 1282 | HLUA_CLR_WAKEREQWR(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1283 | |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1284 | /* Update the start time and reset wake_time. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1285 | lua->start_time = now_ms; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1286 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1287 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1288 | /* Call the function. */ |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1289 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1290 | 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] | 1291 | #else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1292 | ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs); |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1293 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1294 | switch (ret) { |
| 1295 | |
| 1296 | case LUA_OK: |
| 1297 | ret = HLUA_E_OK; |
| 1298 | break; |
| 1299 | |
| 1300 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1301 | /* Check if the execution timeout is expired. It it is the case, we |
| 1302 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1303 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1304 | tv_update_date(0, 1); |
| 1305 | lua->run_time += now_ms - lua->start_time; |
| 1306 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1307 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1308 | ret = HLUA_E_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1309 | break; |
| 1310 | } |
| 1311 | /* Process the forced yield. if the general yield is not allowed or |
| 1312 | * if no task were associated this the current Lua execution |
| 1313 | * coroutine, we resume the execution. Else we want to return in the |
| 1314 | * scheduler and we want to be waked up again, to continue the |
| 1315 | * current Lua execution. So we schedule our own task. |
| 1316 | */ |
| 1317 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1318 | if (!yield_allowed || !lua->task) |
| 1319 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1320 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1321 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1322 | if (!yield_allowed) { |
| 1323 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1324 | ret = HLUA_E_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1325 | break; |
| 1326 | } |
| 1327 | ret = HLUA_E_AGAIN; |
| 1328 | break; |
| 1329 | |
| 1330 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1331 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1332 | /* Special exit case. The traditional exit is returned as an error |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1333 | * because the errors ares the only one mean to return immediately |
| 1334 | * from and lua execution. |
| 1335 | */ |
| 1336 | if (lua->flags & HLUA_EXIT) { |
| 1337 | ret = HLUA_E_OK; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 1338 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1339 | break; |
| 1340 | } |
| 1341 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1342 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1343 | if (!lua_checkstack(lua->T, 1)) { |
| 1344 | ret = HLUA_E_ERR; |
| 1345 | break; |
| 1346 | } |
| 1347 | msg = lua_tostring(lua->T, -1); |
| 1348 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1349 | lua_pop(lua->T, 1); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1350 | trace = hlua_traceback(lua->T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1351 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1352 | 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] | 1353 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1354 | 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] | 1355 | ret = HLUA_E_ERRMSG; |
| 1356 | break; |
| 1357 | |
| 1358 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1359 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1360 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1361 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1362 | break; |
| 1363 | |
| 1364 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1365 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1366 | if (!lua_checkstack(lua->T, 1)) { |
| 1367 | ret = HLUA_E_ERR; |
| 1368 | break; |
| 1369 | } |
| 1370 | msg = lua_tostring(lua->T, -1); |
| 1371 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1372 | lua_pop(lua->T, 1); |
| 1373 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1374 | 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] | 1375 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1376 | 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] | 1377 | ret = HLUA_E_ERRMSG; |
| 1378 | break; |
| 1379 | |
| 1380 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1381 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1382 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1383 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1384 | break; |
| 1385 | } |
| 1386 | |
| 1387 | switch (ret) { |
| 1388 | case HLUA_E_AGAIN: |
| 1389 | break; |
| 1390 | |
| 1391 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1392 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1393 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1394 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1395 | break; |
| 1396 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1397 | case HLUA_E_ETMOUT: |
| 1398 | case HLUA_E_NOMEM: |
| 1399 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1400 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1401 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1402 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1403 | hlua_ctx_renew(lua, 0); |
| 1404 | break; |
| 1405 | |
| 1406 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1407 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1408 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1409 | break; |
| 1410 | } |
| 1411 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1412 | /* This is the main exit point, remove the Lua lock. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1413 | if (lua->state_id == 0) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1414 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1415 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1416 | return ret; |
| 1417 | } |
| 1418 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1419 | /* This function exit the current code. */ |
| 1420 | __LJMP static int hlua_done(lua_State *L) |
| 1421 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1422 | struct hlua *hlua; |
| 1423 | |
| 1424 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1425 | hlua = hlua_gethlua(L); |
| 1426 | if (!hlua) |
| 1427 | return 0; |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1428 | |
| 1429 | hlua->flags |= HLUA_EXIT; |
| 1430 | WILL_LJMP(lua_error(L)); |
| 1431 | |
| 1432 | return 0; |
| 1433 | } |
| 1434 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1435 | /* This function is an LUA binding. It provides a function |
| 1436 | * for deleting ACL from a referenced ACL file. |
| 1437 | */ |
| 1438 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1439 | { |
| 1440 | const char *name; |
| 1441 | const char *key; |
| 1442 | struct pat_ref *ref; |
| 1443 | |
| 1444 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1445 | |
| 1446 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1447 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1448 | |
| 1449 | ref = pat_ref_lookup(name); |
| 1450 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1451 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1452 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1453 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1454 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1455 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1456 | return 0; |
| 1457 | } |
| 1458 | |
| 1459 | /* This function is an LUA binding. It provides a function |
| 1460 | * for deleting map entry from a referenced map file. |
| 1461 | */ |
| 1462 | static int hlua_del_map(lua_State *L) |
| 1463 | { |
| 1464 | const char *name; |
| 1465 | const char *key; |
| 1466 | struct pat_ref *ref; |
| 1467 | |
| 1468 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1469 | |
| 1470 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1471 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1472 | |
| 1473 | ref = pat_ref_lookup(name); |
| 1474 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1475 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1476 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1477 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1478 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1479 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1480 | return 0; |
| 1481 | } |
| 1482 | |
| 1483 | /* This function is an LUA binding. It provides a function |
| 1484 | * for adding ACL pattern from a referenced ACL file. |
| 1485 | */ |
| 1486 | static int hlua_add_acl(lua_State *L) |
| 1487 | { |
| 1488 | const char *name; |
| 1489 | const char *key; |
| 1490 | struct pat_ref *ref; |
| 1491 | |
| 1492 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1493 | |
| 1494 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1495 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1496 | |
| 1497 | ref = pat_ref_lookup(name); |
| 1498 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1499 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1500 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1501 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1502 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1503 | pat_ref_add(ref, key, NULL, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1504 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1505 | return 0; |
| 1506 | } |
| 1507 | |
| 1508 | /* This function is an LUA binding. It provides a function |
| 1509 | * for setting map pattern and sample from a referenced map |
| 1510 | * file. |
| 1511 | */ |
| 1512 | static int hlua_set_map(lua_State *L) |
| 1513 | { |
| 1514 | const char *name; |
| 1515 | const char *key; |
| 1516 | const char *value; |
| 1517 | struct pat_ref *ref; |
| 1518 | |
| 1519 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1520 | |
| 1521 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1522 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1523 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1524 | |
| 1525 | ref = pat_ref_lookup(name); |
| 1526 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1527 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1528 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1529 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1530 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1531 | pat_ref_set(ref, key, value, NULL); |
| 1532 | else |
| 1533 | pat_ref_add(ref, key, value, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1534 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1535 | return 0; |
| 1536 | } |
| 1537 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1538 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1539 | * 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] | 1540 | * metatable have an original version registered in the global context with |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1541 | * the name of the object (_G[<name>] = <metable> ). |
| 1542 | * |
| 1543 | * A metable is a table that modify the standard behavior of a standard |
| 1544 | * access to the associated data. The entries of this new metatable are |
| 1545 | * defined as is: |
| 1546 | * |
| 1547 | * http://lua-users.org/wiki/MetatableEvents |
| 1548 | * |
| 1549 | * __index |
| 1550 | * |
| 1551 | * we access an absent field in a table, the result is nil. This is |
| 1552 | * true, but it is not the whole truth. Actually, such access triggers |
| 1553 | * the interpreter to look for an __index metamethod: If there is no |
| 1554 | * such method, as usually happens, then the access results in nil; |
| 1555 | * otherwise, the metamethod will provide the result. |
| 1556 | * |
| 1557 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1558 | * the key does not appear in the table, but the metatable has an __index |
| 1559 | * property: |
| 1560 | * |
| 1561 | * - if the value is a function, the function is called, passing in the |
| 1562 | * table and the key; the return value of that function is returned as |
| 1563 | * the result. |
| 1564 | * |
| 1565 | * - if the value is another table, the value of the key in that table is |
| 1566 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1567 | * table's metatable has an __index property, then it continues on up) |
| 1568 | * |
| 1569 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1570 | * |
| 1571 | * http://www.lua.org/pil/13.4.1.html |
| 1572 | * |
| 1573 | * __newindex |
| 1574 | * |
| 1575 | * Like __index, but control property assignment. |
| 1576 | * |
| 1577 | * __mode - Control weak references. A string value with one or both |
| 1578 | * of the characters 'k' and 'v' which specifies that the the |
| 1579 | * keys and/or values in the table are weak references. |
| 1580 | * |
| 1581 | * __call - Treat a table like a function. When a table is followed by |
| 1582 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1583 | * a __call key pointing to a function, that function is invoked |
| 1584 | * (passing any specified arguments) and the return value is |
| 1585 | * returned. |
| 1586 | * |
| 1587 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1588 | * called, if the metatable for myTable has a __metatable |
| 1589 | * key, the value of that key is returned instead of the |
| 1590 | * actual metatable. |
| 1591 | * |
| 1592 | * __tostring - Control string representation. When the builtin |
| 1593 | * "tostring( myTable )" function is called, if the metatable |
| 1594 | * for myTable has a __tostring property set to a function, |
| 1595 | * that function is invoked (passing myTable to it) and the |
| 1596 | * return value is used as the string representation. |
| 1597 | * |
| 1598 | * __len - Control table length. When the table length is requested using |
| 1599 | * the length operator ( '#' ), if the metatable for myTable has |
| 1600 | * a __len key pointing to a function, that function is invoked |
| 1601 | * (passing myTable to it) and the return value used as the value |
| 1602 | * of "#myTable". |
| 1603 | * |
| 1604 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1605 | * collected, if the metatable has a __gc field pointing to a |
| 1606 | * function, that function is first invoked, passing the userdata |
| 1607 | * to it. The __gc metamethod is not called for tables. |
| 1608 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1609 | * |
| 1610 | * Special metamethods for redefining standard operators: |
| 1611 | * http://www.lua.org/pil/13.1.html |
| 1612 | * |
| 1613 | * __add "+" |
| 1614 | * __sub "-" |
| 1615 | * __mul "*" |
| 1616 | * __div "/" |
| 1617 | * __unm "!" |
| 1618 | * __pow "^" |
| 1619 | * __concat ".." |
| 1620 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1621 | * Special methods for redefining standard relations |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1622 | * http://www.lua.org/pil/13.2.html |
| 1623 | * |
| 1624 | * __eq "==" |
| 1625 | * __lt "<" |
| 1626 | * __le "<=" |
| 1627 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1628 | |
| 1629 | /* |
| 1630 | * |
| 1631 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1632 | * Class Map |
| 1633 | * |
| 1634 | * |
| 1635 | */ |
| 1636 | |
| 1637 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1638 | * a class session, otherwise it throws an error. |
| 1639 | */ |
| 1640 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1641 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1642 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1643 | } |
| 1644 | |
| 1645 | /* This function is the map constructor. It don't need |
| 1646 | * the class Map object. It creates and return a new Map |
| 1647 | * object. It must be called only during "body" or "init" |
| 1648 | * context because it process some filesystem accesses. |
| 1649 | */ |
| 1650 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1651 | { |
| 1652 | const char *fn; |
| 1653 | int match = PAT_MATCH_STR; |
| 1654 | struct sample_conv conv; |
| 1655 | const char *file = ""; |
| 1656 | int line = 0; |
| 1657 | lua_Debug ar; |
| 1658 | char *err = NULL; |
| 1659 | struct arg args[2]; |
| 1660 | |
| 1661 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1662 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1663 | |
| 1664 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1665 | |
| 1666 | if (lua_gettop(L) >= 2) { |
| 1667 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1668 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1669 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1670 | } |
| 1671 | |
| 1672 | /* Get Lua filename and line number. */ |
| 1673 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1674 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1675 | if (ar.currentline > 0) { /* is there info? */ |
| 1676 | file = ar.short_src; |
| 1677 | line = ar.currentline; |
| 1678 | } |
| 1679 | } |
| 1680 | |
| 1681 | /* fill fake sample_conv struct. */ |
| 1682 | conv.kw = ""; /* unused. */ |
| 1683 | conv.process = NULL; /* unused. */ |
| 1684 | conv.arg_mask = 0; /* unused. */ |
| 1685 | conv.val_args = NULL; /* unused. */ |
| 1686 | conv.out_type = SMP_T_STR; |
| 1687 | conv.private = (void *)(long)match; |
| 1688 | switch (match) { |
| 1689 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1690 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1691 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1692 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1693 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1694 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1695 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1696 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1697 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1698 | default: |
| 1699 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1700 | } |
| 1701 | |
| 1702 | /* fill fake args. */ |
| 1703 | args[0].type = ARGT_STR; |
Christopher Faulet | 73292e9 | 2020-08-06 08:40:09 +0200 | [diff] [blame] | 1704 | args[0].data.str.area = strdup(fn); |
| 1705 | args[0].data.str.data = strlen(fn); |
| 1706 | args[0].data.str.size = args[0].data.str.data+1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1707 | args[1].type = ARGT_STOP; |
| 1708 | |
| 1709 | /* load the map. */ |
| 1710 | if (!sample_load_map(args, &conv, file, line, &err)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1711 | /* error case: we can't use luaL_error because we must |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1712 | * free the err variable. |
| 1713 | */ |
| 1714 | luaL_where(L, 1); |
| 1715 | lua_pushfstring(L, "'new': %s.", err); |
| 1716 | lua_concat(L, 2); |
| 1717 | free(err); |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 1718 | chunk_destroy(&args[0].data.str); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1719 | WILL_LJMP(lua_error(L)); |
| 1720 | } |
| 1721 | |
| 1722 | /* create the lua object. */ |
| 1723 | lua_newtable(L); |
| 1724 | lua_pushlightuserdata(L, args[0].data.map); |
| 1725 | lua_rawseti(L, -2, 0); |
| 1726 | |
| 1727 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1728 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1729 | lua_setmetatable(L, -2); |
| 1730 | |
| 1731 | |
| 1732 | return 1; |
| 1733 | } |
| 1734 | |
| 1735 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1736 | { |
| 1737 | struct map_descriptor *desc; |
| 1738 | struct pattern *pat; |
| 1739 | struct sample smp; |
| 1740 | |
| 1741 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1742 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1743 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1744 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1745 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1746 | } |
| 1747 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1748 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1749 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1750 | 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] | 1751 | smp.data.u.str.size = smp.data.u.str.data + 1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1752 | } |
| 1753 | |
| 1754 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1755 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1756 | if (str) |
| 1757 | lua_pushstring(L, ""); |
| 1758 | else |
| 1759 | lua_pushnil(L); |
| 1760 | return 1; |
| 1761 | } |
| 1762 | |
| 1763 | /* 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] | 1764 | 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] | 1765 | return 1; |
| 1766 | } |
| 1767 | |
| 1768 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1769 | { |
| 1770 | return _hlua_map_lookup(L, 0); |
| 1771 | } |
| 1772 | |
| 1773 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1774 | { |
| 1775 | return _hlua_map_lookup(L, 1); |
| 1776 | } |
| 1777 | |
| 1778 | /* |
| 1779 | * |
| 1780 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1781 | * Class Socket |
| 1782 | * |
| 1783 | * |
| 1784 | */ |
| 1785 | |
| 1786 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1787 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1788 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1789 | } |
| 1790 | |
| 1791 | /* 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] | 1792 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1793 | * received. |
| 1794 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1795 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1796 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1797 | struct stream_interface *si = appctx->owner; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1798 | |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1799 | if (appctx->ctx.hlua_cosocket.die) { |
| 1800 | si_shutw(si); |
| 1801 | si_shutr(si); |
| 1802 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1803 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1804 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1805 | stream_shutdown(si_strm(si), SF_ERR_KILLED); |
| 1806 | } |
| 1807 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1808 | /* If we can't write, wakeup the pending write signals. */ |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1809 | if (channel_output_closed(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1810 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1811 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1812 | /* If we can't read, wakeup the pending read signals. */ |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1813 | if (channel_input_closed(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1814 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1815 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1816 | /* if the connection is not established, inform the stream that we want |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1817 | * to be notified whenever the connection completes. |
| 1818 | */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1819 | if (si_opposite(si)->state < SI_ST_EST) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 1820 | si_cant_get(si); |
Willy Tarreau | 12c2423 | 2018-12-06 15:29:50 +0100 | [diff] [blame] | 1821 | si_rx_conn_blk(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1822 | si_rx_endp_more(si); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1823 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1824 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1825 | |
| 1826 | /* This function is called after the connect. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1827 | appctx->ctx.hlua_cosocket.connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1828 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1829 | /* 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] | 1830 | if (channel_may_recv(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1831 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1832 | |
| 1833 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1834 | if (!channel_is_empty(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1835 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1836 | |
| 1837 | /* Some data were injected in the buffer, notify the stream |
| 1838 | * interface. |
| 1839 | */ |
| 1840 | if (!channel_is_empty(si_ic(si))) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 1841 | si_update(si); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1842 | |
| 1843 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1844 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1845 | */ |
| 1846 | if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write)) |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1847 | si_rx_endp_more(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1848 | } |
| 1849 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1850 | /* This function is called when the "struct stream" is destroyed. |
| 1851 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1852 | * Wake all the pending signals. |
| 1853 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1854 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1855 | { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1856 | struct xref *peer; |
| 1857 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1858 | /* Remove my link in the original object. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1859 | peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref); |
| 1860 | if (peer) |
| 1861 | xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1862 | |
| 1863 | /* Wake all the task waiting for me. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1864 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1865 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1866 | } |
| 1867 | |
| 1868 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1869 | * uses this object. If the stream does not exists, just quit. |
| 1870 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1871 | * pending signal can rest in the read and write lists. destroy |
| 1872 | * it. |
| 1873 | */ |
| 1874 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1875 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1876 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1877 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1878 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1879 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1880 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 1881 | |
| 1882 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1883 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1884 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1885 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1886 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1887 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1888 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1889 | appctx->ctx.hlua_cosocket.die = 1; |
| 1890 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1891 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1892 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1893 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1894 | return 0; |
| 1895 | } |
| 1896 | |
| 1897 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1898 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1899 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1900 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1901 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1902 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1903 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1904 | struct xref *peer; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1905 | struct hlua *hlua; |
| 1906 | |
| 1907 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1908 | hlua = hlua_gethlua(L); |
| 1909 | if (!hlua) |
| 1910 | return 0; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1911 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1912 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1913 | |
| 1914 | /* Check if we run on the same thread than the xreator thread. |
| 1915 | * We cannot access to the socket if the thread is different. |
| 1916 | */ |
| 1917 | if (socket->tid != tid) |
| 1918 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1919 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1920 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1921 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1922 | return 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1923 | |
| 1924 | hlua->gc_count--; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1925 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1926 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1927 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1928 | appctx->ctx.hlua_cosocket.die = 1; |
| 1929 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1930 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1931 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1932 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1933 | return 0; |
| 1934 | } |
| 1935 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1936 | /* The close function calls close_helper. |
| 1937 | */ |
| 1938 | __LJMP static int hlua_socket_close(lua_State *L) |
| 1939 | { |
| 1940 | MAY_LJMP(check_args(L, 1, "close")); |
| 1941 | return hlua_socket_close_helper(L); |
| 1942 | } |
| 1943 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1944 | /* This Lua function assumes that the stack contain three parameters. |
| 1945 | * 1 - USERDATA containing a struct socket |
| 1946 | * 2 - INTEGER with values of the macro defined below |
| 1947 | * If the integer is -1, we must read at most one line. |
| 1948 | * If the integer is -2, we ust read all the data until the |
| 1949 | * end of the stream. |
| 1950 | * If the integer is positive value, we must read a number of |
| 1951 | * bytes corresponding to this value. |
| 1952 | */ |
| 1953 | #define HLSR_READ_LINE (-1) |
| 1954 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1955 | __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] | 1956 | { |
| 1957 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1958 | int wanted = lua_tointeger(L, 2); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1959 | struct hlua *hlua; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1960 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1961 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1962 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1963 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1964 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1965 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1966 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1967 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1968 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1969 | struct stream_interface *si; |
| 1970 | struct stream *s; |
| 1971 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1972 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1973 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1974 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1975 | hlua = hlua_gethlua(L); |
| 1976 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1977 | /* Check if this lua stack is schedulable. */ |
| 1978 | if (!hlua || !hlua->task) |
| 1979 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 1980 | "'frontend', 'backend' or 'task'")); |
| 1981 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1982 | /* Check if we run on the same thread than the xreator thread. |
| 1983 | * We cannot access to the socket if the thread is different. |
| 1984 | */ |
| 1985 | if (socket->tid != tid) |
| 1986 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1987 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1988 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1989 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1990 | if (!peer) |
| 1991 | goto no_peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1992 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 1993 | si = appctx->owner; |
| 1994 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1995 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1996 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1997 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1998 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1999 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2000 | if (nblk < 0) /* Connection close. */ |
| 2001 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2002 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2003 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 2004 | |
| 2005 | /* remove final \r\n. */ |
| 2006 | if (nblk == 1) { |
| 2007 | if (blk1[len1-1] == '\n') { |
| 2008 | len1--; |
| 2009 | skip_at_end++; |
| 2010 | if (blk1[len1-1] == '\r') { |
| 2011 | len1--; |
| 2012 | skip_at_end++; |
| 2013 | } |
| 2014 | } |
| 2015 | } |
| 2016 | else { |
| 2017 | if (blk2[len2-1] == '\n') { |
| 2018 | len2--; |
| 2019 | skip_at_end++; |
| 2020 | if (blk2[len2-1] == '\r') { |
| 2021 | len2--; |
| 2022 | skip_at_end++; |
| 2023 | } |
| 2024 | } |
| 2025 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2026 | } |
| 2027 | |
| 2028 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2029 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2030 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2031 | if (nblk < 0) /* Connection close. */ |
| 2032 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2033 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2034 | goto connection_empty; |
| 2035 | } |
| 2036 | |
| 2037 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2038 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2039 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2040 | if (nblk < 0) /* Connection close. */ |
| 2041 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2042 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2043 | goto connection_empty; |
| 2044 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2045 | missing_bytes = wanted - socket->b.n; |
| 2046 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2047 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2048 | len1 = missing_bytes; |
| 2049 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 2050 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2051 | } |
| 2052 | |
| 2053 | len = len1; |
| 2054 | |
| 2055 | luaL_addlstring(&socket->b, blk1, len1); |
| 2056 | if (nblk == 2) { |
| 2057 | len += len2; |
| 2058 | luaL_addlstring(&socket->b, blk2, len2); |
| 2059 | } |
| 2060 | |
| 2061 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2062 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2063 | |
| 2064 | /* Don't wait anything. */ |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 2065 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2066 | |
| 2067 | /* If the pattern reclaim to read all the data |
| 2068 | * in the connection, got out. |
| 2069 | */ |
| 2070 | if (wanted == HLSR_READ_ALL) |
| 2071 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2072 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2073 | goto connection_empty; |
| 2074 | |
| 2075 | /* Return result. */ |
| 2076 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2077 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2078 | return 1; |
| 2079 | |
| 2080 | connection_closed: |
| 2081 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2082 | xref_unlock(&socket->xref, peer); |
| 2083 | |
| 2084 | no_peer: |
| 2085 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2086 | /* If the buffer containds data. */ |
| 2087 | if (socket->b.n > 0) { |
| 2088 | luaL_pushresult(&socket->b); |
| 2089 | return 1; |
| 2090 | } |
| 2091 | lua_pushnil(L); |
| 2092 | lua_pushstring(L, "connection closed."); |
| 2093 | return 2; |
| 2094 | |
| 2095 | connection_empty: |
| 2096 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2097 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) { |
| 2098 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2099 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2100 | } |
| 2101 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2102 | 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] | 2103 | return 0; |
| 2104 | } |
| 2105 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2106 | /* This Lua function gets two parameters. The first one can be string |
| 2107 | * or a number. If the string is "*l", the user requires one line. If |
| 2108 | * 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] | 2109 | * If the value is a number, the user require a number of bytes equal |
| 2110 | * to the value. The default value is "*l" (a line). |
| 2111 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2112 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2113 | * integer takes this values: |
| 2114 | * -1 : read a line |
| 2115 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2116 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2117 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2118 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2119 | * concatenated with the read data. |
| 2120 | */ |
| 2121 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 2122 | { |
| 2123 | int wanted = HLSR_READ_LINE; |
| 2124 | const char *pattern; |
| 2125 | int type; |
| 2126 | char *error; |
| 2127 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2128 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2129 | |
| 2130 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 2131 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 2132 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2133 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2134 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2135 | /* Check if we run on the same thread than the xreator thread. |
| 2136 | * We cannot access to the socket if the thread is different. |
| 2137 | */ |
| 2138 | if (socket->tid != tid) |
| 2139 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2140 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2141 | /* check for pattern. */ |
| 2142 | if (lua_gettop(L) >= 2) { |
| 2143 | type = lua_type(L, 2); |
| 2144 | if (type == LUA_TSTRING) { |
| 2145 | pattern = lua_tostring(L, 2); |
| 2146 | if (strcmp(pattern, "*a") == 0) |
| 2147 | wanted = HLSR_READ_ALL; |
| 2148 | else if (strcmp(pattern, "*l") == 0) |
| 2149 | wanted = HLSR_READ_LINE; |
| 2150 | else { |
| 2151 | wanted = strtoll(pattern, &error, 10); |
| 2152 | if (*error != '\0') |
| 2153 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 2154 | } |
| 2155 | } |
| 2156 | else if (type == LUA_TNUMBER) { |
| 2157 | wanted = lua_tointeger(L, 2); |
| 2158 | if (wanted < 0) |
| 2159 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 2160 | } |
| 2161 | } |
| 2162 | |
| 2163 | /* Set pattern. */ |
| 2164 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 2165 | |
| 2166 | /* Check if we would replace the top by itself. */ |
| 2167 | if (lua_gettop(L) != 2) |
| 2168 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2169 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2170 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2171 | luaL_buffinit(L, &socket->b); |
| 2172 | |
| 2173 | /* Check prefix. */ |
| 2174 | if (lua_gettop(L) >= 3) { |
| 2175 | if (lua_type(L, 3) != LUA_TSTRING) |
| 2176 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 2177 | pattern = lua_tolstring(L, 3, &len); |
| 2178 | luaL_addlstring(&socket->b, pattern, len); |
| 2179 | } |
| 2180 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2181 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2182 | } |
| 2183 | |
| 2184 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 2185 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2186 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2187 | 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] | 2188 | { |
| 2189 | struct hlua_socket *socket; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2190 | struct hlua *hlua; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2191 | struct appctx *appctx; |
| 2192 | size_t buf_len; |
| 2193 | const char *buf; |
| 2194 | int len; |
| 2195 | int send_len; |
| 2196 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2197 | struct xref *peer; |
| 2198 | struct stream_interface *si; |
| 2199 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2200 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2201 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2202 | hlua = hlua_gethlua(L); |
| 2203 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2204 | /* Check if this lua stack is schedulable. */ |
| 2205 | if (!hlua || !hlua->task) |
| 2206 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 2207 | "'frontend', 'backend' or 'task'")); |
| 2208 | |
| 2209 | /* Get object */ |
| 2210 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2211 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2212 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2213 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2214 | /* Check if we run on the same thread than the xreator thread. |
| 2215 | * We cannot access to the socket if the thread is different. |
| 2216 | */ |
| 2217 | if (socket->tid != tid) |
| 2218 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2219 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2220 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2221 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2222 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2223 | lua_pushinteger(L, -1); |
| 2224 | return 1; |
| 2225 | } |
| 2226 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2227 | si = appctx->owner; |
| 2228 | s = si_strm(si); |
| 2229 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2230 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2231 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2232 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2233 | lua_pushinteger(L, -1); |
| 2234 | return 1; |
| 2235 | } |
| 2236 | |
| 2237 | /* Update the input buffer data. */ |
| 2238 | buf += sent; |
| 2239 | send_len = buf_len - sent; |
| 2240 | |
| 2241 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2242 | if (sent >= buf_len) { |
| 2243 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2244 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2245 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2246 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2247 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2248 | * the request buffer if its not required. |
| 2249 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2250 | if (s->req.buf.size == 0) { |
Willy Tarreau | 581abd3 | 2018-10-25 10:21:41 +0200 | [diff] [blame] | 2251 | if (!si_alloc_ibuf(si, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2252 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2253 | } |
| 2254 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2255 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2256 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2257 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2258 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2259 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2260 | |
| 2261 | /* send data */ |
| 2262 | if (len < send_len) |
| 2263 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2264 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2265 | |
| 2266 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2267 | * the data" (-2) are not expected because the available length |
| 2268 | * is tested. |
| 2269 | * Other unknown error are also not expected. |
| 2270 | */ |
| 2271 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2272 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2273 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2274 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2275 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2276 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2277 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2278 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2279 | return 1; |
| 2280 | } |
| 2281 | |
| 2282 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2283 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2284 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2285 | s->req.rex = TICK_ETERNITY; |
| 2286 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2287 | |
| 2288 | /* Update length sent. */ |
| 2289 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2290 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2291 | |
| 2292 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2293 | if (sent + len >= buf_len) { |
| 2294 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2295 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2296 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2297 | |
| 2298 | hlua_socket_write_yield_return: |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2299 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2300 | xref_unlock(&socket->xref, peer); |
| 2301 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2302 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2303 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2304 | 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] | 2305 | return 0; |
| 2306 | } |
| 2307 | |
| 2308 | /* This function initiate the send of data. It just check the input |
| 2309 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2310 | * 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] | 2311 | * "hlua_socket_write_yield" that can yield. |
| 2312 | * |
| 2313 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2314 | * the associated object. The second is a string buffer. The third is |
| 2315 | * a facultative integer that represents where is the buffer position |
| 2316 | * of the start of the data that can send. The first byte is the |
| 2317 | * position "1". The default value is "1". The fourth argument is a |
| 2318 | * facultative integer that represents where is the buffer position |
| 2319 | * of the end of the data that can send. The default is the last byte. |
| 2320 | */ |
| 2321 | static int hlua_socket_send(struct lua_State *L) |
| 2322 | { |
| 2323 | int i; |
| 2324 | int j; |
| 2325 | const char *buf; |
| 2326 | size_t buf_len; |
| 2327 | |
| 2328 | /* Check number of arguments. */ |
| 2329 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2330 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2331 | |
| 2332 | /* Get the string. */ |
| 2333 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2334 | |
| 2335 | /* Get and check j. */ |
| 2336 | if (lua_gettop(L) == 4) { |
| 2337 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2338 | if (j < 0) |
| 2339 | j = buf_len + j + 1; |
| 2340 | if (j > buf_len) |
| 2341 | j = buf_len + 1; |
| 2342 | lua_pop(L, 1); |
| 2343 | } |
| 2344 | else |
| 2345 | j = buf_len; |
| 2346 | |
| 2347 | /* Get and check i. */ |
| 2348 | if (lua_gettop(L) == 3) { |
| 2349 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2350 | if (i < 0) |
| 2351 | i = buf_len + i + 1; |
| 2352 | if (i > buf_len) |
| 2353 | i = buf_len + 1; |
| 2354 | lua_pop(L, 1); |
| 2355 | } else |
| 2356 | i = 1; |
| 2357 | |
| 2358 | /* Check bth i and j. */ |
| 2359 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2360 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2361 | return 1; |
| 2362 | } |
| 2363 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2364 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2365 | return 1; |
| 2366 | } |
| 2367 | if (i == 0) |
| 2368 | i = 1; |
| 2369 | if (j == 0) |
| 2370 | j = 1; |
| 2371 | |
| 2372 | /* Pop the string. */ |
| 2373 | lua_pop(L, 1); |
| 2374 | |
| 2375 | /* Update the buffer length. */ |
| 2376 | buf += i - 1; |
| 2377 | buf_len = j - i + 1; |
| 2378 | lua_pushlstring(L, buf, buf_len); |
| 2379 | |
| 2380 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2381 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2382 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2383 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2384 | } |
| 2385 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2386 | #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] | 2387 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 2388 | { |
| 2389 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2390 | int ret; |
| 2391 | int len; |
| 2392 | char *p; |
| 2393 | |
| 2394 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2395 | if (ret <= 0) { |
| 2396 | lua_pushnil(L); |
| 2397 | return 1; |
| 2398 | } |
| 2399 | |
| 2400 | if (ret == AF_UNIX) { |
| 2401 | lua_pushstring(L, buffer+1); |
| 2402 | return 1; |
| 2403 | } |
| 2404 | else if (ret == AF_INET6) { |
| 2405 | buffer[0] = '['; |
| 2406 | len = strlen(buffer); |
| 2407 | buffer[len] = ']'; |
| 2408 | len++; |
| 2409 | buffer[len] = ':'; |
| 2410 | len++; |
| 2411 | p = buffer; |
| 2412 | } |
| 2413 | else if (ret == AF_INET) { |
| 2414 | p = buffer + 1; |
| 2415 | len = strlen(p); |
| 2416 | p[len] = ':'; |
| 2417 | len++; |
| 2418 | } |
| 2419 | else { |
| 2420 | lua_pushnil(L); |
| 2421 | return 1; |
| 2422 | } |
| 2423 | |
| 2424 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2425 | lua_pushnil(L); |
| 2426 | return 1; |
| 2427 | } |
| 2428 | |
| 2429 | lua_pushstring(L, p); |
| 2430 | return 1; |
| 2431 | } |
| 2432 | |
| 2433 | /* Returns information about the peer of the connection. */ |
| 2434 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2435 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2436 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2437 | struct xref *peer; |
| 2438 | struct appctx *appctx; |
| 2439 | struct stream_interface *si; |
| 2440 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2441 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2442 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2443 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2444 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2445 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2446 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2447 | /* Check if we run on the same thread than the xreator thread. |
| 2448 | * We cannot access to the socket if the thread is different. |
| 2449 | */ |
| 2450 | if (socket->tid != tid) |
| 2451 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2452 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2453 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2454 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2455 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2456 | lua_pushnil(L); |
| 2457 | return 1; |
| 2458 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2459 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2460 | si = appctx->owner; |
| 2461 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2462 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2463 | if (!s->target_addr) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2464 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2465 | lua_pushnil(L); |
| 2466 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2467 | } |
| 2468 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2469 | ret = MAY_LJMP(hlua_socket_info(L, s->target_addr)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2470 | xref_unlock(&socket->xref, peer); |
| 2471 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2472 | } |
| 2473 | |
| 2474 | /* Returns information about my connection side. */ |
| 2475 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2476 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2477 | struct hlua_socket *socket; |
| 2478 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2479 | struct appctx *appctx; |
| 2480 | struct xref *peer; |
| 2481 | struct stream_interface *si; |
| 2482 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2483 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2484 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2485 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2486 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2487 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2488 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2489 | /* Check if we run on the same thread than the xreator thread. |
| 2490 | * We cannot access to the socket if the thread is different. |
| 2491 | */ |
| 2492 | if (socket->tid != tid) |
| 2493 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2494 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2495 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2496 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2497 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2498 | lua_pushnil(L); |
| 2499 | return 1; |
| 2500 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2501 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2502 | si = appctx->owner; |
| 2503 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2504 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2505 | conn = cs_conn(objt_cs(s->si[1].end)); |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2506 | if (!conn || !conn_get_src(conn)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2507 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2508 | lua_pushnil(L); |
| 2509 | return 1; |
| 2510 | } |
| 2511 | |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2512 | ret = hlua_socket_info(L, conn->src); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2513 | xref_unlock(&socket->xref, peer); |
| 2514 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2515 | } |
| 2516 | |
| 2517 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2518 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2519 | .obj_type = OBJ_TYPE_APPLET, |
| 2520 | .name = "<LUA_TCP>", |
| 2521 | .fct = hlua_socket_handler, |
| 2522 | .release = hlua_socket_release, |
| 2523 | }; |
| 2524 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2525 | __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] | 2526 | { |
| 2527 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2528 | struct hlua *hlua; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2529 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2530 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2531 | struct stream_interface *si; |
| 2532 | struct stream *s; |
| 2533 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2534 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2535 | hlua = hlua_gethlua(L); |
| 2536 | if (!hlua) |
| 2537 | return 0; |
| 2538 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2539 | /* Check if we run on the same thread than the xreator thread. |
| 2540 | * We cannot access to the socket if the thread is different. |
| 2541 | */ |
| 2542 | if (socket->tid != tid) |
| 2543 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2544 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2545 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2546 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2547 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2548 | lua_pushnil(L); |
| 2549 | lua_pushstring(L, "Can't connect"); |
| 2550 | return 2; |
| 2551 | } |
| 2552 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2553 | si = appctx->owner; |
| 2554 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2555 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2556 | /* Check if we run on the same thread than the xreator thread. |
| 2557 | * We cannot access to the socket if the thread is different. |
| 2558 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2559 | if (socket->tid != tid) { |
| 2560 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2561 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2562 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2563 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2564 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2565 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2566 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2567 | lua_pushnil(L); |
| 2568 | lua_pushstring(L, "Can't connect"); |
| 2569 | return 2; |
| 2570 | } |
| 2571 | |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2572 | appctx = __objt_appctx(s->si[0].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2573 | |
| 2574 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2575 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2576 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2577 | lua_pushinteger(L, 1); |
| 2578 | return 1; |
| 2579 | } |
| 2580 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2581 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2582 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2583 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2584 | } |
| 2585 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2586 | 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] | 2587 | return 0; |
| 2588 | } |
| 2589 | |
| 2590 | /* This function fail or initite the connection. */ |
| 2591 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2592 | { |
| 2593 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2594 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2595 | const char *ip; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2596 | struct hlua *hlua; |
| 2597 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2598 | int low, high; |
| 2599 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2600 | struct xref *peer; |
| 2601 | struct stream_interface *si; |
| 2602 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2603 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2604 | if (lua_gettop(L) < 2) |
| 2605 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2606 | |
| 2607 | /* Get args. */ |
| 2608 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2609 | |
| 2610 | /* Check if we run on the same thread than the xreator thread. |
| 2611 | * We cannot access to the socket if the thread is different. |
| 2612 | */ |
| 2613 | if (socket->tid != tid) |
| 2614 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2615 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2616 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2617 | if (lua_gettop(L) >= 3) { |
| 2618 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2619 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2620 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2621 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2622 | * that are not enclosed within square brackets. |
| 2623 | */ |
| 2624 | if (port > 0) { |
| 2625 | luaL_buffinit(L, &b); |
| 2626 | luaL_addstring(&b, ip); |
| 2627 | luaL_addchar(&b, ':'); |
| 2628 | luaL_pushresult(&b); |
| 2629 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2630 | } |
| 2631 | } |
| 2632 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2633 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2634 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2635 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2636 | lua_pushnil(L); |
| 2637 | return 1; |
| 2638 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2639 | |
| 2640 | /* Parse ip address. */ |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 2641 | 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] | 2642 | if (!addr) { |
| 2643 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2644 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2645 | } |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2646 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2647 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2648 | if (low == 0) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2649 | if (addr->ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2650 | if (port == -1) { |
| 2651 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2652 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2653 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2654 | ((struct sockaddr_in *)addr)->sin_port = htons(port); |
| 2655 | } else if (addr->ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2656 | if (port == -1) { |
| 2657 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2658 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2659 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2660 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(port); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2661 | } |
| 2662 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2663 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2664 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2665 | si = appctx->owner; |
| 2666 | s = si_strm(si); |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2667 | |
Willy Tarreau | 9b7587a | 2020-10-15 07:32:10 +0200 | [diff] [blame] | 2668 | if (!sockaddr_alloc(&s->target_addr, addr, sizeof(*addr))) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2669 | xref_unlock(&socket->xref, peer); |
| 2670 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
| 2671 | } |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2672 | s->flags |= SF_ADDR_SET; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2673 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2674 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2675 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2676 | if (!hlua) |
| 2677 | return 0; |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2678 | |
| 2679 | /* inform the stream that we want to be notified whenever the |
| 2680 | * connection completes. |
| 2681 | */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 2682 | si_cant_get(&s->si[0]); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2683 | si_rx_endp_more(&s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2684 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2685 | |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2686 | hlua->gc_count++; |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2687 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2688 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2689 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2690 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2691 | } |
| 2692 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2693 | |
| 2694 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2695 | /* Return yield waiting for connection. */ |
| 2696 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2697 | 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] | 2698 | |
| 2699 | return 0; |
| 2700 | } |
| 2701 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2702 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2703 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2704 | { |
| 2705 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2706 | struct xref *peer; |
| 2707 | struct appctx *appctx; |
| 2708 | struct stream_interface *si; |
| 2709 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2710 | |
| 2711 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2712 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2713 | |
| 2714 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2715 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2716 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2717 | lua_pushnil(L); |
| 2718 | return 1; |
| 2719 | } |
| 2720 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2721 | si = appctx->owner; |
| 2722 | s = si_strm(si); |
| 2723 | |
| 2724 | s->target = &socket_ssl.obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2725 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2726 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2727 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2728 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2729 | |
| 2730 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2731 | { |
| 2732 | return 0; |
| 2733 | } |
| 2734 | |
| 2735 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2736 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2737 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2738 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2739 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2740 | struct xref *peer; |
| 2741 | struct appctx *appctx; |
| 2742 | struct stream_interface *si; |
| 2743 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2744 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2745 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2746 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2747 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2748 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2749 | /* convert the timeout to millis */ |
| 2750 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2751 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2752 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2753 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2754 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2755 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2756 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2757 | 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] | 2758 | |
| 2759 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2760 | if (tmout == 0) |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2761 | tmout++; /* very small timeouts are adjusted to a minimum of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2762 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2763 | /* Check if we run on the same thread than the xreator thread. |
| 2764 | * We cannot access to the socket if the thread is different. |
| 2765 | */ |
| 2766 | if (socket->tid != tid) |
| 2767 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2768 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2769 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2770 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2771 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2772 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2773 | WILL_LJMP(lua_error(L)); |
| 2774 | return 0; |
| 2775 | } |
| 2776 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2777 | si = appctx->owner; |
| 2778 | s = si_strm(si); |
| 2779 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2780 | s->sess->fe->timeout.connect = tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2781 | s->req.rto = tmout; |
| 2782 | s->req.wto = tmout; |
| 2783 | s->res.rto = tmout; |
| 2784 | s->res.wto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2785 | s->req.rex = tick_add_ifset(now_ms, tmout); |
| 2786 | s->req.wex = tick_add_ifset(now_ms, tmout); |
| 2787 | s->res.rex = tick_add_ifset(now_ms, tmout); |
| 2788 | s->res.wex = tick_add_ifset(now_ms, tmout); |
| 2789 | |
| 2790 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 2791 | task_queue(s->task); |
| 2792 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2793 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2794 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 2795 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 2796 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2797 | } |
| 2798 | |
| 2799 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2800 | { |
| 2801 | struct hlua_socket *socket; |
| 2802 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2803 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2804 | struct stream *strm; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2805 | |
| 2806 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2807 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2808 | hlua_pusherror(L, "socket: full stack"); |
| 2809 | goto out_fail_conf; |
| 2810 | } |
| 2811 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2812 | /* Create the object: obj[0] = userdata. */ |
| 2813 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2814 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2815 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2816 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2817 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2818 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2819 | /* Check if the various memory pools are initialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2820 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2821 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2822 | goto out_fail_conf; |
| 2823 | } |
| 2824 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2825 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2826 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2827 | lua_setmetatable(L, -2); |
| 2828 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2829 | /* Create the applet context */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 2830 | appctx = appctx_new(&update_applet, tid_bit); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2831 | if (!appctx) { |
| 2832 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2833 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2834 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2835 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2836 | appctx->ctx.hlua_cosocket.connected = 0; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2837 | appctx->ctx.hlua_cosocket.die = 0; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2838 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 2839 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2840 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2841 | /* Now create a session, task and stream for this applet */ |
| 2842 | sess = session_new(&socket_proxy, NULL, &appctx->obj_type); |
| 2843 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2844 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2845 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2846 | } |
| 2847 | |
Christopher Faulet | 26256f8 | 2020-09-14 11:40:13 +0200 | [diff] [blame] | 2848 | strm = stream_new(sess, &appctx->obj_type, &BUF_NULL); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2849 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2850 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2851 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2852 | } |
| 2853 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2854 | /* Initialise cross reference between stream and Lua socket object. */ |
| 2855 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2856 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2857 | /* Configure "right" stream interface. this "si" is used to connect |
| 2858 | * and retrieve data from the server. The connection is initialized |
| 2859 | * with the "struct server". |
| 2860 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2861 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2862 | |
| 2863 | /* Force destination server. */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2864 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2865 | strm->target = &socket_tcp.obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2866 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2867 | return 1; |
| 2868 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2869 | out_fail_stream: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 2870 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2871 | out_fail_sess: |
| 2872 | appctx_free(appctx); |
| 2873 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2874 | WILL_LJMP(lua_error(L)); |
| 2875 | return 0; |
| 2876 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2877 | |
| 2878 | /* |
| 2879 | * |
| 2880 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2881 | * Class Channel |
| 2882 | * |
| 2883 | * |
| 2884 | */ |
| 2885 | |
| 2886 | /* Returns the struct hlua_channel join to the class channel in the |
| 2887 | * stack entry "ud" or throws an argument error. |
| 2888 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2889 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2890 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2891 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2892 | } |
| 2893 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2894 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 2895 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2896 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 2897 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2898 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2899 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2900 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2901 | return 0; |
| 2902 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2903 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2904 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2905 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2906 | |
| 2907 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 2908 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 2909 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2910 | return 1; |
| 2911 | } |
| 2912 | |
| 2913 | /* Duplicate all the data present in the input channel and put it |
| 2914 | * in a string LUA variables. Returns -1 and push a nil value in |
| 2915 | * the stack if the channel is closed and all the data are consumed, |
| 2916 | * returns 0 if no data are available, otherwise it returns the length |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 2917 | * of the built string. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2918 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2919 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2920 | { |
| 2921 | char *blk1; |
| 2922 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2923 | size_t len1; |
| 2924 | size_t len2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2925 | int ret; |
| 2926 | luaL_Buffer b; |
| 2927 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2928 | ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2929 | if (unlikely(ret == 0)) |
| 2930 | return 0; |
| 2931 | |
| 2932 | if (unlikely(ret < 0)) { |
| 2933 | lua_pushnil(L); |
| 2934 | return -1; |
| 2935 | } |
| 2936 | |
| 2937 | luaL_buffinit(L, &b); |
| 2938 | luaL_addlstring(&b, blk1, len1); |
| 2939 | if (unlikely(ret == 2)) |
| 2940 | luaL_addlstring(&b, blk2, len2); |
| 2941 | luaL_pushresult(&b); |
| 2942 | |
| 2943 | if (unlikely(ret == 2)) |
| 2944 | return len1 + len2; |
| 2945 | return len1; |
| 2946 | } |
| 2947 | |
| 2948 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2949 | * a yield. This function keep the data in the buffer. |
| 2950 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2951 | __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] | 2952 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2953 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2954 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2955 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2956 | |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2957 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) { |
| 2958 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2959 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2960 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2961 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2962 | if (_hlua_channel_dup(chn, L) == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2963 | 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] | 2964 | return 1; |
| 2965 | } |
| 2966 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2967 | /* Check arguments for the function "hlua_channel_dup_yield". */ |
| 2968 | __LJMP static int hlua_channel_dup(lua_State *L) |
| 2969 | { |
| 2970 | MAY_LJMP(check_args(L, 1, "dup")); |
| 2971 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2972 | return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0)); |
| 2973 | } |
| 2974 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2975 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2976 | * a yield. This function consumes the data in the buffer. It returns |
| 2977 | * a string containing the data or a nil pointer if no data are available |
| 2978 | * and the channel is closed. |
| 2979 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2980 | __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] | 2981 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2982 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2983 | int ret; |
| 2984 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2985 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2986 | |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2987 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) { |
| 2988 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2989 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2990 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2991 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2992 | ret = _hlua_channel_dup(chn, L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2993 | if (unlikely(ret == 0)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2994 | 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] | 2995 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2996 | if (unlikely(ret == -1)) |
| 2997 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2998 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2999 | b_sub(&chn->buf, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3000 | return 1; |
| 3001 | } |
| 3002 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3003 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3004 | __LJMP static int hlua_channel_get(lua_State *L) |
| 3005 | { |
| 3006 | MAY_LJMP(check_args(L, 1, "get")); |
| 3007 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3008 | return MAY_LJMP(hlua_channel_get_yield(L, 0, 0)); |
| 3009 | } |
| 3010 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3011 | /* This functions consumes and returns one line. If the channel is closed, |
| 3012 | * 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] | 3013 | * 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] | 3014 | * value. |
| 3015 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3016 | __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] | 3017 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3018 | char *blk1; |
| 3019 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3020 | size_t len1; |
| 3021 | size_t len2; |
| 3022 | size_t len; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3023 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3024 | int ret; |
| 3025 | luaL_Buffer b; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3026 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3027 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3028 | |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3029 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) { |
| 3030 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3031 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3032 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3033 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3034 | ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3035 | if (ret == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3036 | 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] | 3037 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3038 | if (ret == -1) { |
| 3039 | lua_pushnil(L); |
| 3040 | return 1; |
| 3041 | } |
| 3042 | |
| 3043 | luaL_buffinit(L, &b); |
| 3044 | luaL_addlstring(&b, blk1, len1); |
| 3045 | len = len1; |
| 3046 | if (unlikely(ret == 2)) { |
| 3047 | luaL_addlstring(&b, blk2, len2); |
| 3048 | len += len2; |
| 3049 | } |
| 3050 | luaL_pushresult(&b); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3051 | 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] | 3052 | return 1; |
| 3053 | } |
| 3054 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3055 | /* Check arguments for the function "hlua_channel_getline_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3056 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 3057 | { |
| 3058 | MAY_LJMP(check_args(L, 1, "getline")); |
| 3059 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3060 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 3061 | } |
| 3062 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3063 | /* This function takes a string as input, and append it at the |
| 3064 | * input side of channel. If the data is too big, but a space |
| 3065 | * is probably available after sending some data, the function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3066 | * yields. If the data is bigger than the buffer, or if the |
| 3067 | * channel is closed, it returns -1. Otherwise, it returns the |
| 3068 | * amount of data written. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3069 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3070 | __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] | 3071 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3072 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3073 | size_t len; |
| 3074 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3075 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3076 | int ret; |
| 3077 | int max; |
| 3078 | |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3079 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) { |
| 3080 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3081 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3082 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3083 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3084 | /* Check if the buffer is available because HAProxy doesn't allocate |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 3085 | * the request buffer if its not required. |
| 3086 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3087 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 3088 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3089 | 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] | 3090 | } |
| 3091 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3092 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3093 | if (max > len - l) |
| 3094 | max = len - l; |
| 3095 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3096 | ret = ci_putblk(chn, str + l, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3097 | if (ret == -2 || ret == -3) { |
| 3098 | lua_pushinteger(L, -1); |
| 3099 | return 1; |
| 3100 | } |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 3101 | if (ret == -1) { |
| 3102 | chn->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3103 | 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] | 3104 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3105 | l += ret; |
| 3106 | lua_pop(L, 1); |
| 3107 | lua_pushinteger(L, l); |
| 3108 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3109 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3110 | if (max == 0 && co_data(chn) == 0) { |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3111 | /* There are no space available, and the output buffer is empty. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3112 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3113 | * we return the amount of copied data. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3114 | */ |
| 3115 | return 1; |
| 3116 | } |
| 3117 | if (l < len) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3118 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3119 | return 1; |
| 3120 | } |
| 3121 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3122 | /* Just a wrapper of "hlua_channel_append_yield". It returns the length |
| 3123 | * 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] | 3124 | * buffer size is too little for the data. |
| 3125 | */ |
| 3126 | __LJMP static int hlua_channel_append(lua_State *L) |
| 3127 | { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3128 | size_t len; |
| 3129 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3130 | MAY_LJMP(check_args(L, 2, "append")); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3131 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3132 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3133 | MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3134 | lua_pushinteger(L, 0); |
| 3135 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3136 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3137 | } |
| 3138 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3139 | /* Just a wrapper of "hlua_channel_append_yield". This wrapper starts |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3140 | * his process by cleaning the buffer. The result is a replacement |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3141 | * of the current data. It returns the length of the written string, |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3142 | * or -1 if the channel is closed or if the buffer size is too |
| 3143 | * little for the data. |
| 3144 | */ |
| 3145 | __LJMP static int hlua_channel_set(lua_State *L) |
| 3146 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3147 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3148 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3149 | MAY_LJMP(check_args(L, 2, "set")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3150 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3151 | lua_pushinteger(L, 0); |
| 3152 | |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3153 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) { |
| 3154 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3155 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3156 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3157 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3158 | b_set_data(&chn->buf, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3159 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3160 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3161 | } |
| 3162 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3163 | /* Append data in the output side of the buffer. This data is immediately |
| 3164 | * sent. The function returns the amount of data written. If the buffer |
| 3165 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3166 | * if the channel is closed. |
| 3167 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3168 | __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] | 3169 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3170 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3171 | size_t len; |
| 3172 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3173 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3174 | int max; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3175 | struct hlua *hlua; |
| 3176 | |
| 3177 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3178 | hlua = hlua_gethlua(L); |
| 3179 | if (!hlua) { |
| 3180 | lua_pushnil(L); |
| 3181 | return 1; |
| 3182 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3183 | |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3184 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) { |
| 3185 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3186 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3187 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3188 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3189 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3190 | lua_pushinteger(L, -1); |
| 3191 | return 1; |
| 3192 | } |
| 3193 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3194 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 3195 | * the request buffer if its not required. |
| 3196 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3197 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 3198 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3199 | 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] | 3200 | } |
| 3201 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3202 | /* The written data will be immediately sent, so we can check |
| 3203 | * the available space without taking in account the reserve. |
| 3204 | * The reserve is guaranteed for the processing of incoming |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3205 | * data, because the buffer will be flushed. |
| 3206 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3207 | max = b_room(&chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3208 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3209 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3210 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3211 | * we return the amount of copied data. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3212 | */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3213 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3214 | return 1; |
| 3215 | |
| 3216 | /* Adjust the real required length. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3217 | if (max > len - l) |
| 3218 | max = len - l; |
| 3219 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3220 | /* The buffer available size may be not contiguous. This test |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3221 | * detects a non contiguous buffer and realign it. |
| 3222 | */ |
Willy Tarreau | 3f67999 | 2018-06-15 15:06:42 +0200 | [diff] [blame] | 3223 | if (ci_space_for_replace(chn) < max) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3224 | channel_slow_realign(chn, trash.area); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3225 | |
| 3226 | /* Copy input data in the buffer. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3227 | 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] | 3228 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3229 | /* buffer replace considers that the input part is filled. |
| 3230 | * so, I must forward these new data in the output part. |
| 3231 | */ |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 3232 | c_adv(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3233 | |
| 3234 | l += max; |
| 3235 | lua_pop(L, 1); |
| 3236 | lua_pushinteger(L, l); |
| 3237 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3238 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3239 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3240 | * we return the amount of copied data. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3241 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3242 | max = b_room(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3243 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3244 | return 1; |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3245 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3246 | if (l < len) { |
| 3247 | /* If we are waiting for space in the response buffer, we |
| 3248 | * must set the flag WAKERESWR. This flag required the task |
| 3249 | * wake up if any activity is detected on the response buffer. |
| 3250 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3251 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3252 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3253 | else |
| 3254 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3255 | 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] | 3256 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3257 | |
| 3258 | return 1; |
| 3259 | } |
| 3260 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3261 | /* Just a wrapper of "_hlua_channel_send". This wrapper permits |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3262 | * yield the LUA process, and resume it without checking the |
| 3263 | * input arguments. |
| 3264 | */ |
| 3265 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3266 | { |
| 3267 | MAY_LJMP(check_args(L, 2, "send")); |
| 3268 | lua_pushinteger(L, 0); |
| 3269 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3270 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3271 | } |
| 3272 | |
| 3273 | /* This function forward and amount of butes. The data pass from |
| 3274 | * the input side of the buffer to the output side, and can be |
| 3275 | * forwarded. This function never fails. |
| 3276 | * |
| 3277 | * The Lua function takes an amount of bytes to be forwarded in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3278 | * input. It returns the number of bytes forwarded. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3279 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3280 | __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] | 3281 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3282 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3283 | int len; |
| 3284 | int l; |
| 3285 | int max; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3286 | struct hlua *hlua; |
| 3287 | |
| 3288 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3289 | hlua = hlua_gethlua(L); |
| 3290 | if (!hlua) |
| 3291 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3292 | |
| 3293 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3294 | |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3295 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) { |
| 3296 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3297 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3298 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3299 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3300 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3301 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3302 | |
| 3303 | max = len - l; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3304 | if (max > ci_data(chn)) |
| 3305 | max = ci_data(chn); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3306 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3307 | l += max; |
| 3308 | |
| 3309 | lua_pop(L, 1); |
| 3310 | lua_pushinteger(L, l); |
| 3311 | |
| 3312 | /* Check if it miss bytes to forward. */ |
| 3313 | if (l < len) { |
| 3314 | /* The the input channel or the output channel are closed, we |
| 3315 | * must return the amount of data forwarded. |
| 3316 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3317 | if (channel_input_closed(chn) || channel_output_closed(chn)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3318 | return 1; |
| 3319 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3320 | /* If we are waiting for space data in the response buffer, we |
| 3321 | * must set the flag WAKERESWR. This flag required the task |
| 3322 | * wake up if any activity is detected on the response buffer. |
| 3323 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3324 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3325 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3326 | else |
| 3327 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3328 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3329 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3330 | 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] | 3331 | } |
| 3332 | |
| 3333 | return 1; |
| 3334 | } |
| 3335 | |
| 3336 | /* Just check the input and prepare the stack for the previous |
| 3337 | * function "hlua_channel_forward_yield" |
| 3338 | */ |
| 3339 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3340 | { |
| 3341 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3342 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3343 | MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3344 | |
| 3345 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3346 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3347 | } |
| 3348 | |
| 3349 | /* Just returns the number of bytes available in the input |
| 3350 | * side of the buffer. This function never fails. |
| 3351 | */ |
| 3352 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3353 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3354 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3355 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3356 | MAY_LJMP(check_args(L, 1, "get_in_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3357 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3358 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3359 | struct htx *htx = htxbuf(&chn->buf); |
| 3360 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 3361 | } |
| 3362 | else |
| 3363 | lua_pushinteger(L, ci_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3364 | return 1; |
| 3365 | } |
| 3366 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3367 | /* Returns true if the channel is full. */ |
| 3368 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3369 | { |
| 3370 | struct channel *chn; |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3371 | |
| 3372 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3373 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 0ec740e | 2020-02-26 11:59:19 +0100 | [diff] [blame] | 3374 | /* ignore the reserve, we are not on a producer side (ie in an |
| 3375 | * applet). |
| 3376 | */ |
| 3377 | lua_pushboolean(L, channel_full(chn, 0)); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3378 | return 1; |
| 3379 | } |
| 3380 | |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 3381 | /* Returns true if the channel is the response channel. */ |
| 3382 | __LJMP static int hlua_channel_is_resp(lua_State *L) |
| 3383 | { |
| 3384 | struct channel *chn; |
| 3385 | |
| 3386 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 3387 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3388 | |
| 3389 | lua_pushboolean(L, !!(chn->flags & CF_ISRESP)); |
| 3390 | return 1; |
| 3391 | } |
| 3392 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3393 | /* Just returns the number of bytes available in the output |
| 3394 | * side of the buffer. This function never fails. |
| 3395 | */ |
| 3396 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 3397 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3398 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3399 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3400 | MAY_LJMP(check_args(L, 1, "get_out_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3401 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3402 | lua_pushinteger(L, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3403 | return 1; |
| 3404 | } |
| 3405 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3406 | /* |
| 3407 | * |
| 3408 | * |
| 3409 | * Class Fetches |
| 3410 | * |
| 3411 | * |
| 3412 | */ |
| 3413 | |
| 3414 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3415 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3416 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3417 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3418 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3419 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3420 | } |
| 3421 | |
| 3422 | /* This function creates and push in the stack a fetch object according |
| 3423 | * with a current TXN. |
| 3424 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3425 | 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] | 3426 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3427 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3428 | |
| 3429 | /* Check stack size. */ |
| 3430 | if (!lua_checkstack(L, 3)) |
| 3431 | return 0; |
| 3432 | |
| 3433 | /* Create the object: obj[0] = userdata. |
| 3434 | * Note that the base of the Fetches object is the |
| 3435 | * transaction object. |
| 3436 | */ |
| 3437 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3438 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3439 | lua_rawseti(L, -2, 0); |
| 3440 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3441 | hsmp->s = txn->s; |
| 3442 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3443 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3444 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3445 | |
| 3446 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3447 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 3448 | lua_setmetatable(L, -2); |
| 3449 | |
| 3450 | return 1; |
| 3451 | } |
| 3452 | |
| 3453 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 3454 | * It uses closure argument to store the associated sample-fetch. It |
| 3455 | * returns only one argument or throws an error. An error is thrown |
| 3456 | * only if an error is encountered during the argument parsing. If |
| 3457 | * the "sample-fetch" function fails, nil is returned. |
| 3458 | */ |
| 3459 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 3460 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3461 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3462 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3463 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3464 | int i; |
| 3465 | struct sample smp; |
| 3466 | |
| 3467 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3468 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3469 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3470 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3471 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3472 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 3473 | /* Check execution authorization. */ |
| 3474 | if (f->use & SMP_USE_HTTP_ANY && |
| 3475 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 3476 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 3477 | "is not available in Lua services", f->kw); |
| 3478 | WILL_LJMP(lua_error(L)); |
| 3479 | } |
| 3480 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3481 | /* Get extra arguments. */ |
| 3482 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 3483 | if (i >= ARGM_NBARGS) |
| 3484 | break; |
| 3485 | hlua_lua2arg(L, i + 2, &args[i]); |
| 3486 | } |
| 3487 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3488 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3489 | |
| 3490 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3491 | 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] | 3492 | |
| 3493 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3494 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3495 | lua_pushfstring(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3496 | goto error; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3497 | } |
| 3498 | |
| 3499 | /* Initialise the sample. */ |
| 3500 | memset(&smp, 0, sizeof(smp)); |
| 3501 | |
| 3502 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3503 | 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] | 3504 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3505 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3506 | lua_pushstring(L, ""); |
| 3507 | else |
| 3508 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3509 | goto end; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3510 | } |
| 3511 | |
| 3512 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3513 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3514 | hlua_smp2lua_str(L, &smp); |
| 3515 | else |
| 3516 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3517 | |
| 3518 | end: |
| 3519 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3520 | if (args[i].type == ARGT_STR) |
| 3521 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3522 | else if (args[i].type == ARGT_REG) |
| 3523 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3524 | } |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3525 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3526 | |
| 3527 | error: |
| 3528 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3529 | if (args[i].type == ARGT_STR) |
| 3530 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3531 | else if (args[i].type == ARGT_REG) |
| 3532 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3533 | } |
| 3534 | WILL_LJMP(lua_error(L)); |
| 3535 | return 0; /* Never reached */ |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3536 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3537 | |
| 3538 | /* |
| 3539 | * |
| 3540 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3541 | * Class Converters |
| 3542 | * |
| 3543 | * |
| 3544 | */ |
| 3545 | |
| 3546 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3547 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3548 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3549 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3550 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3551 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3552 | } |
| 3553 | |
| 3554 | /* This function creates and push in the stack a Converters object |
| 3555 | * according with a current TXN. |
| 3556 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3557 | 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] | 3558 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3559 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3560 | |
| 3561 | /* Check stack size. */ |
| 3562 | if (!lua_checkstack(L, 3)) |
| 3563 | return 0; |
| 3564 | |
| 3565 | /* Create the object: obj[0] = userdata. |
| 3566 | * Note that the base of the Converters object is the |
| 3567 | * same than the TXN object. |
| 3568 | */ |
| 3569 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3570 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3571 | lua_rawseti(L, -2, 0); |
| 3572 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3573 | hsmp->s = txn->s; |
| 3574 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3575 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3576 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3577 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3578 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3579 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 3580 | lua_setmetatable(L, -2); |
| 3581 | |
| 3582 | return 1; |
| 3583 | } |
| 3584 | |
| 3585 | /* This function is an LUA binding. It is called with each converter. |
| 3586 | * It uses closure argument to store the associated converter. It |
| 3587 | * returns only one argument or throws an error. An error is thrown |
| 3588 | * only if an error is encountered during the argument parsing. If |
| 3589 | * the converter function function fails, nil is returned. |
| 3590 | */ |
| 3591 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 3592 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3593 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3594 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3595 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3596 | int i; |
| 3597 | struct sample smp; |
| 3598 | |
| 3599 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3600 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3601 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3602 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3603 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3604 | |
| 3605 | /* Get extra arguments. */ |
| 3606 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 3607 | if (i >= ARGM_NBARGS) |
| 3608 | break; |
| 3609 | hlua_lua2arg(L, i + 3, &args[i]); |
| 3610 | } |
| 3611 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3612 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3613 | |
| 3614 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3615 | 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] | 3616 | |
| 3617 | /* Run the special args checker. */ |
| 3618 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 3619 | hlua_pusherror(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3620 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3621 | } |
| 3622 | |
| 3623 | /* Initialise the sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 3624 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3625 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 3626 | hlua_pusherror(L, "error in the input argument"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3627 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3628 | } |
| 3629 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3630 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 3631 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3632 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3633 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3634 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3635 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3636 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3637 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3638 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 3639 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3640 | hlua_pusherror(L, "error during the input argument casting"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3641 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3642 | } |
| 3643 | |
| 3644 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3645 | if (!conv->process(args, &smp, conv->private)) { |
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 | lua_pushstring(L, ""); |
| 3648 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3649 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3650 | goto end; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3651 | } |
| 3652 | |
| 3653 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3654 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3655 | hlua_smp2lua_str(L, &smp); |
| 3656 | else |
| 3657 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3658 | end: |
| 3659 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3660 | if (args[i].type == ARGT_STR) |
| 3661 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3662 | else if (args[i].type == ARGT_REG) |
| 3663 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3664 | } |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3665 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3666 | |
| 3667 | error: |
| 3668 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3669 | if (args[i].type == ARGT_STR) |
| 3670 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3671 | else if (args[i].type == ARGT_REG) |
| 3672 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3673 | } |
| 3674 | WILL_LJMP(lua_error(L)); |
| 3675 | return 0; /* Never reached */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3676 | } |
| 3677 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3678 | /* |
| 3679 | * |
| 3680 | * |
| 3681 | * Class AppletTCP |
| 3682 | * |
| 3683 | * |
| 3684 | */ |
| 3685 | |
| 3686 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 3687 | * a class stream, otherwise it throws an error. |
| 3688 | */ |
| 3689 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 3690 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3691 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3692 | } |
| 3693 | |
| 3694 | /* This function creates and push in the stack an Applet object |
| 3695 | * according with a current TXN. |
| 3696 | */ |
| 3697 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 3698 | { |
| 3699 | struct hlua_appctx *appctx; |
| 3700 | struct stream_interface *si = ctx->owner; |
| 3701 | struct stream *s = si_strm(si); |
| 3702 | struct proxy *p = s->be; |
| 3703 | |
| 3704 | /* Check stack size. */ |
| 3705 | if (!lua_checkstack(L, 3)) |
| 3706 | return 0; |
| 3707 | |
| 3708 | /* Create the object: obj[0] = userdata. |
| 3709 | * Note that the base of the Converters object is the |
| 3710 | * same than the TXN object. |
| 3711 | */ |
| 3712 | lua_newtable(L); |
| 3713 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
| 3714 | lua_rawseti(L, -2, 0); |
| 3715 | appctx->appctx = ctx; |
| 3716 | appctx->htxn.s = s; |
| 3717 | appctx->htxn.p = p; |
| 3718 | |
| 3719 | /* Create the "f" field that contains a list of fetches. */ |
| 3720 | lua_pushstring(L, "f"); |
| 3721 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3722 | return 0; |
| 3723 | lua_settable(L, -3); |
| 3724 | |
| 3725 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3726 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3727 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3728 | return 0; |
| 3729 | lua_settable(L, -3); |
| 3730 | |
| 3731 | /* Create the "c" field that contains a list of converters. */ |
| 3732 | lua_pushstring(L, "c"); |
| 3733 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3734 | return 0; |
| 3735 | lua_settable(L, -3); |
| 3736 | |
| 3737 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3738 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3739 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3740 | return 0; |
| 3741 | lua_settable(L, -3); |
| 3742 | |
| 3743 | /* Pop a class stream metatable and affect it to the table. */ |
| 3744 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 3745 | lua_setmetatable(L, -2); |
| 3746 | |
| 3747 | return 1; |
| 3748 | } |
| 3749 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3750 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 3751 | { |
| 3752 | struct hlua_appctx *appctx; |
| 3753 | struct stream *s; |
| 3754 | const char *name; |
| 3755 | size_t len; |
| 3756 | struct sample smp; |
| 3757 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3758 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 3759 | 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] | 3760 | |
| 3761 | /* It is useles to retrieve the stream, but this function |
| 3762 | * runs only in a stream context. |
| 3763 | */ |
| 3764 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3765 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3766 | s = appctx->htxn.s; |
| 3767 | |
| 3768 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 3769 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3770 | hlua_lua2smp(L, 3, &smp); |
| 3771 | |
| 3772 | /* Store the sample in a variable. */ |
| 3773 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3774 | |
| 3775 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 3776 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 3777 | else |
| 3778 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 3779 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 3780 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3781 | } |
| 3782 | |
| 3783 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 3784 | { |
| 3785 | struct hlua_appctx *appctx; |
| 3786 | struct stream *s; |
| 3787 | const char *name; |
| 3788 | size_t len; |
| 3789 | struct sample smp; |
| 3790 | |
| 3791 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3792 | |
| 3793 | /* It is useles to retrieve the stream, but this function |
| 3794 | * runs only in a stream context. |
| 3795 | */ |
| 3796 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3797 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3798 | s = appctx->htxn.s; |
| 3799 | |
| 3800 | /* Unset the variable. */ |
| 3801 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 3802 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 3803 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3804 | } |
| 3805 | |
| 3806 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 3807 | { |
| 3808 | struct hlua_appctx *appctx; |
| 3809 | struct stream *s; |
| 3810 | const char *name; |
| 3811 | size_t len; |
| 3812 | struct sample smp; |
| 3813 | |
| 3814 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3815 | |
| 3816 | /* It is useles to retrieve the stream, but this function |
| 3817 | * runs only in a stream context. |
| 3818 | */ |
| 3819 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3820 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3821 | s = appctx->htxn.s; |
| 3822 | |
| 3823 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3824 | if (!vars_get_by_name(name, len, &smp)) { |
| 3825 | lua_pushnil(L); |
| 3826 | return 1; |
| 3827 | } |
| 3828 | |
| 3829 | return hlua_smp2lua(L, &smp); |
| 3830 | } |
| 3831 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3832 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 3833 | { |
| 3834 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3835 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3836 | struct hlua *hlua; |
| 3837 | |
| 3838 | /* 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] | 3839 | if (!s->hlua) |
| 3840 | return 0; |
| 3841 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3842 | |
| 3843 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3844 | |
| 3845 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 3846 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3847 | |
| 3848 | /* Get and store new value. */ |
| 3849 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3850 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 3851 | |
| 3852 | return 0; |
| 3853 | } |
| 3854 | |
| 3855 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 3856 | { |
| 3857 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3858 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3859 | struct hlua *hlua; |
| 3860 | |
| 3861 | /* 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] | 3862 | if (!s->hlua) { |
| 3863 | lua_pushnil(L); |
| 3864 | return 1; |
| 3865 | } |
| 3866 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3867 | |
| 3868 | /* Push configuration index in the stack. */ |
| 3869 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 3870 | |
| 3871 | return 1; |
| 3872 | } |
| 3873 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3874 | /* If expected data not yet available, it returns a yield. This function |
| 3875 | * consumes the data in the buffer. It returns a string containing the |
| 3876 | * data. This string can be empty. |
| 3877 | */ |
| 3878 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 3879 | { |
| 3880 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3881 | struct stream_interface *si = appctx->appctx->owner; |
| 3882 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3883 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3884 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3885 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3886 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3887 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3888 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3889 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3890 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3891 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3892 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3893 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3894 | 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] | 3895 | } |
| 3896 | |
| 3897 | /* End of data: commit the total strings and return. */ |
| 3898 | if (ret < 0) { |
| 3899 | luaL_pushresult(&appctx->b); |
| 3900 | return 1; |
| 3901 | } |
| 3902 | |
| 3903 | /* Ensure that the block 2 length is usable. */ |
| 3904 | if (ret == 1) |
| 3905 | len2 = 0; |
| 3906 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 3907 | /* don't check the max length read and don't check. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3908 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3909 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3910 | |
| 3911 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3912 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3913 | luaL_pushresult(&appctx->b); |
| 3914 | return 1; |
| 3915 | } |
| 3916 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3917 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3918 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 3919 | { |
| 3920 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3921 | |
| 3922 | /* Initialise the string catenation. */ |
| 3923 | luaL_buffinit(L, &appctx->b); |
| 3924 | |
| 3925 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 3926 | } |
| 3927 | |
| 3928 | /* If expected data not yet available, it returns a yield. This function |
| 3929 | * consumes the data in the buffer. It returns a string containing the |
| 3930 | * data. This string can be empty. |
| 3931 | */ |
| 3932 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 3933 | { |
| 3934 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3935 | struct stream_interface *si = appctx->appctx->owner; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3936 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3937 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3938 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3939 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3940 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3941 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3942 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3943 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3944 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3945 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3946 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3947 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3948 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3949 | 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] | 3950 | } |
| 3951 | |
| 3952 | /* End of data: commit the total strings and return. */ |
| 3953 | if (ret < 0) { |
| 3954 | luaL_pushresult(&appctx->b); |
| 3955 | return 1; |
| 3956 | } |
| 3957 | |
| 3958 | /* Ensure that the block 2 length is usable. */ |
| 3959 | if (ret == 1) |
| 3960 | len2 = 0; |
| 3961 | |
| 3962 | if (len == -1) { |
| 3963 | |
| 3964 | /* If len == -1, catenate all the data avalaile and |
| 3965 | * yield because we want to get all the data until |
| 3966 | * the end of data stream. |
| 3967 | */ |
| 3968 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3969 | luaL_addlstring(&appctx->b, blk2, len2); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3970 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3971 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3972 | 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] | 3973 | |
| 3974 | } else { |
| 3975 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3976 | /* Copy the first block caping to the length required. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3977 | if (len1 > len) |
| 3978 | len1 = len; |
| 3979 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3980 | len -= len1; |
| 3981 | |
| 3982 | /* Copy the second block. */ |
| 3983 | if (len2 > len) |
| 3984 | len2 = len; |
| 3985 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3986 | len -= len2; |
| 3987 | |
| 3988 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3989 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3990 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3991 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3992 | if (len > 0) { |
| 3993 | lua_pushinteger(L, len); |
| 3994 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3995 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3996 | 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] | 3997 | } |
| 3998 | |
| 3999 | /* return the result. */ |
| 4000 | luaL_pushresult(&appctx->b); |
| 4001 | return 1; |
| 4002 | } |
| 4003 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4004 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4005 | hlua_pusherror(L, "Lua: internal error"); |
| 4006 | WILL_LJMP(lua_error(L)); |
| 4007 | return 0; |
| 4008 | } |
| 4009 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4010 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4011 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 4012 | { |
| 4013 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4014 | int len = -1; |
| 4015 | |
| 4016 | if (lua_gettop(L) > 2) |
| 4017 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4018 | if (lua_gettop(L) >= 2) { |
| 4019 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4020 | lua_pop(L, 1); |
| 4021 | } |
| 4022 | |
| 4023 | /* Confirm or set the required length */ |
| 4024 | lua_pushinteger(L, len); |
| 4025 | |
| 4026 | /* Initialise the string catenation. */ |
| 4027 | luaL_buffinit(L, &appctx->b); |
| 4028 | |
| 4029 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 4030 | } |
| 4031 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4032 | /* Append data in the output side of the buffer. This data is immediately |
| 4033 | * sent. The function returns the amount of data written. If the buffer |
| 4034 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4035 | * if the channel is closed. |
| 4036 | */ |
| 4037 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4038 | { |
| 4039 | size_t len; |
| 4040 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4041 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4042 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4043 | struct stream_interface *si = appctx->appctx->owner; |
| 4044 | struct channel *chn = si_ic(si); |
| 4045 | int max; |
| 4046 | |
| 4047 | /* Get the max amount of data which can write as input in the channel. */ |
| 4048 | max = channel_recv_max(chn); |
| 4049 | if (max > (len - l)) |
| 4050 | max = len - l; |
| 4051 | |
| 4052 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4053 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4054 | |
| 4055 | /* update counters. */ |
| 4056 | l += max; |
| 4057 | lua_pop(L, 1); |
| 4058 | lua_pushinteger(L, l); |
| 4059 | |
| 4060 | /* If some data is not send, declares the situation to the |
| 4061 | * applet, and returns a yield. |
| 4062 | */ |
| 4063 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 4064 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4065 | 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] | 4066 | } |
| 4067 | |
| 4068 | return 1; |
| 4069 | } |
| 4070 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4071 | /* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4072 | * yield the LUA process, and resume it without checking the |
| 4073 | * input arguments. |
| 4074 | */ |
| 4075 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 4076 | { |
| 4077 | MAY_LJMP(check_args(L, 2, "send")); |
| 4078 | lua_pushinteger(L, 0); |
| 4079 | |
| 4080 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 4081 | } |
| 4082 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4083 | /* |
| 4084 | * |
| 4085 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4086 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4087 | * |
| 4088 | * |
| 4089 | */ |
| 4090 | |
| 4091 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4092 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4093 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4094 | __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] | 4095 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4096 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4097 | } |
| 4098 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4099 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4100 | * according with a current TXN. |
| 4101 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4102 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4103 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4104 | struct hlua_appctx *appctx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 4105 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4106 | struct stream_interface *si = ctx->owner; |
| 4107 | struct stream *s = si_strm(si); |
| 4108 | struct proxy *px = s->be; |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4109 | struct htx *htx; |
| 4110 | struct htx_blk *blk; |
| 4111 | struct htx_sl *sl; |
| 4112 | struct ist path; |
| 4113 | unsigned long long len = 0; |
| 4114 | int32_t pos; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4115 | |
| 4116 | /* Check stack size. */ |
| 4117 | if (!lua_checkstack(L, 3)) |
| 4118 | return 0; |
| 4119 | |
| 4120 | /* Create the object: obj[0] = userdata. |
| 4121 | * Note that the base of the Converters object is the |
| 4122 | * same than the TXN object. |
| 4123 | */ |
| 4124 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4125 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4126 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4127 | appctx->appctx = ctx; |
| 4128 | appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4129 | appctx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4130 | appctx->htxn.s = s; |
| 4131 | appctx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4132 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4133 | /* Create the "f" field that contains a list of fetches. */ |
| 4134 | lua_pushstring(L, "f"); |
| 4135 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 4136 | return 0; |
| 4137 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4138 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4139 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4140 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4141 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4142 | return 0; |
| 4143 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4144 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4145 | /* Create the "c" field that contains a list of converters. */ |
| 4146 | lua_pushstring(L, "c"); |
| 4147 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 4148 | return 0; |
| 4149 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4150 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4151 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4152 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4153 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4154 | return 0; |
| 4155 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4156 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4157 | htx = htxbuf(&s->req.buf); |
| 4158 | blk = htx_get_first_blk(htx); |
Christopher Faulet | ea00973 | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 4159 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4160 | sl = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4161 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4162 | /* Stores the request method. */ |
| 4163 | lua_pushstring(L, "method"); |
| 4164 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 4165 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4166 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4167 | /* Stores the http version. */ |
| 4168 | lua_pushstring(L, "version"); |
| 4169 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 4170 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4171 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4172 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 4173 | * the array on the top of the stack. |
| 4174 | */ |
| 4175 | lua_pushstring(L, "headers"); |
| 4176 | htxn.s = s; |
| 4177 | htxn.p = px; |
| 4178 | htxn.dir = SMP_OPT_DIR_REQ; |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4179 | if (!hlua_http_get_headers(L, &htxn.s->txn->req)) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4180 | return 0; |
| 4181 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4182 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4183 | path = http_get_path(htx_sl_req_uri(sl)); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 4184 | if (isttest(path)) { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4185 | char *p, *q, *end; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4186 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4187 | p = path.ptr; |
| 4188 | end = path.ptr + path.len; |
| 4189 | q = p; |
| 4190 | while (q < end && *q != '?') |
| 4191 | q++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4192 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4193 | /* Stores the request path. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4194 | lua_pushstring(L, "path"); |
| 4195 | lua_pushlstring(L, p, q - p); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4196 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4197 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4198 | /* Stores the query string. */ |
| 4199 | lua_pushstring(L, "qs"); |
| 4200 | if (*q == '?') |
| 4201 | q++; |
| 4202 | lua_pushlstring(L, q, end - q); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4203 | lua_settable(L, -3); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4204 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4205 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4206 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4207 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4208 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4209 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4210 | if (type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4211 | break; |
| 4212 | if (type == HTX_BLK_DATA) |
| 4213 | len += htx_get_blksz(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4214 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4215 | if (htx->extra != ULLONG_MAX) |
| 4216 | len += htx->extra; |
| 4217 | |
| 4218 | /* Stores the request path. */ |
| 4219 | lua_pushstring(L, "length"); |
| 4220 | lua_pushinteger(L, len); |
| 4221 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4222 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4223 | /* Create an empty array of HTTP request headers. */ |
| 4224 | lua_pushstring(L, "response"); |
| 4225 | lua_newtable(L); |
| 4226 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4227 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4228 | /* Pop a class stream metatable and affect it to the table. */ |
| 4229 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 4230 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4231 | |
| 4232 | return 1; |
| 4233 | } |
| 4234 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4235 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 4236 | { |
| 4237 | struct hlua_appctx *appctx; |
| 4238 | struct stream *s; |
| 4239 | const char *name; |
| 4240 | size_t len; |
| 4241 | struct sample smp; |
| 4242 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4243 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 4244 | 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] | 4245 | |
| 4246 | /* It is useles to retrieve the stream, but this function |
| 4247 | * runs only in a stream context. |
| 4248 | */ |
| 4249 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4250 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4251 | s = appctx->htxn.s; |
| 4252 | |
| 4253 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4254 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4255 | hlua_lua2smp(L, 3, &smp); |
| 4256 | |
| 4257 | /* Store the sample in a variable. */ |
| 4258 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4259 | |
| 4260 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 4261 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 4262 | else |
| 4263 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 4264 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4265 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4266 | } |
| 4267 | |
| 4268 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 4269 | { |
| 4270 | struct hlua_appctx *appctx; |
| 4271 | struct stream *s; |
| 4272 | const char *name; |
| 4273 | size_t len; |
| 4274 | struct sample smp; |
| 4275 | |
| 4276 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4277 | |
| 4278 | /* It is useles to retrieve the stream, but this function |
| 4279 | * runs only in a stream context. |
| 4280 | */ |
| 4281 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4282 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4283 | s = appctx->htxn.s; |
| 4284 | |
| 4285 | /* Unset the variable. */ |
| 4286 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4287 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4288 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4289 | } |
| 4290 | |
| 4291 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4292 | { |
| 4293 | struct hlua_appctx *appctx; |
| 4294 | struct stream *s; |
| 4295 | const char *name; |
| 4296 | size_t len; |
| 4297 | struct sample smp; |
| 4298 | |
| 4299 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4300 | |
| 4301 | /* It is useles to retrieve the stream, but this function |
| 4302 | * runs only in a stream context. |
| 4303 | */ |
| 4304 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4305 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4306 | s = appctx->htxn.s; |
| 4307 | |
| 4308 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4309 | if (!vars_get_by_name(name, len, &smp)) { |
| 4310 | lua_pushnil(L); |
| 4311 | return 1; |
| 4312 | } |
| 4313 | |
| 4314 | return hlua_smp2lua(L, &smp); |
| 4315 | } |
| 4316 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4317 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 4318 | { |
| 4319 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4320 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4321 | struct hlua *hlua; |
| 4322 | |
| 4323 | /* 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] | 4324 | if (!s->hlua) |
| 4325 | return 0; |
| 4326 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4327 | |
| 4328 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4329 | |
| 4330 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4331 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4332 | |
| 4333 | /* Get and store new value. */ |
| 4334 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4335 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4336 | |
| 4337 | return 0; |
| 4338 | } |
| 4339 | |
| 4340 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4341 | { |
| 4342 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4343 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4344 | struct hlua *hlua; |
| 4345 | |
| 4346 | /* 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] | 4347 | if (!s->hlua) { |
| 4348 | lua_pushnil(L); |
| 4349 | return 1; |
| 4350 | } |
| 4351 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4352 | |
| 4353 | /* Push configuration index in the stack. */ |
| 4354 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4355 | |
| 4356 | return 1; |
| 4357 | } |
| 4358 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4359 | /* If expected data not yet available, it returns a yield. This function |
| 4360 | * consumes the data in the buffer. It returns a string containing the |
| 4361 | * data. This string can be empty. |
| 4362 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4363 | __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] | 4364 | { |
| 4365 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4366 | struct stream_interface *si = appctx->appctx->owner; |
| 4367 | struct channel *req = si_oc(si); |
| 4368 | struct htx *htx; |
| 4369 | struct htx_blk *blk; |
| 4370 | size_t count; |
| 4371 | int stop = 0; |
| 4372 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4373 | htx = htx_from_buf(&req->buf); |
| 4374 | count = co_data(req); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4375 | blk = htx_get_first_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 4376 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4377 | while (count && !stop && blk) { |
| 4378 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4379 | uint32_t sz = htx_get_blksz(blk); |
| 4380 | struct ist v; |
| 4381 | uint32_t vlen; |
| 4382 | char *nl; |
| 4383 | |
| 4384 | vlen = sz; |
| 4385 | if (vlen > count) { |
| 4386 | if (type != HTX_BLK_DATA) |
| 4387 | break; |
| 4388 | vlen = count; |
| 4389 | } |
| 4390 | |
| 4391 | switch (type) { |
| 4392 | case HTX_BLK_UNUSED: |
| 4393 | break; |
| 4394 | |
| 4395 | case HTX_BLK_DATA: |
| 4396 | v = htx_get_blk_value(htx, blk); |
| 4397 | v.len = vlen; |
| 4398 | nl = istchr(v, '\n'); |
| 4399 | if (nl != NULL) { |
| 4400 | stop = 1; |
| 4401 | vlen = nl - v.ptr + 1; |
| 4402 | } |
| 4403 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4404 | break; |
| 4405 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4406 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4407 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4408 | stop = 1; |
| 4409 | break; |
| 4410 | |
| 4411 | default: |
| 4412 | break; |
| 4413 | } |
| 4414 | |
| 4415 | co_set_data(req, co_data(req) - vlen); |
| 4416 | count -= vlen; |
| 4417 | if (sz == vlen) |
| 4418 | blk = htx_remove_blk(htx, blk); |
| 4419 | else { |
| 4420 | htx_cut_data_blk(htx, blk, vlen); |
| 4421 | break; |
| 4422 | } |
| 4423 | } |
| 4424 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4425 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4426 | if (!stop) { |
| 4427 | si_cant_get(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4428 | 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] | 4429 | } |
| 4430 | |
| 4431 | /* return the result. */ |
| 4432 | luaL_pushresult(&appctx->b); |
| 4433 | return 1; |
| 4434 | } |
| 4435 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4436 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4437 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4438 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4439 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4440 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4441 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4442 | /* Initialise the string catenation. */ |
| 4443 | luaL_buffinit(L, &appctx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4444 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4445 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4446 | } |
| 4447 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4448 | /* If expected data not yet available, it returns a yield. This function |
| 4449 | * consumes the data in the buffer. It returns a string containing the |
| 4450 | * data. This string can be empty. |
| 4451 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4452 | __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] | 4453 | { |
| 4454 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4455 | struct stream_interface *si = appctx->appctx->owner; |
| 4456 | struct channel *req = si_oc(si); |
| 4457 | struct htx *htx; |
| 4458 | struct htx_blk *blk; |
| 4459 | size_t count; |
| 4460 | int len; |
| 4461 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4462 | htx = htx_from_buf(&req->buf); |
| 4463 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4464 | count = co_data(req); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 4465 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4466 | while (count && len && blk) { |
| 4467 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4468 | uint32_t sz = htx_get_blksz(blk); |
| 4469 | struct ist v; |
| 4470 | uint32_t vlen; |
| 4471 | |
| 4472 | vlen = sz; |
| 4473 | if (len > 0 && vlen > len) |
| 4474 | vlen = len; |
| 4475 | if (vlen > count) { |
| 4476 | if (type != HTX_BLK_DATA) |
| 4477 | break; |
| 4478 | vlen = count; |
| 4479 | } |
| 4480 | |
| 4481 | switch (type) { |
| 4482 | case HTX_BLK_UNUSED: |
| 4483 | break; |
| 4484 | |
| 4485 | case HTX_BLK_DATA: |
| 4486 | v = htx_get_blk_value(htx, blk); |
| 4487 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4488 | break; |
| 4489 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4490 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4491 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4492 | len = 0; |
| 4493 | break; |
| 4494 | |
| 4495 | default: |
| 4496 | break; |
| 4497 | } |
| 4498 | |
| 4499 | co_set_data(req, co_data(req) - vlen); |
| 4500 | count -= vlen; |
| 4501 | if (len > 0) |
| 4502 | len -= vlen; |
| 4503 | if (sz == vlen) |
| 4504 | blk = htx_remove_blk(htx, blk); |
| 4505 | else { |
| 4506 | htx_cut_data_blk(htx, blk, vlen); |
| 4507 | break; |
| 4508 | } |
| 4509 | } |
| 4510 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4511 | htx_to_buf(htx, &req->buf); |
| 4512 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4513 | /* If we are no other data available, yield waiting for new data. */ |
| 4514 | if (len) { |
| 4515 | if (len > 0) { |
| 4516 | lua_pushinteger(L, len); |
| 4517 | lua_replace(L, 2); |
| 4518 | } |
| 4519 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4520 | 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] | 4521 | } |
| 4522 | |
| 4523 | /* return the result. */ |
| 4524 | luaL_pushresult(&appctx->b); |
| 4525 | return 1; |
| 4526 | } |
| 4527 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4528 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4529 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 4530 | { |
| 4531 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4532 | int len = -1; |
| 4533 | |
| 4534 | /* Check arguments. */ |
| 4535 | if (lua_gettop(L) > 2) |
| 4536 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4537 | if (lua_gettop(L) >= 2) { |
| 4538 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4539 | lua_pop(L, 1); |
| 4540 | } |
| 4541 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4542 | lua_pushinteger(L, len); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4543 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4544 | /* Initialise the string catenation. */ |
| 4545 | luaL_buffinit(L, &appctx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4546 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4547 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4548 | } |
| 4549 | |
| 4550 | /* Append data in the output side of the buffer. This data is immediately |
| 4551 | * sent. The function returns the amount of data written. If the buffer |
| 4552 | * cannot contain the data, the function yields. The function returns -1 |
| 4553 | * if the channel is closed. |
| 4554 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4555 | __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] | 4556 | { |
| 4557 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4558 | struct stream_interface *si = appctx->appctx->owner; |
| 4559 | struct channel *res = si_ic(si); |
| 4560 | struct htx *htx = htx_from_buf(&res->buf); |
| 4561 | const char *data; |
| 4562 | size_t len; |
| 4563 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4564 | int max; |
| 4565 | |
Christopher Faulet | 9060fc0 | 2019-07-03 11:39:30 +0200 | [diff] [blame] | 4566 | max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx)); |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4567 | if (!max) |
| 4568 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4569 | |
| 4570 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4571 | |
| 4572 | /* Get the max amount of data which can write as input in the channel. */ |
| 4573 | if (max > (len - l)) |
| 4574 | max = len - l; |
| 4575 | |
| 4576 | /* Copy data. */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 4577 | max = htx_add_data(htx, ist2(data + l, max)); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4578 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4579 | |
| 4580 | /* update counters. */ |
| 4581 | l += max; |
| 4582 | lua_pop(L, 1); |
| 4583 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4584 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4585 | /* If some data is not send, declares the situation to the |
| 4586 | * applet, and returns a yield. |
| 4587 | */ |
| 4588 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4589 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4590 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4591 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4592 | 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] | 4593 | } |
| 4594 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4595 | htx_to_buf(htx, &res->buf); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4596 | return 1; |
| 4597 | } |
| 4598 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4599 | /* Just a wrapper of "hlua_applet_send_yield". This wrapper permits |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4600 | * yield the LUA process, and resume it without checking the |
| 4601 | * input arguments. |
| 4602 | */ |
| 4603 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 4604 | { |
| 4605 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4606 | |
| 4607 | /* We want to send some data. Headers must be sent. */ |
| 4608 | if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 4609 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 4610 | WILL_LJMP(lua_error(L)); |
| 4611 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4612 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4613 | /* This integer is used for followinf the amount of data sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4614 | lua_pushinteger(L, 0); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4615 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4616 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4617 | } |
| 4618 | |
| 4619 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 4620 | { |
| 4621 | const char *name; |
| 4622 | int ret; |
| 4623 | |
| 4624 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4625 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4626 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 4627 | |
| 4628 | /* Push in the stack the "response" entry. */ |
| 4629 | ret = lua_getfield(L, 1, "response"); |
| 4630 | if (ret != LUA_TTABLE) { |
| 4631 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 4632 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 4633 | WILL_LJMP(lua_error(L)); |
| 4634 | } |
| 4635 | |
| 4636 | /* check if the header is already registered if it is not |
| 4637 | * the case, register it. |
| 4638 | */ |
| 4639 | ret = lua_getfield(L, -1, name); |
| 4640 | if (ret == LUA_TNIL) { |
| 4641 | |
| 4642 | /* Entry not found. */ |
| 4643 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 4644 | |
| 4645 | /* Insert the new header name in the array in the top of the stack. |
| 4646 | * It left the new array in the top of the stack. |
| 4647 | */ |
| 4648 | lua_newtable(L); |
| 4649 | lua_pushvalue(L, 2); |
| 4650 | lua_pushvalue(L, -2); |
| 4651 | lua_settable(L, -4); |
| 4652 | |
| 4653 | } else if (ret != LUA_TTABLE) { |
| 4654 | |
| 4655 | /* corruption error. */ |
| 4656 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 4657 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 4658 | WILL_LJMP(lua_error(L)); |
| 4659 | } |
| 4660 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 4661 | /* Now the top of thestack is an array of values. We push |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4662 | * the header value as new entry. |
| 4663 | */ |
| 4664 | lua_pushvalue(L, 3); |
| 4665 | ret = lua_rawlen(L, -2); |
| 4666 | lua_rawseti(L, -2, ret + 1); |
| 4667 | lua_pushboolean(L, 1); |
| 4668 | return 1; |
| 4669 | } |
| 4670 | |
| 4671 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 4672 | { |
| 4673 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4674 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4675 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4676 | |
| 4677 | if (status < 100 || status > 599) { |
| 4678 | lua_pushboolean(L, 0); |
| 4679 | return 1; |
| 4680 | } |
| 4681 | |
| 4682 | appctx->appctx->ctx.hlua_apphttp.status = status; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4683 | appctx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4684 | lua_pushboolean(L, 1); |
| 4685 | return 1; |
| 4686 | } |
| 4687 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4688 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4689 | __LJMP static int hlua_applet_http_send_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4690 | { |
| 4691 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4692 | struct stream_interface *si = appctx->appctx->owner; |
| 4693 | struct channel *res = si_ic(si); |
| 4694 | struct htx *htx; |
| 4695 | struct htx_sl *sl; |
| 4696 | struct h1m h1m; |
| 4697 | const char *status, *reason; |
| 4698 | const char *name, *value; |
| 4699 | size_t nlen, vlen; |
| 4700 | unsigned int flags; |
| 4701 | |
| 4702 | /* Send the message at once. */ |
| 4703 | htx = htx_from_buf(&res->buf); |
| 4704 | h1m_init_res(&h1m); |
| 4705 | |
| 4706 | /* Use the same http version than the request. */ |
| 4707 | status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size); |
| 4708 | reason = appctx->appctx->ctx.hlua_apphttp.reason; |
| 4709 | if (reason == NULL) |
| 4710 | reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status); |
| 4711 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) { |
| 4712 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 4713 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 4714 | } |
| 4715 | else { |
| 4716 | flags = HTX_SL_F_IS_RESP; |
| 4717 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 4718 | } |
| 4719 | if (!sl) { |
| 4720 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 4721 | appctx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4722 | WILL_LJMP(lua_error(L)); |
| 4723 | } |
| 4724 | sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status; |
| 4725 | |
| 4726 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 4727 | lua_pushvalue(L, 0); |
| 4728 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 4729 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 4730 | appctx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4731 | WILL_LJMP(lua_error(L)); |
| 4732 | } |
| 4733 | |
| 4734 | /* Browse the list of headers. */ |
| 4735 | lua_pushnil(L); |
| 4736 | while(lua_next(L, -2) != 0) { |
| 4737 | /* We expect a string as -2. */ |
| 4738 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 4739 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 4740 | appctx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4741 | lua_typename(L, lua_type(L, -2))); |
| 4742 | WILL_LJMP(lua_error(L)); |
| 4743 | } |
| 4744 | name = lua_tolstring(L, -2, &nlen); |
| 4745 | |
| 4746 | /* We expect an array as -1. */ |
| 4747 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 4748 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 4749 | appctx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4750 | name, |
| 4751 | lua_typename(L, lua_type(L, -1))); |
| 4752 | WILL_LJMP(lua_error(L)); |
| 4753 | } |
| 4754 | |
| 4755 | /* Browse the table who is on the top of the stack. */ |
| 4756 | lua_pushnil(L); |
| 4757 | while(lua_next(L, -2) != 0) { |
| 4758 | int id; |
| 4759 | |
| 4760 | /* We expect a number as -2. */ |
| 4761 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 4762 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 4763 | appctx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4764 | name, |
| 4765 | lua_typename(L, lua_type(L, -2))); |
| 4766 | WILL_LJMP(lua_error(L)); |
| 4767 | } |
| 4768 | id = lua_tointeger(L, -2); |
| 4769 | |
| 4770 | /* We expect a string as -2. */ |
| 4771 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 4772 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 4773 | appctx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4774 | name, id, |
| 4775 | lua_typename(L, lua_type(L, -1))); |
| 4776 | WILL_LJMP(lua_error(L)); |
| 4777 | } |
| 4778 | value = lua_tolstring(L, -1, &vlen); |
| 4779 | |
| 4780 | /* Simple Protocol checks. */ |
| 4781 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 4782 | h1_parse_xfer_enc_header(&h1m, ist2(value, vlen)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4783 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 4784 | struct ist v = ist2(value, vlen); |
| 4785 | int ret; |
| 4786 | |
| 4787 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 4788 | if (ret < 0) { |
| 4789 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 4790 | appctx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4791 | name); |
| 4792 | WILL_LJMP(lua_error(L)); |
| 4793 | } |
| 4794 | else if (ret == 0) |
| 4795 | goto next; /* Skip it */ |
| 4796 | } |
| 4797 | |
| 4798 | /* Add a new header */ |
| 4799 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 4800 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 4801 | appctx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4802 | name); |
| 4803 | WILL_LJMP(lua_error(L)); |
| 4804 | } |
| 4805 | next: |
| 4806 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4807 | lua_pop(L, 1); |
| 4808 | } |
| 4809 | |
| 4810 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4811 | lua_pop(L, 1); |
| 4812 | } |
| 4813 | |
| 4814 | if (h1m.flags & H1_MF_CHNK) |
| 4815 | h1m.flags &= ~H1_MF_CLEN; |
| 4816 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 4817 | h1m.flags |= H1_MF_XFER_LEN; |
| 4818 | |
| 4819 | /* Uset HTX start-line flags */ |
| 4820 | if (h1m.flags & H1_MF_XFER_ENC) |
| 4821 | flags |= HTX_SL_F_XFER_ENC; |
| 4822 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 4823 | flags |= HTX_SL_F_XFER_LEN; |
| 4824 | if (h1m.flags & H1_MF_CHNK) |
| 4825 | flags |= HTX_SL_F_CHNK; |
| 4826 | else if (h1m.flags & H1_MF_CLEN) |
| 4827 | flags |= HTX_SL_F_CLEN; |
| 4828 | if (h1m.body_len == 0) |
| 4829 | flags |= HTX_SL_F_BODYLESS; |
| 4830 | } |
| 4831 | sl->flags |= flags; |
| 4832 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 4833 | /* 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] | 4834 | * and the status code implies the presence of a message body, we must |
| 4835 | * announce a transfer encoding chunked. This is required by haproxy |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4836 | * for the keepalive compliance. If the applet announces a transfer-encoding |
| 4837 | * chunked itself, don't do anything. |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4838 | */ |
| 4839 | if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 && |
| 4840 | appctx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 4841 | appctx->appctx->ctx.hlua_apphttp.status != 204 && |
| 4842 | appctx->appctx->ctx.hlua_apphttp.status != 304) { |
| 4843 | /* Add a new header */ |
| 4844 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 4845 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 4846 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 4847 | appctx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4848 | WILL_LJMP(lua_error(L)); |
| 4849 | } |
| 4850 | } |
| 4851 | |
| 4852 | /* Finalize headers. */ |
| 4853 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 4854 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 4855 | appctx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4856 | WILL_LJMP(lua_error(L)); |
| 4857 | } |
| 4858 | |
| 4859 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 4860 | b_reset(&res->buf); |
| 4861 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 4862 | WILL_LJMP(lua_error(L)); |
| 4863 | } |
| 4864 | |
| 4865 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4866 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4867 | |
| 4868 | /* Headers sent, set the flag. */ |
| 4869 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
| 4870 | return 0; |
| 4871 | |
| 4872 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4873 | /* We will build the status line and the headers of the HTTP response. |
| 4874 | * We will try send at once if its not possible, we give back the hand |
| 4875 | * waiting for more room. |
| 4876 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4877 | __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] | 4878 | { |
| 4879 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4880 | struct stream_interface *si = appctx->appctx->owner; |
| 4881 | struct channel *res = si_ic(si); |
| 4882 | |
| 4883 | if (co_data(res)) { |
| 4884 | si_rx_room_blk(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4885 | 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] | 4886 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4887 | return MAY_LJMP(hlua_applet_http_send_response(L)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4888 | } |
| 4889 | |
| 4890 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4891 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4892 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4893 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4894 | } |
| 4895 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4896 | /* |
| 4897 | * |
| 4898 | * |
| 4899 | * Class HTTP |
| 4900 | * |
| 4901 | * |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4902 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4903 | |
| 4904 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4905 | * a class stream, otherwise it throws an error. |
| 4906 | */ |
| 4907 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4908 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4909 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
| 4910 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4911 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4912 | /* This function creates and push in the stack a HTTP object |
| 4913 | * according with a current TXN. |
| 4914 | */ |
| 4915 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 4916 | { |
| 4917 | struct hlua_txn *htxn; |
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 | /* Check stack size. */ |
| 4920 | if (!lua_checkstack(L, 3)) |
| 4921 | return 0; |
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 | /* Create the object: obj[0] = userdata. |
| 4924 | * Note that the base of the Converters object is the |
| 4925 | * same than the TXN object. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4926 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4927 | lua_newtable(L); |
| 4928 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 4929 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4930 | |
| 4931 | htxn->s = txn->s; |
| 4932 | htxn->p = txn->p; |
Christopher Faulet | 256b69a | 2019-05-23 11:14:21 +0200 | [diff] [blame] | 4933 | htxn->dir = txn->dir; |
| 4934 | htxn->flags = txn->flags; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4935 | |
| 4936 | /* Pop a class stream metatable and affect it to the table. */ |
| 4937 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 4938 | lua_setmetatable(L, -2); |
| 4939 | |
| 4940 | return 1; |
| 4941 | } |
| 4942 | |
| 4943 | /* This function creates ans returns an array of HTTP headers. |
| 4944 | * This function does not fails. It is used as wrapper with the |
| 4945 | * 2 following functions. |
| 4946 | */ |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4947 | __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] | 4948 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4949 | struct htx *htx; |
| 4950 | int32_t pos; |
| 4951 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4952 | /* Create the table. */ |
| 4953 | lua_newtable(L); |
| 4954 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4955 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4956 | htx = htxbuf(&msg->chn->buf); |
| 4957 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4958 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4959 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4960 | struct ist n, v; |
| 4961 | int len; |
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 | if (type == HTX_BLK_HDR) { |
| 4964 | n = htx_get_blk_name(htx,blk); |
| 4965 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4966 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4967 | else if (type == HTX_BLK_EOH) |
| 4968 | break; |
| 4969 | else |
| 4970 | continue; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4971 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4972 | /* Check for existing entry: |
| 4973 | * assume that the table is on the top of the stack, and |
| 4974 | * push the key in the stack, the function lua_gettable() |
| 4975 | * perform the lookup. |
| 4976 | */ |
| 4977 | lua_pushlstring(L, n.ptr, n.len); |
| 4978 | lua_gettable(L, -2); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4979 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4980 | switch (lua_type(L, -1)) { |
| 4981 | case LUA_TNIL: |
| 4982 | /* Table not found, create it. */ |
| 4983 | lua_pop(L, 1); /* remove the nil value. */ |
| 4984 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 4985 | lua_newtable(L); /* create and push empty table. */ |
| 4986 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 4987 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 4988 | 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] | 4989 | break; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4990 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4991 | case LUA_TTABLE: |
| 4992 | /* Entry found: push the value in the table. */ |
| 4993 | len = lua_rawlen(L, -1); |
| 4994 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 4995 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 4996 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 4997 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4998 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4999 | default: |
| 5000 | /* Other cases are errors. */ |
| 5001 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 5002 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5003 | } |
| 5004 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5005 | return 1; |
| 5006 | } |
| 5007 | |
| 5008 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 5009 | { |
| 5010 | struct hlua_txn *htxn; |
| 5011 | |
| 5012 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 5013 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5014 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5015 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5016 | WILL_LJMP(lua_error(L)); |
| 5017 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5018 | return hlua_http_get_headers(L, &htxn->s->txn->req); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5019 | } |
| 5020 | |
| 5021 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 5022 | { |
| 5023 | struct hlua_txn *htxn; |
| 5024 | |
| 5025 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 5026 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5027 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5028 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5029 | WILL_LJMP(lua_error(L)); |
| 5030 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5031 | return hlua_http_get_headers(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5032 | } |
| 5033 | |
| 5034 | /* This function replace full header, or just a value in |
| 5035 | * the request or in the response. It is a wrapper fir the |
| 5036 | * 4 following functions. |
| 5037 | */ |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5038 | __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] | 5039 | { |
| 5040 | size_t name_len; |
| 5041 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5042 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5043 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5044 | struct htx *htx; |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5045 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5046 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5047 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5048 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 5049 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5050 | htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5051 | 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] | 5052 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5053 | return 0; |
| 5054 | } |
| 5055 | |
| 5056 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 5057 | { |
| 5058 | struct hlua_txn *htxn; |
| 5059 | |
| 5060 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5061 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5062 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5063 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5064 | WILL_LJMP(lua_error(L)); |
| 5065 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5066 | 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] | 5067 | } |
| 5068 | |
| 5069 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 5070 | { |
| 5071 | struct hlua_txn *htxn; |
| 5072 | |
| 5073 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 5074 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5075 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5076 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5077 | WILL_LJMP(lua_error(L)); |
| 5078 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5079 | return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5080 | } |
| 5081 | |
| 5082 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 5083 | { |
| 5084 | struct hlua_txn *htxn; |
| 5085 | |
| 5086 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5087 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5088 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5089 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5090 | WILL_LJMP(lua_error(L)); |
| 5091 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5092 | return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5093 | } |
| 5094 | |
| 5095 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 5096 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5097 | struct hlua_txn *htxn; |
| 5098 | |
| 5099 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 5100 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5101 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5102 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5103 | WILL_LJMP(lua_error(L)); |
| 5104 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5105 | return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5106 | } |
| 5107 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5108 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5109 | * It is a wrapper for the 2 following functions. |
| 5110 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5111 | __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] | 5112 | { |
| 5113 | size_t len; |
| 5114 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5115 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5116 | struct http_hdr_ctx ctx; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5117 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5118 | ctx.blk = NULL; |
| 5119 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 5120 | http_remove_header(htx, &ctx); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5121 | return 0; |
| 5122 | } |
| 5123 | |
| 5124 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 5125 | { |
| 5126 | struct hlua_txn *htxn; |
| 5127 | |
| 5128 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 5129 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5130 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5131 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5132 | WILL_LJMP(lua_error(L)); |
| 5133 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5134 | return hlua_http_del_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5135 | } |
| 5136 | |
| 5137 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 5138 | { |
| 5139 | struct hlua_txn *htxn; |
| 5140 | |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5141 | MAY_LJMP(check_args(L, 2, "res_del_hdr")); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5142 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5143 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5144 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5145 | WILL_LJMP(lua_error(L)); |
| 5146 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5147 | return hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5148 | } |
| 5149 | |
| 5150 | /* This function adds an header. It is a wrapper used by |
| 5151 | * the 2 following functions. |
| 5152 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5153 | __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] | 5154 | { |
| 5155 | size_t name_len; |
| 5156 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5157 | size_t value_len; |
| 5158 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5159 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5160 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5161 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 5162 | ist2(value, value_len))); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5163 | return 0; |
| 5164 | } |
| 5165 | |
| 5166 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 5167 | { |
| 5168 | struct hlua_txn *htxn; |
| 5169 | |
| 5170 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 5171 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5172 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5173 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5174 | WILL_LJMP(lua_error(L)); |
| 5175 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5176 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5177 | } |
| 5178 | |
| 5179 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 5180 | { |
| 5181 | struct hlua_txn *htxn; |
| 5182 | |
| 5183 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 5184 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5185 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5186 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5187 | WILL_LJMP(lua_error(L)); |
| 5188 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5189 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5190 | } |
| 5191 | |
| 5192 | static int hlua_http_req_set_hdr(lua_State *L) |
| 5193 | { |
| 5194 | struct hlua_txn *htxn; |
| 5195 | |
| 5196 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 5197 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5198 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5199 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5200 | WILL_LJMP(lua_error(L)); |
| 5201 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5202 | hlua_http_del_hdr(L, &htxn->s->txn->req); |
| 5203 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5204 | } |
| 5205 | |
| 5206 | static int hlua_http_res_set_hdr(lua_State *L) |
| 5207 | { |
| 5208 | struct hlua_txn *htxn; |
| 5209 | |
| 5210 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 5211 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5212 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5213 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5214 | WILL_LJMP(lua_error(L)); |
| 5215 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5216 | hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
| 5217 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5218 | } |
| 5219 | |
| 5220 | /* This function set the method. */ |
| 5221 | static int hlua_http_req_set_meth(lua_State *L) |
| 5222 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5223 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5224 | size_t name_len; |
| 5225 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5226 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5227 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5228 | WILL_LJMP(lua_error(L)); |
| 5229 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5230 | 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] | 5231 | return 1; |
| 5232 | } |
| 5233 | |
| 5234 | /* This function set the method. */ |
| 5235 | static int hlua_http_req_set_path(lua_State *L) |
| 5236 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5237 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5238 | size_t name_len; |
| 5239 | 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] | 5240 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5241 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5242 | WILL_LJMP(lua_error(L)); |
| 5243 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5244 | 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] | 5245 | return 1; |
| 5246 | } |
| 5247 | |
| 5248 | /* This function set the query-string. */ |
| 5249 | static int hlua_http_req_set_query(lua_State *L) |
| 5250 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5251 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5252 | size_t name_len; |
| 5253 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5254 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5255 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5256 | WILL_LJMP(lua_error(L)); |
| 5257 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5258 | /* Check length. */ |
| 5259 | if (name_len > trash.size - 1) { |
| 5260 | lua_pushboolean(L, 0); |
| 5261 | return 1; |
| 5262 | } |
| 5263 | |
| 5264 | /* Add the mark question as prefix. */ |
| 5265 | chunk_reset(&trash); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5266 | trash.area[trash.data++] = '?'; |
| 5267 | memcpy(trash.area + trash.data, name, name_len); |
| 5268 | trash.data += name_len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5269 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5270 | lua_pushboolean(L, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5271 | 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] | 5272 | return 1; |
| 5273 | } |
| 5274 | |
| 5275 | /* This function set the uri. */ |
| 5276 | static int hlua_http_req_set_uri(lua_State *L) |
| 5277 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5278 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5279 | size_t name_len; |
| 5280 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5281 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5282 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5283 | WILL_LJMP(lua_error(L)); |
| 5284 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5285 | lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5286 | return 1; |
| 5287 | } |
| 5288 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5289 | /* This function set the response code & optionally reason. */ |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5290 | static int hlua_http_res_set_status(lua_State *L) |
| 5291 | { |
| 5292 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5293 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 5294 | const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 5295 | const struct ist reason = ist2(str, (str ? strlen(str) : 0)); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5296 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5297 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5298 | WILL_LJMP(lua_error(L)); |
| 5299 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5300 | http_res_set_status(code, reason, htxn->s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5301 | return 0; |
| 5302 | } |
| 5303 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5304 | /* |
| 5305 | * |
| 5306 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5307 | * Class TXN |
| 5308 | * |
| 5309 | * |
| 5310 | */ |
| 5311 | |
| 5312 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5313 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5314 | */ |
| 5315 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 5316 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5317 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5318 | } |
| 5319 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5320 | __LJMP static int hlua_set_var(lua_State *L) |
| 5321 | { |
| 5322 | struct hlua_txn *htxn; |
| 5323 | const char *name; |
| 5324 | size_t len; |
| 5325 | struct sample smp; |
| 5326 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 5327 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 5328 | 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] | 5329 | |
| 5330 | /* It is useles to retrieve the stream, but this function |
| 5331 | * runs only in a stream context. |
| 5332 | */ |
| 5333 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5334 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5335 | |
| 5336 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 5337 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5338 | hlua_lua2smp(L, 3, &smp); |
| 5339 | |
| 5340 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5341 | 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] | 5342 | |
| 5343 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 5344 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 5345 | else |
| 5346 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 5347 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 5348 | return 1; |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5349 | } |
| 5350 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5351 | __LJMP static int hlua_unset_var(lua_State *L) |
| 5352 | { |
| 5353 | struct hlua_txn *htxn; |
| 5354 | const char *name; |
| 5355 | size_t len; |
| 5356 | struct sample smp; |
| 5357 | |
| 5358 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 5359 | |
| 5360 | /* It is useles to retrieve the stream, but this function |
| 5361 | * runs only in a stream context. |
| 5362 | */ |
| 5363 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5364 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5365 | |
| 5366 | /* Unset the variable. */ |
| 5367 | 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] | 5368 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 5369 | return 1; |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5370 | } |
| 5371 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5372 | __LJMP static int hlua_get_var(lua_State *L) |
| 5373 | { |
| 5374 | struct hlua_txn *htxn; |
| 5375 | const char *name; |
| 5376 | size_t len; |
| 5377 | struct sample smp; |
| 5378 | |
| 5379 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 5380 | |
| 5381 | /* It is useles to retrieve the stream, but this function |
| 5382 | * runs only in a stream context. |
| 5383 | */ |
| 5384 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5385 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5386 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5387 | 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] | 5388 | if (!vars_get_by_name(name, len, &smp)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5389 | lua_pushnil(L); |
| 5390 | return 1; |
| 5391 | } |
| 5392 | |
| 5393 | return hlua_smp2lua(L, &smp); |
| 5394 | } |
| 5395 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5396 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5397 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5398 | struct hlua *hlua; |
| 5399 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5400 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 5401 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5402 | /* It is useles to retrieve the stream, but this function |
| 5403 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5404 | */ |
| 5405 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5406 | |
| 5407 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5408 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5409 | if (!hlua) |
| 5410 | return 0; |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5411 | |
| 5412 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5413 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5414 | |
| 5415 | /* Get and store new value. */ |
| 5416 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5417 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5418 | |
| 5419 | return 0; |
| 5420 | } |
| 5421 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5422 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5423 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5424 | struct hlua *hlua; |
| 5425 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5426 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 5427 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5428 | /* It is useles to retrieve the stream, but this function |
| 5429 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5430 | */ |
| 5431 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5432 | |
| 5433 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5434 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5435 | if (!hlua) { |
| 5436 | lua_pushnil(L); |
| 5437 | return 1; |
| 5438 | } |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5439 | |
| 5440 | /* Push configuration index in the stack. */ |
| 5441 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5442 | |
| 5443 | return 1; |
| 5444 | } |
| 5445 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5446 | /* Create stack entry containing a class TXN. This function |
| 5447 | * return 0 if the stack does not contains free slots, |
| 5448 | * otherwise it returns 1. |
| 5449 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5450 | 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] | 5451 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5452 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5453 | |
| 5454 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5455 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5456 | return 0; |
| 5457 | |
| 5458 | /* NOTE: The allocation never fails. The failure |
| 5459 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5460 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5461 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5462 | /* Create the object: obj[0] = userdata. */ |
| 5463 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5464 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5465 | lua_rawseti(L, -2, 0); |
| 5466 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5467 | htxn->s = s; |
| 5468 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 5469 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5470 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5471 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5472 | /* Create the "f" field that contains a list of fetches. */ |
| 5473 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5474 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5475 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5476 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5477 | |
| 5478 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 5479 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5480 | 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] | 5481 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5482 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5483 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5484 | /* Create the "c" field that contains a list of converters. */ |
| 5485 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5486 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5487 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5488 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5489 | |
| 5490 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 5491 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 5492 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5493 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5494 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5495 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5496 | /* Create the "req" field that contains the request channel object. */ |
| 5497 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5498 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5499 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5500 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5501 | |
| 5502 | /* Create the "res" field that contains the response channel object. */ |
| 5503 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5504 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5505 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5506 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5507 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5508 | /* Creates the HTTP object is the current proxy allows http. */ |
| 5509 | lua_pushstring(L, "http"); |
| 5510 | if (p->mode == PR_MODE_HTTP) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5511 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5512 | return 0; |
| 5513 | } |
| 5514 | else |
| 5515 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5516 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5517 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5518 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5519 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 5520 | lua_setmetatable(L, -2); |
| 5521 | |
| 5522 | return 1; |
| 5523 | } |
| 5524 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5525 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 5526 | { |
| 5527 | const char *msg; |
| 5528 | struct hlua_txn *htxn; |
| 5529 | |
| 5530 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 5531 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5532 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5533 | |
| 5534 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 5535 | return 0; |
| 5536 | } |
| 5537 | |
| 5538 | __LJMP static int hlua_txn_log(lua_State *L) |
| 5539 | { |
| 5540 | int level; |
| 5541 | const char *msg; |
| 5542 | struct hlua_txn *htxn; |
| 5543 | |
| 5544 | MAY_LJMP(check_args(L, 3, "log")); |
| 5545 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5546 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5547 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5548 | |
| 5549 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5550 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5551 | |
| 5552 | hlua_sendlog(htxn->s->be, level, msg); |
| 5553 | return 0; |
| 5554 | } |
| 5555 | |
| 5556 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 5557 | { |
| 5558 | const char *msg; |
| 5559 | struct hlua_txn *htxn; |
| 5560 | |
| 5561 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 5562 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5563 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5564 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 5565 | return 0; |
| 5566 | } |
| 5567 | |
| 5568 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 5569 | { |
| 5570 | const char *msg; |
| 5571 | struct hlua_txn *htxn; |
| 5572 | |
| 5573 | MAY_LJMP(check_args(L, 2, "Info")); |
| 5574 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5575 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5576 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 5577 | return 0; |
| 5578 | } |
| 5579 | |
| 5580 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 5581 | { |
| 5582 | const char *msg; |
| 5583 | struct hlua_txn *htxn; |
| 5584 | |
| 5585 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 5586 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5587 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5588 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 5589 | return 0; |
| 5590 | } |
| 5591 | |
| 5592 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 5593 | { |
| 5594 | const char *msg; |
| 5595 | struct hlua_txn *htxn; |
| 5596 | |
| 5597 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 5598 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5599 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5600 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 5601 | return 0; |
| 5602 | } |
| 5603 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5604 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 5605 | { |
| 5606 | struct hlua_txn *htxn; |
| 5607 | int ll; |
| 5608 | |
| 5609 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 5610 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5611 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5612 | |
| 5613 | if (ll < 0 || ll > 7) |
| 5614 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 5615 | |
| 5616 | htxn->s->logs.level = ll; |
| 5617 | return 0; |
| 5618 | } |
| 5619 | |
| 5620 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 5621 | { |
| 5622 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5623 | int tos; |
| 5624 | |
| 5625 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 5626 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5627 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5628 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 5629 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5630 | return 0; |
| 5631 | } |
| 5632 | |
| 5633 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 5634 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5635 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5636 | int mark; |
| 5637 | |
| 5638 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 5639 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5640 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5641 | |
Lukas Tribus | 579e3e3 | 2019-08-11 18:03:45 +0200 | [diff] [blame] | 5642 | conn_set_mark(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5643 | return 0; |
| 5644 | } |
| 5645 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 5646 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 5647 | { |
| 5648 | struct hlua_txn *htxn; |
| 5649 | |
| 5650 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 5651 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5652 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5653 | return 0; |
| 5654 | } |
| 5655 | |
| 5656 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 5657 | { |
| 5658 | struct hlua_txn *htxn; |
| 5659 | |
| 5660 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 5661 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5662 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5663 | return 0; |
| 5664 | } |
| 5665 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5666 | /* 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] | 5667 | * 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] | 5668 | * message and terminate the transaction. It returns 1 on success and 0 on |
| 5669 | * error. The Reply must be on top of the stack. |
| 5670 | */ |
| 5671 | __LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s) |
| 5672 | { |
| 5673 | struct htx *htx; |
| 5674 | struct htx_sl *sl; |
| 5675 | struct h1m h1m; |
| 5676 | const char *status, *reason, *body; |
| 5677 | size_t status_len, reason_len, body_len; |
| 5678 | int ret, code, flags; |
| 5679 | |
| 5680 | code = 200; |
| 5681 | status = "200"; |
| 5682 | status_len = 3; |
| 5683 | ret = lua_getfield(L, -1, "status"); |
| 5684 | if (ret == LUA_TNUMBER) { |
| 5685 | code = lua_tointeger(L, -1); |
| 5686 | status = lua_tolstring(L, -1, &status_len); |
| 5687 | } |
| 5688 | lua_pop(L, 1); |
| 5689 | |
| 5690 | reason = http_get_reason(code); |
| 5691 | reason_len = strlen(reason); |
| 5692 | ret = lua_getfield(L, -1, "reason"); |
| 5693 | if (ret == LUA_TSTRING) |
| 5694 | reason = lua_tolstring(L, -1, &reason_len); |
| 5695 | lua_pop(L, 1); |
| 5696 | |
| 5697 | body = NULL; |
| 5698 | body_len = 0; |
| 5699 | ret = lua_getfield(L, -1, "body"); |
| 5700 | if (ret == LUA_TSTRING) |
| 5701 | body = lua_tolstring(L, -1, &body_len); |
| 5702 | lua_pop(L, 1); |
| 5703 | |
| 5704 | /* Prepare the response before inserting the headers */ |
| 5705 | h1m_init_res(&h1m); |
| 5706 | htx = htx_from_buf(&s->res.buf); |
| 5707 | channel_htx_truncate(&s->res, htx); |
| 5708 | if (s->txn->req.flags & HTTP_MSGF_VER_11) { |
| 5709 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 5710 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), |
| 5711 | ist2(status, status_len), ist2(reason, reason_len)); |
| 5712 | } |
| 5713 | else { |
| 5714 | flags = HTX_SL_F_IS_RESP; |
| 5715 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), |
| 5716 | ist2(status, status_len), ist2(reason, reason_len)); |
| 5717 | } |
| 5718 | if (!sl) |
| 5719 | goto fail; |
| 5720 | sl->info.res.status = code; |
| 5721 | |
| 5722 | /* Push in the stack the "headers" entry. */ |
| 5723 | ret = lua_getfield(L, -1, "headers"); |
| 5724 | if (ret != LUA_TTABLE) |
| 5725 | goto skip_headers; |
| 5726 | |
| 5727 | lua_pushnil(L); |
| 5728 | while (lua_next(L, -2) != 0) { |
| 5729 | struct ist name, value; |
| 5730 | const char *n, *v; |
| 5731 | size_t nlen, vlen; |
| 5732 | |
| 5733 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 5734 | /* Skip element if the key is not a string or if the value is not a table */ |
| 5735 | goto next_hdr; |
| 5736 | } |
| 5737 | |
| 5738 | n = lua_tolstring(L, -2, &nlen); |
| 5739 | name = ist2(n, nlen); |
| 5740 | if (isteqi(name, ist("content-length"))) { |
| 5741 | /* Always skip content-length header. It will be added |
| 5742 | * later with the correct len |
| 5743 | */ |
| 5744 | goto next_hdr; |
| 5745 | } |
| 5746 | |
| 5747 | /* Loop on header's values */ |
| 5748 | lua_pushnil(L); |
| 5749 | while (lua_next(L, -2)) { |
| 5750 | if (!lua_isstring(L, -1)) { |
| 5751 | /* Skip the value if it is not a string */ |
| 5752 | goto next_value; |
| 5753 | } |
| 5754 | |
| 5755 | v = lua_tolstring(L, -1, &vlen); |
| 5756 | value = ist2(v, vlen); |
| 5757 | |
| 5758 | if (isteqi(name, ist("transfer-encoding"))) |
| 5759 | h1_parse_xfer_enc_header(&h1m, value); |
| 5760 | if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen))) |
| 5761 | goto fail; |
| 5762 | |
| 5763 | next_value: |
| 5764 | lua_pop(L, 1); |
| 5765 | } |
| 5766 | |
| 5767 | next_hdr: |
| 5768 | lua_pop(L, 1); |
| 5769 | } |
| 5770 | skip_headers: |
| 5771 | lua_pop(L, 1); |
| 5772 | |
| 5773 | /* Update h1m flags: CLEN is set if CHNK is not present */ |
| 5774 | if (!(h1m.flags & H1_MF_CHNK)) { |
| 5775 | const char *clen = ultoa(body_len); |
| 5776 | |
| 5777 | h1m.flags |= H1_MF_CLEN; |
| 5778 | if (!htx_add_header(htx, ist("content-length"), ist(clen))) |
| 5779 | goto fail; |
| 5780 | } |
| 5781 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 5782 | h1m.flags |= H1_MF_XFER_LEN; |
| 5783 | |
| 5784 | /* Update HTX start-line flags */ |
| 5785 | if (h1m.flags & H1_MF_XFER_ENC) |
| 5786 | flags |= HTX_SL_F_XFER_ENC; |
| 5787 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 5788 | flags |= HTX_SL_F_XFER_LEN; |
| 5789 | if (h1m.flags & H1_MF_CHNK) |
| 5790 | flags |= HTX_SL_F_CHNK; |
| 5791 | else if (h1m.flags & H1_MF_CLEN) |
| 5792 | flags |= HTX_SL_F_CLEN; |
| 5793 | if (h1m.body_len == 0) |
| 5794 | flags |= HTX_SL_F_BODYLESS; |
| 5795 | } |
| 5796 | sl->flags |= flags; |
| 5797 | |
| 5798 | |
| 5799 | if (!htx_add_endof(htx, HTX_BLK_EOH) || |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5800 | (body_len && !htx_add_data_atonce(htx, ist2(body, body_len)))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5801 | goto fail; |
| 5802 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5803 | htx->flags |= HTX_FL_EOM; |
| 5804 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5805 | /* Now, forward the response and terminate the transaction */ |
| 5806 | s->txn->status = code; |
| 5807 | htx_to_buf(htx, &s->res.buf); |
| 5808 | if (!http_forward_proxy_resp(s, 1)) |
| 5809 | goto fail; |
| 5810 | |
| 5811 | return 1; |
| 5812 | |
| 5813 | fail: |
| 5814 | channel_htx_truncate(&s->res, htx); |
| 5815 | return 0; |
| 5816 | } |
| 5817 | |
| 5818 | /* Terminate a transaction if called from a lua action. For TCP streams, |
| 5819 | * processing is just aborted. Nothing is returned to the client and all |
| 5820 | * arguments are ignored. For HTTP streams, if a reply is passed as argument, it |
| 5821 | * is forwarded to the client before terminating the transaction. On success, |
| 5822 | * the function exits with ACT_RET_DONE code. If an error occurred, it exits |
| 5823 | * with ACT_RET_ERR code. If this function is not called from a lua action, it |
| 5824 | * just exits without any processing. |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5825 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5826 | __LJMP static int hlua_txn_done(lua_State *L) |
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 | struct hlua_txn *htxn; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5829 | struct stream *s; |
| 5830 | int finst; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5831 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5832 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5833 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5834 | /* If the flags NOTERM is set, we cannot terminate the session, so we |
| 5835 | * just end the execution of the current lua code. */ |
| 5836 | if (htxn->flags & HLUA_TXN_NOTERM) |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5837 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5838 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5839 | s = htxn->s; |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5840 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5841 | struct channel *req = &s->req; |
| 5842 | struct channel *res = &s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5843 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5844 | channel_auto_read(req); |
| 5845 | channel_abort(req); |
| 5846 | channel_auto_close(req); |
| 5847 | channel_erase(req); |
| 5848 | |
| 5849 | res->wex = tick_add_ifset(now_ms, res->wto); |
| 5850 | channel_auto_read(res); |
| 5851 | channel_auto_close(res); |
| 5852 | channel_shutr_now(res); |
| 5853 | |
| 5854 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D); |
| 5855 | goto done; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5856 | } |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 5857 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5858 | if (lua_gettop(L) == 1 || !lua_istable(L, 2)) { |
| 5859 | /* No reply or invalid reply */ |
| 5860 | s->txn->status = 0; |
| 5861 | http_reply_and_close(s, 0, NULL); |
| 5862 | } |
| 5863 | else { |
| 5864 | /* Remove extra args to have the reply on top of the stack */ |
| 5865 | if (lua_gettop(L) > 2) |
| 5866 | lua_pop(L, lua_gettop(L) - 2); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5867 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5868 | if (!hlua_txn_forward_reply(L, s)) { |
| 5869 | if (!(s->flags & SF_ERR_MASK)) |
| 5870 | s->flags |= SF_ERR_PRXCOND; |
| 5871 | lua_pushinteger(L, ACT_RET_ERR); |
| 5872 | WILL_LJMP(hlua_done(L)); |
| 5873 | return 0; /* Never reached */ |
| 5874 | } |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 5875 | } |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5876 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5877 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H); |
| 5878 | if (htxn->dir == SMP_OPT_DIR_REQ) { |
| 5879 | /* let's log the request time */ |
| 5880 | s->logs.tv_request = now; |
| 5881 | if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */ |
| 5882 | _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1); |
| 5883 | } |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5884 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5885 | done: |
| 5886 | if (!(s->flags & SF_ERR_MASK)) |
| 5887 | s->flags |= SF_ERR_LOCAL; |
| 5888 | if (!(s->flags & SF_FINST_MASK)) |
| 5889 | s->flags |= finst; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5890 | |
Christopher Faulet | 4ad7310 | 2020-03-05 11:07:31 +0100 | [diff] [blame] | 5891 | lua_pushinteger(L, ACT_RET_ABRT); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5892 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5893 | return 0; |
| 5894 | } |
| 5895 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5896 | /* |
| 5897 | * |
| 5898 | * |
| 5899 | * Class REPLY |
| 5900 | * |
| 5901 | * |
| 5902 | */ |
| 5903 | |
| 5904 | /* Pushes the TXN reply onto the top of the stack. If the stask does not have a |
| 5905 | * free slots, the function fails and returns 0; |
| 5906 | */ |
| 5907 | static int hlua_txn_reply_new(lua_State *L) |
| 5908 | { |
| 5909 | struct hlua_txn *htxn; |
| 5910 | const char *reason, *body = NULL; |
| 5911 | int ret, status; |
| 5912 | |
| 5913 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5914 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5915 | hlua_pusherror(L, "txn object is not an HTTP transaction."); |
| 5916 | WILL_LJMP(lua_error(L)); |
| 5917 | } |
| 5918 | |
| 5919 | /* Default value */ |
| 5920 | status = 200; |
| 5921 | reason = http_get_reason(status); |
| 5922 | |
| 5923 | if (lua_istable(L, 2)) { |
| 5924 | /* load status and reason from the table argument at index 2 */ |
| 5925 | ret = lua_getfield(L, 2, "status"); |
| 5926 | if (ret == LUA_TNIL) |
| 5927 | goto reason; |
| 5928 | else if (ret != LUA_TNUMBER) { |
| 5929 | /* invalid status: ignore the reason */ |
| 5930 | goto body; |
| 5931 | } |
| 5932 | status = lua_tointeger(L, -1); |
| 5933 | |
| 5934 | reason: |
| 5935 | lua_pop(L, 1); /* restore the stack: remove status */ |
| 5936 | ret = lua_getfield(L, 2, "reason"); |
| 5937 | if (ret == LUA_TSTRING) |
| 5938 | reason = lua_tostring(L, -1); |
| 5939 | |
| 5940 | body: |
| 5941 | lua_pop(L, 1); /* restore the stack: remove invalid status or reason */ |
| 5942 | ret = lua_getfield(L, 2, "body"); |
| 5943 | if (ret == LUA_TSTRING) |
| 5944 | body = lua_tostring(L, -1); |
| 5945 | lua_pop(L, 1); /* restore the stack: remove body */ |
| 5946 | } |
| 5947 | |
| 5948 | /* Create the Reply table */ |
| 5949 | lua_newtable(L); |
| 5950 | |
| 5951 | /* Add status element */ |
| 5952 | lua_pushstring(L, "status"); |
| 5953 | lua_pushinteger(L, status); |
| 5954 | lua_settable(L, -3); |
| 5955 | |
| 5956 | /* Add reason element */ |
| 5957 | reason = http_get_reason(status); |
| 5958 | lua_pushstring(L, "reason"); |
| 5959 | lua_pushstring(L, reason); |
| 5960 | lua_settable(L, -3); |
| 5961 | |
| 5962 | /* Add body element, nil if undefined */ |
| 5963 | lua_pushstring(L, "body"); |
| 5964 | if (body) |
| 5965 | lua_pushstring(L, body); |
| 5966 | else |
| 5967 | lua_pushnil(L); |
| 5968 | lua_settable(L, -3); |
| 5969 | |
| 5970 | /* Add headers element */ |
| 5971 | lua_pushstring(L, "headers"); |
| 5972 | lua_newtable(L); |
| 5973 | |
| 5974 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 5975 | if (lua_istable(L, 2)) { |
| 5976 | /* load headers from the table argument at index 2. If it is a table, copy it. */ |
| 5977 | ret = lua_getfield(L, 2, "headers"); |
| 5978 | if (ret == LUA_TTABLE) { |
| 5979 | /* stack: [ ... <headers:table>, <table> ] */ |
| 5980 | lua_pushnil(L); |
| 5981 | while (lua_next(L, -2) != 0) { |
| 5982 | /* stack: [ ... <headers:table>, <table>, k, v] */ |
| 5983 | if (!lua_isstring(L, -1) && !lua_istable(L, -1)) { |
| 5984 | /* invalid value type, skip it */ |
| 5985 | lua_pop(L, 1); |
| 5986 | continue; |
| 5987 | } |
| 5988 | |
| 5989 | |
| 5990 | /* Duplicate the key and swap it with the value. */ |
| 5991 | lua_pushvalue(L, -2); |
| 5992 | lua_insert(L, -2); |
| 5993 | /* stack: [ ... <headers:table>, <table>, k, k, v ] */ |
| 5994 | |
| 5995 | lua_newtable(L); |
| 5996 | lua_insert(L, -2); |
| 5997 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */ |
| 5998 | |
| 5999 | if (lua_isstring(L, -1)) { |
| 6000 | /* push the value in the inner table */ |
| 6001 | lua_rawseti(L, -2, 1); |
| 6002 | } |
| 6003 | else { /* table */ |
| 6004 | lua_pushnil(L); |
| 6005 | while (lua_next(L, -2) != 0) { |
| 6006 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */ |
| 6007 | if (!lua_isstring(L, -1)) { |
| 6008 | /* invalid value type, skip it*/ |
| 6009 | lua_pop(L, 1); |
| 6010 | continue; |
| 6011 | } |
| 6012 | /* push the value in the inner table */ |
| 6013 | lua_rawseti(L, -4, lua_rawlen(L, -4) + 1); |
| 6014 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */ |
| 6015 | } |
| 6016 | lua_pop(L, 1); |
| 6017 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */ |
| 6018 | } |
| 6019 | |
| 6020 | /* push (k,v) on the stack in the headers table: |
| 6021 | * stack: [ ... <headers:table>, <table>, k, k, v ] |
| 6022 | */ |
| 6023 | lua_settable(L, -5); |
| 6024 | /* stack: [ ... <headers:table>, <table>, k ] */ |
| 6025 | } |
| 6026 | } |
| 6027 | lua_pop(L, 1); |
| 6028 | } |
| 6029 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 6030 | lua_settable(L, -3); |
| 6031 | /* stack: [ txn, <Arg:table>, <Reply:table> ] */ |
| 6032 | |
| 6033 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 6034 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref); |
| 6035 | lua_setmetatable(L, -2); |
| 6036 | return 1; |
| 6037 | } |
| 6038 | |
| 6039 | /* Set the reply status code, and optionally the reason. If no reason is |
| 6040 | * provided, the default one corresponding to the status code is used. |
| 6041 | */ |
| 6042 | __LJMP static int hlua_txn_reply_set_status(lua_State *L) |
| 6043 | { |
| 6044 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6045 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 6046 | |
| 6047 | /* First argument (self) must be a table */ |
| 6048 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6049 | |
| 6050 | if (status < 100 || status > 599) { |
| 6051 | lua_pushboolean(L, 0); |
| 6052 | return 1; |
| 6053 | } |
| 6054 | if (!reason) |
| 6055 | reason = http_get_reason(status); |
| 6056 | |
| 6057 | lua_pushinteger(L, status); |
| 6058 | lua_setfield(L, 1, "status"); |
| 6059 | |
| 6060 | lua_pushstring(L, reason); |
| 6061 | lua_setfield(L, 1, "reason"); |
| 6062 | |
| 6063 | lua_pushboolean(L, 1); |
| 6064 | return 1; |
| 6065 | } |
| 6066 | |
| 6067 | /* Add a header into the reply object. Each header name is associated to an |
| 6068 | * array of values in the "headers" table. If the header name is not found, a |
| 6069 | * new entry is created. |
| 6070 | */ |
| 6071 | __LJMP static int hlua_txn_reply_add_header(lua_State *L) |
| 6072 | { |
| 6073 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6074 | const char *value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 6075 | int ret; |
| 6076 | |
| 6077 | /* First argument (self) must be a table */ |
| 6078 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6079 | |
| 6080 | /* Push in the stack the "headers" entry. */ |
| 6081 | ret = lua_getfield(L, 1, "headers"); |
| 6082 | if (ret != LUA_TTABLE) { |
| 6083 | hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret)); |
| 6084 | WILL_LJMP(lua_error(L)); |
| 6085 | } |
| 6086 | |
| 6087 | /* check if the header is already registered. If not, register it. */ |
| 6088 | ret = lua_getfield(L, -1, name); |
| 6089 | if (ret == LUA_TNIL) { |
| 6090 | /* Entry not found. */ |
| 6091 | lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */ |
| 6092 | |
| 6093 | /* Insert the new header name in the array in the top of the stack. |
| 6094 | * It left the new array in the top of the stack. |
| 6095 | */ |
| 6096 | lua_newtable(L); |
| 6097 | lua_pushstring(L, name); |
| 6098 | lua_pushvalue(L, -2); |
| 6099 | lua_settable(L, -4); |
| 6100 | } |
| 6101 | else if (ret != LUA_TTABLE) { |
| 6102 | hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret)); |
| 6103 | WILL_LJMP(lua_error(L)); |
| 6104 | } |
| 6105 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 6106 | /* Now the top of thestack is an array of values. We push |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6107 | * the header value as new entry. |
| 6108 | */ |
| 6109 | lua_pushstring(L, value); |
| 6110 | ret = lua_rawlen(L, -2); |
| 6111 | lua_rawseti(L, -2, ret + 1); |
| 6112 | |
| 6113 | lua_pushboolean(L, 1); |
| 6114 | return 1; |
| 6115 | } |
| 6116 | |
| 6117 | /* Remove all occurrences of a given header name. */ |
| 6118 | __LJMP static int hlua_txn_reply_del_header(lua_State *L) |
| 6119 | { |
| 6120 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6121 | int ret; |
| 6122 | |
| 6123 | /* First argument (self) must be a table */ |
| 6124 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6125 | |
| 6126 | /* Push in the stack the "headers" entry. */ |
| 6127 | ret = lua_getfield(L, 1, "headers"); |
| 6128 | if (ret != LUA_TTABLE) { |
| 6129 | hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret)); |
| 6130 | WILL_LJMP(lua_error(L)); |
| 6131 | } |
| 6132 | |
| 6133 | lua_pushstring(L, name); |
| 6134 | lua_pushnil(L); |
| 6135 | lua_settable(L, -3); |
| 6136 | |
| 6137 | lua_pushboolean(L, 1); |
| 6138 | return 1; |
| 6139 | } |
| 6140 | |
| 6141 | /* Set the reply's body. Overwrite any existing entry. */ |
| 6142 | __LJMP static int hlua_txn_reply_set_body(lua_State *L) |
| 6143 | { |
| 6144 | const char *payload = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6145 | |
| 6146 | /* First argument (self) must be a table */ |
| 6147 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6148 | |
| 6149 | lua_pushstring(L, payload); |
| 6150 | lua_setfield(L, 1, "body"); |
| 6151 | |
| 6152 | lua_pushboolean(L, 1); |
| 6153 | return 1; |
| 6154 | } |
| 6155 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 6156 | __LJMP static int hlua_log(lua_State *L) |
| 6157 | { |
| 6158 | int level; |
| 6159 | const char *msg; |
| 6160 | |
| 6161 | MAY_LJMP(check_args(L, 2, "log")); |
| 6162 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 6163 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6164 | |
| 6165 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 6166 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 6167 | |
| 6168 | hlua_sendlog(NULL, level, msg); |
| 6169 | return 0; |
| 6170 | } |
| 6171 | |
| 6172 | __LJMP static int hlua_log_debug(lua_State *L) |
| 6173 | { |
| 6174 | const char *msg; |
| 6175 | |
| 6176 | MAY_LJMP(check_args(L, 1, "debug")); |
| 6177 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6178 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 6179 | return 0; |
| 6180 | } |
| 6181 | |
| 6182 | __LJMP static int hlua_log_info(lua_State *L) |
| 6183 | { |
| 6184 | const char *msg; |
| 6185 | |
| 6186 | MAY_LJMP(check_args(L, 1, "info")); |
| 6187 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6188 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 6189 | return 0; |
| 6190 | } |
| 6191 | |
| 6192 | __LJMP static int hlua_log_warning(lua_State *L) |
| 6193 | { |
| 6194 | const char *msg; |
| 6195 | |
| 6196 | MAY_LJMP(check_args(L, 1, "warning")); |
| 6197 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6198 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 6199 | return 0; |
| 6200 | } |
| 6201 | |
| 6202 | __LJMP static int hlua_log_alert(lua_State *L) |
| 6203 | { |
| 6204 | const char *msg; |
| 6205 | |
| 6206 | MAY_LJMP(check_args(L, 1, "alert")); |
| 6207 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6208 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6209 | return 0; |
| 6210 | } |
| 6211 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6212 | __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] | 6213 | { |
| 6214 | int wakeup_ms = lua_tointeger(L, -1); |
| 6215 | if (now_ms < wakeup_ms) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6216 | 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] | 6217 | return 0; |
| 6218 | } |
| 6219 | |
| 6220 | __LJMP static int hlua_sleep(lua_State *L) |
| 6221 | { |
| 6222 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6223 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6224 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6225 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6226 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6227 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6228 | wakeup_ms = tick_add(now_ms, delay); |
| 6229 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6230 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6231 | 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] | 6232 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6233 | } |
| 6234 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6235 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6236 | { |
| 6237 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6238 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6239 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6240 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6241 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6242 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6243 | wakeup_ms = tick_add(now_ms, delay); |
| 6244 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6245 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6246 | 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] | 6247 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6248 | } |
| 6249 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6250 | /* This functionis an LUA binding. it permits to give back |
| 6251 | * the hand at the HAProxy scheduler. It is used when the |
| 6252 | * LUA processing consumes a lot of time. |
| 6253 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6254 | __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] | 6255 | { |
| 6256 | return 0; |
| 6257 | } |
| 6258 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6259 | __LJMP static int hlua_yield(lua_State *L) |
| 6260 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6261 | 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] | 6262 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6263 | } |
| 6264 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6265 | /* This function change the nice of the currently executed |
| 6266 | * task. It is used set low or high priority at the current |
| 6267 | * task. |
| 6268 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 6269 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6270 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6271 | struct hlua *hlua; |
| 6272 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6273 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6274 | MAY_LJMP(check_args(L, 1, "set_nice")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6275 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6276 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 6277 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 6278 | hlua = hlua_gethlua(L); |
| 6279 | |
| 6280 | /* If the task is not set, I'm in a start mode. */ |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6281 | if (!hlua || !hlua->task) |
| 6282 | return 0; |
| 6283 | |
| 6284 | if (nice < -1024) |
| 6285 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6286 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6287 | nice = 1024; |
| 6288 | |
| 6289 | hlua->task->nice = nice; |
| 6290 | return 0; |
| 6291 | } |
| 6292 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6293 | /* 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] | 6294 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 6295 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 6296 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6297 | * |
| 6298 | * Task wrapper are longjmp safe because the only one Lua code |
| 6299 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6300 | */ |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 6301 | struct task *hlua_process_task(struct task *task, void *context, unsigned short state) |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6302 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6303 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6304 | enum hlua_exec status; |
| 6305 | |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 6306 | if (task->thread_mask == MAX_THREADS_MASK) |
| 6307 | task_set_affinity(task, tid_bit); |
| 6308 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6309 | /* If it is the first call to the task, we must initialize the |
| 6310 | * execution timeouts. |
| 6311 | */ |
| 6312 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6313 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6314 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6315 | /* Execute the Lua code. */ |
| 6316 | status = hlua_ctx_resume(hlua, 1); |
| 6317 | |
| 6318 | switch (status) { |
| 6319 | /* finished or yield */ |
| 6320 | case HLUA_E_OK: |
| 6321 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6322 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 6323 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6324 | break; |
| 6325 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6326 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 6327 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 6328 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6329 | break; |
| 6330 | |
| 6331 | /* finished with error. */ |
| 6332 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6333 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6334 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6335 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6336 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6337 | break; |
| 6338 | |
| 6339 | case HLUA_E_ERR: |
| 6340 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6341 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6342 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6343 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6344 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6345 | break; |
| 6346 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6347 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6348 | } |
| 6349 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6350 | /* This function is an LUA binding that register LUA function to be |
| 6351 | * executed after the HAProxy configuration parsing and before the |
| 6352 | * HAProxy scheduler starts. This function expect only one LUA |
| 6353 | * argument that is a function. This function returns nothing, but |
| 6354 | * throws if an error is encountered. |
| 6355 | */ |
| 6356 | __LJMP static int hlua_register_init(lua_State *L) |
| 6357 | { |
| 6358 | struct hlua_init_function *init; |
| 6359 | int ref; |
| 6360 | |
| 6361 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 6362 | |
| 6363 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6364 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6365 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6366 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6367 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6368 | |
| 6369 | init->function_ref = ref; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6370 | LIST_ADDQ(&hlua_init_functions[hlua_state_id], &init->l); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6371 | return 0; |
| 6372 | } |
| 6373 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6374 | /* This functio is an LUA binding. It permits to register a task |
| 6375 | * executed in parallel of the main HAroxy activity. The task is |
| 6376 | * created and it is set in the HAProxy scheduler. It can be called |
| 6377 | * from the "init" section, "post init" or during the runtime. |
| 6378 | * |
| 6379 | * Lua prototype: |
| 6380 | * |
| 6381 | * <none> core.register_task(<function>) |
| 6382 | */ |
| 6383 | static int hlua_register_task(lua_State *L) |
| 6384 | { |
| 6385 | struct hlua *hlua; |
| 6386 | struct task *task; |
| 6387 | int ref; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 6388 | int state_id; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6389 | |
| 6390 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 6391 | |
| 6392 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6393 | |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 6394 | /* Get the reference state. If the reference is NULL, L is the master |
| 6395 | * state, otherwise hlua->T is. |
| 6396 | */ |
| 6397 | hlua = hlua_gethlua(L); |
| 6398 | if (hlua) |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 6399 | /* we are in runtime processing */ |
| 6400 | state_id = hlua->state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 6401 | else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 6402 | /* we are in initialization mode */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6403 | state_id = hlua_state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 6404 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6405 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6406 | if (!hlua) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6407 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6408 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 6409 | /* We are in the common lua state, execute the task anywhere, |
| 6410 | * otherwise, inherit the current thread identifier |
| 6411 | */ |
| 6412 | if (state_id == 0) |
| 6413 | task = task_new(MAX_THREADS_MASK); |
| 6414 | else |
| 6415 | task = task_new(tid_bit); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 6416 | if (!task) |
| 6417 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 6418 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6419 | task->context = hlua; |
| 6420 | task->process = hlua_process_task; |
| 6421 | |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 6422 | if (!hlua_ctx_init(hlua, state_id, task, 1)) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6423 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6424 | |
| 6425 | /* Restore the function in the stack. */ |
| 6426 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 6427 | hlua->nargs = 0; |
| 6428 | |
| 6429 | /* Schedule task. */ |
| 6430 | task_schedule(task, now_ms); |
| 6431 | |
| 6432 | return 0; |
| 6433 | } |
| 6434 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6435 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 6436 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 6437 | * resume converters. |
| 6438 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6439 | 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] | 6440 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6441 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6442 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6443 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6444 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6445 | if (!stream) |
| 6446 | return 0; |
| 6447 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6448 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6449 | * Lua context can be not initialized. This behavior |
| 6450 | * permits to save performances because a systematic |
| 6451 | * Lua initialization cause 5% performances loss. |
| 6452 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6453 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6454 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6455 | if (!stream->hlua) { |
| 6456 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6457 | return 0; |
| 6458 | } |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6459 | 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] | 6460 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6461 | return 0; |
| 6462 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6463 | } |
| 6464 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6465 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6466 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6467 | |
| 6468 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6469 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6470 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6471 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6472 | else |
| 6473 | error = "critical error"; |
| 6474 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6475 | return 0; |
| 6476 | } |
| 6477 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6478 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6479 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6480 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6481 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6482 | return 0; |
| 6483 | } |
| 6484 | |
| 6485 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6486 | 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] | 6487 | |
| 6488 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6489 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6490 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6491 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6492 | return 0; |
| 6493 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6494 | hlua_smp2lua(stream->hlua->T, smp); |
| 6495 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6496 | |
| 6497 | /* push keywords in the stack. */ |
| 6498 | if (arg_p) { |
| 6499 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6500 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6501 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6502 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6503 | return 0; |
| 6504 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6505 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6506 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6507 | } |
| 6508 | } |
| 6509 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6510 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6511 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6512 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6513 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6514 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6515 | } |
| 6516 | |
| 6517 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6518 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6519 | /* finished. */ |
| 6520 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6521 | /* If the stack is empty, the function fails. */ |
| 6522 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6523 | return 0; |
| 6524 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6525 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6526 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6527 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6528 | return 1; |
| 6529 | |
| 6530 | /* yield. */ |
| 6531 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6532 | 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] | 6533 | return 0; |
| 6534 | |
| 6535 | /* finished with error. */ |
| 6536 | case HLUA_E_ERRMSG: |
| 6537 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6538 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6539 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6540 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6541 | return 0; |
| 6542 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6543 | case HLUA_E_ETMOUT: |
| 6544 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 6545 | return 0; |
| 6546 | |
| 6547 | case HLUA_E_NOMEM: |
| 6548 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 6549 | return 0; |
| 6550 | |
| 6551 | case HLUA_E_YIELD: |
| 6552 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 6553 | return 0; |
| 6554 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6555 | case HLUA_E_ERR: |
| 6556 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6557 | 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] | 6558 | /* fall through */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6559 | |
| 6560 | default: |
| 6561 | return 0; |
| 6562 | } |
| 6563 | } |
| 6564 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6565 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 6566 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6567 | * resume sample-fetches. This function will be called by the sample |
| 6568 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6569 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6570 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 6571 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6572 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6573 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6574 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6575 | const char *error; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6576 | unsigned int hflags = HLUA_TXN_NOTERM; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6577 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6578 | if (!stream) |
| 6579 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 6580 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6581 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6582 | * Lua context can be not initialized. This behavior |
| 6583 | * permits to save performances because a systematic |
| 6584 | * Lua initialization cause 5% performances loss. |
| 6585 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6586 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6587 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6588 | if (!stream->hlua) { |
| 6589 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6590 | return 0; |
| 6591 | } |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6592 | 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] | 6593 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6594 | return 0; |
| 6595 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6596 | } |
| 6597 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6598 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6599 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6600 | |
| 6601 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6602 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6603 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6604 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6605 | else |
| 6606 | error = "critical error"; |
| 6607 | 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] | 6608 | return 0; |
| 6609 | } |
| 6610 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6611 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6612 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6613 | 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] | 6614 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6615 | return 0; |
| 6616 | } |
| 6617 | |
| 6618 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6619 | 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] | 6620 | |
| 6621 | /* push arguments in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6622 | 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] | 6623 | 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] | 6624 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6625 | return 0; |
| 6626 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6627 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6628 | |
| 6629 | /* push keywords in the stack. */ |
| 6630 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 6631 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6632 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6633 | 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] | 6634 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6635 | return 0; |
| 6636 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6637 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6638 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6639 | } |
| 6640 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6641 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6642 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6643 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6644 | /* At this point the execution is safe. */ |
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 | } |
| 6647 | |
| 6648 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6649 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6650 | /* finished. */ |
| 6651 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6652 | /* If the stack is empty, the function fails. */ |
| 6653 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6654 | return 0; |
| 6655 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6656 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6657 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6658 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6659 | |
| 6660 | /* Set the end of execution flag. */ |
| 6661 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 6662 | return 1; |
| 6663 | |
| 6664 | /* yield. */ |
| 6665 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6666 | 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] | 6667 | return 0; |
| 6668 | |
| 6669 | /* finished with error. */ |
| 6670 | case HLUA_E_ERRMSG: |
| 6671 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6672 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6673 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6674 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6675 | return 0; |
| 6676 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6677 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6678 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 6679 | return 0; |
| 6680 | |
| 6681 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6682 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 6683 | return 0; |
| 6684 | |
| 6685 | case HLUA_E_YIELD: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6686 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 6687 | return 0; |
| 6688 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6689 | case HLUA_E_ERR: |
| 6690 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6691 | 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] | 6692 | /* fall through */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6693 | |
| 6694 | default: |
| 6695 | return 0; |
| 6696 | } |
| 6697 | } |
| 6698 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6699 | /* This function is an LUA binding used for registering |
| 6700 | * "sample-conv" functions. It expects a converter name used |
| 6701 | * in the haproxy configuration file, and an LUA function. |
| 6702 | */ |
| 6703 | __LJMP static int hlua_register_converters(lua_State *L) |
| 6704 | { |
| 6705 | struct sample_conv_kw_list *sck; |
| 6706 | const char *name; |
| 6707 | int ref; |
| 6708 | int len; |
| 6709 | struct hlua_function *fcn; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6710 | struct sample_conv *sc; |
| 6711 | struct buffer *trash; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6712 | |
| 6713 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 6714 | |
| 6715 | /* First argument : converter name. */ |
| 6716 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6717 | |
| 6718 | /* Second argument : lua function. */ |
| 6719 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6720 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6721 | /* Check if the converter is already registered */ |
| 6722 | trash = get_trash_chunk(); |
| 6723 | chunk_printf(trash, "lua.%s", name); |
| 6724 | sc = find_sample_conv(trash->area, trash->data); |
| 6725 | if (sc != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 6726 | fcn = sc->private; |
| 6727 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 6728 | ha_warning("Trying to register converter 'lua.%s' more than once. " |
| 6729 | "This will become a hard error in version 2.5.\n", name); |
| 6730 | } |
| 6731 | fcn->function_ref[hlua_state_id] = ref; |
| 6732 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6733 | } |
| 6734 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6735 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6736 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6737 | if (!sck) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6738 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 6739 | fcn = new_hlua_function(); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6740 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6741 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6742 | |
| 6743 | /* Fill fcn. */ |
| 6744 | fcn->name = strdup(name); |
| 6745 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6746 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6747 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6748 | |
| 6749 | /* List head */ |
| 6750 | sck->list.n = sck->list.p = NULL; |
| 6751 | |
| 6752 | /* converter keyword. */ |
| 6753 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6754 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6755 | if (!sck->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6756 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6757 | |
| 6758 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 6759 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6760 | 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] | 6761 | sck->kw[0].val_args = NULL; |
| 6762 | sck->kw[0].in_type = SMP_T_STR; |
| 6763 | sck->kw[0].out_type = SMP_T_STR; |
| 6764 | sck->kw[0].private = fcn; |
| 6765 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6766 | /* Register this new converter */ |
| 6767 | sample_register_convs(sck); |
| 6768 | |
| 6769 | return 0; |
| 6770 | } |
| 6771 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6772 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6773 | * "sample-fetch" functions. It expects a converter name used |
| 6774 | * in the haproxy configuration file, and an LUA function. |
| 6775 | */ |
| 6776 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 6777 | { |
| 6778 | const char *name; |
| 6779 | int ref; |
| 6780 | int len; |
| 6781 | struct sample_fetch_kw_list *sfk; |
| 6782 | struct hlua_function *fcn; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6783 | struct sample_fetch *sf; |
| 6784 | struct buffer *trash; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6785 | |
| 6786 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 6787 | |
| 6788 | /* First argument : sample-fetch name. */ |
| 6789 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6790 | |
| 6791 | /* Second argument : lua function. */ |
| 6792 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6793 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6794 | /* Check if the sample-fetch is already registered */ |
| 6795 | trash = get_trash_chunk(); |
| 6796 | chunk_printf(trash, "lua.%s", name); |
| 6797 | sf = find_sample_fetch(trash->area, trash->data); |
| 6798 | if (sf != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 6799 | fcn = sf->private; |
| 6800 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 6801 | ha_warning("Trying to register sample-fetch 'lua.%s' more than once. " |
| 6802 | "This will become a hard error in version 2.5.\n", name); |
| 6803 | } |
| 6804 | fcn->function_ref[hlua_state_id] = ref; |
| 6805 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6806 | } |
| 6807 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6808 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6809 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6810 | if (!sfk) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6811 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 6812 | fcn = new_hlua_function(); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6813 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6814 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6815 | |
| 6816 | /* Fill fcn. */ |
| 6817 | fcn->name = strdup(name); |
| 6818 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6819 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6820 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6821 | |
| 6822 | /* List head */ |
| 6823 | sfk->list.n = sfk->list.p = NULL; |
| 6824 | |
| 6825 | /* sample-fetch keyword. */ |
| 6826 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6827 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6828 | if (!sfk->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6829 | return luaL_error(L, "Lua out of memory error."); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6830 | |
| 6831 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 6832 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6833 | 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] | 6834 | sfk->kw[0].val_args = NULL; |
| 6835 | sfk->kw[0].out_type = SMP_T_STR; |
| 6836 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 6837 | sfk->kw[0].val = 0; |
| 6838 | sfk->kw[0].private = fcn; |
| 6839 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6840 | /* Register this new fetch. */ |
| 6841 | sample_register_fetches(sfk); |
| 6842 | |
| 6843 | return 0; |
| 6844 | } |
| 6845 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 6846 | /* This function is a lua binding to set the wake_time. |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6847 | */ |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 6848 | __LJMP static int hlua_set_wake_time(lua_State *L) |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6849 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 6850 | struct hlua *hlua; |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6851 | unsigned int delay; |
| 6852 | unsigned int wakeup_ms; |
| 6853 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 6854 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 6855 | hlua = hlua_gethlua(L); |
| 6856 | if (!hlua) { |
| 6857 | return 0; |
| 6858 | } |
| 6859 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6860 | MAY_LJMP(check_args(L, 1, "wake_time")); |
| 6861 | |
| 6862 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 6863 | wakeup_ms = tick_add(now_ms, delay); |
| 6864 | hlua->wake_time = wakeup_ms; |
| 6865 | return 0; |
| 6866 | } |
| 6867 | |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6868 | /* This function is a wrapper to execute each LUA function declared as an action |
| 6869 | * wrapper during the initialisation period. This function may return any |
| 6870 | * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is |
| 6871 | * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the |
| 6872 | * return value is the first element on the stack. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6873 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6874 | 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] | 6875 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6876 | { |
| 6877 | char **arg; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6878 | unsigned int hflags = 0; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6879 | int dir, act_ret = ACT_RET_CONT; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6880 | const char *error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6881 | |
| 6882 | switch (rule->from) { |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 6883 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 6884 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 6885 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 6886 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6887 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6888 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6889 | goto end; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6890 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6891 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6892 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6893 | * Lua context can be not initialized. This behavior |
| 6894 | * permits to save performances because a systematic |
| 6895 | * Lua initialization cause 5% performances loss. |
| 6896 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6897 | if (!s->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6898 | s->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6899 | if (!s->hlua) { |
| 6900 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6901 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6902 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6903 | } |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6904 | 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] | 6905 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6906 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6907 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6908 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6909 | } |
| 6910 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6911 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6912 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6913 | |
| 6914 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6915 | if (!SET_SAFE_LJMP(s->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6916 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 6917 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6918 | else |
| 6919 | error = "critical error"; |
| 6920 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6921 | rule->arg.hlua_rule->fcn->name, error); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6922 | goto end; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6923 | } |
| 6924 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6925 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6926 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6927 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6928 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6929 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6930 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6931 | } |
| 6932 | |
| 6933 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6934 | 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] | 6935 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6936 | /* Create and and push object stream in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6937 | if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6938 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6939 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6940 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6941 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6942 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6943 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6944 | |
| 6945 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6946 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6947 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6948 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6949 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6950 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6951 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6952 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6953 | lua_pushstring(s->hlua->T, *arg); |
| 6954 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6955 | } |
| 6956 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6957 | /* Now the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6958 | RESET_SAFE_LJMP(s->hlua); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6959 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6960 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6961 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6962 | } |
| 6963 | |
| 6964 | /* Execute the function. */ |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 6965 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6966 | /* finished. */ |
| 6967 | case HLUA_E_OK: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6968 | /* Catch the return value */ |
| 6969 | if (lua_gettop(s->hlua->T) > 0) |
| 6970 | act_ret = lua_tointeger(s->hlua->T, -1); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6971 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6972 | /* Set timeout in the required channel. */ |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 6973 | if (act_ret == ACT_RET_YIELD) { |
| 6974 | if (flags & ACT_OPT_FINAL) |
| 6975 | goto err_yield; |
| 6976 | |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 6977 | if (dir == SMP_OPT_DIR_REQ) |
| 6978 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 6979 | s->hlua->wake_time); |
| 6980 | else |
| 6981 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 6982 | s->hlua->wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6983 | } |
| 6984 | goto end; |
| 6985 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6986 | /* yield. */ |
| 6987 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6988 | /* Set timeout in the required channel. */ |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 6989 | if (dir == SMP_OPT_DIR_REQ) |
| 6990 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 6991 | s->hlua->wake_time); |
| 6992 | else |
| 6993 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 6994 | s->hlua->wake_time); |
| 6995 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6996 | /* Some actions can be wake up when a "write" event |
| 6997 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6998 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6999 | */ |
Christopher Faulet | 51fa358 | 2019-07-26 14:54:52 +0200 | [diff] [blame] | 7000 | if (HLUA_IS_WAKERESWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7001 | s->res.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7002 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7003 | s->req.flags |= CF_WAKE_WRITE; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7004 | act_ret = ACT_RET_YIELD; |
| 7005 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7006 | |
| 7007 | /* finished with error. */ |
| 7008 | case HLUA_E_ERRMSG: |
| 7009 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7010 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7011 | rule->arg.hlua_rule->fcn->name, lua_tostring(s->hlua->T, -1)); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7012 | lua_pop(s->hlua->T, 1); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7013 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7014 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7015 | case HLUA_E_ETMOUT: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7016 | 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] | 7017 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7018 | |
| 7019 | case HLUA_E_NOMEM: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7020 | 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] | 7021 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7022 | |
| 7023 | case HLUA_E_YIELD: |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 7024 | err_yield: |
| 7025 | act_ret = ACT_RET_CONT; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7026 | 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] | 7027 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7028 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7029 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7030 | case HLUA_E_ERR: |
| 7031 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7032 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7033 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7034 | |
| 7035 | default: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7036 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7037 | } |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7038 | |
| 7039 | end: |
Christopher Faulet | 2361fd9 | 2020-07-30 10:40:58 +0200 | [diff] [blame] | 7040 | if (act_ret != ACT_RET_YIELD && s->hlua) |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 7041 | s->hlua->wake_time = TICK_ETERNITY; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7042 | return act_ret; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7043 | } |
| 7044 | |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 7045 | struct task *hlua_applet_wakeup(struct task *t, void *context, unsigned short state) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7046 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 7047 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7048 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7049 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 7050 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 7051 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7052 | } |
| 7053 | |
| 7054 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 7055 | { |
| 7056 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7057 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7058 | struct task *task; |
| 7059 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7060 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7061 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7062 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7063 | if (!hlua) { |
| 7064 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7065 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7066 | return 0; |
| 7067 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7068 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7069 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7070 | ctx->ctx.hlua_apptcp.flags = 0; |
| 7071 | |
| 7072 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7073 | task = task_new(tid_bit); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7074 | if (!task) { |
| 7075 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7076 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7077 | return 0; |
| 7078 | } |
| 7079 | task->nice = 0; |
| 7080 | task->context = ctx; |
| 7081 | task->process = hlua_applet_wakeup; |
| 7082 | ctx->ctx.hlua_apptcp.task = task; |
| 7083 | |
| 7084 | /* In the execution wrappers linked with a stream, the |
| 7085 | * Lua context can be not initialized. This behavior |
| 7086 | * permits to save performances because a systematic |
| 7087 | * Lua initialization cause 5% performances loss. |
| 7088 | */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7089 | 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] | 7090 | 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] | 7091 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7092 | return 0; |
| 7093 | } |
| 7094 | |
| 7095 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 7096 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7097 | |
| 7098 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7099 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7100 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7101 | error = lua_tostring(hlua->T, -1); |
| 7102 | else |
| 7103 | error = "critical error"; |
| 7104 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7105 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7106 | return 0; |
| 7107 | } |
| 7108 | |
| 7109 | /* Check stack available size. */ |
| 7110 | if (!lua_checkstack(hlua->T, 1)) { |
| 7111 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7112 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7113 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7114 | return 0; |
| 7115 | } |
| 7116 | |
| 7117 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7118 | 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] | 7119 | |
| 7120 | /* Create and and push object stream in the stack. */ |
| 7121 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 7122 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7123 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7124 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7125 | return 0; |
| 7126 | } |
| 7127 | hlua->nargs = 1; |
| 7128 | |
| 7129 | /* push keywords in the stack. */ |
| 7130 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 7131 | if (!lua_checkstack(hlua->T, 1)) { |
| 7132 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7133 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7134 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7135 | return 0; |
| 7136 | } |
| 7137 | lua_pushstring(hlua->T, *arg); |
| 7138 | hlua->nargs++; |
| 7139 | } |
| 7140 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7141 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7142 | |
| 7143 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7144 | si_cant_get(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 7145 | si_rx_endp_more(si); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7146 | |
| 7147 | return 1; |
| 7148 | } |
| 7149 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7150 | void hlua_applet_tcp_fct(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7151 | { |
| 7152 | struct stream_interface *si = ctx->owner; |
| 7153 | struct stream *strm = si_strm(si); |
| 7154 | struct channel *res = si_ic(si); |
| 7155 | struct act_rule *rule = ctx->rule; |
| 7156 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7157 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7158 | |
| 7159 | /* The applet execution is already done. */ |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 7160 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) { |
| 7161 | /* eat the whole request */ |
| 7162 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7163 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 7164 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7165 | |
| 7166 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7167 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7168 | return; |
| 7169 | |
| 7170 | /* Execute the function. */ |
| 7171 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7172 | /* finished. */ |
| 7173 | case HLUA_E_OK: |
| 7174 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 7175 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7176 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 7177 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7178 | res->flags |= CF_READ_NULL; |
| 7179 | si_shutr(si); |
| 7180 | return; |
| 7181 | |
| 7182 | /* yield. */ |
| 7183 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 7184 | if (hlua->wake_time != TICK_ETERNITY) |
| 7185 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7186 | return; |
| 7187 | |
| 7188 | /* finished with error. */ |
| 7189 | case HLUA_E_ERRMSG: |
| 7190 | /* Display log. */ |
| 7191 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7192 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7193 | lua_pop(hlua->T, 1); |
| 7194 | goto error; |
| 7195 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7196 | case HLUA_E_ETMOUT: |
| 7197 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7198 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7199 | goto error; |
| 7200 | |
| 7201 | case HLUA_E_NOMEM: |
| 7202 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7203 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7204 | goto error; |
| 7205 | |
| 7206 | case HLUA_E_YIELD: /* unexpected */ |
| 7207 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7208 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7209 | goto error; |
| 7210 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7211 | case HLUA_E_ERR: |
| 7212 | /* Display log. */ |
| 7213 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7214 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7215 | goto error; |
| 7216 | |
| 7217 | default: |
| 7218 | goto error; |
| 7219 | } |
| 7220 | |
| 7221 | error: |
| 7222 | |
| 7223 | /* For all other cases, just close the stream. */ |
| 7224 | si_shutw(si); |
| 7225 | si_shutr(si); |
| 7226 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 7227 | } |
| 7228 | |
| 7229 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 7230 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7231 | task_destroy(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7232 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7233 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7234 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7235 | } |
| 7236 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7237 | /* The function returns 1 if the initialisation is complete, 0 if |
| 7238 | * an errors occurs and -1 if more data are required for initializing |
| 7239 | * the applet. |
| 7240 | */ |
| 7241 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 7242 | { |
| 7243 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7244 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7245 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7246 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7247 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7248 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7249 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7250 | txn = strm->txn; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7251 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7252 | if (!hlua) { |
| 7253 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7254 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7255 | return 0; |
| 7256 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7257 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7258 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7259 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 7260 | ctx->ctx.hlua_apphttp.flags = 0; |
| 7261 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 7262 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 7263 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 7264 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7265 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7266 | task = task_new(tid_bit); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7267 | if (!task) { |
| 7268 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7269 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7270 | return 0; |
| 7271 | } |
| 7272 | task->nice = 0; |
| 7273 | task->context = ctx; |
| 7274 | task->process = hlua_applet_wakeup; |
| 7275 | ctx->ctx.hlua_apphttp.task = task; |
| 7276 | |
| 7277 | /* In the execution wrappers linked with a stream, the |
| 7278 | * Lua context can be not initialized. This behavior |
| 7279 | * permits to save performances because a systematic |
| 7280 | * Lua initialization cause 5% performances loss. |
| 7281 | */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7282 | 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] | 7283 | 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] | 7284 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7285 | return 0; |
| 7286 | } |
| 7287 | |
| 7288 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 7289 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7290 | |
| 7291 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7292 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7293 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7294 | error = lua_tostring(hlua->T, -1); |
| 7295 | else |
| 7296 | error = "critical error"; |
| 7297 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7298 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7299 | return 0; |
| 7300 | } |
| 7301 | |
| 7302 | /* Check stack available size. */ |
| 7303 | if (!lua_checkstack(hlua->T, 1)) { |
| 7304 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7305 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7306 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7307 | return 0; |
| 7308 | } |
| 7309 | |
| 7310 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7311 | 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] | 7312 | |
| 7313 | /* Create and and push object stream in the stack. */ |
| 7314 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 7315 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7316 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7317 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7318 | return 0; |
| 7319 | } |
| 7320 | hlua->nargs = 1; |
| 7321 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7322 | /* push keywords in the stack. */ |
| 7323 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 7324 | if (!lua_checkstack(hlua->T, 1)) { |
| 7325 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7326 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7327 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7328 | return 0; |
| 7329 | } |
| 7330 | lua_pushstring(hlua->T, *arg); |
| 7331 | hlua->nargs++; |
| 7332 | } |
| 7333 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7334 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7335 | |
| 7336 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7337 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7338 | |
| 7339 | return 1; |
| 7340 | } |
| 7341 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7342 | void hlua_applet_http_fct(struct appctx *ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7343 | { |
| 7344 | struct stream_interface *si = ctx->owner; |
| 7345 | struct stream *strm = si_strm(si); |
| 7346 | struct channel *req = si_oc(si); |
| 7347 | struct channel *res = si_ic(si); |
| 7348 | struct act_rule *rule = ctx->rule; |
| 7349 | struct proxy *px = strm->be; |
| 7350 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
| 7351 | struct htx *req_htx, *res_htx; |
| 7352 | |
| 7353 | res_htx = htx_from_buf(&res->buf); |
| 7354 | |
| 7355 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7356 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7357 | goto out; |
| 7358 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7359 | /* Check if the input buffer is available. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7360 | if (!b_size(&res->buf)) { |
| 7361 | si_rx_room_blk(si); |
| 7362 | goto out; |
| 7363 | } |
| 7364 | /* check that the output is not closed */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7365 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7366 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7367 | |
| 7368 | /* Set the currently running flag. */ |
| 7369 | if (!HLUA_IS_RUNNING(hlua) && |
| 7370 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7371 | struct htx_blk *blk; |
| 7372 | size_t count = co_data(req); |
| 7373 | |
| 7374 | if (!count) { |
| 7375 | si_cant_get(si); |
| 7376 | goto out; |
| 7377 | } |
| 7378 | |
| 7379 | /* We need to flush the request header. This left the body for |
| 7380 | * the Lua. |
| 7381 | */ |
| 7382 | req_htx = htx_from_buf(&req->buf); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 7383 | blk = htx_get_first_blk(req_htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7384 | while (count && blk) { |
| 7385 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 7386 | uint32_t sz = htx_get_blksz(blk); |
| 7387 | |
| 7388 | if (sz > count) { |
| 7389 | si_cant_get(si); |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 7390 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7391 | goto out; |
| 7392 | } |
| 7393 | |
| 7394 | count -= sz; |
| 7395 | co_set_data(req, co_data(req) - sz); |
| 7396 | blk = htx_remove_blk(req_htx, blk); |
| 7397 | |
| 7398 | if (type == HTX_BLK_EOH) |
| 7399 | break; |
| 7400 | } |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 7401 | htx_to_buf(req_htx, &req->buf); |
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"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7533 | return ACT_RET_PRS_ERR; |
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"); |
| 7541 | return ACT_RET_PRS_ERR; |
| 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); |
| 7551 | return ACT_RET_PRS_ERR; |
| 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"); |
| 7556 | return ACT_RET_PRS_ERR; |
| 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; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7565 | } |
| 7566 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7567 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 7568 | struct act_rule *rule, char **err) |
| 7569 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7570 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7571 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7572 | /* HTTP applets are forbidden in tcp-request rules. |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7573 | * HTTP applet request requires everything initialized by |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7574 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7575 | * The applet will be immediately initialized, but its before |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7576 | * the call of this analyzer. |
| 7577 | */ |
| 7578 | if (rule->from != ACT_F_HTTP_REQ) { |
| 7579 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 7580 | return ACT_RET_PRS_ERR; |
| 7581 | } |
| 7582 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7583 | /* Memory for the rule. */ |
| 7584 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7585 | if (!rule->arg.hlua_rule) { |
| 7586 | memprintf(err, "out of memory error"); |
| 7587 | return ACT_RET_PRS_ERR; |
| 7588 | } |
| 7589 | |
| 7590 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7591 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7592 | |
| 7593 | /* TODO: later accept arguments. */ |
| 7594 | rule->arg.hlua_rule->args = NULL; |
| 7595 | |
| 7596 | /* Add applet pointer in the rule. */ |
| 7597 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7598 | rule->applet.name = fcn->name; |
| 7599 | rule->applet.init = hlua_applet_http_init; |
| 7600 | rule->applet.fct = hlua_applet_http_fct; |
| 7601 | rule->applet.release = hlua_applet_http_release; |
| 7602 | rule->applet.timeout = hlua_timeout_applet; |
| 7603 | |
| 7604 | return ACT_RET_PRS_OK; |
| 7605 | } |
| 7606 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7607 | /* This function is an LUA binding used for registering |
| 7608 | * "sample-conv" functions. It expects a converter name used |
| 7609 | * in the haproxy configuration file, and an LUA function. |
| 7610 | */ |
| 7611 | __LJMP static int hlua_register_action(lua_State *L) |
| 7612 | { |
| 7613 | struct action_kw_list *akl; |
| 7614 | const char *name; |
| 7615 | int ref; |
| 7616 | int len; |
| 7617 | struct hlua_function *fcn; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7618 | int nargs; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7619 | struct buffer *trash; |
| 7620 | struct action_kw *akw; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7621 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7622 | /* Initialise the number of expected arguments at 0. */ |
| 7623 | nargs = 0; |
| 7624 | |
| 7625 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 7626 | 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] | 7627 | |
| 7628 | /* First argument : converter name. */ |
| 7629 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7630 | |
| 7631 | /* Second argument : environment. */ |
| 7632 | if (lua_type(L, 2) != LUA_TTABLE) |
| 7633 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7634 | |
| 7635 | /* Third argument : lua function. */ |
| 7636 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7637 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7638 | /* 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] | 7639 | if (lua_gettop(L) >= 4) |
| 7640 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 7641 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7642 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7643 | lua_pushnil(L); |
| 7644 | while (lua_next(L, 2) != 0) { |
| 7645 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7646 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7647 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7648 | /* Check if action exists */ |
| 7649 | trash = get_trash_chunk(); |
| 7650 | chunk_printf(trash, "lua.%s", name); |
| 7651 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) { |
| 7652 | akw = tcp_req_cont_action(trash->area); |
| 7653 | } else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) { |
| 7654 | akw = tcp_res_cont_action(trash->area); |
| 7655 | } else if (strcmp(lua_tostring(L, -1), "http-req") == 0) { |
| 7656 | akw = action_http_req_custom(trash->area); |
| 7657 | } else if (strcmp(lua_tostring(L, -1), "http-res") == 0) { |
| 7658 | akw = action_http_res_custom(trash->area); |
| 7659 | } else { |
| 7660 | akw = NULL; |
| 7661 | } |
| 7662 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 7663 | fcn = akw->private; |
| 7664 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 7665 | ha_warning("Trying to register action 'lua.%s' more than once. " |
| 7666 | "This will become a hard error in version 2.5.\n", name); |
| 7667 | } |
| 7668 | fcn->function_ref[hlua_state_id] = ref; |
| 7669 | |
| 7670 | /* pop the environment string. */ |
| 7671 | lua_pop(L, 1); |
| 7672 | continue; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7673 | } |
| 7674 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7675 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 7676 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7677 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7678 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7679 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 7680 | fcn = new_hlua_function(); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7681 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7682 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7683 | |
| 7684 | /* Fill fcn. */ |
| 7685 | fcn->name = strdup(name); |
| 7686 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7687 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7688 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7689 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 7690 | /* Set the expected number of arguments. */ |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7691 | fcn->nargs = nargs; |
| 7692 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7693 | /* List head */ |
| 7694 | akl->list.n = akl->list.p = NULL; |
| 7695 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7696 | /* action keyword. */ |
| 7697 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7698 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7699 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7700 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7701 | |
| 7702 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7703 | |
| 7704 | akl->kw[0].match_pfx = 0; |
| 7705 | akl->kw[0].private = fcn; |
| 7706 | akl->kw[0].parse = action_register_lua; |
| 7707 | |
| 7708 | /* select the action registering point. */ |
| 7709 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 7710 | tcp_req_cont_keywords_register(akl); |
| 7711 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 7712 | tcp_res_cont_keywords_register(akl); |
| 7713 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 7714 | http_req_keywords_register(akl); |
| 7715 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 7716 | http_res_keywords_register(akl); |
| 7717 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7718 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7719 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 7720 | "are expected.", lua_tostring(L, -1))); |
| 7721 | |
| 7722 | /* pop the environment string. */ |
| 7723 | lua_pop(L, 1); |
| 7724 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7725 | return ACT_RET_PRS_OK; |
| 7726 | } |
| 7727 | |
| 7728 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 7729 | struct act_rule *rule, char **err) |
| 7730 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7731 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7732 | |
Christopher Faulet | 280f85b | 2019-07-15 15:02:04 +0200 | [diff] [blame] | 7733 | if (px->mode == PR_MODE_HTTP) { |
| 7734 | memprintf(err, "Lua TCP services cannot be used on HTTP proxies"); |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 7735 | return ACT_RET_PRS_ERR; |
| 7736 | } |
| 7737 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7738 | /* Memory for the rule. */ |
| 7739 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7740 | if (!rule->arg.hlua_rule) { |
| 7741 | memprintf(err, "out of memory error"); |
| 7742 | return ACT_RET_PRS_ERR; |
| 7743 | } |
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 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7746 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7747 | |
| 7748 | /* TODO: later accept arguments. */ |
| 7749 | rule->arg.hlua_rule->args = NULL; |
| 7750 | |
| 7751 | /* Add applet pointer in the rule. */ |
| 7752 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7753 | rule->applet.name = fcn->name; |
| 7754 | rule->applet.init = hlua_applet_tcp_init; |
| 7755 | rule->applet.fct = hlua_applet_tcp_fct; |
| 7756 | rule->applet.release = hlua_applet_tcp_release; |
| 7757 | rule->applet.timeout = hlua_timeout_applet; |
| 7758 | |
| 7759 | return 0; |
| 7760 | } |
| 7761 | |
| 7762 | /* This function is an LUA binding used for registering |
| 7763 | * "sample-conv" functions. It expects a converter name used |
| 7764 | * in the haproxy configuration file, and an LUA function. |
| 7765 | */ |
| 7766 | __LJMP static int hlua_register_service(lua_State *L) |
| 7767 | { |
| 7768 | struct action_kw_list *akl; |
| 7769 | const char *name; |
| 7770 | const char *env; |
| 7771 | int ref; |
| 7772 | int len; |
| 7773 | struct hlua_function *fcn; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7774 | struct buffer *trash; |
| 7775 | struct action_kw *akw; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7776 | |
| 7777 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 7778 | |
| 7779 | /* First argument : converter name. */ |
| 7780 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7781 | |
| 7782 | /* Second argument : environment. */ |
| 7783 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7784 | |
| 7785 | /* Third argument : lua function. */ |
| 7786 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7787 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7788 | /* Check for service already registered */ |
| 7789 | trash = get_trash_chunk(); |
| 7790 | chunk_printf(trash, "lua.%s", name); |
| 7791 | akw = service_find(trash->area); |
| 7792 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 7793 | fcn = akw->private; |
| 7794 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 7795 | ha_warning("Trying to register service 'lua.%s' more than once. " |
| 7796 | "This will become a hard error in version 2.5.\n", name); |
| 7797 | } |
| 7798 | fcn->function_ref[hlua_state_id] = ref; |
| 7799 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7800 | } |
| 7801 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7802 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7803 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 7804 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7805 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 7806 | fcn = new_hlua_function(); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7807 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7808 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7809 | |
| 7810 | /* Fill fcn. */ |
| 7811 | len = strlen("<lua.>") + strlen(name) + 1; |
| 7812 | fcn->name = calloc(1, len); |
| 7813 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7814 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7815 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7816 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7817 | |
| 7818 | /* List head */ |
| 7819 | akl->list.n = akl->list.p = NULL; |
| 7820 | |
| 7821 | /* converter keyword. */ |
| 7822 | len = strlen("lua.") + strlen(name) + 1; |
| 7823 | akl->kw[0].kw = calloc(1, len); |
| 7824 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7825 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7826 | |
| 7827 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7828 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 7829 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7830 | if (strcmp(env, "tcp") == 0) |
| 7831 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7832 | else if (strcmp(env, "http") == 0) |
| 7833 | akl->kw[0].parse = action_register_service_http; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7834 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7835 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 7836 | "'tcp' or 'http' are expected.", env)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7837 | |
| 7838 | akl->kw[0].match_pfx = 0; |
| 7839 | akl->kw[0].private = fcn; |
| 7840 | |
| 7841 | /* End of array. */ |
| 7842 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 7843 | |
| 7844 | /* Register this new converter */ |
| 7845 | service_keywords_register(akl); |
| 7846 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7847 | return 0; |
| 7848 | } |
| 7849 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7850 | /* This function initialises Lua cli handler. It copies the |
| 7851 | * arguments in the Lua stack and create channel IO objects. |
| 7852 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 7853 | 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] | 7854 | { |
| 7855 | struct hlua *hlua; |
| 7856 | struct hlua_function *fcn; |
| 7857 | int i; |
| 7858 | const char *error; |
| 7859 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7860 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7861 | appctx->ctx.hlua_cli.fcn = private; |
| 7862 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7863 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7864 | if (!hlua) { |
| 7865 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7866 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7867 | } |
| 7868 | HLUA_INIT(hlua); |
| 7869 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7870 | |
| 7871 | /* Create task used by signal to wakeup applets. |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 7872 | * 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] | 7873 | * applet_http. It is absolutely compatible. |
| 7874 | */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7875 | appctx->ctx.hlua_cli.task = task_new(tid_bit); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7876 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 7877 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7878 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7879 | } |
| 7880 | appctx->ctx.hlua_cli.task->nice = 0; |
| 7881 | appctx->ctx.hlua_cli.task->context = appctx; |
| 7882 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 7883 | |
| 7884 | /* Initialises the Lua context */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7885 | 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] | 7886 | 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] | 7887 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7888 | } |
| 7889 | |
| 7890 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7891 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7892 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7893 | error = lua_tostring(hlua->T, -1); |
| 7894 | else |
| 7895 | error = "critical error"; |
| 7896 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 7897 | goto error; |
| 7898 | } |
| 7899 | |
| 7900 | /* Check stack available size. */ |
| 7901 | if (!lua_checkstack(hlua->T, 2)) { |
| 7902 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7903 | goto error; |
| 7904 | } |
| 7905 | |
| 7906 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7907 | 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] | 7908 | |
| 7909 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 7910 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7911 | */ |
| 7912 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 7913 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7914 | goto error; |
| 7915 | } |
| 7916 | hlua->nargs = 1; |
| 7917 | |
| 7918 | /* push keywords in the stack. */ |
| 7919 | for (i = 0; *args[i]; i++) { |
| 7920 | /* Check stack available size. */ |
| 7921 | if (!lua_checkstack(hlua->T, 1)) { |
| 7922 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7923 | goto error; |
| 7924 | } |
| 7925 | lua_pushstring(hlua->T, args[i]); |
| 7926 | hlua->nargs++; |
| 7927 | } |
| 7928 | |
| 7929 | /* We must initialize the execution timeouts. */ |
| 7930 | hlua->max_time = hlua_timeout_session; |
| 7931 | |
| 7932 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7933 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7934 | |
| 7935 | /* It's ok */ |
| 7936 | return 0; |
| 7937 | |
| 7938 | /* It's not ok. */ |
| 7939 | error: |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7940 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7941 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7942 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7943 | return 1; |
| 7944 | } |
| 7945 | |
| 7946 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 7947 | { |
| 7948 | struct hlua *hlua; |
| 7949 | struct stream_interface *si; |
| 7950 | struct hlua_function *fcn; |
| 7951 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7952 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7953 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 7954 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7955 | |
| 7956 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7957 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7958 | return 1; |
| 7959 | |
| 7960 | /* Execute the function. */ |
| 7961 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7962 | |
| 7963 | /* finished. */ |
| 7964 | case HLUA_E_OK: |
| 7965 | return 1; |
| 7966 | |
| 7967 | /* yield. */ |
| 7968 | case HLUA_E_AGAIN: |
| 7969 | /* We want write. */ |
| 7970 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 7971 | si_rx_room_blk(si); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7972 | /* Set the timeout. */ |
| 7973 | if (hlua->wake_time != TICK_ETERNITY) |
| 7974 | task_schedule(hlua->task, hlua->wake_time); |
| 7975 | return 0; |
| 7976 | |
| 7977 | /* finished with error. */ |
| 7978 | case HLUA_E_ERRMSG: |
| 7979 | /* Display log. */ |
| 7980 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 7981 | fcn->name, lua_tostring(hlua->T, -1)); |
| 7982 | lua_pop(hlua->T, 1); |
| 7983 | return 1; |
| 7984 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7985 | case HLUA_E_ETMOUT: |
| 7986 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 7987 | fcn->name); |
| 7988 | return 1; |
| 7989 | |
| 7990 | case HLUA_E_NOMEM: |
| 7991 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 7992 | fcn->name); |
| 7993 | return 1; |
| 7994 | |
| 7995 | case HLUA_E_YIELD: /* unexpected */ |
| 7996 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 7997 | fcn->name); |
| 7998 | return 1; |
| 7999 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8000 | case HLUA_E_ERR: |
| 8001 | /* Display log. */ |
| 8002 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 8003 | fcn->name); |
| 8004 | return 1; |
| 8005 | |
| 8006 | default: |
| 8007 | return 1; |
| 8008 | } |
| 8009 | |
| 8010 | return 1; |
| 8011 | } |
| 8012 | |
| 8013 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 8014 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8015 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8016 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8017 | } |
| 8018 | |
| 8019 | /* This function is an LUA binding used for registering |
| 8020 | * new keywords in the cli. It expects a list of keywords |
| 8021 | * which are the "path". It is limited to 5 keywords. A |
| 8022 | * description of the command, a function to be executed |
| 8023 | * for the parsing and a function for io handlers. |
| 8024 | */ |
| 8025 | __LJMP static int hlua_register_cli(lua_State *L) |
| 8026 | { |
| 8027 | struct cli_kw_list *cli_kws; |
| 8028 | const char *message; |
| 8029 | int ref_io; |
| 8030 | int len; |
| 8031 | struct hlua_function *fcn; |
| 8032 | int index; |
| 8033 | int i; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8034 | struct buffer *trash; |
| 8035 | const char *kw[5]; |
| 8036 | struct cli_kw *cli_kw; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8037 | |
| 8038 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 8039 | |
| 8040 | /* First argument : an array of maximum 5 keywords. */ |
| 8041 | if (!lua_istable(L, 1)) |
| 8042 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 8043 | |
| 8044 | /* Second argument : string with contextual message. */ |
| 8045 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8046 | |
| 8047 | /* Third and fourth argument : lua function. */ |
| 8048 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 8049 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8050 | /* Check for CLI service already registered */ |
| 8051 | trash = get_trash_chunk(); |
| 8052 | index = 0; |
| 8053 | lua_pushnil(L); |
| 8054 | memset(kw, 0, sizeof(kw)); |
| 8055 | while (lua_next(L, 1) != 0) { |
| 8056 | if (index >= CLI_PREFIX_KW_NB) |
| 8057 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 8058 | if (lua_type(L, -1) != LUA_TSTRING) |
| 8059 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 8060 | kw[index] = lua_tostring(L, -1); |
| 8061 | if (index == 0) |
| 8062 | chunk_printf(trash, "%s", kw[index]); |
| 8063 | else |
| 8064 | chunk_appendf(trash, " %s", kw[index]); |
| 8065 | index++; |
| 8066 | lua_pop(L, 1); |
| 8067 | } |
| 8068 | cli_kw = cli_find_kw_exact((char **)kw); |
| 8069 | if (cli_kw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8070 | fcn = cli_kw->private; |
| 8071 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 8072 | ha_warning("Trying to register CLI keyword 'lua.%s' more than once. " |
| 8073 | "This will become a hard error in version 2.5.\n", trash->area); |
| 8074 | } |
| 8075 | fcn->function_ref[hlua_state_id] = ref_io; |
| 8076 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8077 | } |
| 8078 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8079 | /* Allocate and fill the sample fetch keyword struct. */ |
| 8080 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
| 8081 | if (!cli_kws) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8082 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 8083 | fcn = new_hlua_function(); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8084 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8085 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8086 | |
| 8087 | /* Fill path. */ |
| 8088 | index = 0; |
| 8089 | lua_pushnil(L); |
| 8090 | while(lua_next(L, 1) != 0) { |
| 8091 | if (index >= 5) |
| 8092 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 8093 | if (lua_type(L, -1) != LUA_TSTRING) |
| 8094 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 8095 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
| 8096 | if (!cli_kws->kw[0].str_kw[index]) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8097 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8098 | index++; |
| 8099 | lua_pop(L, 1); |
| 8100 | } |
| 8101 | |
| 8102 | /* Copy help message. */ |
| 8103 | cli_kws->kw[0].usage = strdup(message); |
| 8104 | if (!cli_kws->kw[0].usage) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8105 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8106 | |
| 8107 | /* Fill fcn io handler. */ |
| 8108 | len = strlen("<lua.cli>") + 1; |
| 8109 | for (i = 0; i < index; i++) |
| 8110 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 8111 | fcn->name = calloc(1, len); |
| 8112 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8113 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8114 | strncat((char *)fcn->name, "<lua.cli", len); |
| 8115 | for (i = 0; i < index; i++) { |
| 8116 | strncat((char *)fcn->name, ".", len); |
| 8117 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 8118 | } |
| 8119 | strncat((char *)fcn->name, ">", len); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8120 | fcn->function_ref[hlua_state_id] = ref_io; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8121 | |
| 8122 | /* Fill last entries. */ |
| 8123 | cli_kws->kw[0].private = fcn; |
| 8124 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 8125 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 8126 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 8127 | |
| 8128 | /* Register this new converter */ |
| 8129 | cli_register_kw(cli_kws); |
| 8130 | |
| 8131 | return 0; |
| 8132 | } |
| 8133 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8134 | static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx, |
| 8135 | struct proxy *defpx, const char *file, int line, |
| 8136 | char **err, unsigned int *timeout) |
| 8137 | { |
| 8138 | const char *error; |
| 8139 | |
| 8140 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 8141 | if (error == PARSE_TIME_OVER) { |
| 8142 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 8143 | args[1], args[0]); |
| 8144 | return -1; |
| 8145 | } |
| 8146 | else if (error == PARSE_TIME_UNDER) { |
| 8147 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 8148 | args[1], args[0]); |
| 8149 | return -1; |
| 8150 | } |
| 8151 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8152 | memprintf(err, "%s: invalid timeout", args[0]); |
| 8153 | return -1; |
| 8154 | } |
| 8155 | return 0; |
| 8156 | } |
| 8157 | |
| 8158 | static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx, |
| 8159 | struct proxy *defpx, const char *file, int line, |
| 8160 | char **err) |
| 8161 | { |
| 8162 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8163 | file, line, err, &hlua_timeout_session); |
| 8164 | } |
| 8165 | |
| 8166 | static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx, |
| 8167 | struct proxy *defpx, const char *file, int line, |
| 8168 | char **err) |
| 8169 | { |
| 8170 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8171 | file, line, err, &hlua_timeout_task); |
| 8172 | } |
| 8173 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8174 | static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx, |
| 8175 | struct proxy *defpx, const char *file, int line, |
| 8176 | char **err) |
| 8177 | { |
| 8178 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8179 | file, line, err, &hlua_timeout_applet); |
| 8180 | } |
| 8181 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8182 | static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx, |
| 8183 | struct proxy *defpx, const char *file, int line, |
| 8184 | char **err) |
| 8185 | { |
| 8186 | char *error; |
| 8187 | |
| 8188 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 8189 | if (*error != '\0') { |
| 8190 | memprintf(err, "%s: invalid number", args[0]); |
| 8191 | return -1; |
| 8192 | } |
| 8193 | return 0; |
| 8194 | } |
| 8195 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8196 | static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx, |
| 8197 | struct proxy *defpx, const char *file, int line, |
| 8198 | char **err) |
| 8199 | { |
| 8200 | char *error; |
| 8201 | |
| 8202 | if (*(args[1]) == 0) { |
| 8203 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 8204 | return -1; |
| 8205 | } |
| 8206 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 8207 | if (*error != '\0') { |
| 8208 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 8209 | return -1; |
| 8210 | } |
| 8211 | return 0; |
| 8212 | } |
| 8213 | |
| 8214 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8215 | /* This function is called by the main configuration key "lua-load". It loads and |
| 8216 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 8217 | * the main lua entry point. |
| 8218 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8219 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 8220 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8221 | * |
| 8222 | * In some error case, LUA set an error message in top of the stack. This function |
| 8223 | * 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] | 8224 | * |
| 8225 | * This function can fail with an abort() due to an Lua critical error. |
| 8226 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 8227 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8228 | */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8229 | static int hlua_load_state(char *filename, lua_State *L, char **err) |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8230 | { |
| 8231 | int error; |
| 8232 | |
| 8233 | /* Just load and compile the file. */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8234 | error = luaL_loadfile(L, filename); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8235 | if (error) { |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8236 | memprintf(err, "error in Lua file '%s': %s", filename, lua_tostring(L, -1)); |
| 8237 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8238 | return -1; |
| 8239 | } |
| 8240 | |
| 8241 | /* If no syntax error where detected, execute the code. */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8242 | error = lua_pcall(L, 0, LUA_MULTRET, 0); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8243 | switch (error) { |
| 8244 | case LUA_OK: |
| 8245 | break; |
| 8246 | case LUA_ERRRUN: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8247 | memprintf(err, "Lua runtime error: %s\n", lua_tostring(L, -1)); |
| 8248 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8249 | return -1; |
| 8250 | case LUA_ERRMEM: |
Thierry Fournier | de6145f | 2020-11-29 00:55:53 +0100 | [diff] [blame] | 8251 | memprintf(err, "Lua out of memory error\n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8252 | return -1; |
| 8253 | case LUA_ERRERR: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8254 | memprintf(err, "Lua message handler error: %s\n", lua_tostring(L, -1)); |
| 8255 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8256 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 8257 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8258 | case LUA_ERRGCMM: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8259 | memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(L, -1)); |
| 8260 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8261 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 8262 | #endif |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8263 | default: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8264 | memprintf(err, "Lua unknown error: %s\n", lua_tostring(L, -1)); |
| 8265 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8266 | return -1; |
| 8267 | } |
| 8268 | |
| 8269 | return 0; |
| 8270 | } |
| 8271 | |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8272 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
| 8273 | struct proxy *defpx, const char *file, int line, |
| 8274 | char **err) |
| 8275 | { |
| 8276 | if (*(args[1]) == 0) { |
| 8277 | memprintf(err, "'%s' expects a file name as parameter.\n", args[0]); |
| 8278 | return -1; |
| 8279 | } |
| 8280 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8281 | /* loading for global state */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8282 | hlua_state_id = 0; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8283 | ha_set_tid(0); |
Thierry Fournier | afc63e2 | 2020-11-28 17:06:51 +0100 | [diff] [blame] | 8284 | return hlua_load_state(args[1], hlua_states[0], err); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8285 | } |
| 8286 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8287 | static int hlua_load_per_thread(char **args, int section_type, struct proxy *curpx, |
| 8288 | struct proxy *defpx, const char *file, int line, |
| 8289 | char **err) |
| 8290 | { |
| 8291 | int len; |
| 8292 | |
| 8293 | if (*(args[1]) == 0) { |
| 8294 | memprintf(err, "'%s' expects a file as parameter.\n", args[0]); |
| 8295 | return -1; |
| 8296 | } |
| 8297 | |
| 8298 | if (per_thread_load == NULL) { |
| 8299 | /* allocate the first entry large enough to store the final NULL */ |
| 8300 | per_thread_load = calloc(1, sizeof(*per_thread_load)); |
| 8301 | if (per_thread_load == NULL) { |
| 8302 | memprintf(err, "out of memory error"); |
| 8303 | return -1; |
| 8304 | } |
| 8305 | } |
| 8306 | |
| 8307 | /* count used entries */ |
| 8308 | for (len = 0; per_thread_load[len] != NULL; len++) |
| 8309 | ; |
| 8310 | |
| 8311 | per_thread_load = realloc(per_thread_load, (len + 2) * sizeof(*per_thread_load)); |
| 8312 | if (per_thread_load == NULL) { |
| 8313 | memprintf(err, "out of memory error"); |
| 8314 | return -1; |
| 8315 | } |
| 8316 | |
| 8317 | per_thread_load[len] = strdup(args[1]); |
| 8318 | per_thread_load[len + 1] = NULL; |
| 8319 | |
| 8320 | if (per_thread_load[len] == NULL) { |
| 8321 | memprintf(err, "out of memory error"); |
| 8322 | return -1; |
| 8323 | } |
| 8324 | |
| 8325 | /* loading for thread 1 only */ |
| 8326 | hlua_state_id = 1; |
| 8327 | ha_set_tid(0); |
| 8328 | return hlua_load_state(args[1], hlua_states[1], err); |
| 8329 | } |
| 8330 | |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 8331 | /* Prepend the given <path> followed by a semicolon to the `package.<type>` variable |
| 8332 | * in the given <ctx>. |
| 8333 | */ |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 8334 | static int hlua_prepend_path(lua_State *L, char *type, char *path) |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 8335 | { |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 8336 | lua_getglobal(L, "package"); /* push package variable */ |
| 8337 | lua_pushstring(L, path); /* push given path */ |
| 8338 | lua_pushstring(L, ";"); /* push semicolon */ |
| 8339 | lua_getfield(L, -3, type); /* push old path */ |
| 8340 | lua_concat(L, 3); /* concatenate to new path */ |
| 8341 | lua_setfield(L, -2, type); /* store new path */ |
| 8342 | lua_pop(L, 1); /* pop package variable */ |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 8343 | |
| 8344 | return 0; |
| 8345 | } |
| 8346 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8347 | static int hlua_config_prepend_path(char **args, int section_type, struct proxy *curpx, |
| 8348 | struct proxy *defpx, const char *file, int line, |
| 8349 | char **err) |
| 8350 | { |
| 8351 | char *path; |
| 8352 | char *type = "path"; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8353 | struct prepend_path *p = NULL; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8354 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8355 | if (too_many_args(2, args, err, NULL)) { |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8356 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8357 | } |
| 8358 | |
| 8359 | if (!(*args[1])) { |
| 8360 | memprintf(err, "'%s' expects to receive a <path> as argument", args[0]); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8361 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8362 | } |
| 8363 | path = args[1]; |
| 8364 | |
| 8365 | if (*args[2]) { |
| 8366 | if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) { |
| 8367 | 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] | 8368 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8369 | } |
| 8370 | type = args[2]; |
| 8371 | } |
| 8372 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8373 | p = calloc(1, sizeof(*p)); |
| 8374 | if (p == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 8375 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8376 | goto err; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8377 | } |
| 8378 | p->path = strdup(path); |
| 8379 | if (p->path == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 8380 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8381 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8382 | } |
| 8383 | p->type = strdup(type); |
| 8384 | if (p->type == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 8385 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8386 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8387 | } |
| 8388 | LIST_ADDQ(&prepend_path_list, &p->l); |
| 8389 | |
| 8390 | hlua_prepend_path(hlua_states[0], type, path); |
| 8391 | hlua_prepend_path(hlua_states[1], type, path); |
| 8392 | return 0; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8393 | |
| 8394 | err2: |
| 8395 | free(p->type); |
| 8396 | free(p->path); |
| 8397 | err: |
| 8398 | free(p); |
| 8399 | return -1; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8400 | } |
| 8401 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8402 | /* configuration keywords declaration */ |
| 8403 | static struct cfg_kw_list cfg_kws = {{ },{ |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8404 | { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8405 | { CFG_GLOBAL, "lua-load", hlua_load }, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8406 | { CFG_GLOBAL, "lua-load-per-thread", hlua_load_per_thread }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8407 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 8408 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 8409 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8410 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8411 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8412 | { 0, NULL, NULL }, |
| 8413 | }}; |
| 8414 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 8415 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 8416 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 8417 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8418 | /* This function can fail with an abort() due to an Lua critical error. |
| 8419 | * We are in the initialisation process of HAProxy, this abort() is |
| 8420 | * tolerated. |
| 8421 | */ |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8422 | int hlua_post_init_state(lua_State *L) |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8423 | { |
| 8424 | struct hlua_init_function *init; |
| 8425 | const char *msg; |
| 8426 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8427 | const char *error; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8428 | const char *kind; |
| 8429 | const char *trace; |
| 8430 | int return_status = 1; |
| 8431 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 8432 | int nres; |
| 8433 | #endif |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8434 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8435 | /* disable memory limit checks if limit is not set */ |
| 8436 | if (!hlua_global_allocator.limit) |
| 8437 | hlua_global_allocator.limit = ~hlua_global_allocator.limit; |
| 8438 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 8439 | /* Call post initialisation function in safe environment. */ |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8440 | if (setjmp(safe_ljmp_env) != 0) { |
| 8441 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8442 | if (lua_type(L, -1) == LUA_TSTRING) |
| 8443 | error = lua_tostring(L, -1); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8444 | else |
| 8445 | error = "critical error"; |
| 8446 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 8447 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8448 | } else { |
| 8449 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8450 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 8451 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8452 | hlua_fcn_post_init(L); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8453 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8454 | list_for_each_entry(init, &hlua_init_functions[hlua_state_id], l) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8455 | lua_rawgeti(L, LUA_REGISTRYINDEX, init->function_ref); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8456 | |
| 8457 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8458 | ret = lua_resume(L, L, 0, &nres); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8459 | #else |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8460 | ret = lua_resume(L, L, 0); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8461 | #endif |
| 8462 | kind = NULL; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8463 | switch (ret) { |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8464 | |
| 8465 | case LUA_OK: |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8466 | lua_pop(L, -1); |
Thierry Fournier | 13d08b7 | 2020-11-28 11:02:58 +0100 | [diff] [blame] | 8467 | break; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8468 | |
| 8469 | case LUA_ERRERR: |
| 8470 | kind = "message handler error"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8471 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8472 | case LUA_ERRRUN: |
| 8473 | if (!kind) |
| 8474 | kind = "runtime error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8475 | msg = lua_tostring(L, -1); |
| 8476 | lua_settop(L, 0); /* Empty the stack. */ |
| 8477 | lua_pop(L, 1); |
| 8478 | trace = hlua_traceback(L); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8479 | if (msg) |
| 8480 | ha_alert("Lua init: %s: '%s' from %s\n", kind, msg, trace); |
| 8481 | else |
| 8482 | ha_alert("Lua init: unknown %s from %s\n", kind, trace); |
| 8483 | return_status = 0; |
| 8484 | break; |
| 8485 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8486 | default: |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8487 | /* Unknown error */ |
| 8488 | kind = "Unknown error"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8489 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8490 | case LUA_YIELD: |
| 8491 | /* yield is not configured at this step, this state doesn't happen */ |
| 8492 | if (!kind) |
| 8493 | kind = "yield not allowed"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8494 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8495 | case LUA_ERRMEM: |
| 8496 | if (!kind) |
| 8497 | kind = "out of memory error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8498 | lua_settop(L, 0); |
| 8499 | lua_pop(L, 1); |
| 8500 | trace = hlua_traceback(L); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8501 | ha_alert("Lua init: %s: %s\n", kind, trace); |
| 8502 | return_status = 0; |
| 8503 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8504 | } |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8505 | if (!return_status) |
| 8506 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8507 | } |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8508 | |
| 8509 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8510 | return return_status; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8511 | } |
| 8512 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8513 | int hlua_post_init() |
| 8514 | { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8515 | int ret; |
| 8516 | int i; |
| 8517 | int errors; |
| 8518 | char *err = NULL; |
| 8519 | struct hlua_function *fcn; |
| 8520 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8521 | #if USE_OPENSSL |
| 8522 | /* Initialize SSL server. */ |
| 8523 | if (socket_ssl.xprt->prepare_srv) { |
| 8524 | int saved_used_backed = global.ssl_used_backend; |
| 8525 | // don't affect maxconn automatic computation |
| 8526 | socket_ssl.xprt->prepare_srv(&socket_ssl); |
| 8527 | global.ssl_used_backend = saved_used_backed; |
| 8528 | } |
| 8529 | #endif |
| 8530 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8531 | /* Perform post init of common thread */ |
| 8532 | hlua_state_id = 0; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8533 | ha_set_tid(0); |
| 8534 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 8535 | if (ret == 0) |
| 8536 | return 0; |
| 8537 | |
| 8538 | /* init remaining lua states and load files */ |
| 8539 | for (hlua_state_id = 2; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 8540 | |
| 8541 | /* set thread context */ |
| 8542 | ha_set_tid(hlua_state_id - 1); |
| 8543 | |
| 8544 | /* Init lua state */ |
| 8545 | hlua_states[hlua_state_id] = hlua_init_state(hlua_state_id); |
| 8546 | |
| 8547 | /* Load lua files */ |
| 8548 | for (i = 0; per_thread_load && per_thread_load[i]; i++) { |
| 8549 | ret = hlua_load_state(per_thread_load[i], hlua_states[hlua_state_id], &err); |
| 8550 | if (ret != 0) { |
| 8551 | ha_alert("Lua init: %s\n", err); |
| 8552 | return 0; |
| 8553 | } |
| 8554 | } |
| 8555 | } |
| 8556 | |
| 8557 | /* Reset thread context */ |
| 8558 | ha_set_tid(0); |
| 8559 | |
| 8560 | /* Execute post init for all states */ |
| 8561 | for (hlua_state_id = 1; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 8562 | |
| 8563 | /* set thread context */ |
| 8564 | ha_set_tid(hlua_state_id - 1); |
| 8565 | |
| 8566 | /* run post init */ |
| 8567 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 8568 | if (ret == 0) |
| 8569 | return 0; |
| 8570 | } |
| 8571 | |
| 8572 | /* Reset thread context */ |
| 8573 | ha_set_tid(0); |
| 8574 | |
| 8575 | /* control functions registering. Each function must have: |
| 8576 | * - only the function_ref[0] set positive and all other to -1 |
| 8577 | * - only the function_ref[0] set to -1 and all other positive |
| 8578 | * This ensure a same reference is not used both in shared |
| 8579 | * lua state and thread dedicated lua state. Note: is the case |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 8580 | * reach, the shared state is priority, but the bug will be |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8581 | * complicated to found for the end user. |
| 8582 | */ |
| 8583 | errors = 0; |
| 8584 | list_for_each_entry(fcn, &referenced_functions, l) { |
| 8585 | ret = 0; |
| 8586 | for (i = 1; i < global.nbthread + 1; i++) { |
| 8587 | if (fcn->function_ref[i] == -1) |
| 8588 | ret--; |
| 8589 | else |
| 8590 | ret++; |
| 8591 | } |
| 8592 | if (abs(ret) != global.nbthread) { |
| 8593 | ha_alert("Lua function '%s' is not referenced in all thread. " |
| 8594 | "Expect function in all thread or in none thread.\n", fcn->name); |
| 8595 | errors++; |
| 8596 | continue; |
| 8597 | } |
| 8598 | |
| 8599 | if ((fcn->function_ref[0] == -1) == (ret < 0)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 8600 | ha_alert("Lua function '%s' is referenced both ins shared Lua context (through lua-load) " |
| 8601 | "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] | 8602 | "exclusive.\n", fcn->name); |
| 8603 | errors++; |
| 8604 | } |
| 8605 | } |
| 8606 | |
| 8607 | if (errors > 0) |
| 8608 | return 0; |
| 8609 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 8610 | /* 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] | 8611 | * -1 in order to have probably a segfault if someone use it |
| 8612 | */ |
| 8613 | hlua_state_id = -1; |
| 8614 | |
| 8615 | return 1; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8616 | } |
| 8617 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8618 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 8619 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 8620 | * 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] | 8621 | * allocation. <nsize> is the requested new size. A new allocation is |
| 8622 | * indicated by <ptr> being NULL. A free is indicated by <nsize> being |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8623 | * zero. This one verifies that the limits are respected but is optimized |
| 8624 | * 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] | 8625 | */ |
| 8626 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 8627 | { |
| 8628 | struct hlua_mem_allocator *zone = ud; |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8629 | size_t limit, old, new; |
| 8630 | |
Tim Duesterhus | 2258652 | 2021-01-08 10:35:33 +0100 | [diff] [blame] | 8631 | if (unlikely(!ptr && !nsize)) |
| 8632 | return NULL; |
| 8633 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8634 | /* a limit of ~0 means unlimited and boot complete, so there's no need |
| 8635 | * for accounting anymore. |
| 8636 | */ |
| 8637 | if (likely(~zone->limit == 0)) |
| 8638 | return realloc(ptr, nsize); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8639 | |
Willy Tarreau | d36c7fa | 2020-12-02 12:26:29 +0100 | [diff] [blame] | 8640 | if (!ptr) |
| 8641 | osize = 0; |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8642 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8643 | /* enforce strict limits across all threads */ |
| 8644 | limit = zone->limit; |
| 8645 | old = _HA_ATOMIC_LOAD(&zone->allocated); |
| 8646 | do { |
| 8647 | new = old + nsize - osize; |
| 8648 | if (unlikely(nsize && limit && new > limit)) |
| 8649 | return NULL; |
| 8650 | } while (!_HA_ATOMIC_CAS(&zone->allocated, &old, new)); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8651 | |
| 8652 | ptr = realloc(ptr, nsize); |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8653 | |
| 8654 | if (unlikely(!ptr && nsize)) // failed |
| 8655 | _HA_ATOMIC_SUB(&zone->allocated, nsize - osize); |
| 8656 | |
| 8657 | __ha_barrier_atomic_store(); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8658 | return ptr; |
| 8659 | } |
| 8660 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 8661 | /* 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] | 8662 | * We are in the initialisation process of HAProxy, this abort() is |
| 8663 | * tolerated. |
| 8664 | */ |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 8665 | lua_State *hlua_init_state(int thread_num) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8666 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8667 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8668 | int idx; |
| 8669 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8670 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8671 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8672 | const char *error_msg; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8673 | void **context; |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8674 | lua_State *L; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8675 | struct prepend_path *pp; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8676 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8677 | /* Init main lua stack. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8678 | L = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8679 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8680 | /* Initialise Lua context to NULL */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8681 | context = lua_getextraspace(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8682 | *context = NULL; |
| 8683 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8684 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8685 | * the Lua function can fail with an abort. We are in the initialisation |
| 8686 | * process of HAProxy, this abort() is tolerated. |
| 8687 | */ |
| 8688 | |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8689 | /* Call post initialisation function in safe environment. */ |
| 8690 | if (setjmp(safe_ljmp_env) != 0) { |
| 8691 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8692 | if (lua_type(L, -1) == LUA_TSTRING) |
| 8693 | error_msg = lua_tostring(L, -1); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 8694 | else |
| 8695 | error_msg = "critical error"; |
| 8696 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
| 8697 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8698 | } else { |
| 8699 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 8700 | } |
| 8701 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8702 | /* Initialise lua. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8703 | luaL_openlibs(L); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 8704 | #define HLUA_PREPEND_PATH_TOSTRING1(x) #x |
| 8705 | #define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x) |
| 8706 | #ifdef HLUA_PREPEND_PATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8707 | hlua_prepend_path(L, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 8708 | #endif |
| 8709 | #ifdef HLUA_PREPEND_CPATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8710 | hlua_prepend_path(L, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 8711 | #endif |
| 8712 | #undef HLUA_PREPEND_PATH_TOSTRING |
| 8713 | #undef HLUA_PREPEND_PATH_TOSTRING1 |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8714 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8715 | /* Apply configured prepend path */ |
| 8716 | list_for_each_entry(pp, &prepend_path_list, l) |
| 8717 | hlua_prepend_path(L, pp->type, pp->path); |
| 8718 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8719 | /* |
| 8720 | * |
| 8721 | * Create "core" object. |
| 8722 | * |
| 8723 | */ |
| 8724 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 8725 | /* This table entry is the object "core" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8726 | lua_newtable(L); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8727 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 8728 | /* set the thread id */ |
| 8729 | hlua_class_const_int(L, "thread", thread_num); |
| 8730 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8731 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8732 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8733 | hlua_class_const_int(L, log_levels[i], i); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8734 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8735 | /* Register special functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8736 | hlua_class_function(L, "register_init", hlua_register_init); |
| 8737 | hlua_class_function(L, "register_task", hlua_register_task); |
| 8738 | hlua_class_function(L, "register_fetches", hlua_register_fetches); |
| 8739 | hlua_class_function(L, "register_converters", hlua_register_converters); |
| 8740 | hlua_class_function(L, "register_action", hlua_register_action); |
| 8741 | hlua_class_function(L, "register_service", hlua_register_service); |
| 8742 | hlua_class_function(L, "register_cli", hlua_register_cli); |
| 8743 | hlua_class_function(L, "yield", hlua_yield); |
| 8744 | hlua_class_function(L, "set_nice", hlua_set_nice); |
| 8745 | hlua_class_function(L, "sleep", hlua_sleep); |
| 8746 | hlua_class_function(L, "msleep", hlua_msleep); |
| 8747 | hlua_class_function(L, "add_acl", hlua_add_acl); |
| 8748 | hlua_class_function(L, "del_acl", hlua_del_acl); |
| 8749 | hlua_class_function(L, "set_map", hlua_set_map); |
| 8750 | hlua_class_function(L, "del_map", hlua_del_map); |
| 8751 | hlua_class_function(L, "tcp", hlua_socket_new); |
| 8752 | hlua_class_function(L, "log", hlua_log); |
| 8753 | hlua_class_function(L, "Debug", hlua_log_debug); |
| 8754 | hlua_class_function(L, "Info", hlua_log_info); |
| 8755 | hlua_class_function(L, "Warning", hlua_log_warning); |
| 8756 | hlua_class_function(L, "Alert", hlua_log_alert); |
| 8757 | hlua_class_function(L, "done", hlua_done); |
| 8758 | hlua_fcn_reg_core_fcn(L); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8759 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8760 | lua_setglobal(L, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8761 | |
| 8762 | /* |
| 8763 | * |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8764 | * Create "act" object. |
| 8765 | * |
| 8766 | */ |
| 8767 | |
| 8768 | /* This table entry is the object "act" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8769 | lua_newtable(L); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8770 | |
| 8771 | /* push action return constants */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8772 | hlua_class_const_int(L, "CONTINUE", ACT_RET_CONT); |
| 8773 | hlua_class_const_int(L, "STOP", ACT_RET_STOP); |
| 8774 | hlua_class_const_int(L, "YIELD", ACT_RET_YIELD); |
| 8775 | hlua_class_const_int(L, "ERROR", ACT_RET_ERR); |
| 8776 | hlua_class_const_int(L, "DONE", ACT_RET_DONE); |
| 8777 | hlua_class_const_int(L, "DENY", ACT_RET_DENY); |
| 8778 | hlua_class_const_int(L, "ABORT", ACT_RET_ABRT); |
| 8779 | hlua_class_const_int(L, "INVALID", ACT_RET_INV); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8780 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8781 | hlua_class_function(L, "wake_time", hlua_set_wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8782 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8783 | lua_setglobal(L, "act"); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8784 | |
| 8785 | /* |
| 8786 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8787 | * Register class Map |
| 8788 | * |
| 8789 | */ |
| 8790 | |
| 8791 | /* This table entry is the object "Map" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8792 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8793 | |
| 8794 | /* register pattern types. */ |
| 8795 | for (i=0; i<PAT_MATCH_NUM; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8796 | hlua_class_const_int(L, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8797 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8798 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8799 | hlua_class_const_int(L, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8800 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8801 | |
| 8802 | /* register constructor. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8803 | hlua_class_function(L, "new", hlua_map_new); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8804 | |
| 8805 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8806 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8807 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8808 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8809 | lua_pushstring(L, "__index"); |
| 8810 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8811 | |
| 8812 | /* Register . */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8813 | hlua_class_function(L, "lookup", hlua_map_lookup); |
| 8814 | hlua_class_function(L, "slookup", hlua_map_slookup); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8815 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8816 | lua_rawset(L, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8817 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8818 | /* Register previous table in the registry with reference and named entry. |
| 8819 | * The function hlua_register_metatable() pops the stack, so we |
| 8820 | * previously create a copy of the table. |
| 8821 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8822 | lua_pushvalue(L, -1); /* Copy the -1 entry and push it on the stack. */ |
| 8823 | class_map_ref = hlua_register_metatable(L, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8824 | |
| 8825 | /* Assign the metatable to the mai Map object. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8826 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8827 | |
| 8828 | /* Set a name to the table. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8829 | lua_setglobal(L, "Map"); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8830 | |
| 8831 | /* |
| 8832 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8833 | * Register class Channel |
| 8834 | * |
| 8835 | */ |
| 8836 | |
| 8837 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8838 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8839 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8840 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8841 | lua_pushstring(L, "__index"); |
| 8842 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8843 | |
| 8844 | /* Register . */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8845 | hlua_class_function(L, "get", hlua_channel_get); |
| 8846 | hlua_class_function(L, "dup", hlua_channel_dup); |
| 8847 | hlua_class_function(L, "getline", hlua_channel_getline); |
| 8848 | hlua_class_function(L, "set", hlua_channel_set); |
| 8849 | hlua_class_function(L, "append", hlua_channel_append); |
| 8850 | hlua_class_function(L, "send", hlua_channel_send); |
| 8851 | hlua_class_function(L, "forward", hlua_channel_forward); |
| 8852 | hlua_class_function(L, "get_in_len", hlua_channel_get_in_len); |
| 8853 | hlua_class_function(L, "get_out_len", hlua_channel_get_out_len); |
| 8854 | hlua_class_function(L, "is_full", hlua_channel_is_full); |
| 8855 | hlua_class_function(L, "is_resp", hlua_channel_is_resp); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8856 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8857 | lua_rawset(L, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8858 | |
| 8859 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8860 | class_channel_ref = hlua_register_metatable(L, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8861 | |
| 8862 | /* |
| 8863 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8864 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8865 | * |
| 8866 | */ |
| 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 | 65f34c6 | 2015-02-16 20:11:43 +0100 | [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 | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8874 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8875 | /* Browse existing fetches and create the associated |
| 8876 | * object method. |
| 8877 | */ |
| 8878 | sf = NULL; |
| 8879 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8880 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 8881 | * by an underscore. |
| 8882 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8883 | strncpy(trash.area, sf->kw, trash.size); |
| 8884 | trash.area[trash.size - 1] = '\0'; |
| 8885 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8886 | if (*p == '.' || *p == '-' || *p == '+') |
| 8887 | *p = '_'; |
| 8888 | |
| 8889 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8890 | lua_pushstring(L, trash.area); |
| 8891 | lua_pushlightuserdata(L, sf); |
| 8892 | lua_pushcclosure(L, hlua_run_sample_fetch, 1); |
| 8893 | lua_rawset(L, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8894 | } |
| 8895 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8896 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8897 | |
| 8898 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8899 | class_fetches_ref = hlua_register_metatable(L, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8900 | |
| 8901 | /* |
| 8902 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8903 | * Register class Converters |
| 8904 | * |
| 8905 | */ |
| 8906 | |
| 8907 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8908 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8909 | |
| 8910 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8911 | lua_pushstring(L, "__index"); |
| 8912 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8913 | |
| 8914 | /* Browse existing converters and create the associated |
| 8915 | * object method. |
| 8916 | */ |
| 8917 | sc = NULL; |
| 8918 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8919 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 8920 | * by an underscore. |
| 8921 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8922 | strncpy(trash.area, sc->kw, trash.size); |
| 8923 | trash.area[trash.size - 1] = '\0'; |
| 8924 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8925 | if (*p == '.' || *p == '-' || *p == '+') |
| 8926 | *p = '_'; |
| 8927 | |
| 8928 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8929 | lua_pushstring(L, trash.area); |
| 8930 | lua_pushlightuserdata(L, sc); |
| 8931 | lua_pushcclosure(L, hlua_run_sample_conv, 1); |
| 8932 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8933 | } |
| 8934 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8935 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8936 | |
| 8937 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8938 | class_converters_ref = hlua_register_metatable(L, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8939 | |
| 8940 | /* |
| 8941 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8942 | * Register class HTTP |
| 8943 | * |
| 8944 | */ |
| 8945 | |
| 8946 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8947 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8948 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8949 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8950 | lua_pushstring(L, "__index"); |
| 8951 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8952 | |
| 8953 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8954 | hlua_class_function(L, "req_get_headers",hlua_http_req_get_headers); |
| 8955 | hlua_class_function(L, "req_del_header", hlua_http_req_del_hdr); |
| 8956 | hlua_class_function(L, "req_rep_header", hlua_http_req_rep_hdr); |
| 8957 | hlua_class_function(L, "req_rep_value", hlua_http_req_rep_val); |
| 8958 | hlua_class_function(L, "req_add_header", hlua_http_req_add_hdr); |
| 8959 | hlua_class_function(L, "req_set_header", hlua_http_req_set_hdr); |
| 8960 | hlua_class_function(L, "req_set_method", hlua_http_req_set_meth); |
| 8961 | hlua_class_function(L, "req_set_path", hlua_http_req_set_path); |
| 8962 | hlua_class_function(L, "req_set_query", hlua_http_req_set_query); |
| 8963 | hlua_class_function(L, "req_set_uri", hlua_http_req_set_uri); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8964 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8965 | hlua_class_function(L, "res_get_headers",hlua_http_res_get_headers); |
| 8966 | hlua_class_function(L, "res_del_header", hlua_http_res_del_hdr); |
| 8967 | hlua_class_function(L, "res_rep_header", hlua_http_res_rep_hdr); |
| 8968 | hlua_class_function(L, "res_rep_value", hlua_http_res_rep_val); |
| 8969 | hlua_class_function(L, "res_add_header", hlua_http_res_add_hdr); |
| 8970 | hlua_class_function(L, "res_set_header", hlua_http_res_set_hdr); |
| 8971 | hlua_class_function(L, "res_set_status", hlua_http_res_set_status); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8972 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8973 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8974 | |
| 8975 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8976 | class_http_ref = hlua_register_metatable(L, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8977 | |
| 8978 | /* |
| 8979 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8980 | * Register class AppletTCP |
| 8981 | * |
| 8982 | */ |
| 8983 | |
| 8984 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8985 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8986 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8987 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8988 | lua_pushstring(L, "__index"); |
| 8989 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8990 | |
| 8991 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8992 | hlua_class_function(L, "getline", hlua_applet_tcp_getline); |
| 8993 | hlua_class_function(L, "receive", hlua_applet_tcp_recv); |
| 8994 | hlua_class_function(L, "send", hlua_applet_tcp_send); |
| 8995 | hlua_class_function(L, "set_priv", hlua_applet_tcp_set_priv); |
| 8996 | hlua_class_function(L, "get_priv", hlua_applet_tcp_get_priv); |
| 8997 | hlua_class_function(L, "set_var", hlua_applet_tcp_set_var); |
| 8998 | hlua_class_function(L, "unset_var", hlua_applet_tcp_unset_var); |
| 8999 | hlua_class_function(L, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9000 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9001 | lua_settable(L, -3); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9002 | |
| 9003 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9004 | class_applet_tcp_ref = hlua_register_metatable(L, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9005 | |
| 9006 | /* |
| 9007 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9008 | * Register class AppletHTTP |
| 9009 | * |
| 9010 | */ |
| 9011 | |
| 9012 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9013 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9014 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9015 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9016 | lua_pushstring(L, "__index"); |
| 9017 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9018 | |
| 9019 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9020 | hlua_class_function(L, "set_priv", hlua_applet_http_set_priv); |
| 9021 | hlua_class_function(L, "get_priv", hlua_applet_http_get_priv); |
| 9022 | hlua_class_function(L, "set_var", hlua_applet_http_set_var); |
| 9023 | hlua_class_function(L, "unset_var", hlua_applet_http_unset_var); |
| 9024 | hlua_class_function(L, "get_var", hlua_applet_http_get_var); |
| 9025 | hlua_class_function(L, "getline", hlua_applet_http_getline); |
| 9026 | hlua_class_function(L, "receive", hlua_applet_http_recv); |
| 9027 | hlua_class_function(L, "send", hlua_applet_http_send); |
| 9028 | hlua_class_function(L, "add_header", hlua_applet_http_addheader); |
| 9029 | hlua_class_function(L, "set_status", hlua_applet_http_status); |
| 9030 | hlua_class_function(L, "start_response", hlua_applet_http_start_response); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9031 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9032 | lua_settable(L, -3); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9033 | |
| 9034 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9035 | class_applet_http_ref = hlua_register_metatable(L, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9036 | |
| 9037 | /* |
| 9038 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9039 | * Register class TXN |
| 9040 | * |
| 9041 | */ |
| 9042 | |
| 9043 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9044 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9045 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9046 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9047 | lua_pushstring(L, "__index"); |
| 9048 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9049 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 9050 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9051 | hlua_class_function(L, "set_priv", hlua_set_priv); |
| 9052 | hlua_class_function(L, "get_priv", hlua_get_priv); |
| 9053 | hlua_class_function(L, "set_var", hlua_set_var); |
| 9054 | hlua_class_function(L, "unset_var", hlua_unset_var); |
| 9055 | hlua_class_function(L, "get_var", hlua_get_var); |
| 9056 | hlua_class_function(L, "done", hlua_txn_done); |
| 9057 | hlua_class_function(L, "reply", hlua_txn_reply_new); |
| 9058 | hlua_class_function(L, "set_loglevel", hlua_txn_set_loglevel); |
| 9059 | hlua_class_function(L, "set_tos", hlua_txn_set_tos); |
| 9060 | hlua_class_function(L, "set_mark", hlua_txn_set_mark); |
| 9061 | hlua_class_function(L, "set_priority_class", hlua_txn_set_priority_class); |
| 9062 | hlua_class_function(L, "set_priority_offset", hlua_txn_set_priority_offset); |
| 9063 | hlua_class_function(L, "deflog", hlua_txn_deflog); |
| 9064 | hlua_class_function(L, "log", hlua_txn_log); |
| 9065 | hlua_class_function(L, "Debug", hlua_txn_log_debug); |
| 9066 | hlua_class_function(L, "Info", hlua_txn_log_info); |
| 9067 | hlua_class_function(L, "Warning", hlua_txn_log_warning); |
| 9068 | hlua_class_function(L, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 9069 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9070 | lua_rawset(L, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 9071 | |
| 9072 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9073 | class_txn_ref = hlua_register_metatable(L, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9074 | |
| 9075 | /* |
| 9076 | * |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 9077 | * Register class reply |
| 9078 | * |
| 9079 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9080 | lua_newtable(L); |
| 9081 | lua_pushstring(L, "__index"); |
| 9082 | lua_newtable(L); |
| 9083 | hlua_class_function(L, "set_status", hlua_txn_reply_set_status); |
| 9084 | hlua_class_function(L, "add_header", hlua_txn_reply_add_header); |
| 9085 | hlua_class_function(L, "del_header", hlua_txn_reply_del_header); |
| 9086 | hlua_class_function(L, "set_body", hlua_txn_reply_set_body); |
| 9087 | lua_settable(L, -3); /* Sets the __index entry. */ |
| 9088 | class_txn_reply_ref = luaL_ref(L, LUA_REGISTRYINDEX); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 9089 | |
| 9090 | |
| 9091 | /* |
| 9092 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9093 | * Register class Socket |
| 9094 | * |
| 9095 | */ |
| 9096 | |
| 9097 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9098 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9099 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9100 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9101 | lua_pushstring(L, "__index"); |
| 9102 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9103 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 9104 | #ifdef USE_OPENSSL |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9105 | hlua_class_function(L, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 9106 | #endif |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9107 | hlua_class_function(L, "connect", hlua_socket_connect); |
| 9108 | hlua_class_function(L, "send", hlua_socket_send); |
| 9109 | hlua_class_function(L, "receive", hlua_socket_receive); |
| 9110 | hlua_class_function(L, "close", hlua_socket_close); |
| 9111 | hlua_class_function(L, "getpeername", hlua_socket_getpeername); |
| 9112 | hlua_class_function(L, "getsockname", hlua_socket_getsockname); |
| 9113 | hlua_class_function(L, "setoption", hlua_socket_setoption); |
| 9114 | hlua_class_function(L, "settimeout", hlua_socket_settimeout); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9115 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9116 | 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] | 9117 | |
| 9118 | /* Register the garbage collector entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9119 | lua_pushstring(L, "__gc"); |
| 9120 | lua_pushcclosure(L, hlua_socket_gc, 0); |
| 9121 | 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] | 9122 | |
| 9123 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9124 | class_socket_ref = hlua_register_metatable(L, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9125 | |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 9126 | lua_atpanic(L, hlua_panic_safe); |
| 9127 | |
| 9128 | return L; |
| 9129 | } |
| 9130 | |
| 9131 | void hlua_init(void) { |
| 9132 | int i; |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 9133 | #ifdef USE_OPENSSL |
| 9134 | struct srv_kw *kw; |
| 9135 | int tmp_error; |
| 9136 | char *error; |
| 9137 | char *args[] = { /* SSL client configuration. */ |
| 9138 | "ssl", |
| 9139 | "verify", |
| 9140 | "none", |
| 9141 | NULL |
| 9142 | }; |
| 9143 | #endif |
| 9144 | |
| 9145 | /* Init post init function list head */ |
| 9146 | for (i = 0; i < MAX_THREADS + 1; i++) |
| 9147 | LIST_INIT(&hlua_init_functions[i]); |
| 9148 | |
| 9149 | /* Init state for common/shared lua parts */ |
| 9150 | hlua_state_id = 0; |
| 9151 | ha_set_tid(0); |
| 9152 | hlua_states[0] = hlua_init_state(0); |
| 9153 | |
| 9154 | /* Init state 1 for thread 0. We have at least one thread. */ |
| 9155 | hlua_state_id = 1; |
| 9156 | ha_set_tid(0); |
| 9157 | hlua_states[1] = hlua_init_state(1); |
| 9158 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9159 | /* Proxy and server configuration initialisation. */ |
| 9160 | memset(&socket_proxy, 0, sizeof(socket_proxy)); |
| 9161 | init_new_proxy(&socket_proxy); |
| 9162 | socket_proxy.parent = NULL; |
| 9163 | socket_proxy.last_change = now.tv_sec; |
| 9164 | socket_proxy.id = "LUA-SOCKET"; |
| 9165 | socket_proxy.cap = PR_CAP_FE | PR_CAP_BE; |
| 9166 | socket_proxy.maxconn = 0; |
| 9167 | socket_proxy.accept = NULL; |
| 9168 | socket_proxy.options2 |= PR_O2_INDEPSTR; |
| 9169 | socket_proxy.srv = NULL; |
| 9170 | socket_proxy.conn_retries = 0; |
| 9171 | socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */ |
| 9172 | |
| 9173 | /* Init TCP server: unchanged parameters */ |
| 9174 | memset(&socket_tcp, 0, sizeof(socket_tcp)); |
| 9175 | socket_tcp.next = NULL; |
| 9176 | socket_tcp.proxy = &socket_proxy; |
| 9177 | socket_tcp.obj_type = OBJ_TYPE_SERVER; |
| 9178 | LIST_INIT(&socket_tcp.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 9179 | socket_tcp.pendconns = EB_ROOT; |
Christopher Faulet | 40a007c | 2017-07-03 15:41:01 +0200 | [diff] [blame] | 9180 | socket_tcp.idle_conns = NULL; |
| 9181 | socket_tcp.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 9182 | socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9183 | socket_tcp.last_change = 0; |
| 9184 | socket_tcp.id = "LUA-TCP-CONN"; |
| 9185 | socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 9186 | socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 9187 | socket_tcp.pp_opts = 0; /* Remove proxy protocol. */ |
| 9188 | |
| 9189 | /* XXX: Copy default parameter from default server, |
| 9190 | * but the default server is not initialized. |
| 9191 | */ |
| 9192 | socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue; |
| 9193 | socket_tcp.minconn = socket_proxy.defsrv.minconn; |
| 9194 | socket_tcp.maxconn = socket_proxy.defsrv.maxconn; |
| 9195 | socket_tcp.slowstart = socket_proxy.defsrv.slowstart; |
| 9196 | socket_tcp.onerror = socket_proxy.defsrv.onerror; |
| 9197 | socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 9198 | socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 9199 | socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 9200 | socket_tcp.uweight = socket_proxy.defsrv.iweight; |
| 9201 | socket_tcp.iweight = socket_proxy.defsrv.iweight; |
| 9202 | |
| 9203 | socket_tcp.check.status = HCHK_STATUS_INI; |
| 9204 | socket_tcp.check.rise = socket_proxy.defsrv.check.rise; |
| 9205 | socket_tcp.check.fall = socket_proxy.defsrv.check.fall; |
| 9206 | socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */ |
| 9207 | socket_tcp.check.server = &socket_tcp; |
| 9208 | |
| 9209 | socket_tcp.agent.status = HCHK_STATUS_INI; |
| 9210 | socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise; |
| 9211 | socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall; |
| 9212 | socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */ |
| 9213 | socket_tcp.agent.server = &socket_tcp; |
| 9214 | |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 9215 | socket_tcp.xprt = xprt_get(XPRT_RAW); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9216 | |
| 9217 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9218 | /* Init TCP server: unchanged parameters */ |
| 9219 | memset(&socket_ssl, 0, sizeof(socket_ssl)); |
| 9220 | socket_ssl.next = NULL; |
| 9221 | socket_ssl.proxy = &socket_proxy; |
| 9222 | socket_ssl.obj_type = OBJ_TYPE_SERVER; |
| 9223 | LIST_INIT(&socket_ssl.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 9224 | socket_ssl.pendconns = EB_ROOT; |
Willy Tarreau | b784b35 | 2019-02-07 14:48:24 +0100 | [diff] [blame] | 9225 | socket_ssl.idle_conns = NULL; |
| 9226 | socket_ssl.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 9227 | socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9228 | socket_ssl.last_change = 0; |
| 9229 | socket_ssl.id = "LUA-SSL-CONN"; |
| 9230 | socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 9231 | socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 9232 | socket_ssl.pp_opts = 0; /* Remove proxy protocol. */ |
| 9233 | |
| 9234 | /* XXX: Copy default parameter from default server, |
| 9235 | * but the default server is not initialized. |
| 9236 | */ |
| 9237 | socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue; |
| 9238 | socket_ssl.minconn = socket_proxy.defsrv.minconn; |
| 9239 | socket_ssl.maxconn = socket_proxy.defsrv.maxconn; |
| 9240 | socket_ssl.slowstart = socket_proxy.defsrv.slowstart; |
| 9241 | socket_ssl.onerror = socket_proxy.defsrv.onerror; |
| 9242 | socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 9243 | socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 9244 | socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 9245 | socket_ssl.uweight = socket_proxy.defsrv.iweight; |
| 9246 | socket_ssl.iweight = socket_proxy.defsrv.iweight; |
| 9247 | |
| 9248 | socket_ssl.check.status = HCHK_STATUS_INI; |
| 9249 | socket_ssl.check.rise = socket_proxy.defsrv.check.rise; |
| 9250 | socket_ssl.check.fall = socket_proxy.defsrv.check.fall; |
| 9251 | socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */ |
| 9252 | socket_ssl.check.server = &socket_ssl; |
| 9253 | |
| 9254 | socket_ssl.agent.status = HCHK_STATUS_INI; |
| 9255 | socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise; |
| 9256 | socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall; |
| 9257 | socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */ |
| 9258 | socket_ssl.agent.server = &socket_ssl; |
| 9259 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 9260 | socket_ssl.use_ssl = 1; |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 9261 | socket_ssl.xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9262 | |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 9263 | for (i = 0; args[i] != NULL; i++) { |
| 9264 | 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] | 9265 | /* |
| 9266 | * |
| 9267 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9268 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9269 | * features like client certificates and ssl_verify. |
| 9270 | * |
| 9271 | */ |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 9272 | tmp_error = kw->parse(args, &i, &socket_proxy, &socket_ssl, &error); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9273 | if (tmp_error != 0) { |
| 9274 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 9275 | abort(); /* This must be never arrives because the command line |
| 9276 | not editable by the user. */ |
| 9277 | } |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 9278 | i += kw->skip; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9279 | } |
| 9280 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9281 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 9282 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 9283 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 9284 | |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 9285 | static void hlua_deinit() |
| 9286 | { |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 9287 | int thr; |
| 9288 | |
| 9289 | for (thr = 0; thr < MAX_THREADS+1; thr++) { |
| 9290 | if (hlua_states[thr]) |
| 9291 | lua_close(hlua_states[thr]); |
| 9292 | } |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 9293 | } |
| 9294 | |
| 9295 | REGISTER_POST_DEINIT(hlua_deinit); |
| 9296 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9297 | static void hlua_register_build_options(void) |
| 9298 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 9299 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9300 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 9301 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 9302 | hap_register_build_opts(ptr, 1); |
| 9303 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9304 | |
| 9305 | INITCALL0(STG_REGISTER, hlua_register_build_options); |