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 | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4210 | if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
| 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 | |
Christopher Faulet | e461e34 | 2018-12-18 16:43:35 +0100 | [diff] [blame] | 4384 | if (type == HTX_BLK_EOM) { |
| 4385 | stop = 1; |
| 4386 | break; |
| 4387 | } |
| 4388 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4389 | vlen = sz; |
| 4390 | if (vlen > count) { |
| 4391 | if (type != HTX_BLK_DATA) |
| 4392 | break; |
| 4393 | vlen = count; |
| 4394 | } |
| 4395 | |
| 4396 | switch (type) { |
| 4397 | case HTX_BLK_UNUSED: |
| 4398 | break; |
| 4399 | |
| 4400 | case HTX_BLK_DATA: |
| 4401 | v = htx_get_blk_value(htx, blk); |
| 4402 | v.len = vlen; |
| 4403 | nl = istchr(v, '\n'); |
| 4404 | if (nl != NULL) { |
| 4405 | stop = 1; |
| 4406 | vlen = nl - v.ptr + 1; |
| 4407 | } |
| 4408 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4409 | break; |
| 4410 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4411 | case HTX_BLK_TLR: |
| 4412 | case HTX_BLK_EOM: |
| 4413 | stop = 1; |
| 4414 | break; |
| 4415 | |
| 4416 | default: |
| 4417 | break; |
| 4418 | } |
| 4419 | |
| 4420 | co_set_data(req, co_data(req) - vlen); |
| 4421 | count -= vlen; |
| 4422 | if (sz == vlen) |
| 4423 | blk = htx_remove_blk(htx, blk); |
| 4424 | else { |
| 4425 | htx_cut_data_blk(htx, blk, vlen); |
| 4426 | break; |
| 4427 | } |
| 4428 | } |
| 4429 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4430 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4431 | if (!stop) { |
| 4432 | si_cant_get(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4433 | 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] | 4434 | } |
| 4435 | |
| 4436 | /* return the result. */ |
| 4437 | luaL_pushresult(&appctx->b); |
| 4438 | return 1; |
| 4439 | } |
| 4440 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4441 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4442 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4443 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4444 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4445 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4446 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4447 | /* Initialise the string catenation. */ |
| 4448 | luaL_buffinit(L, &appctx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4449 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4450 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4451 | } |
| 4452 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4453 | /* If expected data not yet available, it returns a yield. This function |
| 4454 | * consumes the data in the buffer. It returns a string containing the |
| 4455 | * data. This string can be empty. |
| 4456 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4457 | __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] | 4458 | { |
| 4459 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4460 | struct stream_interface *si = appctx->appctx->owner; |
| 4461 | struct channel *req = si_oc(si); |
| 4462 | struct htx *htx; |
| 4463 | struct htx_blk *blk; |
| 4464 | size_t count; |
| 4465 | int len; |
| 4466 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4467 | htx = htx_from_buf(&req->buf); |
| 4468 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4469 | count = co_data(req); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 4470 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4471 | while (count && len && blk) { |
| 4472 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4473 | uint32_t sz = htx_get_blksz(blk); |
| 4474 | struct ist v; |
| 4475 | uint32_t vlen; |
| 4476 | |
Christopher Faulet | e461e34 | 2018-12-18 16:43:35 +0100 | [diff] [blame] | 4477 | if (type == HTX_BLK_EOM) { |
| 4478 | len = 0; |
| 4479 | break; |
| 4480 | } |
| 4481 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4482 | vlen = sz; |
| 4483 | if (len > 0 && vlen > len) |
| 4484 | vlen = len; |
| 4485 | if (vlen > count) { |
| 4486 | if (type != HTX_BLK_DATA) |
| 4487 | break; |
| 4488 | vlen = count; |
| 4489 | } |
| 4490 | |
| 4491 | switch (type) { |
| 4492 | case HTX_BLK_UNUSED: |
| 4493 | break; |
| 4494 | |
| 4495 | case HTX_BLK_DATA: |
| 4496 | v = htx_get_blk_value(htx, blk); |
| 4497 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4498 | break; |
| 4499 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4500 | case HTX_BLK_TLR: |
| 4501 | case HTX_BLK_EOM: |
| 4502 | len = 0; |
| 4503 | break; |
| 4504 | |
| 4505 | default: |
| 4506 | break; |
| 4507 | } |
| 4508 | |
| 4509 | co_set_data(req, co_data(req) - vlen); |
| 4510 | count -= vlen; |
| 4511 | if (len > 0) |
| 4512 | len -= vlen; |
| 4513 | if (sz == vlen) |
| 4514 | blk = htx_remove_blk(htx, blk); |
| 4515 | else { |
| 4516 | htx_cut_data_blk(htx, blk, vlen); |
| 4517 | break; |
| 4518 | } |
| 4519 | } |
| 4520 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4521 | htx_to_buf(htx, &req->buf); |
| 4522 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4523 | /* If we are no other data available, yield waiting for new data. */ |
| 4524 | if (len) { |
| 4525 | if (len > 0) { |
| 4526 | lua_pushinteger(L, len); |
| 4527 | lua_replace(L, 2); |
| 4528 | } |
| 4529 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4530 | 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] | 4531 | } |
| 4532 | |
| 4533 | /* return the result. */ |
| 4534 | luaL_pushresult(&appctx->b); |
| 4535 | return 1; |
| 4536 | } |
| 4537 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4538 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4539 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 4540 | { |
| 4541 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4542 | int len = -1; |
| 4543 | |
| 4544 | /* Check arguments. */ |
| 4545 | if (lua_gettop(L) > 2) |
| 4546 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4547 | if (lua_gettop(L) >= 2) { |
| 4548 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4549 | lua_pop(L, 1); |
| 4550 | } |
| 4551 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4552 | lua_pushinteger(L, len); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4553 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4554 | /* Initialise the string catenation. */ |
| 4555 | luaL_buffinit(L, &appctx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4556 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4557 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4558 | } |
| 4559 | |
| 4560 | /* Append data in the output side of the buffer. This data is immediately |
| 4561 | * sent. The function returns the amount of data written. If the buffer |
| 4562 | * cannot contain the data, the function yields. The function returns -1 |
| 4563 | * if the channel is closed. |
| 4564 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4565 | __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] | 4566 | { |
| 4567 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4568 | struct stream_interface *si = appctx->appctx->owner; |
| 4569 | struct channel *res = si_ic(si); |
| 4570 | struct htx *htx = htx_from_buf(&res->buf); |
| 4571 | const char *data; |
| 4572 | size_t len; |
| 4573 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4574 | int max; |
| 4575 | |
Christopher Faulet | 9060fc0 | 2019-07-03 11:39:30 +0200 | [diff] [blame] | 4576 | max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx)); |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4577 | if (!max) |
| 4578 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4579 | |
| 4580 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4581 | |
| 4582 | /* Get the max amount of data which can write as input in the channel. */ |
| 4583 | if (max > (len - l)) |
| 4584 | max = len - l; |
| 4585 | |
| 4586 | /* Copy data. */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 4587 | max = htx_add_data(htx, ist2(data + l, max)); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4588 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4589 | |
| 4590 | /* update counters. */ |
| 4591 | l += max; |
| 4592 | lua_pop(L, 1); |
| 4593 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4594 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4595 | /* If some data is not send, declares the situation to the |
| 4596 | * applet, and returns a yield. |
| 4597 | */ |
| 4598 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4599 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4600 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4601 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4602 | 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] | 4603 | } |
| 4604 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4605 | htx_to_buf(htx, &res->buf); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4606 | return 1; |
| 4607 | } |
| 4608 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4609 | /* Just a wrapper of "hlua_applet_send_yield". This wrapper permits |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4610 | * yield the LUA process, and resume it without checking the |
| 4611 | * input arguments. |
| 4612 | */ |
| 4613 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 4614 | { |
| 4615 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4616 | |
| 4617 | /* We want to send some data. Headers must be sent. */ |
| 4618 | if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 4619 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 4620 | WILL_LJMP(lua_error(L)); |
| 4621 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4622 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4623 | /* This integer is used for followinf the amount of data sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4624 | lua_pushinteger(L, 0); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4625 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4626 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4627 | } |
| 4628 | |
| 4629 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 4630 | { |
| 4631 | const char *name; |
| 4632 | int ret; |
| 4633 | |
| 4634 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4635 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4636 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 4637 | |
| 4638 | /* Push in the stack the "response" entry. */ |
| 4639 | ret = lua_getfield(L, 1, "response"); |
| 4640 | if (ret != LUA_TTABLE) { |
| 4641 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 4642 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 4643 | WILL_LJMP(lua_error(L)); |
| 4644 | } |
| 4645 | |
| 4646 | /* check if the header is already registered if it is not |
| 4647 | * the case, register it. |
| 4648 | */ |
| 4649 | ret = lua_getfield(L, -1, name); |
| 4650 | if (ret == LUA_TNIL) { |
| 4651 | |
| 4652 | /* Entry not found. */ |
| 4653 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 4654 | |
| 4655 | /* Insert the new header name in the array in the top of the stack. |
| 4656 | * It left the new array in the top of the stack. |
| 4657 | */ |
| 4658 | lua_newtable(L); |
| 4659 | lua_pushvalue(L, 2); |
| 4660 | lua_pushvalue(L, -2); |
| 4661 | lua_settable(L, -4); |
| 4662 | |
| 4663 | } else if (ret != LUA_TTABLE) { |
| 4664 | |
| 4665 | /* corruption error. */ |
| 4666 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 4667 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 4668 | WILL_LJMP(lua_error(L)); |
| 4669 | } |
| 4670 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 4671 | /* Now the top of thestack is an array of values. We push |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4672 | * the header value as new entry. |
| 4673 | */ |
| 4674 | lua_pushvalue(L, 3); |
| 4675 | ret = lua_rawlen(L, -2); |
| 4676 | lua_rawseti(L, -2, ret + 1); |
| 4677 | lua_pushboolean(L, 1); |
| 4678 | return 1; |
| 4679 | } |
| 4680 | |
| 4681 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 4682 | { |
| 4683 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4684 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4685 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4686 | |
| 4687 | if (status < 100 || status > 599) { |
| 4688 | lua_pushboolean(L, 0); |
| 4689 | return 1; |
| 4690 | } |
| 4691 | |
| 4692 | appctx->appctx->ctx.hlua_apphttp.status = status; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4693 | appctx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4694 | lua_pushboolean(L, 1); |
| 4695 | return 1; |
| 4696 | } |
| 4697 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4698 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4699 | __LJMP static int hlua_applet_http_send_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4700 | { |
| 4701 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4702 | struct stream_interface *si = appctx->appctx->owner; |
| 4703 | struct channel *res = si_ic(si); |
| 4704 | struct htx *htx; |
| 4705 | struct htx_sl *sl; |
| 4706 | struct h1m h1m; |
| 4707 | const char *status, *reason; |
| 4708 | const char *name, *value; |
| 4709 | size_t nlen, vlen; |
| 4710 | unsigned int flags; |
| 4711 | |
| 4712 | /* Send the message at once. */ |
| 4713 | htx = htx_from_buf(&res->buf); |
| 4714 | h1m_init_res(&h1m); |
| 4715 | |
| 4716 | /* Use the same http version than the request. */ |
| 4717 | status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size); |
| 4718 | reason = appctx->appctx->ctx.hlua_apphttp.reason; |
| 4719 | if (reason == NULL) |
| 4720 | reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status); |
| 4721 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) { |
| 4722 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 4723 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 4724 | } |
| 4725 | else { |
| 4726 | flags = HTX_SL_F_IS_RESP; |
| 4727 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 4728 | } |
| 4729 | if (!sl) { |
| 4730 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 4731 | appctx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4732 | WILL_LJMP(lua_error(L)); |
| 4733 | } |
| 4734 | sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status; |
| 4735 | |
| 4736 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 4737 | lua_pushvalue(L, 0); |
| 4738 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 4739 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\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 | WILL_LJMP(lua_error(L)); |
| 4742 | } |
| 4743 | |
| 4744 | /* Browse the list of headers. */ |
| 4745 | lua_pushnil(L); |
| 4746 | while(lua_next(L, -2) != 0) { |
| 4747 | /* We expect a string as -2. */ |
| 4748 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 4749 | 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] | 4750 | appctx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4751 | lua_typename(L, lua_type(L, -2))); |
| 4752 | WILL_LJMP(lua_error(L)); |
| 4753 | } |
| 4754 | name = lua_tolstring(L, -2, &nlen); |
| 4755 | |
| 4756 | /* We expect an array as -1. */ |
| 4757 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 4758 | 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] | 4759 | appctx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4760 | name, |
| 4761 | lua_typename(L, lua_type(L, -1))); |
| 4762 | WILL_LJMP(lua_error(L)); |
| 4763 | } |
| 4764 | |
| 4765 | /* Browse the table who is on the top of the stack. */ |
| 4766 | lua_pushnil(L); |
| 4767 | while(lua_next(L, -2) != 0) { |
| 4768 | int id; |
| 4769 | |
| 4770 | /* We expect a number as -2. */ |
| 4771 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 4772 | 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] | 4773 | appctx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4774 | name, |
| 4775 | lua_typename(L, lua_type(L, -2))); |
| 4776 | WILL_LJMP(lua_error(L)); |
| 4777 | } |
| 4778 | id = lua_tointeger(L, -2); |
| 4779 | |
| 4780 | /* We expect a string as -2. */ |
| 4781 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 4782 | 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] | 4783 | appctx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4784 | name, id, |
| 4785 | lua_typename(L, lua_type(L, -1))); |
| 4786 | WILL_LJMP(lua_error(L)); |
| 4787 | } |
| 4788 | value = lua_tolstring(L, -1, &vlen); |
| 4789 | |
| 4790 | /* Simple Protocol checks. */ |
| 4791 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 4792 | h1_parse_xfer_enc_header(&h1m, ist2(value, vlen)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4793 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 4794 | struct ist v = ist2(value, vlen); |
| 4795 | int ret; |
| 4796 | |
| 4797 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 4798 | if (ret < 0) { |
| 4799 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 4800 | appctx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4801 | name); |
| 4802 | WILL_LJMP(lua_error(L)); |
| 4803 | } |
| 4804 | else if (ret == 0) |
| 4805 | goto next; /* Skip it */ |
| 4806 | } |
| 4807 | |
| 4808 | /* Add a new header */ |
| 4809 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 4810 | 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] | 4811 | appctx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4812 | name); |
| 4813 | WILL_LJMP(lua_error(L)); |
| 4814 | } |
| 4815 | next: |
| 4816 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4817 | lua_pop(L, 1); |
| 4818 | } |
| 4819 | |
| 4820 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4821 | lua_pop(L, 1); |
| 4822 | } |
| 4823 | |
| 4824 | if (h1m.flags & H1_MF_CHNK) |
| 4825 | h1m.flags &= ~H1_MF_CLEN; |
| 4826 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 4827 | h1m.flags |= H1_MF_XFER_LEN; |
| 4828 | |
| 4829 | /* Uset HTX start-line flags */ |
| 4830 | if (h1m.flags & H1_MF_XFER_ENC) |
| 4831 | flags |= HTX_SL_F_XFER_ENC; |
| 4832 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 4833 | flags |= HTX_SL_F_XFER_LEN; |
| 4834 | if (h1m.flags & H1_MF_CHNK) |
| 4835 | flags |= HTX_SL_F_CHNK; |
| 4836 | else if (h1m.flags & H1_MF_CLEN) |
| 4837 | flags |= HTX_SL_F_CLEN; |
| 4838 | if (h1m.body_len == 0) |
| 4839 | flags |= HTX_SL_F_BODYLESS; |
| 4840 | } |
| 4841 | sl->flags |= flags; |
| 4842 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 4843 | /* 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] | 4844 | * and the status code implies the presence of a message body, we must |
| 4845 | * announce a transfer encoding chunked. This is required by haproxy |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4846 | * for the keepalive compliance. If the applet announces a transfer-encoding |
| 4847 | * chunked itself, don't do anything. |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4848 | */ |
| 4849 | if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 && |
| 4850 | appctx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 4851 | appctx->appctx->ctx.hlua_apphttp.status != 204 && |
| 4852 | appctx->appctx->ctx.hlua_apphttp.status != 304) { |
| 4853 | /* Add a new header */ |
| 4854 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 4855 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 4856 | 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] | 4857 | appctx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4858 | WILL_LJMP(lua_error(L)); |
| 4859 | } |
| 4860 | } |
| 4861 | |
| 4862 | /* Finalize headers. */ |
| 4863 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 4864 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 4865 | appctx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4866 | WILL_LJMP(lua_error(L)); |
| 4867 | } |
| 4868 | |
| 4869 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 4870 | b_reset(&res->buf); |
| 4871 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 4872 | WILL_LJMP(lua_error(L)); |
| 4873 | } |
| 4874 | |
| 4875 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4876 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4877 | |
| 4878 | /* Headers sent, set the flag. */ |
| 4879 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
| 4880 | return 0; |
| 4881 | |
| 4882 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4883 | /* We will build the status line and the headers of the HTTP response. |
| 4884 | * We will try send at once if its not possible, we give back the hand |
| 4885 | * waiting for more room. |
| 4886 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4887 | __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] | 4888 | { |
| 4889 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4890 | struct stream_interface *si = appctx->appctx->owner; |
| 4891 | struct channel *res = si_ic(si); |
| 4892 | |
| 4893 | if (co_data(res)) { |
| 4894 | si_rx_room_blk(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4895 | 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] | 4896 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4897 | return MAY_LJMP(hlua_applet_http_send_response(L)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4898 | } |
| 4899 | |
| 4900 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4901 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
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 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4904 | } |
| 4905 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4906 | /* |
| 4907 | * |
| 4908 | * |
| 4909 | * Class HTTP |
| 4910 | * |
| 4911 | * |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4912 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4913 | |
| 4914 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4915 | * a class stream, otherwise it throws an error. |
| 4916 | */ |
| 4917 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
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 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
| 4920 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4921 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4922 | /* This function creates and push in the stack a HTTP object |
| 4923 | * according with a current TXN. |
| 4924 | */ |
| 4925 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 4926 | { |
| 4927 | struct hlua_txn *htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4928 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4929 | /* Check stack size. */ |
| 4930 | if (!lua_checkstack(L, 3)) |
| 4931 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4932 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4933 | /* Create the object: obj[0] = userdata. |
| 4934 | * Note that the base of the Converters object is the |
| 4935 | * same than the TXN object. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4936 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4937 | lua_newtable(L); |
| 4938 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 4939 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4940 | |
| 4941 | htxn->s = txn->s; |
| 4942 | htxn->p = txn->p; |
Christopher Faulet | 256b69a | 2019-05-23 11:14:21 +0200 | [diff] [blame] | 4943 | htxn->dir = txn->dir; |
| 4944 | htxn->flags = txn->flags; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4945 | |
| 4946 | /* Pop a class stream metatable and affect it to the table. */ |
| 4947 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 4948 | lua_setmetatable(L, -2); |
| 4949 | |
| 4950 | return 1; |
| 4951 | } |
| 4952 | |
| 4953 | /* This function creates ans returns an array of HTTP headers. |
| 4954 | * This function does not fails. It is used as wrapper with the |
| 4955 | * 2 following functions. |
| 4956 | */ |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4957 | __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] | 4958 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4959 | struct htx *htx; |
| 4960 | int32_t pos; |
| 4961 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4962 | /* Create the table. */ |
| 4963 | lua_newtable(L); |
| 4964 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4965 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4966 | htx = htxbuf(&msg->chn->buf); |
| 4967 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4968 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4969 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4970 | struct ist n, v; |
| 4971 | int len; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4972 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4973 | if (type == HTX_BLK_HDR) { |
| 4974 | n = htx_get_blk_name(htx,blk); |
| 4975 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4976 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4977 | else if (type == HTX_BLK_EOH) |
| 4978 | break; |
| 4979 | else |
| 4980 | continue; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4981 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4982 | /* Check for existing entry: |
| 4983 | * assume that the table is on the top of the stack, and |
| 4984 | * push the key in the stack, the function lua_gettable() |
| 4985 | * perform the lookup. |
| 4986 | */ |
| 4987 | lua_pushlstring(L, n.ptr, n.len); |
| 4988 | lua_gettable(L, -2); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4989 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4990 | switch (lua_type(L, -1)) { |
| 4991 | case LUA_TNIL: |
| 4992 | /* Table not found, create it. */ |
| 4993 | lua_pop(L, 1); /* remove the nil value. */ |
| 4994 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 4995 | lua_newtable(L); /* create and push empty table. */ |
| 4996 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 4997 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 4998 | 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] | 4999 | break; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5000 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5001 | case LUA_TTABLE: |
| 5002 | /* Entry found: push the value in the table. */ |
| 5003 | len = lua_rawlen(L, -1); |
| 5004 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5005 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 5006 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 5007 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5008 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5009 | default: |
| 5010 | /* Other cases are errors. */ |
| 5011 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 5012 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5013 | } |
| 5014 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5015 | return 1; |
| 5016 | } |
| 5017 | |
| 5018 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 5019 | { |
| 5020 | struct hlua_txn *htxn; |
| 5021 | |
| 5022 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 5023 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5024 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5025 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5026 | WILL_LJMP(lua_error(L)); |
| 5027 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5028 | return hlua_http_get_headers(L, &htxn->s->txn->req); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5029 | } |
| 5030 | |
| 5031 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 5032 | { |
| 5033 | struct hlua_txn *htxn; |
| 5034 | |
| 5035 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 5036 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5037 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5038 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5039 | WILL_LJMP(lua_error(L)); |
| 5040 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5041 | return hlua_http_get_headers(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5042 | } |
| 5043 | |
| 5044 | /* This function replace full header, or just a value in |
| 5045 | * the request or in the response. It is a wrapper fir the |
| 5046 | * 4 following functions. |
| 5047 | */ |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5048 | __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] | 5049 | { |
| 5050 | size_t name_len; |
| 5051 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5052 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5053 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5054 | struct htx *htx; |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5055 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5056 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5057 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5058 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 5059 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5060 | htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5061 | 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] | 5062 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5063 | return 0; |
| 5064 | } |
| 5065 | |
| 5066 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 5067 | { |
| 5068 | struct hlua_txn *htxn; |
| 5069 | |
| 5070 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5071 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5072 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5073 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5074 | WILL_LJMP(lua_error(L)); |
| 5075 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5076 | 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] | 5077 | } |
| 5078 | |
| 5079 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 5080 | { |
| 5081 | struct hlua_txn *htxn; |
| 5082 | |
| 5083 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 5084 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5085 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5086 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5087 | WILL_LJMP(lua_error(L)); |
| 5088 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5089 | 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] | 5090 | } |
| 5091 | |
| 5092 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 5093 | { |
| 5094 | struct hlua_txn *htxn; |
| 5095 | |
| 5096 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5097 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5098 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5099 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5100 | WILL_LJMP(lua_error(L)); |
| 5101 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5102 | 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] | 5103 | } |
| 5104 | |
| 5105 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 5106 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5107 | struct hlua_txn *htxn; |
| 5108 | |
| 5109 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 5110 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5111 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5112 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5113 | WILL_LJMP(lua_error(L)); |
| 5114 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5115 | 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] | 5116 | } |
| 5117 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5118 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5119 | * It is a wrapper for the 2 following functions. |
| 5120 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5121 | __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] | 5122 | { |
| 5123 | size_t len; |
| 5124 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5125 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5126 | struct http_hdr_ctx ctx; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5127 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5128 | ctx.blk = NULL; |
| 5129 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 5130 | http_remove_header(htx, &ctx); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5131 | return 0; |
| 5132 | } |
| 5133 | |
| 5134 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 5135 | { |
| 5136 | struct hlua_txn *htxn; |
| 5137 | |
| 5138 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 5139 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5140 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5141 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5142 | WILL_LJMP(lua_error(L)); |
| 5143 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5144 | return hlua_http_del_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5145 | } |
| 5146 | |
| 5147 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 5148 | { |
| 5149 | struct hlua_txn *htxn; |
| 5150 | |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5151 | MAY_LJMP(check_args(L, 2, "res_del_hdr")); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5152 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5153 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5154 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5155 | WILL_LJMP(lua_error(L)); |
| 5156 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5157 | return hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5158 | } |
| 5159 | |
| 5160 | /* This function adds an header. It is a wrapper used by |
| 5161 | * the 2 following functions. |
| 5162 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5163 | __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] | 5164 | { |
| 5165 | size_t name_len; |
| 5166 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5167 | size_t value_len; |
| 5168 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5169 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5170 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5171 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 5172 | ist2(value, value_len))); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5173 | return 0; |
| 5174 | } |
| 5175 | |
| 5176 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 5177 | { |
| 5178 | struct hlua_txn *htxn; |
| 5179 | |
| 5180 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 5181 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5182 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5183 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5184 | WILL_LJMP(lua_error(L)); |
| 5185 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5186 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5187 | } |
| 5188 | |
| 5189 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 5190 | { |
| 5191 | struct hlua_txn *htxn; |
| 5192 | |
| 5193 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 5194 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5195 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5196 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5197 | WILL_LJMP(lua_error(L)); |
| 5198 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5199 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5200 | } |
| 5201 | |
| 5202 | static int hlua_http_req_set_hdr(lua_State *L) |
| 5203 | { |
| 5204 | struct hlua_txn *htxn; |
| 5205 | |
| 5206 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 5207 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5208 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5209 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5210 | WILL_LJMP(lua_error(L)); |
| 5211 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5212 | hlua_http_del_hdr(L, &htxn->s->txn->req); |
| 5213 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5214 | } |
| 5215 | |
| 5216 | static int hlua_http_res_set_hdr(lua_State *L) |
| 5217 | { |
| 5218 | struct hlua_txn *htxn; |
| 5219 | |
| 5220 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 5221 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5222 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5223 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5224 | WILL_LJMP(lua_error(L)); |
| 5225 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5226 | hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
| 5227 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5228 | } |
| 5229 | |
| 5230 | /* This function set the method. */ |
| 5231 | static int hlua_http_req_set_meth(lua_State *L) |
| 5232 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5233 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5234 | size_t name_len; |
| 5235 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5236 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5237 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5238 | WILL_LJMP(lua_error(L)); |
| 5239 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5240 | lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5241 | return 1; |
| 5242 | } |
| 5243 | |
| 5244 | /* This function set the method. */ |
| 5245 | static int hlua_http_req_set_path(lua_State *L) |
| 5246 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5247 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5248 | size_t name_len; |
| 5249 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 5250 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5251 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5252 | WILL_LJMP(lua_error(L)); |
| 5253 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5254 | 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] | 5255 | return 1; |
| 5256 | } |
| 5257 | |
| 5258 | /* This function set the query-string. */ |
| 5259 | static int hlua_http_req_set_query(lua_State *L) |
| 5260 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5261 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5262 | size_t name_len; |
| 5263 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5264 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5265 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5266 | WILL_LJMP(lua_error(L)); |
| 5267 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5268 | /* Check length. */ |
| 5269 | if (name_len > trash.size - 1) { |
| 5270 | lua_pushboolean(L, 0); |
| 5271 | return 1; |
| 5272 | } |
| 5273 | |
| 5274 | /* Add the mark question as prefix. */ |
| 5275 | chunk_reset(&trash); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5276 | trash.area[trash.data++] = '?'; |
| 5277 | memcpy(trash.area + trash.data, name, name_len); |
| 5278 | trash.data += name_len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5279 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5280 | lua_pushboolean(L, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5281 | 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] | 5282 | return 1; |
| 5283 | } |
| 5284 | |
| 5285 | /* This function set the uri. */ |
| 5286 | static int hlua_http_req_set_uri(lua_State *L) |
| 5287 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5288 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5289 | size_t name_len; |
| 5290 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5291 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5292 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5293 | WILL_LJMP(lua_error(L)); |
| 5294 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5295 | 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] | 5296 | return 1; |
| 5297 | } |
| 5298 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5299 | /* This function set the response code & optionally reason. */ |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5300 | static int hlua_http_res_set_status(lua_State *L) |
| 5301 | { |
| 5302 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5303 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 5304 | const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 5305 | const struct ist reason = ist2(str, (str ? strlen(str) : 0)); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5306 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5307 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5308 | WILL_LJMP(lua_error(L)); |
| 5309 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5310 | http_res_set_status(code, reason, htxn->s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5311 | return 0; |
| 5312 | } |
| 5313 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5314 | /* |
| 5315 | * |
| 5316 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5317 | * Class TXN |
| 5318 | * |
| 5319 | * |
| 5320 | */ |
| 5321 | |
| 5322 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5323 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5324 | */ |
| 5325 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 5326 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5327 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5328 | } |
| 5329 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5330 | __LJMP static int hlua_set_var(lua_State *L) |
| 5331 | { |
| 5332 | struct hlua_txn *htxn; |
| 5333 | const char *name; |
| 5334 | size_t len; |
| 5335 | struct sample smp; |
| 5336 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 5337 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 5338 | 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] | 5339 | |
| 5340 | /* It is useles to retrieve the stream, but this function |
| 5341 | * runs only in a stream context. |
| 5342 | */ |
| 5343 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5344 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5345 | |
| 5346 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 5347 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5348 | hlua_lua2smp(L, 3, &smp); |
| 5349 | |
| 5350 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5351 | 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] | 5352 | |
| 5353 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 5354 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 5355 | else |
| 5356 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 5357 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 5358 | return 1; |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5359 | } |
| 5360 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5361 | __LJMP static int hlua_unset_var(lua_State *L) |
| 5362 | { |
| 5363 | struct hlua_txn *htxn; |
| 5364 | const char *name; |
| 5365 | size_t len; |
| 5366 | struct sample smp; |
| 5367 | |
| 5368 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 5369 | |
| 5370 | /* It is useles to retrieve the stream, but this function |
| 5371 | * runs only in a stream context. |
| 5372 | */ |
| 5373 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5374 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5375 | |
| 5376 | /* Unset the variable. */ |
| 5377 | 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] | 5378 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 5379 | return 1; |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5380 | } |
| 5381 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5382 | __LJMP static int hlua_get_var(lua_State *L) |
| 5383 | { |
| 5384 | struct hlua_txn *htxn; |
| 5385 | const char *name; |
| 5386 | size_t len; |
| 5387 | struct sample smp; |
| 5388 | |
| 5389 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 5390 | |
| 5391 | /* It is useles to retrieve the stream, but this function |
| 5392 | * runs only in a stream context. |
| 5393 | */ |
| 5394 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5395 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5396 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5397 | 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] | 5398 | if (!vars_get_by_name(name, len, &smp)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5399 | lua_pushnil(L); |
| 5400 | return 1; |
| 5401 | } |
| 5402 | |
| 5403 | return hlua_smp2lua(L, &smp); |
| 5404 | } |
| 5405 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5406 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5407 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5408 | struct hlua *hlua; |
| 5409 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5410 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 5411 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5412 | /* It is useles to retrieve the stream, but this function |
| 5413 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5414 | */ |
| 5415 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5416 | |
| 5417 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5418 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5419 | if (!hlua) |
| 5420 | return 0; |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5421 | |
| 5422 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5423 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5424 | |
| 5425 | /* Get and store new value. */ |
| 5426 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5427 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5428 | |
| 5429 | return 0; |
| 5430 | } |
| 5431 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5432 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5433 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5434 | struct hlua *hlua; |
| 5435 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5436 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 5437 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5438 | /* It is useles to retrieve the stream, but this function |
| 5439 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5440 | */ |
| 5441 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5442 | |
| 5443 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5444 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5445 | if (!hlua) { |
| 5446 | lua_pushnil(L); |
| 5447 | return 1; |
| 5448 | } |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5449 | |
| 5450 | /* Push configuration index in the stack. */ |
| 5451 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5452 | |
| 5453 | return 1; |
| 5454 | } |
| 5455 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5456 | /* Create stack entry containing a class TXN. This function |
| 5457 | * return 0 if the stack does not contains free slots, |
| 5458 | * otherwise it returns 1. |
| 5459 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5460 | 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] | 5461 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5462 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5463 | |
| 5464 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5465 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5466 | return 0; |
| 5467 | |
| 5468 | /* NOTE: The allocation never fails. The failure |
| 5469 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5470 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5471 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5472 | /* Create the object: obj[0] = userdata. */ |
| 5473 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5474 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5475 | lua_rawseti(L, -2, 0); |
| 5476 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5477 | htxn->s = s; |
| 5478 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 5479 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5480 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5481 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5482 | /* Create the "f" field that contains a list of fetches. */ |
| 5483 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5484 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5485 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5486 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5487 | |
| 5488 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 5489 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5490 | 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] | 5491 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5492 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5493 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5494 | /* Create the "c" field that contains a list of converters. */ |
| 5495 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5496 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5497 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5498 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5499 | |
| 5500 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 5501 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 5502 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5503 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5504 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5505 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5506 | /* Create the "req" field that contains the request channel object. */ |
| 5507 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5508 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5509 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5510 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5511 | |
| 5512 | /* Create the "res" field that contains the response channel object. */ |
| 5513 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5514 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5515 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5516 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5517 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5518 | /* Creates the HTTP object is the current proxy allows http. */ |
| 5519 | lua_pushstring(L, "http"); |
| 5520 | if (p->mode == PR_MODE_HTTP) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5521 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5522 | return 0; |
| 5523 | } |
| 5524 | else |
| 5525 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5526 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5527 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5528 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5529 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 5530 | lua_setmetatable(L, -2); |
| 5531 | |
| 5532 | return 1; |
| 5533 | } |
| 5534 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5535 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 5536 | { |
| 5537 | const char *msg; |
| 5538 | struct hlua_txn *htxn; |
| 5539 | |
| 5540 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 5541 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5542 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5543 | |
| 5544 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 5545 | return 0; |
| 5546 | } |
| 5547 | |
| 5548 | __LJMP static int hlua_txn_log(lua_State *L) |
| 5549 | { |
| 5550 | int level; |
| 5551 | const char *msg; |
| 5552 | struct hlua_txn *htxn; |
| 5553 | |
| 5554 | MAY_LJMP(check_args(L, 3, "log")); |
| 5555 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5556 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5557 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5558 | |
| 5559 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5560 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5561 | |
| 5562 | hlua_sendlog(htxn->s->be, level, msg); |
| 5563 | return 0; |
| 5564 | } |
| 5565 | |
| 5566 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 5567 | { |
| 5568 | const char *msg; |
| 5569 | struct hlua_txn *htxn; |
| 5570 | |
| 5571 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 5572 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5573 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5574 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 5575 | return 0; |
| 5576 | } |
| 5577 | |
| 5578 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 5579 | { |
| 5580 | const char *msg; |
| 5581 | struct hlua_txn *htxn; |
| 5582 | |
| 5583 | MAY_LJMP(check_args(L, 2, "Info")); |
| 5584 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5585 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5586 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 5587 | return 0; |
| 5588 | } |
| 5589 | |
| 5590 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 5591 | { |
| 5592 | const char *msg; |
| 5593 | struct hlua_txn *htxn; |
| 5594 | |
| 5595 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 5596 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5597 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5598 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 5599 | return 0; |
| 5600 | } |
| 5601 | |
| 5602 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 5603 | { |
| 5604 | const char *msg; |
| 5605 | struct hlua_txn *htxn; |
| 5606 | |
| 5607 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 5608 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5609 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5610 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 5611 | return 0; |
| 5612 | } |
| 5613 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5614 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 5615 | { |
| 5616 | struct hlua_txn *htxn; |
| 5617 | int ll; |
| 5618 | |
| 5619 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 5620 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5621 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5622 | |
| 5623 | if (ll < 0 || ll > 7) |
| 5624 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 5625 | |
| 5626 | htxn->s->logs.level = ll; |
| 5627 | return 0; |
| 5628 | } |
| 5629 | |
| 5630 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 5631 | { |
| 5632 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5633 | int tos; |
| 5634 | |
| 5635 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 5636 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5637 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5638 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 5639 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5640 | return 0; |
| 5641 | } |
| 5642 | |
| 5643 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 5644 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5645 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5646 | int mark; |
| 5647 | |
| 5648 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 5649 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5650 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5651 | |
Lukas Tribus | 579e3e3 | 2019-08-11 18:03:45 +0200 | [diff] [blame] | 5652 | conn_set_mark(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5653 | return 0; |
| 5654 | } |
| 5655 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 5656 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 5657 | { |
| 5658 | struct hlua_txn *htxn; |
| 5659 | |
| 5660 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 5661 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5662 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5663 | return 0; |
| 5664 | } |
| 5665 | |
| 5666 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 5667 | { |
| 5668 | struct hlua_txn *htxn; |
| 5669 | |
| 5670 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 5671 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5672 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5673 | return 0; |
| 5674 | } |
| 5675 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5676 | /* 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] | 5677 | * 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] | 5678 | * message and terminate the transaction. It returns 1 on success and 0 on |
| 5679 | * error. The Reply must be on top of the stack. |
| 5680 | */ |
| 5681 | __LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s) |
| 5682 | { |
| 5683 | struct htx *htx; |
| 5684 | struct htx_sl *sl; |
| 5685 | struct h1m h1m; |
| 5686 | const char *status, *reason, *body; |
| 5687 | size_t status_len, reason_len, body_len; |
| 5688 | int ret, code, flags; |
| 5689 | |
| 5690 | code = 200; |
| 5691 | status = "200"; |
| 5692 | status_len = 3; |
| 5693 | ret = lua_getfield(L, -1, "status"); |
| 5694 | if (ret == LUA_TNUMBER) { |
| 5695 | code = lua_tointeger(L, -1); |
| 5696 | status = lua_tolstring(L, -1, &status_len); |
| 5697 | } |
| 5698 | lua_pop(L, 1); |
| 5699 | |
| 5700 | reason = http_get_reason(code); |
| 5701 | reason_len = strlen(reason); |
| 5702 | ret = lua_getfield(L, -1, "reason"); |
| 5703 | if (ret == LUA_TSTRING) |
| 5704 | reason = lua_tolstring(L, -1, &reason_len); |
| 5705 | lua_pop(L, 1); |
| 5706 | |
| 5707 | body = NULL; |
| 5708 | body_len = 0; |
| 5709 | ret = lua_getfield(L, -1, "body"); |
| 5710 | if (ret == LUA_TSTRING) |
| 5711 | body = lua_tolstring(L, -1, &body_len); |
| 5712 | lua_pop(L, 1); |
| 5713 | |
| 5714 | /* Prepare the response before inserting the headers */ |
| 5715 | h1m_init_res(&h1m); |
| 5716 | htx = htx_from_buf(&s->res.buf); |
| 5717 | channel_htx_truncate(&s->res, htx); |
| 5718 | if (s->txn->req.flags & HTTP_MSGF_VER_11) { |
| 5719 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 5720 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), |
| 5721 | ist2(status, status_len), ist2(reason, reason_len)); |
| 5722 | } |
| 5723 | else { |
| 5724 | flags = HTX_SL_F_IS_RESP; |
| 5725 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), |
| 5726 | ist2(status, status_len), ist2(reason, reason_len)); |
| 5727 | } |
| 5728 | if (!sl) |
| 5729 | goto fail; |
| 5730 | sl->info.res.status = code; |
| 5731 | |
| 5732 | /* Push in the stack the "headers" entry. */ |
| 5733 | ret = lua_getfield(L, -1, "headers"); |
| 5734 | if (ret != LUA_TTABLE) |
| 5735 | goto skip_headers; |
| 5736 | |
| 5737 | lua_pushnil(L); |
| 5738 | while (lua_next(L, -2) != 0) { |
| 5739 | struct ist name, value; |
| 5740 | const char *n, *v; |
| 5741 | size_t nlen, vlen; |
| 5742 | |
| 5743 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 5744 | /* Skip element if the key is not a string or if the value is not a table */ |
| 5745 | goto next_hdr; |
| 5746 | } |
| 5747 | |
| 5748 | n = lua_tolstring(L, -2, &nlen); |
| 5749 | name = ist2(n, nlen); |
| 5750 | if (isteqi(name, ist("content-length"))) { |
| 5751 | /* Always skip content-length header. It will be added |
| 5752 | * later with the correct len |
| 5753 | */ |
| 5754 | goto next_hdr; |
| 5755 | } |
| 5756 | |
| 5757 | /* Loop on header's values */ |
| 5758 | lua_pushnil(L); |
| 5759 | while (lua_next(L, -2)) { |
| 5760 | if (!lua_isstring(L, -1)) { |
| 5761 | /* Skip the value if it is not a string */ |
| 5762 | goto next_value; |
| 5763 | } |
| 5764 | |
| 5765 | v = lua_tolstring(L, -1, &vlen); |
| 5766 | value = ist2(v, vlen); |
| 5767 | |
| 5768 | if (isteqi(name, ist("transfer-encoding"))) |
| 5769 | h1_parse_xfer_enc_header(&h1m, value); |
| 5770 | if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen))) |
| 5771 | goto fail; |
| 5772 | |
| 5773 | next_value: |
| 5774 | lua_pop(L, 1); |
| 5775 | } |
| 5776 | |
| 5777 | next_hdr: |
| 5778 | lua_pop(L, 1); |
| 5779 | } |
| 5780 | skip_headers: |
| 5781 | lua_pop(L, 1); |
| 5782 | |
| 5783 | /* Update h1m flags: CLEN is set if CHNK is not present */ |
| 5784 | if (!(h1m.flags & H1_MF_CHNK)) { |
| 5785 | const char *clen = ultoa(body_len); |
| 5786 | |
| 5787 | h1m.flags |= H1_MF_CLEN; |
| 5788 | if (!htx_add_header(htx, ist("content-length"), ist(clen))) |
| 5789 | goto fail; |
| 5790 | } |
| 5791 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 5792 | h1m.flags |= H1_MF_XFER_LEN; |
| 5793 | |
| 5794 | /* Update HTX start-line flags */ |
| 5795 | if (h1m.flags & H1_MF_XFER_ENC) |
| 5796 | flags |= HTX_SL_F_XFER_ENC; |
| 5797 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 5798 | flags |= HTX_SL_F_XFER_LEN; |
| 5799 | if (h1m.flags & H1_MF_CHNK) |
| 5800 | flags |= HTX_SL_F_CHNK; |
| 5801 | else if (h1m.flags & H1_MF_CLEN) |
| 5802 | flags |= HTX_SL_F_CLEN; |
| 5803 | if (h1m.body_len == 0) |
| 5804 | flags |= HTX_SL_F_BODYLESS; |
| 5805 | } |
| 5806 | sl->flags |= flags; |
| 5807 | |
| 5808 | |
| 5809 | if (!htx_add_endof(htx, HTX_BLK_EOH) || |
| 5810 | (body_len && !htx_add_data_atonce(htx, ist2(body, body_len))) || |
| 5811 | !htx_add_endof(htx, HTX_BLK_EOM)) |
| 5812 | goto fail; |
| 5813 | |
| 5814 | /* Now, forward the response and terminate the transaction */ |
| 5815 | s->txn->status = code; |
| 5816 | htx_to_buf(htx, &s->res.buf); |
| 5817 | if (!http_forward_proxy_resp(s, 1)) |
| 5818 | goto fail; |
| 5819 | |
| 5820 | return 1; |
| 5821 | |
| 5822 | fail: |
| 5823 | channel_htx_truncate(&s->res, htx); |
| 5824 | return 0; |
| 5825 | } |
| 5826 | |
| 5827 | /* Terminate a transaction if called from a lua action. For TCP streams, |
| 5828 | * processing is just aborted. Nothing is returned to the client and all |
| 5829 | * arguments are ignored. For HTTP streams, if a reply is passed as argument, it |
| 5830 | * is forwarded to the client before terminating the transaction. On success, |
| 5831 | * the function exits with ACT_RET_DONE code. If an error occurred, it exits |
| 5832 | * with ACT_RET_ERR code. If this function is not called from a lua action, it |
| 5833 | * just exits without any processing. |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5834 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5835 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5836 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5837 | struct hlua_txn *htxn; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5838 | struct stream *s; |
| 5839 | int finst; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5840 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5841 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5842 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5843 | /* If the flags NOTERM is set, we cannot terminate the session, so we |
| 5844 | * just end the execution of the current lua code. */ |
| 5845 | if (htxn->flags & HLUA_TXN_NOTERM) |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5846 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5847 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5848 | s = htxn->s; |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5849 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5850 | struct channel *req = &s->req; |
| 5851 | struct channel *res = &s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5852 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5853 | channel_auto_read(req); |
| 5854 | channel_abort(req); |
| 5855 | channel_auto_close(req); |
| 5856 | channel_erase(req); |
| 5857 | |
| 5858 | res->wex = tick_add_ifset(now_ms, res->wto); |
| 5859 | channel_auto_read(res); |
| 5860 | channel_auto_close(res); |
| 5861 | channel_shutr_now(res); |
| 5862 | |
| 5863 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D); |
| 5864 | goto done; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5865 | } |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 5866 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5867 | if (lua_gettop(L) == 1 || !lua_istable(L, 2)) { |
| 5868 | /* No reply or invalid reply */ |
| 5869 | s->txn->status = 0; |
| 5870 | http_reply_and_close(s, 0, NULL); |
| 5871 | } |
| 5872 | else { |
| 5873 | /* Remove extra args to have the reply on top of the stack */ |
| 5874 | if (lua_gettop(L) > 2) |
| 5875 | lua_pop(L, lua_gettop(L) - 2); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5876 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5877 | if (!hlua_txn_forward_reply(L, s)) { |
| 5878 | if (!(s->flags & SF_ERR_MASK)) |
| 5879 | s->flags |= SF_ERR_PRXCOND; |
| 5880 | lua_pushinteger(L, ACT_RET_ERR); |
| 5881 | WILL_LJMP(hlua_done(L)); |
| 5882 | return 0; /* Never reached */ |
| 5883 | } |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 5884 | } |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5885 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5886 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H); |
| 5887 | if (htxn->dir == SMP_OPT_DIR_REQ) { |
| 5888 | /* let's log the request time */ |
| 5889 | s->logs.tv_request = now; |
| 5890 | if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */ |
| 5891 | _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1); |
| 5892 | } |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5893 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5894 | done: |
| 5895 | if (!(s->flags & SF_ERR_MASK)) |
| 5896 | s->flags |= SF_ERR_LOCAL; |
| 5897 | if (!(s->flags & SF_FINST_MASK)) |
| 5898 | s->flags |= finst; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5899 | |
Christopher Faulet | 4ad7310 | 2020-03-05 11:07:31 +0100 | [diff] [blame] | 5900 | lua_pushinteger(L, ACT_RET_ABRT); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5901 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5902 | return 0; |
| 5903 | } |
| 5904 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5905 | /* |
| 5906 | * |
| 5907 | * |
| 5908 | * Class REPLY |
| 5909 | * |
| 5910 | * |
| 5911 | */ |
| 5912 | |
| 5913 | /* Pushes the TXN reply onto the top of the stack. If the stask does not have a |
| 5914 | * free slots, the function fails and returns 0; |
| 5915 | */ |
| 5916 | static int hlua_txn_reply_new(lua_State *L) |
| 5917 | { |
| 5918 | struct hlua_txn *htxn; |
| 5919 | const char *reason, *body = NULL; |
| 5920 | int ret, status; |
| 5921 | |
| 5922 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5923 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5924 | hlua_pusherror(L, "txn object is not an HTTP transaction."); |
| 5925 | WILL_LJMP(lua_error(L)); |
| 5926 | } |
| 5927 | |
| 5928 | /* Default value */ |
| 5929 | status = 200; |
| 5930 | reason = http_get_reason(status); |
| 5931 | |
| 5932 | if (lua_istable(L, 2)) { |
| 5933 | /* load status and reason from the table argument at index 2 */ |
| 5934 | ret = lua_getfield(L, 2, "status"); |
| 5935 | if (ret == LUA_TNIL) |
| 5936 | goto reason; |
| 5937 | else if (ret != LUA_TNUMBER) { |
| 5938 | /* invalid status: ignore the reason */ |
| 5939 | goto body; |
| 5940 | } |
| 5941 | status = lua_tointeger(L, -1); |
| 5942 | |
| 5943 | reason: |
| 5944 | lua_pop(L, 1); /* restore the stack: remove status */ |
| 5945 | ret = lua_getfield(L, 2, "reason"); |
| 5946 | if (ret == LUA_TSTRING) |
| 5947 | reason = lua_tostring(L, -1); |
| 5948 | |
| 5949 | body: |
| 5950 | lua_pop(L, 1); /* restore the stack: remove invalid status or reason */ |
| 5951 | ret = lua_getfield(L, 2, "body"); |
| 5952 | if (ret == LUA_TSTRING) |
| 5953 | body = lua_tostring(L, -1); |
| 5954 | lua_pop(L, 1); /* restore the stack: remove body */ |
| 5955 | } |
| 5956 | |
| 5957 | /* Create the Reply table */ |
| 5958 | lua_newtable(L); |
| 5959 | |
| 5960 | /* Add status element */ |
| 5961 | lua_pushstring(L, "status"); |
| 5962 | lua_pushinteger(L, status); |
| 5963 | lua_settable(L, -3); |
| 5964 | |
| 5965 | /* Add reason element */ |
| 5966 | reason = http_get_reason(status); |
| 5967 | lua_pushstring(L, "reason"); |
| 5968 | lua_pushstring(L, reason); |
| 5969 | lua_settable(L, -3); |
| 5970 | |
| 5971 | /* Add body element, nil if undefined */ |
| 5972 | lua_pushstring(L, "body"); |
| 5973 | if (body) |
| 5974 | lua_pushstring(L, body); |
| 5975 | else |
| 5976 | lua_pushnil(L); |
| 5977 | lua_settable(L, -3); |
| 5978 | |
| 5979 | /* Add headers element */ |
| 5980 | lua_pushstring(L, "headers"); |
| 5981 | lua_newtable(L); |
| 5982 | |
| 5983 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 5984 | if (lua_istable(L, 2)) { |
| 5985 | /* load headers from the table argument at index 2. If it is a table, copy it. */ |
| 5986 | ret = lua_getfield(L, 2, "headers"); |
| 5987 | if (ret == LUA_TTABLE) { |
| 5988 | /* stack: [ ... <headers:table>, <table> ] */ |
| 5989 | lua_pushnil(L); |
| 5990 | while (lua_next(L, -2) != 0) { |
| 5991 | /* stack: [ ... <headers:table>, <table>, k, v] */ |
| 5992 | if (!lua_isstring(L, -1) && !lua_istable(L, -1)) { |
| 5993 | /* invalid value type, skip it */ |
| 5994 | lua_pop(L, 1); |
| 5995 | continue; |
| 5996 | } |
| 5997 | |
| 5998 | |
| 5999 | /* Duplicate the key and swap it with the value. */ |
| 6000 | lua_pushvalue(L, -2); |
| 6001 | lua_insert(L, -2); |
| 6002 | /* stack: [ ... <headers:table>, <table>, k, k, v ] */ |
| 6003 | |
| 6004 | lua_newtable(L); |
| 6005 | lua_insert(L, -2); |
| 6006 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */ |
| 6007 | |
| 6008 | if (lua_isstring(L, -1)) { |
| 6009 | /* push the value in the inner table */ |
| 6010 | lua_rawseti(L, -2, 1); |
| 6011 | } |
| 6012 | else { /* table */ |
| 6013 | lua_pushnil(L); |
| 6014 | while (lua_next(L, -2) != 0) { |
| 6015 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */ |
| 6016 | if (!lua_isstring(L, -1)) { |
| 6017 | /* invalid value type, skip it*/ |
| 6018 | lua_pop(L, 1); |
| 6019 | continue; |
| 6020 | } |
| 6021 | /* push the value in the inner table */ |
| 6022 | lua_rawseti(L, -4, lua_rawlen(L, -4) + 1); |
| 6023 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */ |
| 6024 | } |
| 6025 | lua_pop(L, 1); |
| 6026 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */ |
| 6027 | } |
| 6028 | |
| 6029 | /* push (k,v) on the stack in the headers table: |
| 6030 | * stack: [ ... <headers:table>, <table>, k, k, v ] |
| 6031 | */ |
| 6032 | lua_settable(L, -5); |
| 6033 | /* stack: [ ... <headers:table>, <table>, k ] */ |
| 6034 | } |
| 6035 | } |
| 6036 | lua_pop(L, 1); |
| 6037 | } |
| 6038 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 6039 | lua_settable(L, -3); |
| 6040 | /* stack: [ txn, <Arg:table>, <Reply:table> ] */ |
| 6041 | |
| 6042 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 6043 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref); |
| 6044 | lua_setmetatable(L, -2); |
| 6045 | return 1; |
| 6046 | } |
| 6047 | |
| 6048 | /* Set the reply status code, and optionally the reason. If no reason is |
| 6049 | * provided, the default one corresponding to the status code is used. |
| 6050 | */ |
| 6051 | __LJMP static int hlua_txn_reply_set_status(lua_State *L) |
| 6052 | { |
| 6053 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6054 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 6055 | |
| 6056 | /* First argument (self) must be a table */ |
| 6057 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6058 | |
| 6059 | if (status < 100 || status > 599) { |
| 6060 | lua_pushboolean(L, 0); |
| 6061 | return 1; |
| 6062 | } |
| 6063 | if (!reason) |
| 6064 | reason = http_get_reason(status); |
| 6065 | |
| 6066 | lua_pushinteger(L, status); |
| 6067 | lua_setfield(L, 1, "status"); |
| 6068 | |
| 6069 | lua_pushstring(L, reason); |
| 6070 | lua_setfield(L, 1, "reason"); |
| 6071 | |
| 6072 | lua_pushboolean(L, 1); |
| 6073 | return 1; |
| 6074 | } |
| 6075 | |
| 6076 | /* Add a header into the reply object. Each header name is associated to an |
| 6077 | * array of values in the "headers" table. If the header name is not found, a |
| 6078 | * new entry is created. |
| 6079 | */ |
| 6080 | __LJMP static int hlua_txn_reply_add_header(lua_State *L) |
| 6081 | { |
| 6082 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6083 | const char *value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 6084 | int ret; |
| 6085 | |
| 6086 | /* First argument (self) must be a table */ |
| 6087 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6088 | |
| 6089 | /* Push in the stack the "headers" entry. */ |
| 6090 | ret = lua_getfield(L, 1, "headers"); |
| 6091 | if (ret != LUA_TTABLE) { |
| 6092 | hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret)); |
| 6093 | WILL_LJMP(lua_error(L)); |
| 6094 | } |
| 6095 | |
| 6096 | /* check if the header is already registered. If not, register it. */ |
| 6097 | ret = lua_getfield(L, -1, name); |
| 6098 | if (ret == LUA_TNIL) { |
| 6099 | /* Entry not found. */ |
| 6100 | lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */ |
| 6101 | |
| 6102 | /* Insert the new header name in the array in the top of the stack. |
| 6103 | * It left the new array in the top of the stack. |
| 6104 | */ |
| 6105 | lua_newtable(L); |
| 6106 | lua_pushstring(L, name); |
| 6107 | lua_pushvalue(L, -2); |
| 6108 | lua_settable(L, -4); |
| 6109 | } |
| 6110 | else if (ret != LUA_TTABLE) { |
| 6111 | hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret)); |
| 6112 | WILL_LJMP(lua_error(L)); |
| 6113 | } |
| 6114 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 6115 | /* Now the top of thestack is an array of values. We push |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6116 | * the header value as new entry. |
| 6117 | */ |
| 6118 | lua_pushstring(L, value); |
| 6119 | ret = lua_rawlen(L, -2); |
| 6120 | lua_rawseti(L, -2, ret + 1); |
| 6121 | |
| 6122 | lua_pushboolean(L, 1); |
| 6123 | return 1; |
| 6124 | } |
| 6125 | |
| 6126 | /* Remove all occurrences of a given header name. */ |
| 6127 | __LJMP static int hlua_txn_reply_del_header(lua_State *L) |
| 6128 | { |
| 6129 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6130 | int ret; |
| 6131 | |
| 6132 | /* First argument (self) must be a table */ |
| 6133 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6134 | |
| 6135 | /* Push in the stack the "headers" entry. */ |
| 6136 | ret = lua_getfield(L, 1, "headers"); |
| 6137 | if (ret != LUA_TTABLE) { |
| 6138 | hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret)); |
| 6139 | WILL_LJMP(lua_error(L)); |
| 6140 | } |
| 6141 | |
| 6142 | lua_pushstring(L, name); |
| 6143 | lua_pushnil(L); |
| 6144 | lua_settable(L, -3); |
| 6145 | |
| 6146 | lua_pushboolean(L, 1); |
| 6147 | return 1; |
| 6148 | } |
| 6149 | |
| 6150 | /* Set the reply's body. Overwrite any existing entry. */ |
| 6151 | __LJMP static int hlua_txn_reply_set_body(lua_State *L) |
| 6152 | { |
| 6153 | const char *payload = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6154 | |
| 6155 | /* First argument (self) must be a table */ |
| 6156 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6157 | |
| 6158 | lua_pushstring(L, payload); |
| 6159 | lua_setfield(L, 1, "body"); |
| 6160 | |
| 6161 | lua_pushboolean(L, 1); |
| 6162 | return 1; |
| 6163 | } |
| 6164 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 6165 | __LJMP static int hlua_log(lua_State *L) |
| 6166 | { |
| 6167 | int level; |
| 6168 | const char *msg; |
| 6169 | |
| 6170 | MAY_LJMP(check_args(L, 2, "log")); |
| 6171 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 6172 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6173 | |
| 6174 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 6175 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 6176 | |
| 6177 | hlua_sendlog(NULL, level, msg); |
| 6178 | return 0; |
| 6179 | } |
| 6180 | |
| 6181 | __LJMP static int hlua_log_debug(lua_State *L) |
| 6182 | { |
| 6183 | const char *msg; |
| 6184 | |
| 6185 | MAY_LJMP(check_args(L, 1, "debug")); |
| 6186 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6187 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 6188 | return 0; |
| 6189 | } |
| 6190 | |
| 6191 | __LJMP static int hlua_log_info(lua_State *L) |
| 6192 | { |
| 6193 | const char *msg; |
| 6194 | |
| 6195 | MAY_LJMP(check_args(L, 1, "info")); |
| 6196 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6197 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 6198 | return 0; |
| 6199 | } |
| 6200 | |
| 6201 | __LJMP static int hlua_log_warning(lua_State *L) |
| 6202 | { |
| 6203 | const char *msg; |
| 6204 | |
| 6205 | MAY_LJMP(check_args(L, 1, "warning")); |
| 6206 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6207 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 6208 | return 0; |
| 6209 | } |
| 6210 | |
| 6211 | __LJMP static int hlua_log_alert(lua_State *L) |
| 6212 | { |
| 6213 | const char *msg; |
| 6214 | |
| 6215 | MAY_LJMP(check_args(L, 1, "alert")); |
| 6216 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6217 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6218 | return 0; |
| 6219 | } |
| 6220 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6221 | __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] | 6222 | { |
| 6223 | int wakeup_ms = lua_tointeger(L, -1); |
| 6224 | if (now_ms < wakeup_ms) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6225 | 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] | 6226 | return 0; |
| 6227 | } |
| 6228 | |
| 6229 | __LJMP static int hlua_sleep(lua_State *L) |
| 6230 | { |
| 6231 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6232 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6233 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6234 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6235 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6236 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6237 | wakeup_ms = tick_add(now_ms, delay); |
| 6238 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6239 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6240 | 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] | 6241 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6242 | } |
| 6243 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6244 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6245 | { |
| 6246 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6247 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6248 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6249 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6250 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6251 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6252 | wakeup_ms = tick_add(now_ms, delay); |
| 6253 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6254 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6255 | 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] | 6256 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6257 | } |
| 6258 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6259 | /* This functionis an LUA binding. it permits to give back |
| 6260 | * the hand at the HAProxy scheduler. It is used when the |
| 6261 | * LUA processing consumes a lot of time. |
| 6262 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6263 | __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] | 6264 | { |
| 6265 | return 0; |
| 6266 | } |
| 6267 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6268 | __LJMP static int hlua_yield(lua_State *L) |
| 6269 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6270 | 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] | 6271 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6272 | } |
| 6273 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6274 | /* This function change the nice of the currently executed |
| 6275 | * task. It is used set low or high priority at the current |
| 6276 | * task. |
| 6277 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 6278 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6279 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6280 | struct hlua *hlua; |
| 6281 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6282 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6283 | MAY_LJMP(check_args(L, 1, "set_nice")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6284 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6285 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 6286 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 6287 | hlua = hlua_gethlua(L); |
| 6288 | |
| 6289 | /* If the task is not set, I'm in a start mode. */ |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6290 | if (!hlua || !hlua->task) |
| 6291 | return 0; |
| 6292 | |
| 6293 | if (nice < -1024) |
| 6294 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6295 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6296 | nice = 1024; |
| 6297 | |
| 6298 | hlua->task->nice = nice; |
| 6299 | return 0; |
| 6300 | } |
| 6301 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6302 | /* 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] | 6303 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 6304 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 6305 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6306 | * |
| 6307 | * Task wrapper are longjmp safe because the only one Lua code |
| 6308 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6309 | */ |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 6310 | 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] | 6311 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6312 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6313 | enum hlua_exec status; |
| 6314 | |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 6315 | if (task->thread_mask == MAX_THREADS_MASK) |
| 6316 | task_set_affinity(task, tid_bit); |
| 6317 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6318 | /* If it is the first call to the task, we must initialize the |
| 6319 | * execution timeouts. |
| 6320 | */ |
| 6321 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6322 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6323 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6324 | /* Execute the Lua code. */ |
| 6325 | status = hlua_ctx_resume(hlua, 1); |
| 6326 | |
| 6327 | switch (status) { |
| 6328 | /* finished or yield */ |
| 6329 | case HLUA_E_OK: |
| 6330 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6331 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 6332 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6333 | break; |
| 6334 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6335 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 6336 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 6337 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6338 | break; |
| 6339 | |
| 6340 | /* finished with error. */ |
| 6341 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6342 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6343 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6344 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6345 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6346 | break; |
| 6347 | |
| 6348 | case HLUA_E_ERR: |
| 6349 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6350 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6351 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6352 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6353 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6354 | break; |
| 6355 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6356 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6357 | } |
| 6358 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6359 | /* This function is an LUA binding that register LUA function to be |
| 6360 | * executed after the HAProxy configuration parsing and before the |
| 6361 | * HAProxy scheduler starts. This function expect only one LUA |
| 6362 | * argument that is a function. This function returns nothing, but |
| 6363 | * throws if an error is encountered. |
| 6364 | */ |
| 6365 | __LJMP static int hlua_register_init(lua_State *L) |
| 6366 | { |
| 6367 | struct hlua_init_function *init; |
| 6368 | int ref; |
| 6369 | |
| 6370 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 6371 | |
| 6372 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6373 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6374 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6375 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6376 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6377 | |
| 6378 | init->function_ref = ref; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6379 | LIST_ADDQ(&hlua_init_functions[hlua_state_id], &init->l); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6380 | return 0; |
| 6381 | } |
| 6382 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6383 | /* This functio is an LUA binding. It permits to register a task |
| 6384 | * executed in parallel of the main HAroxy activity. The task is |
| 6385 | * created and it is set in the HAProxy scheduler. It can be called |
| 6386 | * from the "init" section, "post init" or during the runtime. |
| 6387 | * |
| 6388 | * Lua prototype: |
| 6389 | * |
| 6390 | * <none> core.register_task(<function>) |
| 6391 | */ |
| 6392 | static int hlua_register_task(lua_State *L) |
| 6393 | { |
| 6394 | struct hlua *hlua; |
| 6395 | struct task *task; |
| 6396 | int ref; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 6397 | int state_id; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6398 | |
| 6399 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 6400 | |
| 6401 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6402 | |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 6403 | /* Get the reference state. If the reference is NULL, L is the master |
| 6404 | * state, otherwise hlua->T is. |
| 6405 | */ |
| 6406 | hlua = hlua_gethlua(L); |
| 6407 | if (hlua) |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 6408 | /* we are in runtime processing */ |
| 6409 | state_id = hlua->state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 6410 | else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 6411 | /* we are in initialization mode */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6412 | state_id = hlua_state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 6413 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6414 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6415 | if (!hlua) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6416 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6417 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 6418 | /* We are in the common lua state, execute the task anywhere, |
| 6419 | * otherwise, inherit the current thread identifier |
| 6420 | */ |
| 6421 | if (state_id == 0) |
| 6422 | task = task_new(MAX_THREADS_MASK); |
| 6423 | else |
| 6424 | task = task_new(tid_bit); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 6425 | if (!task) |
| 6426 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 6427 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6428 | task->context = hlua; |
| 6429 | task->process = hlua_process_task; |
| 6430 | |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 6431 | if (!hlua_ctx_init(hlua, state_id, task, 1)) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6432 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6433 | |
| 6434 | /* Restore the function in the stack. */ |
| 6435 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 6436 | hlua->nargs = 0; |
| 6437 | |
| 6438 | /* Schedule task. */ |
| 6439 | task_schedule(task, now_ms); |
| 6440 | |
| 6441 | return 0; |
| 6442 | } |
| 6443 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6444 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 6445 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 6446 | * resume converters. |
| 6447 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6448 | 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] | 6449 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6450 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6451 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6452 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6453 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6454 | if (!stream) |
| 6455 | return 0; |
| 6456 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6457 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6458 | * Lua context can be not initialized. This behavior |
| 6459 | * permits to save performances because a systematic |
| 6460 | * Lua initialization cause 5% performances loss. |
| 6461 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6462 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6463 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6464 | if (!stream->hlua) { |
| 6465 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6466 | return 0; |
| 6467 | } |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6468 | 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] | 6469 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6470 | return 0; |
| 6471 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6472 | } |
| 6473 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6474 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6475 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6476 | |
| 6477 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6478 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6479 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6480 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6481 | else |
| 6482 | error = "critical error"; |
| 6483 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6484 | return 0; |
| 6485 | } |
| 6486 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6487 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6488 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6489 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6490 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6491 | return 0; |
| 6492 | } |
| 6493 | |
| 6494 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6495 | 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] | 6496 | |
| 6497 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6498 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6499 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6500 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6501 | return 0; |
| 6502 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6503 | hlua_smp2lua(stream->hlua->T, smp); |
| 6504 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6505 | |
| 6506 | /* push keywords in the stack. */ |
| 6507 | if (arg_p) { |
| 6508 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6509 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6510 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6511 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6512 | return 0; |
| 6513 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6514 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6515 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6516 | } |
| 6517 | } |
| 6518 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6519 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6520 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6521 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6522 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6523 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6524 | } |
| 6525 | |
| 6526 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6527 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6528 | /* finished. */ |
| 6529 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6530 | /* If the stack is empty, the function fails. */ |
| 6531 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6532 | return 0; |
| 6533 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6534 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6535 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6536 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6537 | return 1; |
| 6538 | |
| 6539 | /* yield. */ |
| 6540 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6541 | 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] | 6542 | return 0; |
| 6543 | |
| 6544 | /* finished with error. */ |
| 6545 | case HLUA_E_ERRMSG: |
| 6546 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6547 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6548 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6549 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6550 | return 0; |
| 6551 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6552 | case HLUA_E_ETMOUT: |
| 6553 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 6554 | return 0; |
| 6555 | |
| 6556 | case HLUA_E_NOMEM: |
| 6557 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 6558 | return 0; |
| 6559 | |
| 6560 | case HLUA_E_YIELD: |
| 6561 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 6562 | return 0; |
| 6563 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6564 | case HLUA_E_ERR: |
| 6565 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6566 | 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] | 6567 | /* fall through */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6568 | |
| 6569 | default: |
| 6570 | return 0; |
| 6571 | } |
| 6572 | } |
| 6573 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6574 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 6575 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6576 | * resume sample-fetches. This function will be called by the sample |
| 6577 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6578 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6579 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 6580 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6581 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6582 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6583 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6584 | const char *error; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6585 | unsigned int hflags = HLUA_TXN_NOTERM; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6586 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6587 | if (!stream) |
| 6588 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 6589 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6590 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6591 | * Lua context can be not initialized. This behavior |
| 6592 | * permits to save performances because a systematic |
| 6593 | * Lua initialization cause 5% performances loss. |
| 6594 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6595 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6596 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6597 | if (!stream->hlua) { |
| 6598 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6599 | return 0; |
| 6600 | } |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6601 | 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] | 6602 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6603 | return 0; |
| 6604 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6605 | } |
| 6606 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6607 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6608 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6609 | |
| 6610 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6611 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6612 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6613 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6614 | else |
| 6615 | error = "critical error"; |
| 6616 | 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] | 6617 | return 0; |
| 6618 | } |
| 6619 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6620 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6621 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6622 | 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] | 6623 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6624 | return 0; |
| 6625 | } |
| 6626 | |
| 6627 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6628 | 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] | 6629 | |
| 6630 | /* push arguments in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6631 | 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] | 6632 | 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] | 6633 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6634 | return 0; |
| 6635 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6636 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6637 | |
| 6638 | /* push keywords in the stack. */ |
| 6639 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 6640 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6641 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6642 | 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] | 6643 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6644 | return 0; |
| 6645 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6646 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6647 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6648 | } |
| 6649 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6650 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6651 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6652 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6653 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6654 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6655 | } |
| 6656 | |
| 6657 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6658 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6659 | /* finished. */ |
| 6660 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6661 | /* If the stack is empty, the function fails. */ |
| 6662 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6663 | return 0; |
| 6664 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6665 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6666 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6667 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6668 | |
| 6669 | /* Set the end of execution flag. */ |
| 6670 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 6671 | return 1; |
| 6672 | |
| 6673 | /* yield. */ |
| 6674 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6675 | 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] | 6676 | return 0; |
| 6677 | |
| 6678 | /* finished with error. */ |
| 6679 | case HLUA_E_ERRMSG: |
| 6680 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6681 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6682 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6683 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6684 | return 0; |
| 6685 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6686 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6687 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 6688 | return 0; |
| 6689 | |
| 6690 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6691 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 6692 | return 0; |
| 6693 | |
| 6694 | case HLUA_E_YIELD: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6695 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 6696 | return 0; |
| 6697 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6698 | case HLUA_E_ERR: |
| 6699 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6700 | 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] | 6701 | /* fall through */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6702 | |
| 6703 | default: |
| 6704 | return 0; |
| 6705 | } |
| 6706 | } |
| 6707 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6708 | /* This function is an LUA binding used for registering |
| 6709 | * "sample-conv" functions. It expects a converter name used |
| 6710 | * in the haproxy configuration file, and an LUA function. |
| 6711 | */ |
| 6712 | __LJMP static int hlua_register_converters(lua_State *L) |
| 6713 | { |
| 6714 | struct sample_conv_kw_list *sck; |
| 6715 | const char *name; |
| 6716 | int ref; |
| 6717 | int len; |
| 6718 | struct hlua_function *fcn; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6719 | struct sample_conv *sc; |
| 6720 | struct buffer *trash; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6721 | |
| 6722 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 6723 | |
| 6724 | /* First argument : converter name. */ |
| 6725 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6726 | |
| 6727 | /* Second argument : lua function. */ |
| 6728 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6729 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6730 | /* Check if the converter is already registered */ |
| 6731 | trash = get_trash_chunk(); |
| 6732 | chunk_printf(trash, "lua.%s", name); |
| 6733 | sc = find_sample_conv(trash->area, trash->data); |
| 6734 | if (sc != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 6735 | fcn = sc->private; |
| 6736 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 6737 | ha_warning("Trying to register converter 'lua.%s' more than once. " |
| 6738 | "This will become a hard error in version 2.5.\n", name); |
| 6739 | } |
| 6740 | fcn->function_ref[hlua_state_id] = ref; |
| 6741 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6742 | } |
| 6743 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6744 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6745 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6746 | if (!sck) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6747 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 6748 | fcn = new_hlua_function(); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6749 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6750 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6751 | |
| 6752 | /* Fill fcn. */ |
| 6753 | fcn->name = strdup(name); |
| 6754 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6755 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6756 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6757 | |
| 6758 | /* List head */ |
| 6759 | sck->list.n = sck->list.p = NULL; |
| 6760 | |
| 6761 | /* converter keyword. */ |
| 6762 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6763 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6764 | if (!sck->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6765 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6766 | |
| 6767 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 6768 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6769 | 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] | 6770 | sck->kw[0].val_args = NULL; |
| 6771 | sck->kw[0].in_type = SMP_T_STR; |
| 6772 | sck->kw[0].out_type = SMP_T_STR; |
| 6773 | sck->kw[0].private = fcn; |
| 6774 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6775 | /* Register this new converter */ |
| 6776 | sample_register_convs(sck); |
| 6777 | |
| 6778 | return 0; |
| 6779 | } |
| 6780 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6781 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6782 | * "sample-fetch" functions. It expects a converter name used |
| 6783 | * in the haproxy configuration file, and an LUA function. |
| 6784 | */ |
| 6785 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 6786 | { |
| 6787 | const char *name; |
| 6788 | int ref; |
| 6789 | int len; |
| 6790 | struct sample_fetch_kw_list *sfk; |
| 6791 | struct hlua_function *fcn; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6792 | struct sample_fetch *sf; |
| 6793 | struct buffer *trash; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6794 | |
| 6795 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 6796 | |
| 6797 | /* First argument : sample-fetch name. */ |
| 6798 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6799 | |
| 6800 | /* Second argument : lua function. */ |
| 6801 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6802 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6803 | /* Check if the sample-fetch is already registered */ |
| 6804 | trash = get_trash_chunk(); |
| 6805 | chunk_printf(trash, "lua.%s", name); |
| 6806 | sf = find_sample_fetch(trash->area, trash->data); |
| 6807 | if (sf != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 6808 | fcn = sf->private; |
| 6809 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 6810 | ha_warning("Trying to register sample-fetch 'lua.%s' more than once. " |
| 6811 | "This will become a hard error in version 2.5.\n", name); |
| 6812 | } |
| 6813 | fcn->function_ref[hlua_state_id] = ref; |
| 6814 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6815 | } |
| 6816 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6817 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6818 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6819 | if (!sfk) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6820 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 6821 | fcn = new_hlua_function(); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6822 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6823 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6824 | |
| 6825 | /* Fill fcn. */ |
| 6826 | fcn->name = strdup(name); |
| 6827 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6828 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6829 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6830 | |
| 6831 | /* List head */ |
| 6832 | sfk->list.n = sfk->list.p = NULL; |
| 6833 | |
| 6834 | /* sample-fetch keyword. */ |
| 6835 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6836 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6837 | if (!sfk->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6838 | return luaL_error(L, "Lua out of memory error."); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6839 | |
| 6840 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 6841 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6842 | 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] | 6843 | sfk->kw[0].val_args = NULL; |
| 6844 | sfk->kw[0].out_type = SMP_T_STR; |
| 6845 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 6846 | sfk->kw[0].val = 0; |
| 6847 | sfk->kw[0].private = fcn; |
| 6848 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6849 | /* Register this new fetch. */ |
| 6850 | sample_register_fetches(sfk); |
| 6851 | |
| 6852 | return 0; |
| 6853 | } |
| 6854 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 6855 | /* This function is a lua binding to set the wake_time. |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6856 | */ |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 6857 | __LJMP static int hlua_set_wake_time(lua_State *L) |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6858 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 6859 | struct hlua *hlua; |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6860 | unsigned int delay; |
| 6861 | unsigned int wakeup_ms; |
| 6862 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 6863 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 6864 | hlua = hlua_gethlua(L); |
| 6865 | if (!hlua) { |
| 6866 | return 0; |
| 6867 | } |
| 6868 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6869 | MAY_LJMP(check_args(L, 1, "wake_time")); |
| 6870 | |
| 6871 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 6872 | wakeup_ms = tick_add(now_ms, delay); |
| 6873 | hlua->wake_time = wakeup_ms; |
| 6874 | return 0; |
| 6875 | } |
| 6876 | |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6877 | /* This function is a wrapper to execute each LUA function declared as an action |
| 6878 | * wrapper during the initialisation period. This function may return any |
| 6879 | * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is |
| 6880 | * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the |
| 6881 | * return value is the first element on the stack. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6882 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6883 | 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] | 6884 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6885 | { |
| 6886 | char **arg; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6887 | unsigned int hflags = 0; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6888 | int dir, act_ret = ACT_RET_CONT; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6889 | const char *error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6890 | |
| 6891 | switch (rule->from) { |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 6892 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 6893 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 6894 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 6895 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6896 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6897 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6898 | goto end; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6899 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6900 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6901 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6902 | * Lua context can be not initialized. This behavior |
| 6903 | * permits to save performances because a systematic |
| 6904 | * Lua initialization cause 5% performances loss. |
| 6905 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6906 | if (!s->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6907 | s->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6908 | if (!s->hlua) { |
| 6909 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6910 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6911 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6912 | } |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6913 | 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] | 6914 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6915 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6916 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6917 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6918 | } |
| 6919 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6920 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6921 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6922 | |
| 6923 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6924 | if (!SET_SAFE_LJMP(s->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6925 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 6926 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6927 | else |
| 6928 | error = "critical error"; |
| 6929 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6930 | rule->arg.hlua_rule->fcn->name, error); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6931 | goto end; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6932 | } |
| 6933 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6934 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6935 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6936 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6937 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6938 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6939 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6940 | } |
| 6941 | |
| 6942 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6943 | 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] | 6944 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6945 | /* Create and and push object stream in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6946 | if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6947 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6948 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6949 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6950 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6951 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6952 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6953 | |
| 6954 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6955 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6956 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6957 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6958 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6959 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6960 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6961 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6962 | lua_pushstring(s->hlua->T, *arg); |
| 6963 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6964 | } |
| 6965 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6966 | /* Now the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6967 | RESET_SAFE_LJMP(s->hlua); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6968 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6969 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6970 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6971 | } |
| 6972 | |
| 6973 | /* Execute the function. */ |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 6974 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6975 | /* finished. */ |
| 6976 | case HLUA_E_OK: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6977 | /* Catch the return value */ |
| 6978 | if (lua_gettop(s->hlua->T) > 0) |
| 6979 | act_ret = lua_tointeger(s->hlua->T, -1); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6980 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6981 | /* Set timeout in the required channel. */ |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 6982 | if (act_ret == ACT_RET_YIELD) { |
| 6983 | if (flags & ACT_OPT_FINAL) |
| 6984 | goto err_yield; |
| 6985 | |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 6986 | if (dir == SMP_OPT_DIR_REQ) |
| 6987 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 6988 | s->hlua->wake_time); |
| 6989 | else |
| 6990 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 6991 | s->hlua->wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6992 | } |
| 6993 | goto end; |
| 6994 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6995 | /* yield. */ |
| 6996 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6997 | /* Set timeout in the required channel. */ |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 6998 | if (dir == SMP_OPT_DIR_REQ) |
| 6999 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 7000 | s->hlua->wake_time); |
| 7001 | else |
| 7002 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 7003 | s->hlua->wake_time); |
| 7004 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7005 | /* Some actions can be wake up when a "write" event |
| 7006 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7007 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7008 | */ |
Christopher Faulet | 51fa358 | 2019-07-26 14:54:52 +0200 | [diff] [blame] | 7009 | if (HLUA_IS_WAKERESWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7010 | s->res.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7011 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7012 | s->req.flags |= CF_WAKE_WRITE; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7013 | act_ret = ACT_RET_YIELD; |
| 7014 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7015 | |
| 7016 | /* finished with error. */ |
| 7017 | case HLUA_E_ERRMSG: |
| 7018 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7019 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7020 | rule->arg.hlua_rule->fcn->name, lua_tostring(s->hlua->T, -1)); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7021 | lua_pop(s->hlua->T, 1); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7022 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7023 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7024 | case HLUA_E_ETMOUT: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7025 | 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] | 7026 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7027 | |
| 7028 | case HLUA_E_NOMEM: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7029 | 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] | 7030 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7031 | |
| 7032 | case HLUA_E_YIELD: |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 7033 | err_yield: |
| 7034 | act_ret = ACT_RET_CONT; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7035 | 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] | 7036 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7037 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7038 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7039 | case HLUA_E_ERR: |
| 7040 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7041 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7042 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7043 | |
| 7044 | default: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7045 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7046 | } |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7047 | |
| 7048 | end: |
Christopher Faulet | 2361fd9 | 2020-07-30 10:40:58 +0200 | [diff] [blame] | 7049 | if (act_ret != ACT_RET_YIELD && s->hlua) |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 7050 | s->hlua->wake_time = TICK_ETERNITY; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7051 | return act_ret; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7052 | } |
| 7053 | |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 7054 | 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] | 7055 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 7056 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7057 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7058 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 7059 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 7060 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7061 | } |
| 7062 | |
| 7063 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 7064 | { |
| 7065 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7066 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7067 | struct task *task; |
| 7068 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7069 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7070 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7071 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7072 | if (!hlua) { |
| 7073 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7074 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7075 | return 0; |
| 7076 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7077 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7078 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7079 | ctx->ctx.hlua_apptcp.flags = 0; |
| 7080 | |
| 7081 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7082 | task = task_new(tid_bit); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7083 | if (!task) { |
| 7084 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7085 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7086 | return 0; |
| 7087 | } |
| 7088 | task->nice = 0; |
| 7089 | task->context = ctx; |
| 7090 | task->process = hlua_applet_wakeup; |
| 7091 | ctx->ctx.hlua_apptcp.task = task; |
| 7092 | |
| 7093 | /* In the execution wrappers linked with a stream, the |
| 7094 | * Lua context can be not initialized. This behavior |
| 7095 | * permits to save performances because a systematic |
| 7096 | * Lua initialization cause 5% performances loss. |
| 7097 | */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7098 | 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] | 7099 | 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] | 7100 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7101 | return 0; |
| 7102 | } |
| 7103 | |
| 7104 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 7105 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7106 | |
| 7107 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7108 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7109 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7110 | error = lua_tostring(hlua->T, -1); |
| 7111 | else |
| 7112 | error = "critical error"; |
| 7113 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7114 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7115 | return 0; |
| 7116 | } |
| 7117 | |
| 7118 | /* Check stack available size. */ |
| 7119 | if (!lua_checkstack(hlua->T, 1)) { |
| 7120 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7121 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7122 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7123 | return 0; |
| 7124 | } |
| 7125 | |
| 7126 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7127 | 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] | 7128 | |
| 7129 | /* Create and and push object stream in the stack. */ |
| 7130 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 7131 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7132 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7133 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7134 | return 0; |
| 7135 | } |
| 7136 | hlua->nargs = 1; |
| 7137 | |
| 7138 | /* push keywords in the stack. */ |
| 7139 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 7140 | if (!lua_checkstack(hlua->T, 1)) { |
| 7141 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7142 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7143 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7144 | return 0; |
| 7145 | } |
| 7146 | lua_pushstring(hlua->T, *arg); |
| 7147 | hlua->nargs++; |
| 7148 | } |
| 7149 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7150 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7151 | |
| 7152 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7153 | si_cant_get(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 7154 | si_rx_endp_more(si); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7155 | |
| 7156 | return 1; |
| 7157 | } |
| 7158 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7159 | void hlua_applet_tcp_fct(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7160 | { |
| 7161 | struct stream_interface *si = ctx->owner; |
| 7162 | struct stream *strm = si_strm(si); |
| 7163 | struct channel *res = si_ic(si); |
| 7164 | struct act_rule *rule = ctx->rule; |
| 7165 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7166 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7167 | |
| 7168 | /* The applet execution is already done. */ |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 7169 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) { |
| 7170 | /* eat the whole request */ |
| 7171 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7172 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 7173 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7174 | |
| 7175 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7176 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7177 | return; |
| 7178 | |
| 7179 | /* Execute the function. */ |
| 7180 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7181 | /* finished. */ |
| 7182 | case HLUA_E_OK: |
| 7183 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 7184 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7185 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 7186 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7187 | res->flags |= CF_READ_NULL; |
| 7188 | si_shutr(si); |
| 7189 | return; |
| 7190 | |
| 7191 | /* yield. */ |
| 7192 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 7193 | if (hlua->wake_time != TICK_ETERNITY) |
| 7194 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7195 | return; |
| 7196 | |
| 7197 | /* finished with error. */ |
| 7198 | case HLUA_E_ERRMSG: |
| 7199 | /* Display log. */ |
| 7200 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7201 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7202 | lua_pop(hlua->T, 1); |
| 7203 | goto error; |
| 7204 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7205 | case HLUA_E_ETMOUT: |
| 7206 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7207 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7208 | goto error; |
| 7209 | |
| 7210 | case HLUA_E_NOMEM: |
| 7211 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7212 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7213 | goto error; |
| 7214 | |
| 7215 | case HLUA_E_YIELD: /* unexpected */ |
| 7216 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7217 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7218 | goto error; |
| 7219 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7220 | case HLUA_E_ERR: |
| 7221 | /* Display log. */ |
| 7222 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7223 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7224 | goto error; |
| 7225 | |
| 7226 | default: |
| 7227 | goto error; |
| 7228 | } |
| 7229 | |
| 7230 | error: |
| 7231 | |
| 7232 | /* For all other cases, just close the stream. */ |
| 7233 | si_shutw(si); |
| 7234 | si_shutr(si); |
| 7235 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 7236 | } |
| 7237 | |
| 7238 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 7239 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7240 | task_destroy(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7241 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7242 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7243 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7244 | } |
| 7245 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7246 | /* The function returns 1 if the initialisation is complete, 0 if |
| 7247 | * an errors occurs and -1 if more data are required for initializing |
| 7248 | * the applet. |
| 7249 | */ |
| 7250 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 7251 | { |
| 7252 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7253 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7254 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7255 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7256 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7257 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7258 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7259 | txn = strm->txn; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7260 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7261 | if (!hlua) { |
| 7262 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7263 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7264 | return 0; |
| 7265 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7266 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7267 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7268 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 7269 | ctx->ctx.hlua_apphttp.flags = 0; |
| 7270 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 7271 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 7272 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 7273 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7274 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7275 | task = task_new(tid_bit); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7276 | if (!task) { |
| 7277 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7278 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7279 | return 0; |
| 7280 | } |
| 7281 | task->nice = 0; |
| 7282 | task->context = ctx; |
| 7283 | task->process = hlua_applet_wakeup; |
| 7284 | ctx->ctx.hlua_apphttp.task = task; |
| 7285 | |
| 7286 | /* In the execution wrappers linked with a stream, the |
| 7287 | * Lua context can be not initialized. This behavior |
| 7288 | * permits to save performances because a systematic |
| 7289 | * Lua initialization cause 5% performances loss. |
| 7290 | */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7291 | 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] | 7292 | 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] | 7293 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7294 | return 0; |
| 7295 | } |
| 7296 | |
| 7297 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 7298 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7299 | |
| 7300 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7301 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7302 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7303 | error = lua_tostring(hlua->T, -1); |
| 7304 | else |
| 7305 | error = "critical error"; |
| 7306 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7307 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7308 | return 0; |
| 7309 | } |
| 7310 | |
| 7311 | /* Check stack available size. */ |
| 7312 | if (!lua_checkstack(hlua->T, 1)) { |
| 7313 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7314 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7315 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7316 | return 0; |
| 7317 | } |
| 7318 | |
| 7319 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7320 | 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] | 7321 | |
| 7322 | /* Create and and push object stream in the stack. */ |
| 7323 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 7324 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7325 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7326 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7327 | return 0; |
| 7328 | } |
| 7329 | hlua->nargs = 1; |
| 7330 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7331 | /* push keywords in the stack. */ |
| 7332 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 7333 | if (!lua_checkstack(hlua->T, 1)) { |
| 7334 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7335 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7336 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7337 | return 0; |
| 7338 | } |
| 7339 | lua_pushstring(hlua->T, *arg); |
| 7340 | hlua->nargs++; |
| 7341 | } |
| 7342 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7343 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7344 | |
| 7345 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7346 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7347 | |
| 7348 | return 1; |
| 7349 | } |
| 7350 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7351 | void hlua_applet_http_fct(struct appctx *ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7352 | { |
| 7353 | struct stream_interface *si = ctx->owner; |
| 7354 | struct stream *strm = si_strm(si); |
| 7355 | struct channel *req = si_oc(si); |
| 7356 | struct channel *res = si_ic(si); |
| 7357 | struct act_rule *rule = ctx->rule; |
| 7358 | struct proxy *px = strm->be; |
| 7359 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
| 7360 | struct htx *req_htx, *res_htx; |
| 7361 | |
| 7362 | res_htx = htx_from_buf(&res->buf); |
| 7363 | |
| 7364 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7365 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7366 | goto out; |
| 7367 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7368 | /* Check if the input buffer is available. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7369 | if (!b_size(&res->buf)) { |
| 7370 | si_rx_room_blk(si); |
| 7371 | goto out; |
| 7372 | } |
| 7373 | /* check that the output is not closed */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7374 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7375 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7376 | |
| 7377 | /* Set the currently running flag. */ |
| 7378 | if (!HLUA_IS_RUNNING(hlua) && |
| 7379 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7380 | struct htx_blk *blk; |
| 7381 | size_t count = co_data(req); |
| 7382 | |
| 7383 | if (!count) { |
| 7384 | si_cant_get(si); |
| 7385 | goto out; |
| 7386 | } |
| 7387 | |
| 7388 | /* We need to flush the request header. This left the body for |
| 7389 | * the Lua. |
| 7390 | */ |
| 7391 | req_htx = htx_from_buf(&req->buf); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 7392 | blk = htx_get_first_blk(req_htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7393 | while (count && blk) { |
| 7394 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 7395 | uint32_t sz = htx_get_blksz(blk); |
| 7396 | |
| 7397 | if (sz > count) { |
| 7398 | si_cant_get(si); |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 7399 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7400 | goto out; |
| 7401 | } |
| 7402 | |
| 7403 | count -= sz; |
| 7404 | co_set_data(req, co_data(req) - sz); |
| 7405 | blk = htx_remove_blk(req_htx, blk); |
| 7406 | |
| 7407 | if (type == HTX_BLK_EOH) |
| 7408 | break; |
| 7409 | } |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 7410 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7411 | } |
| 7412 | |
| 7413 | /* Executes The applet if it is not done. */ |
| 7414 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7415 | |
| 7416 | /* Execute the function. */ |
| 7417 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7418 | /* finished. */ |
| 7419 | case HLUA_E_OK: |
| 7420 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7421 | break; |
| 7422 | |
| 7423 | /* yield. */ |
| 7424 | case HLUA_E_AGAIN: |
| 7425 | if (hlua->wake_time != TICK_ETERNITY) |
| 7426 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7427 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7428 | |
| 7429 | /* finished with error. */ |
| 7430 | case HLUA_E_ERRMSG: |
| 7431 | /* Display log. */ |
| 7432 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7433 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7434 | lua_pop(hlua->T, 1); |
| 7435 | goto error; |
| 7436 | |
| 7437 | case HLUA_E_ETMOUT: |
| 7438 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7439 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7440 | goto error; |
| 7441 | |
| 7442 | case HLUA_E_NOMEM: |
| 7443 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7444 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7445 | goto error; |
| 7446 | |
| 7447 | case HLUA_E_YIELD: /* unexpected */ |
| 7448 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7449 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7450 | goto error; |
| 7451 | |
| 7452 | case HLUA_E_ERR: |
| 7453 | /* Display log. */ |
| 7454 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7455 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7456 | goto error; |
| 7457 | |
| 7458 | default: |
| 7459 | goto error; |
| 7460 | } |
| 7461 | } |
| 7462 | |
| 7463 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7464 | if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT) |
| 7465 | goto done; |
| 7466 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7467 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) |
| 7468 | goto error; |
| 7469 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 7470 | /* Don't add TLR because mux-h1 will take care of it */ |
Willy Tarreau | f1ea47d | 2020-07-23 06:53:27 +0200 | [diff] [blame] | 7471 | res_htx->flags |= HTX_FL_EOI; /* no more data are expected. Only EOM remains to add now */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7472 | if (!htx_add_endof(res_htx, HTX_BLK_EOM)) { |
| 7473 | si_rx_room_blk(si); |
| 7474 | goto out; |
| 7475 | } |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 7476 | channel_add_input(res, 1); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7477 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 7478 | ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7479 | } |
| 7480 | |
| 7481 | done: |
| 7482 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7483 | if (!(res->flags & CF_SHUTR)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7484 | res->flags |= CF_READ_NULL; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7485 | si_shutr(si); |
| 7486 | } |
| 7487 | |
| 7488 | /* eat the whole request */ |
| 7489 | if (co_data(req)) { |
| 7490 | req_htx = htx_from_buf(&req->buf); |
| 7491 | co_htx_skip(req, req_htx, co_data(req)); |
| 7492 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7493 | } |
| 7494 | } |
| 7495 | |
| 7496 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7497 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7498 | return; |
| 7499 | |
| 7500 | error: |
| 7501 | |
| 7502 | /* If we are in HTTP mode, and we are not send any |
| 7503 | * data, return a 500 server error in best effort: |
| 7504 | * if there is no room available in the buffer, |
| 7505 | * just close the connection. |
| 7506 | */ |
| 7507 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 7508 | struct buffer *err = &http_err_chunks[HTTP_ERR_500]; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7509 | |
| 7510 | channel_erase(res); |
| 7511 | res->buf.data = b_data(err); |
| 7512 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 7513 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 7514 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7515 | } |
| 7516 | if (!(strm->flags & SF_ERR_MASK)) |
| 7517 | strm->flags |= SF_ERR_RESOURCE; |
| 7518 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7519 | goto done; |
| 7520 | } |
| 7521 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7522 | static void hlua_applet_http_release(struct appctx *ctx) |
| 7523 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7524 | task_destroy(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7525 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7526 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7527 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7528 | } |
| 7529 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7530 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7531 | * success case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7532 | * |
| 7533 | * This function can fail with an abort() due to an Lua critical error. |
| 7534 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7535 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7536 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7537 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 7538 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7539 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7540 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7541 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7542 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7543 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7544 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7545 | if (!rule->arg.hlua_rule) { |
| 7546 | memprintf(err, "out of memory error"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7547 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7548 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7549 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7550 | /* Memory for arguments. */ |
Tim Duesterhus | e52b6e5 | 2020-09-12 20:26:43 +0200 | [diff] [blame] | 7551 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, |
| 7552 | sizeof(*rule->arg.hlua_rule->args)); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7553 | if (!rule->arg.hlua_rule->args) { |
| 7554 | memprintf(err, "out of memory error"); |
| 7555 | return ACT_RET_PRS_ERR; |
| 7556 | } |
| 7557 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7558 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7559 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7560 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7561 | /* Expect some arguments */ |
| 7562 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7563 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7564 | memprintf(err, "expect %d arguments", fcn->nargs); |
| 7565 | return ACT_RET_PRS_ERR; |
| 7566 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7567 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7568 | if (!rule->arg.hlua_rule->args[i]) { |
| 7569 | memprintf(err, "out of memory error"); |
| 7570 | return ACT_RET_PRS_ERR; |
| 7571 | } |
| 7572 | (*cur_arg)++; |
| 7573 | } |
| 7574 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7575 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 7576 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7577 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7578 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7579 | } |
| 7580 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7581 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 7582 | struct act_rule *rule, char **err) |
| 7583 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7584 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7585 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7586 | /* HTTP applets are forbidden in tcp-request rules. |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7587 | * HTTP applet request requires everything initialized by |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7588 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7589 | * The applet will be immediately initialized, but its before |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7590 | * the call of this analyzer. |
| 7591 | */ |
| 7592 | if (rule->from != ACT_F_HTTP_REQ) { |
| 7593 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 7594 | return ACT_RET_PRS_ERR; |
| 7595 | } |
| 7596 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7597 | /* Memory for the rule. */ |
| 7598 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7599 | if (!rule->arg.hlua_rule) { |
| 7600 | memprintf(err, "out of memory error"); |
| 7601 | return ACT_RET_PRS_ERR; |
| 7602 | } |
| 7603 | |
| 7604 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7605 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7606 | |
| 7607 | /* TODO: later accept arguments. */ |
| 7608 | rule->arg.hlua_rule->args = NULL; |
| 7609 | |
| 7610 | /* Add applet pointer in the rule. */ |
| 7611 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7612 | rule->applet.name = fcn->name; |
| 7613 | rule->applet.init = hlua_applet_http_init; |
| 7614 | rule->applet.fct = hlua_applet_http_fct; |
| 7615 | rule->applet.release = hlua_applet_http_release; |
| 7616 | rule->applet.timeout = hlua_timeout_applet; |
| 7617 | |
| 7618 | return ACT_RET_PRS_OK; |
| 7619 | } |
| 7620 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7621 | /* This function is an LUA binding used for registering |
| 7622 | * "sample-conv" functions. It expects a converter name used |
| 7623 | * in the haproxy configuration file, and an LUA function. |
| 7624 | */ |
| 7625 | __LJMP static int hlua_register_action(lua_State *L) |
| 7626 | { |
| 7627 | struct action_kw_list *akl; |
| 7628 | const char *name; |
| 7629 | int ref; |
| 7630 | int len; |
| 7631 | struct hlua_function *fcn; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7632 | int nargs; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7633 | struct buffer *trash; |
| 7634 | struct action_kw *akw; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7635 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7636 | /* Initialise the number of expected arguments at 0. */ |
| 7637 | nargs = 0; |
| 7638 | |
| 7639 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 7640 | 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] | 7641 | |
| 7642 | /* First argument : converter name. */ |
| 7643 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7644 | |
| 7645 | /* Second argument : environment. */ |
| 7646 | if (lua_type(L, 2) != LUA_TTABLE) |
| 7647 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7648 | |
| 7649 | /* Third argument : lua function. */ |
| 7650 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7651 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7652 | /* 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] | 7653 | if (lua_gettop(L) >= 4) |
| 7654 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 7655 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7656 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7657 | lua_pushnil(L); |
| 7658 | while (lua_next(L, 2) != 0) { |
| 7659 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7660 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7661 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7662 | /* Check if action exists */ |
| 7663 | trash = get_trash_chunk(); |
| 7664 | chunk_printf(trash, "lua.%s", name); |
| 7665 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) { |
| 7666 | akw = tcp_req_cont_action(trash->area); |
| 7667 | } else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) { |
| 7668 | akw = tcp_res_cont_action(trash->area); |
| 7669 | } else if (strcmp(lua_tostring(L, -1), "http-req") == 0) { |
| 7670 | akw = action_http_req_custom(trash->area); |
| 7671 | } else if (strcmp(lua_tostring(L, -1), "http-res") == 0) { |
| 7672 | akw = action_http_res_custom(trash->area); |
| 7673 | } else { |
| 7674 | akw = NULL; |
| 7675 | } |
| 7676 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 7677 | fcn = akw->private; |
| 7678 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 7679 | ha_warning("Trying to register action 'lua.%s' more than once. " |
| 7680 | "This will become a hard error in version 2.5.\n", name); |
| 7681 | } |
| 7682 | fcn->function_ref[hlua_state_id] = ref; |
| 7683 | |
| 7684 | /* pop the environment string. */ |
| 7685 | lua_pop(L, 1); |
| 7686 | continue; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7687 | } |
| 7688 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7689 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 7690 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7691 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7692 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7693 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 7694 | fcn = new_hlua_function(); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7695 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7696 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7697 | |
| 7698 | /* Fill fcn. */ |
| 7699 | fcn->name = strdup(name); |
| 7700 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7701 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7702 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7703 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 7704 | /* Set the expected number of arguments. */ |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7705 | fcn->nargs = nargs; |
| 7706 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7707 | /* List head */ |
| 7708 | akl->list.n = akl->list.p = NULL; |
| 7709 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7710 | /* action keyword. */ |
| 7711 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7712 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7713 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7714 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7715 | |
| 7716 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7717 | |
| 7718 | akl->kw[0].match_pfx = 0; |
| 7719 | akl->kw[0].private = fcn; |
| 7720 | akl->kw[0].parse = action_register_lua; |
| 7721 | |
| 7722 | /* select the action registering point. */ |
| 7723 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 7724 | tcp_req_cont_keywords_register(akl); |
| 7725 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 7726 | tcp_res_cont_keywords_register(akl); |
| 7727 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 7728 | http_req_keywords_register(akl); |
| 7729 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 7730 | http_res_keywords_register(akl); |
| 7731 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7732 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7733 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 7734 | "are expected.", lua_tostring(L, -1))); |
| 7735 | |
| 7736 | /* pop the environment string. */ |
| 7737 | lua_pop(L, 1); |
| 7738 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7739 | return ACT_RET_PRS_OK; |
| 7740 | } |
| 7741 | |
| 7742 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 7743 | struct act_rule *rule, char **err) |
| 7744 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7745 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7746 | |
Christopher Faulet | 280f85b | 2019-07-15 15:02:04 +0200 | [diff] [blame] | 7747 | if (px->mode == PR_MODE_HTTP) { |
| 7748 | memprintf(err, "Lua TCP services cannot be used on HTTP proxies"); |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 7749 | return ACT_RET_PRS_ERR; |
| 7750 | } |
| 7751 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7752 | /* Memory for the rule. */ |
| 7753 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7754 | if (!rule->arg.hlua_rule) { |
| 7755 | memprintf(err, "out of memory error"); |
| 7756 | return ACT_RET_PRS_ERR; |
| 7757 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7758 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7759 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7760 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7761 | |
| 7762 | /* TODO: later accept arguments. */ |
| 7763 | rule->arg.hlua_rule->args = NULL; |
| 7764 | |
| 7765 | /* Add applet pointer in the rule. */ |
| 7766 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7767 | rule->applet.name = fcn->name; |
| 7768 | rule->applet.init = hlua_applet_tcp_init; |
| 7769 | rule->applet.fct = hlua_applet_tcp_fct; |
| 7770 | rule->applet.release = hlua_applet_tcp_release; |
| 7771 | rule->applet.timeout = hlua_timeout_applet; |
| 7772 | |
| 7773 | return 0; |
| 7774 | } |
| 7775 | |
| 7776 | /* This function is an LUA binding used for registering |
| 7777 | * "sample-conv" functions. It expects a converter name used |
| 7778 | * in the haproxy configuration file, and an LUA function. |
| 7779 | */ |
| 7780 | __LJMP static int hlua_register_service(lua_State *L) |
| 7781 | { |
| 7782 | struct action_kw_list *akl; |
| 7783 | const char *name; |
| 7784 | const char *env; |
| 7785 | int ref; |
| 7786 | int len; |
| 7787 | struct hlua_function *fcn; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7788 | struct buffer *trash; |
| 7789 | struct action_kw *akw; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7790 | |
| 7791 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 7792 | |
| 7793 | /* First argument : converter name. */ |
| 7794 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7795 | |
| 7796 | /* Second argument : environment. */ |
| 7797 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7798 | |
| 7799 | /* Third argument : lua function. */ |
| 7800 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7801 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7802 | /* Check for service already registered */ |
| 7803 | trash = get_trash_chunk(); |
| 7804 | chunk_printf(trash, "lua.%s", name); |
| 7805 | akw = service_find(trash->area); |
| 7806 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 7807 | fcn = akw->private; |
| 7808 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 7809 | ha_warning("Trying to register service 'lua.%s' more than once. " |
| 7810 | "This will become a hard error in version 2.5.\n", name); |
| 7811 | } |
| 7812 | fcn->function_ref[hlua_state_id] = ref; |
| 7813 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7814 | } |
| 7815 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7816 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7817 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 7818 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7819 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 7820 | fcn = new_hlua_function(); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7821 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7822 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7823 | |
| 7824 | /* Fill fcn. */ |
| 7825 | len = strlen("<lua.>") + strlen(name) + 1; |
| 7826 | fcn->name = calloc(1, len); |
| 7827 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7828 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7829 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7830 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7831 | |
| 7832 | /* List head */ |
| 7833 | akl->list.n = akl->list.p = NULL; |
| 7834 | |
| 7835 | /* converter keyword. */ |
| 7836 | len = strlen("lua.") + strlen(name) + 1; |
| 7837 | akl->kw[0].kw = calloc(1, len); |
| 7838 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7839 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7840 | |
| 7841 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7842 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 7843 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7844 | if (strcmp(env, "tcp") == 0) |
| 7845 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7846 | else if (strcmp(env, "http") == 0) |
| 7847 | akl->kw[0].parse = action_register_service_http; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7848 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7849 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 7850 | "'tcp' or 'http' are expected.", env)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7851 | |
| 7852 | akl->kw[0].match_pfx = 0; |
| 7853 | akl->kw[0].private = fcn; |
| 7854 | |
| 7855 | /* End of array. */ |
| 7856 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 7857 | |
| 7858 | /* Register this new converter */ |
| 7859 | service_keywords_register(akl); |
| 7860 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7861 | return 0; |
| 7862 | } |
| 7863 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7864 | /* This function initialises Lua cli handler. It copies the |
| 7865 | * arguments in the Lua stack and create channel IO objects. |
| 7866 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 7867 | 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] | 7868 | { |
| 7869 | struct hlua *hlua; |
| 7870 | struct hlua_function *fcn; |
| 7871 | int i; |
| 7872 | const char *error; |
| 7873 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7874 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7875 | appctx->ctx.hlua_cli.fcn = private; |
| 7876 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7877 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7878 | if (!hlua) { |
| 7879 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7880 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7881 | } |
| 7882 | HLUA_INIT(hlua); |
| 7883 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7884 | |
| 7885 | /* Create task used by signal to wakeup applets. |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 7886 | * 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] | 7887 | * applet_http. It is absolutely compatible. |
| 7888 | */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7889 | appctx->ctx.hlua_cli.task = task_new(tid_bit); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7890 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 7891 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7892 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7893 | } |
| 7894 | appctx->ctx.hlua_cli.task->nice = 0; |
| 7895 | appctx->ctx.hlua_cli.task->context = appctx; |
| 7896 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 7897 | |
| 7898 | /* Initialises the Lua context */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7899 | 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] | 7900 | 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] | 7901 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7902 | } |
| 7903 | |
| 7904 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7905 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7906 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7907 | error = lua_tostring(hlua->T, -1); |
| 7908 | else |
| 7909 | error = "critical error"; |
| 7910 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 7911 | goto error; |
| 7912 | } |
| 7913 | |
| 7914 | /* Check stack available size. */ |
| 7915 | if (!lua_checkstack(hlua->T, 2)) { |
| 7916 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7917 | goto error; |
| 7918 | } |
| 7919 | |
| 7920 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7921 | 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] | 7922 | |
| 7923 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 7924 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7925 | */ |
| 7926 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 7927 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7928 | goto error; |
| 7929 | } |
| 7930 | hlua->nargs = 1; |
| 7931 | |
| 7932 | /* push keywords in the stack. */ |
| 7933 | for (i = 0; *args[i]; i++) { |
| 7934 | /* Check stack available size. */ |
| 7935 | if (!lua_checkstack(hlua->T, 1)) { |
| 7936 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7937 | goto error; |
| 7938 | } |
| 7939 | lua_pushstring(hlua->T, args[i]); |
| 7940 | hlua->nargs++; |
| 7941 | } |
| 7942 | |
| 7943 | /* We must initialize the execution timeouts. */ |
| 7944 | hlua->max_time = hlua_timeout_session; |
| 7945 | |
| 7946 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7947 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7948 | |
| 7949 | /* It's ok */ |
| 7950 | return 0; |
| 7951 | |
| 7952 | /* It's not ok. */ |
| 7953 | error: |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7954 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7955 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7956 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7957 | return 1; |
| 7958 | } |
| 7959 | |
| 7960 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 7961 | { |
| 7962 | struct hlua *hlua; |
| 7963 | struct stream_interface *si; |
| 7964 | struct hlua_function *fcn; |
| 7965 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7966 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7967 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 7968 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7969 | |
| 7970 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7971 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7972 | return 1; |
| 7973 | |
| 7974 | /* Execute the function. */ |
| 7975 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7976 | |
| 7977 | /* finished. */ |
| 7978 | case HLUA_E_OK: |
| 7979 | return 1; |
| 7980 | |
| 7981 | /* yield. */ |
| 7982 | case HLUA_E_AGAIN: |
| 7983 | /* We want write. */ |
| 7984 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 7985 | si_rx_room_blk(si); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7986 | /* Set the timeout. */ |
| 7987 | if (hlua->wake_time != TICK_ETERNITY) |
| 7988 | task_schedule(hlua->task, hlua->wake_time); |
| 7989 | return 0; |
| 7990 | |
| 7991 | /* finished with error. */ |
| 7992 | case HLUA_E_ERRMSG: |
| 7993 | /* Display log. */ |
| 7994 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 7995 | fcn->name, lua_tostring(hlua->T, -1)); |
| 7996 | lua_pop(hlua->T, 1); |
| 7997 | return 1; |
| 7998 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7999 | case HLUA_E_ETMOUT: |
| 8000 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 8001 | fcn->name); |
| 8002 | return 1; |
| 8003 | |
| 8004 | case HLUA_E_NOMEM: |
| 8005 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 8006 | fcn->name); |
| 8007 | return 1; |
| 8008 | |
| 8009 | case HLUA_E_YIELD: /* unexpected */ |
| 8010 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 8011 | fcn->name); |
| 8012 | return 1; |
| 8013 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8014 | case HLUA_E_ERR: |
| 8015 | /* Display log. */ |
| 8016 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 8017 | fcn->name); |
| 8018 | return 1; |
| 8019 | |
| 8020 | default: |
| 8021 | return 1; |
| 8022 | } |
| 8023 | |
| 8024 | return 1; |
| 8025 | } |
| 8026 | |
| 8027 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 8028 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8029 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8030 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8031 | } |
| 8032 | |
| 8033 | /* This function is an LUA binding used for registering |
| 8034 | * new keywords in the cli. It expects a list of keywords |
| 8035 | * which are the "path". It is limited to 5 keywords. A |
| 8036 | * description of the command, a function to be executed |
| 8037 | * for the parsing and a function for io handlers. |
| 8038 | */ |
| 8039 | __LJMP static int hlua_register_cli(lua_State *L) |
| 8040 | { |
| 8041 | struct cli_kw_list *cli_kws; |
| 8042 | const char *message; |
| 8043 | int ref_io; |
| 8044 | int len; |
| 8045 | struct hlua_function *fcn; |
| 8046 | int index; |
| 8047 | int i; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8048 | struct buffer *trash; |
| 8049 | const char *kw[5]; |
| 8050 | struct cli_kw *cli_kw; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8051 | |
| 8052 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 8053 | |
| 8054 | /* First argument : an array of maximum 5 keywords. */ |
| 8055 | if (!lua_istable(L, 1)) |
| 8056 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 8057 | |
| 8058 | /* Second argument : string with contextual message. */ |
| 8059 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8060 | |
| 8061 | /* Third and fourth argument : lua function. */ |
| 8062 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 8063 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8064 | /* Check for CLI service already registered */ |
| 8065 | trash = get_trash_chunk(); |
| 8066 | index = 0; |
| 8067 | lua_pushnil(L); |
| 8068 | memset(kw, 0, sizeof(kw)); |
| 8069 | while (lua_next(L, 1) != 0) { |
| 8070 | if (index >= CLI_PREFIX_KW_NB) |
| 8071 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 8072 | if (lua_type(L, -1) != LUA_TSTRING) |
| 8073 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 8074 | kw[index] = lua_tostring(L, -1); |
| 8075 | if (index == 0) |
| 8076 | chunk_printf(trash, "%s", kw[index]); |
| 8077 | else |
| 8078 | chunk_appendf(trash, " %s", kw[index]); |
| 8079 | index++; |
| 8080 | lua_pop(L, 1); |
| 8081 | } |
| 8082 | cli_kw = cli_find_kw_exact((char **)kw); |
| 8083 | if (cli_kw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8084 | fcn = cli_kw->private; |
| 8085 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 8086 | ha_warning("Trying to register CLI keyword 'lua.%s' more than once. " |
| 8087 | "This will become a hard error in version 2.5.\n", trash->area); |
| 8088 | } |
| 8089 | fcn->function_ref[hlua_state_id] = ref_io; |
| 8090 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8091 | } |
| 8092 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8093 | /* Allocate and fill the sample fetch keyword struct. */ |
| 8094 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
| 8095 | if (!cli_kws) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8096 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 8097 | fcn = new_hlua_function(); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8098 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8099 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8100 | |
| 8101 | /* Fill path. */ |
| 8102 | index = 0; |
| 8103 | lua_pushnil(L); |
| 8104 | while(lua_next(L, 1) != 0) { |
| 8105 | if (index >= 5) |
| 8106 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 8107 | if (lua_type(L, -1) != LUA_TSTRING) |
| 8108 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 8109 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
| 8110 | if (!cli_kws->kw[0].str_kw[index]) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8111 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8112 | index++; |
| 8113 | lua_pop(L, 1); |
| 8114 | } |
| 8115 | |
| 8116 | /* Copy help message. */ |
| 8117 | cli_kws->kw[0].usage = strdup(message); |
| 8118 | if (!cli_kws->kw[0].usage) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8119 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8120 | |
| 8121 | /* Fill fcn io handler. */ |
| 8122 | len = strlen("<lua.cli>") + 1; |
| 8123 | for (i = 0; i < index; i++) |
| 8124 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 8125 | fcn->name = calloc(1, len); |
| 8126 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8127 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8128 | strncat((char *)fcn->name, "<lua.cli", len); |
| 8129 | for (i = 0; i < index; i++) { |
| 8130 | strncat((char *)fcn->name, ".", len); |
| 8131 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 8132 | } |
| 8133 | strncat((char *)fcn->name, ">", len); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8134 | fcn->function_ref[hlua_state_id] = ref_io; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8135 | |
| 8136 | /* Fill last entries. */ |
| 8137 | cli_kws->kw[0].private = fcn; |
| 8138 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 8139 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 8140 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 8141 | |
| 8142 | /* Register this new converter */ |
| 8143 | cli_register_kw(cli_kws); |
| 8144 | |
| 8145 | return 0; |
| 8146 | } |
| 8147 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8148 | static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx, |
| 8149 | struct proxy *defpx, const char *file, int line, |
| 8150 | char **err, unsigned int *timeout) |
| 8151 | { |
| 8152 | const char *error; |
| 8153 | |
| 8154 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 8155 | if (error == PARSE_TIME_OVER) { |
| 8156 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 8157 | args[1], args[0]); |
| 8158 | return -1; |
| 8159 | } |
| 8160 | else if (error == PARSE_TIME_UNDER) { |
| 8161 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 8162 | args[1], args[0]); |
| 8163 | return -1; |
| 8164 | } |
| 8165 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8166 | memprintf(err, "%s: invalid timeout", args[0]); |
| 8167 | return -1; |
| 8168 | } |
| 8169 | return 0; |
| 8170 | } |
| 8171 | |
| 8172 | static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx, |
| 8173 | struct proxy *defpx, const char *file, int line, |
| 8174 | char **err) |
| 8175 | { |
| 8176 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8177 | file, line, err, &hlua_timeout_session); |
| 8178 | } |
| 8179 | |
| 8180 | static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx, |
| 8181 | struct proxy *defpx, const char *file, int line, |
| 8182 | char **err) |
| 8183 | { |
| 8184 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8185 | file, line, err, &hlua_timeout_task); |
| 8186 | } |
| 8187 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8188 | static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx, |
| 8189 | struct proxy *defpx, const char *file, int line, |
| 8190 | char **err) |
| 8191 | { |
| 8192 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8193 | file, line, err, &hlua_timeout_applet); |
| 8194 | } |
| 8195 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8196 | static int hlua_forced_yield(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 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 8203 | if (*error != '\0') { |
| 8204 | memprintf(err, "%s: invalid number", args[0]); |
| 8205 | return -1; |
| 8206 | } |
| 8207 | return 0; |
| 8208 | } |
| 8209 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8210 | static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx, |
| 8211 | struct proxy *defpx, const char *file, int line, |
| 8212 | char **err) |
| 8213 | { |
| 8214 | char *error; |
| 8215 | |
| 8216 | if (*(args[1]) == 0) { |
| 8217 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 8218 | return -1; |
| 8219 | } |
| 8220 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 8221 | if (*error != '\0') { |
| 8222 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 8223 | return -1; |
| 8224 | } |
| 8225 | return 0; |
| 8226 | } |
| 8227 | |
| 8228 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8229 | /* This function is called by the main configuration key "lua-load". It loads and |
| 8230 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 8231 | * the main lua entry point. |
| 8232 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8233 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 8234 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8235 | * |
| 8236 | * In some error case, LUA set an error message in top of the stack. This function |
| 8237 | * 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] | 8238 | * |
| 8239 | * This function can fail with an abort() due to an Lua critical error. |
| 8240 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 8241 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8242 | */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8243 | static int hlua_load_state(char *filename, lua_State *L, char **err) |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8244 | { |
| 8245 | int error; |
| 8246 | |
| 8247 | /* Just load and compile the file. */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8248 | error = luaL_loadfile(L, filename); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8249 | if (error) { |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8250 | memprintf(err, "error in Lua file '%s': %s", filename, lua_tostring(L, -1)); |
| 8251 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8252 | return -1; |
| 8253 | } |
| 8254 | |
| 8255 | /* If no syntax error where detected, execute the code. */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8256 | error = lua_pcall(L, 0, LUA_MULTRET, 0); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8257 | switch (error) { |
| 8258 | case LUA_OK: |
| 8259 | break; |
| 8260 | case LUA_ERRRUN: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8261 | memprintf(err, "Lua runtime error: %s\n", lua_tostring(L, -1)); |
| 8262 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8263 | return -1; |
| 8264 | case LUA_ERRMEM: |
Thierry Fournier | de6145f | 2020-11-29 00:55:53 +0100 | [diff] [blame] | 8265 | memprintf(err, "Lua out of memory error\n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8266 | return -1; |
| 8267 | case LUA_ERRERR: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8268 | memprintf(err, "Lua message handler error: %s\n", lua_tostring(L, -1)); |
| 8269 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8270 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 8271 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8272 | case LUA_ERRGCMM: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8273 | memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(L, -1)); |
| 8274 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8275 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 8276 | #endif |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8277 | default: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8278 | memprintf(err, "Lua unknown error: %s\n", lua_tostring(L, -1)); |
| 8279 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8280 | return -1; |
| 8281 | } |
| 8282 | |
| 8283 | return 0; |
| 8284 | } |
| 8285 | |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8286 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
| 8287 | struct proxy *defpx, const char *file, int line, |
| 8288 | char **err) |
| 8289 | { |
| 8290 | if (*(args[1]) == 0) { |
| 8291 | memprintf(err, "'%s' expects a file name as parameter.\n", args[0]); |
| 8292 | return -1; |
| 8293 | } |
| 8294 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8295 | /* loading for global state */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8296 | hlua_state_id = 0; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8297 | ha_set_tid(0); |
Thierry Fournier | afc63e2 | 2020-11-28 17:06:51 +0100 | [diff] [blame] | 8298 | return hlua_load_state(args[1], hlua_states[0], err); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8299 | } |
| 8300 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8301 | static int hlua_load_per_thread(char **args, int section_type, struct proxy *curpx, |
| 8302 | struct proxy *defpx, const char *file, int line, |
| 8303 | char **err) |
| 8304 | { |
| 8305 | int len; |
| 8306 | |
| 8307 | if (*(args[1]) == 0) { |
| 8308 | memprintf(err, "'%s' expects a file as parameter.\n", args[0]); |
| 8309 | return -1; |
| 8310 | } |
| 8311 | |
| 8312 | if (per_thread_load == NULL) { |
| 8313 | /* allocate the first entry large enough to store the final NULL */ |
| 8314 | per_thread_load = calloc(1, sizeof(*per_thread_load)); |
| 8315 | if (per_thread_load == NULL) { |
| 8316 | memprintf(err, "out of memory error"); |
| 8317 | return -1; |
| 8318 | } |
| 8319 | } |
| 8320 | |
| 8321 | /* count used entries */ |
| 8322 | for (len = 0; per_thread_load[len] != NULL; len++) |
| 8323 | ; |
| 8324 | |
| 8325 | per_thread_load = realloc(per_thread_load, (len + 2) * sizeof(*per_thread_load)); |
| 8326 | if (per_thread_load == NULL) { |
| 8327 | memprintf(err, "out of memory error"); |
| 8328 | return -1; |
| 8329 | } |
| 8330 | |
| 8331 | per_thread_load[len] = strdup(args[1]); |
| 8332 | per_thread_load[len + 1] = NULL; |
| 8333 | |
| 8334 | if (per_thread_load[len] == NULL) { |
| 8335 | memprintf(err, "out of memory error"); |
| 8336 | return -1; |
| 8337 | } |
| 8338 | |
| 8339 | /* loading for thread 1 only */ |
| 8340 | hlua_state_id = 1; |
| 8341 | ha_set_tid(0); |
| 8342 | return hlua_load_state(args[1], hlua_states[1], err); |
| 8343 | } |
| 8344 | |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 8345 | /* Prepend the given <path> followed by a semicolon to the `package.<type>` variable |
| 8346 | * in the given <ctx>. |
| 8347 | */ |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 8348 | static int hlua_prepend_path(lua_State *L, char *type, char *path) |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 8349 | { |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 8350 | lua_getglobal(L, "package"); /* push package variable */ |
| 8351 | lua_pushstring(L, path); /* push given path */ |
| 8352 | lua_pushstring(L, ";"); /* push semicolon */ |
| 8353 | lua_getfield(L, -3, type); /* push old path */ |
| 8354 | lua_concat(L, 3); /* concatenate to new path */ |
| 8355 | lua_setfield(L, -2, type); /* store new path */ |
| 8356 | lua_pop(L, 1); /* pop package variable */ |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 8357 | |
| 8358 | return 0; |
| 8359 | } |
| 8360 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8361 | static int hlua_config_prepend_path(char **args, int section_type, struct proxy *curpx, |
| 8362 | struct proxy *defpx, const char *file, int line, |
| 8363 | char **err) |
| 8364 | { |
| 8365 | char *path; |
| 8366 | char *type = "path"; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8367 | struct prepend_path *p = NULL; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8368 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8369 | if (too_many_args(2, args, err, NULL)) { |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8370 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8371 | } |
| 8372 | |
| 8373 | if (!(*args[1])) { |
| 8374 | memprintf(err, "'%s' expects to receive a <path> as argument", args[0]); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8375 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8376 | } |
| 8377 | path = args[1]; |
| 8378 | |
| 8379 | if (*args[2]) { |
| 8380 | if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) { |
| 8381 | 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] | 8382 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8383 | } |
| 8384 | type = args[2]; |
| 8385 | } |
| 8386 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8387 | p = calloc(1, sizeof(*p)); |
| 8388 | if (p == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 8389 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8390 | goto err; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8391 | } |
| 8392 | p->path = strdup(path); |
| 8393 | if (p->path == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 8394 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8395 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8396 | } |
| 8397 | p->type = strdup(type); |
| 8398 | if (p->type == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 8399 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8400 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8401 | } |
| 8402 | LIST_ADDQ(&prepend_path_list, &p->l); |
| 8403 | |
| 8404 | hlua_prepend_path(hlua_states[0], type, path); |
| 8405 | hlua_prepend_path(hlua_states[1], type, path); |
| 8406 | return 0; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8407 | |
| 8408 | err2: |
| 8409 | free(p->type); |
| 8410 | free(p->path); |
| 8411 | err: |
| 8412 | free(p); |
| 8413 | return -1; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8414 | } |
| 8415 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8416 | /* configuration keywords declaration */ |
| 8417 | static struct cfg_kw_list cfg_kws = {{ },{ |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8418 | { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8419 | { CFG_GLOBAL, "lua-load", hlua_load }, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8420 | { CFG_GLOBAL, "lua-load-per-thread", hlua_load_per_thread }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8421 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 8422 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 8423 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8424 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8425 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8426 | { 0, NULL, NULL }, |
| 8427 | }}; |
| 8428 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 8429 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 8430 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 8431 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8432 | /* This function can fail with an abort() due to an Lua critical error. |
| 8433 | * We are in the initialisation process of HAProxy, this abort() is |
| 8434 | * tolerated. |
| 8435 | */ |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8436 | int hlua_post_init_state(lua_State *L) |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8437 | { |
| 8438 | struct hlua_init_function *init; |
| 8439 | const char *msg; |
| 8440 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8441 | const char *error; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8442 | const char *kind; |
| 8443 | const char *trace; |
| 8444 | int return_status = 1; |
| 8445 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 8446 | int nres; |
| 8447 | #endif |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8448 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8449 | /* disable memory limit checks if limit is not set */ |
| 8450 | if (!hlua_global_allocator.limit) |
| 8451 | hlua_global_allocator.limit = ~hlua_global_allocator.limit; |
| 8452 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 8453 | /* Call post initialisation function in safe environment. */ |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8454 | if (setjmp(safe_ljmp_env) != 0) { |
| 8455 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8456 | if (lua_type(L, -1) == LUA_TSTRING) |
| 8457 | error = lua_tostring(L, -1); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8458 | else |
| 8459 | error = "critical error"; |
| 8460 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 8461 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8462 | } else { |
| 8463 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8464 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 8465 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8466 | hlua_fcn_post_init(L); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8467 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8468 | list_for_each_entry(init, &hlua_init_functions[hlua_state_id], l) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8469 | lua_rawgeti(L, LUA_REGISTRYINDEX, init->function_ref); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8470 | |
| 8471 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8472 | ret = lua_resume(L, L, 0, &nres); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8473 | #else |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8474 | ret = lua_resume(L, L, 0); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8475 | #endif |
| 8476 | kind = NULL; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8477 | switch (ret) { |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8478 | |
| 8479 | case LUA_OK: |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8480 | lua_pop(L, -1); |
Thierry Fournier | 13d08b7 | 2020-11-28 11:02:58 +0100 | [diff] [blame] | 8481 | break; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8482 | |
| 8483 | case LUA_ERRERR: |
| 8484 | kind = "message handler error"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8485 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8486 | case LUA_ERRRUN: |
| 8487 | if (!kind) |
| 8488 | kind = "runtime error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8489 | msg = lua_tostring(L, -1); |
| 8490 | lua_settop(L, 0); /* Empty the stack. */ |
| 8491 | lua_pop(L, 1); |
| 8492 | trace = hlua_traceback(L); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8493 | if (msg) |
| 8494 | ha_alert("Lua init: %s: '%s' from %s\n", kind, msg, trace); |
| 8495 | else |
| 8496 | ha_alert("Lua init: unknown %s from %s\n", kind, trace); |
| 8497 | return_status = 0; |
| 8498 | break; |
| 8499 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8500 | default: |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8501 | /* Unknown error */ |
| 8502 | kind = "Unknown error"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8503 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8504 | case LUA_YIELD: |
| 8505 | /* yield is not configured at this step, this state doesn't happen */ |
| 8506 | if (!kind) |
| 8507 | kind = "yield not allowed"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8508 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8509 | case LUA_ERRMEM: |
| 8510 | if (!kind) |
| 8511 | kind = "out of memory error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8512 | lua_settop(L, 0); |
| 8513 | lua_pop(L, 1); |
| 8514 | trace = hlua_traceback(L); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8515 | ha_alert("Lua init: %s: %s\n", kind, trace); |
| 8516 | return_status = 0; |
| 8517 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8518 | } |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8519 | if (!return_status) |
| 8520 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8521 | } |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8522 | |
| 8523 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8524 | return return_status; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8525 | } |
| 8526 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8527 | int hlua_post_init() |
| 8528 | { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8529 | int ret; |
| 8530 | int i; |
| 8531 | int errors; |
| 8532 | char *err = NULL; |
| 8533 | struct hlua_function *fcn; |
| 8534 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8535 | #if USE_OPENSSL |
| 8536 | /* Initialize SSL server. */ |
| 8537 | if (socket_ssl.xprt->prepare_srv) { |
| 8538 | int saved_used_backed = global.ssl_used_backend; |
| 8539 | // don't affect maxconn automatic computation |
| 8540 | socket_ssl.xprt->prepare_srv(&socket_ssl); |
| 8541 | global.ssl_used_backend = saved_used_backed; |
| 8542 | } |
| 8543 | #endif |
| 8544 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8545 | /* Perform post init of common thread */ |
| 8546 | hlua_state_id = 0; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8547 | ha_set_tid(0); |
| 8548 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 8549 | if (ret == 0) |
| 8550 | return 0; |
| 8551 | |
| 8552 | /* init remaining lua states and load files */ |
| 8553 | for (hlua_state_id = 2; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 8554 | |
| 8555 | /* set thread context */ |
| 8556 | ha_set_tid(hlua_state_id - 1); |
| 8557 | |
| 8558 | /* Init lua state */ |
| 8559 | hlua_states[hlua_state_id] = hlua_init_state(hlua_state_id); |
| 8560 | |
| 8561 | /* Load lua files */ |
| 8562 | for (i = 0; per_thread_load && per_thread_load[i]; i++) { |
| 8563 | ret = hlua_load_state(per_thread_load[i], hlua_states[hlua_state_id], &err); |
| 8564 | if (ret != 0) { |
| 8565 | ha_alert("Lua init: %s\n", err); |
| 8566 | return 0; |
| 8567 | } |
| 8568 | } |
| 8569 | } |
| 8570 | |
| 8571 | /* Reset thread context */ |
| 8572 | ha_set_tid(0); |
| 8573 | |
| 8574 | /* Execute post init for all states */ |
| 8575 | for (hlua_state_id = 1; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 8576 | |
| 8577 | /* set thread context */ |
| 8578 | ha_set_tid(hlua_state_id - 1); |
| 8579 | |
| 8580 | /* run post init */ |
| 8581 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 8582 | if (ret == 0) |
| 8583 | return 0; |
| 8584 | } |
| 8585 | |
| 8586 | /* Reset thread context */ |
| 8587 | ha_set_tid(0); |
| 8588 | |
| 8589 | /* control functions registering. Each function must have: |
| 8590 | * - only the function_ref[0] set positive and all other to -1 |
| 8591 | * - only the function_ref[0] set to -1 and all other positive |
| 8592 | * This ensure a same reference is not used both in shared |
| 8593 | * lua state and thread dedicated lua state. Note: is the case |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 8594 | * reach, the shared state is priority, but the bug will be |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8595 | * complicated to found for the end user. |
| 8596 | */ |
| 8597 | errors = 0; |
| 8598 | list_for_each_entry(fcn, &referenced_functions, l) { |
| 8599 | ret = 0; |
| 8600 | for (i = 1; i < global.nbthread + 1; i++) { |
| 8601 | if (fcn->function_ref[i] == -1) |
| 8602 | ret--; |
| 8603 | else |
| 8604 | ret++; |
| 8605 | } |
| 8606 | if (abs(ret) != global.nbthread) { |
| 8607 | ha_alert("Lua function '%s' is not referenced in all thread. " |
| 8608 | "Expect function in all thread or in none thread.\n", fcn->name); |
| 8609 | errors++; |
| 8610 | continue; |
| 8611 | } |
| 8612 | |
| 8613 | if ((fcn->function_ref[0] == -1) == (ret < 0)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 8614 | ha_alert("Lua function '%s' is referenced both ins shared Lua context (through lua-load) " |
| 8615 | "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] | 8616 | "exclusive.\n", fcn->name); |
| 8617 | errors++; |
| 8618 | } |
| 8619 | } |
| 8620 | |
| 8621 | if (errors > 0) |
| 8622 | return 0; |
| 8623 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 8624 | /* 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] | 8625 | * -1 in order to have probably a segfault if someone use it |
| 8626 | */ |
| 8627 | hlua_state_id = -1; |
| 8628 | |
| 8629 | return 1; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8630 | } |
| 8631 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8632 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 8633 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 8634 | * 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] | 8635 | * allocation. <nsize> is the requested new size. A new allocation is |
| 8636 | * indicated by <ptr> being NULL. A free is indicated by <nsize> being |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8637 | * zero. This one verifies that the limits are respected but is optimized |
| 8638 | * 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] | 8639 | */ |
| 8640 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 8641 | { |
| 8642 | struct hlua_mem_allocator *zone = ud; |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8643 | size_t limit, old, new; |
| 8644 | |
Tim Duesterhus | 2258652 | 2021-01-08 10:35:33 +0100 | [diff] [blame] | 8645 | if (unlikely(!ptr && !nsize)) |
| 8646 | return NULL; |
| 8647 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8648 | /* a limit of ~0 means unlimited and boot complete, so there's no need |
| 8649 | * for accounting anymore. |
| 8650 | */ |
| 8651 | if (likely(~zone->limit == 0)) |
| 8652 | return realloc(ptr, nsize); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8653 | |
Willy Tarreau | d36c7fa | 2020-12-02 12:26:29 +0100 | [diff] [blame] | 8654 | if (!ptr) |
| 8655 | osize = 0; |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8656 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8657 | /* enforce strict limits across all threads */ |
| 8658 | limit = zone->limit; |
| 8659 | old = _HA_ATOMIC_LOAD(&zone->allocated); |
| 8660 | do { |
| 8661 | new = old + nsize - osize; |
| 8662 | if (unlikely(nsize && limit && new > limit)) |
| 8663 | return NULL; |
| 8664 | } while (!_HA_ATOMIC_CAS(&zone->allocated, &old, new)); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8665 | |
| 8666 | ptr = realloc(ptr, nsize); |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8667 | |
| 8668 | if (unlikely(!ptr && nsize)) // failed |
| 8669 | _HA_ATOMIC_SUB(&zone->allocated, nsize - osize); |
| 8670 | |
| 8671 | __ha_barrier_atomic_store(); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8672 | return ptr; |
| 8673 | } |
| 8674 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 8675 | /* 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] | 8676 | * We are in the initialisation process of HAProxy, this abort() is |
| 8677 | * tolerated. |
| 8678 | */ |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 8679 | lua_State *hlua_init_state(int thread_num) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8680 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8681 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8682 | int idx; |
| 8683 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8684 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8685 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8686 | const char *error_msg; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8687 | void **context; |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8688 | lua_State *L; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8689 | struct prepend_path *pp; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8690 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8691 | /* Init main lua stack. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8692 | L = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8693 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8694 | /* Initialise Lua context to NULL */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8695 | context = lua_getextraspace(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8696 | *context = NULL; |
| 8697 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8698 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8699 | * the Lua function can fail with an abort. We are in the initialisation |
| 8700 | * process of HAProxy, this abort() is tolerated. |
| 8701 | */ |
| 8702 | |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8703 | /* Call post initialisation function in safe environment. */ |
| 8704 | if (setjmp(safe_ljmp_env) != 0) { |
| 8705 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8706 | if (lua_type(L, -1) == LUA_TSTRING) |
| 8707 | error_msg = lua_tostring(L, -1); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 8708 | else |
| 8709 | error_msg = "critical error"; |
| 8710 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
| 8711 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8712 | } else { |
| 8713 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 8714 | } |
| 8715 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8716 | /* Initialise lua. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8717 | luaL_openlibs(L); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 8718 | #define HLUA_PREPEND_PATH_TOSTRING1(x) #x |
| 8719 | #define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x) |
| 8720 | #ifdef HLUA_PREPEND_PATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8721 | hlua_prepend_path(L, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 8722 | #endif |
| 8723 | #ifdef HLUA_PREPEND_CPATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8724 | hlua_prepend_path(L, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 8725 | #endif |
| 8726 | #undef HLUA_PREPEND_PATH_TOSTRING |
| 8727 | #undef HLUA_PREPEND_PATH_TOSTRING1 |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8728 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8729 | /* Apply configured prepend path */ |
| 8730 | list_for_each_entry(pp, &prepend_path_list, l) |
| 8731 | hlua_prepend_path(L, pp->type, pp->path); |
| 8732 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8733 | /* |
| 8734 | * |
| 8735 | * Create "core" object. |
| 8736 | * |
| 8737 | */ |
| 8738 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 8739 | /* This table entry is the object "core" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8740 | lua_newtable(L); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8741 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 8742 | /* set the thread id */ |
| 8743 | hlua_class_const_int(L, "thread", thread_num); |
| 8744 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8745 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8746 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8747 | hlua_class_const_int(L, log_levels[i], i); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8748 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8749 | /* Register special functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8750 | hlua_class_function(L, "register_init", hlua_register_init); |
| 8751 | hlua_class_function(L, "register_task", hlua_register_task); |
| 8752 | hlua_class_function(L, "register_fetches", hlua_register_fetches); |
| 8753 | hlua_class_function(L, "register_converters", hlua_register_converters); |
| 8754 | hlua_class_function(L, "register_action", hlua_register_action); |
| 8755 | hlua_class_function(L, "register_service", hlua_register_service); |
| 8756 | hlua_class_function(L, "register_cli", hlua_register_cli); |
| 8757 | hlua_class_function(L, "yield", hlua_yield); |
| 8758 | hlua_class_function(L, "set_nice", hlua_set_nice); |
| 8759 | hlua_class_function(L, "sleep", hlua_sleep); |
| 8760 | hlua_class_function(L, "msleep", hlua_msleep); |
| 8761 | hlua_class_function(L, "add_acl", hlua_add_acl); |
| 8762 | hlua_class_function(L, "del_acl", hlua_del_acl); |
| 8763 | hlua_class_function(L, "set_map", hlua_set_map); |
| 8764 | hlua_class_function(L, "del_map", hlua_del_map); |
| 8765 | hlua_class_function(L, "tcp", hlua_socket_new); |
| 8766 | hlua_class_function(L, "log", hlua_log); |
| 8767 | hlua_class_function(L, "Debug", hlua_log_debug); |
| 8768 | hlua_class_function(L, "Info", hlua_log_info); |
| 8769 | hlua_class_function(L, "Warning", hlua_log_warning); |
| 8770 | hlua_class_function(L, "Alert", hlua_log_alert); |
| 8771 | hlua_class_function(L, "done", hlua_done); |
| 8772 | hlua_fcn_reg_core_fcn(L); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8773 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8774 | lua_setglobal(L, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8775 | |
| 8776 | /* |
| 8777 | * |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8778 | * Create "act" object. |
| 8779 | * |
| 8780 | */ |
| 8781 | |
| 8782 | /* This table entry is the object "act" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8783 | lua_newtable(L); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8784 | |
| 8785 | /* push action return constants */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8786 | hlua_class_const_int(L, "CONTINUE", ACT_RET_CONT); |
| 8787 | hlua_class_const_int(L, "STOP", ACT_RET_STOP); |
| 8788 | hlua_class_const_int(L, "YIELD", ACT_RET_YIELD); |
| 8789 | hlua_class_const_int(L, "ERROR", ACT_RET_ERR); |
| 8790 | hlua_class_const_int(L, "DONE", ACT_RET_DONE); |
| 8791 | hlua_class_const_int(L, "DENY", ACT_RET_DENY); |
| 8792 | hlua_class_const_int(L, "ABORT", ACT_RET_ABRT); |
| 8793 | hlua_class_const_int(L, "INVALID", ACT_RET_INV); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8794 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8795 | hlua_class_function(L, "wake_time", hlua_set_wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8796 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8797 | lua_setglobal(L, "act"); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8798 | |
| 8799 | /* |
| 8800 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8801 | * Register class Map |
| 8802 | * |
| 8803 | */ |
| 8804 | |
| 8805 | /* This table entry is the object "Map" base. */ |
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 | |
| 8808 | /* register pattern types. */ |
| 8809 | for (i=0; i<PAT_MATCH_NUM; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8810 | hlua_class_const_int(L, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8811 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8812 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8813 | hlua_class_const_int(L, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8814 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8815 | |
| 8816 | /* register constructor. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8817 | hlua_class_function(L, "new", hlua_map_new); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8818 | |
| 8819 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8820 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8821 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8822 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8823 | lua_pushstring(L, "__index"); |
| 8824 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8825 | |
| 8826 | /* Register . */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8827 | hlua_class_function(L, "lookup", hlua_map_lookup); |
| 8828 | hlua_class_function(L, "slookup", hlua_map_slookup); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8829 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8830 | lua_rawset(L, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8831 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8832 | /* Register previous table in the registry with reference and named entry. |
| 8833 | * The function hlua_register_metatable() pops the stack, so we |
| 8834 | * previously create a copy of the table. |
| 8835 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8836 | lua_pushvalue(L, -1); /* Copy the -1 entry and push it on the stack. */ |
| 8837 | class_map_ref = hlua_register_metatable(L, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8838 | |
| 8839 | /* Assign the metatable to the mai Map object. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8840 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8841 | |
| 8842 | /* Set a name to the table. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8843 | lua_setglobal(L, "Map"); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8844 | |
| 8845 | /* |
| 8846 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8847 | * Register class Channel |
| 8848 | * |
| 8849 | */ |
| 8850 | |
| 8851 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8852 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8853 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8854 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8855 | lua_pushstring(L, "__index"); |
| 8856 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8857 | |
| 8858 | /* Register . */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8859 | hlua_class_function(L, "get", hlua_channel_get); |
| 8860 | hlua_class_function(L, "dup", hlua_channel_dup); |
| 8861 | hlua_class_function(L, "getline", hlua_channel_getline); |
| 8862 | hlua_class_function(L, "set", hlua_channel_set); |
| 8863 | hlua_class_function(L, "append", hlua_channel_append); |
| 8864 | hlua_class_function(L, "send", hlua_channel_send); |
| 8865 | hlua_class_function(L, "forward", hlua_channel_forward); |
| 8866 | hlua_class_function(L, "get_in_len", hlua_channel_get_in_len); |
| 8867 | hlua_class_function(L, "get_out_len", hlua_channel_get_out_len); |
| 8868 | hlua_class_function(L, "is_full", hlua_channel_is_full); |
| 8869 | hlua_class_function(L, "is_resp", hlua_channel_is_resp); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8870 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8871 | lua_rawset(L, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8872 | |
| 8873 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8874 | class_channel_ref = hlua_register_metatable(L, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8875 | |
| 8876 | /* |
| 8877 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8878 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8879 | * |
| 8880 | */ |
| 8881 | |
| 8882 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8883 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8884 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8885 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8886 | lua_pushstring(L, "__index"); |
| 8887 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8888 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8889 | /* Browse existing fetches and create the associated |
| 8890 | * object method. |
| 8891 | */ |
| 8892 | sf = NULL; |
| 8893 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8894 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 8895 | * by an underscore. |
| 8896 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8897 | strncpy(trash.area, sf->kw, trash.size); |
| 8898 | trash.area[trash.size - 1] = '\0'; |
| 8899 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8900 | if (*p == '.' || *p == '-' || *p == '+') |
| 8901 | *p = '_'; |
| 8902 | |
| 8903 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8904 | lua_pushstring(L, trash.area); |
| 8905 | lua_pushlightuserdata(L, sf); |
| 8906 | lua_pushcclosure(L, hlua_run_sample_fetch, 1); |
| 8907 | lua_rawset(L, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8908 | } |
| 8909 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8910 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8911 | |
| 8912 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8913 | class_fetches_ref = hlua_register_metatable(L, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8914 | |
| 8915 | /* |
| 8916 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8917 | * Register class Converters |
| 8918 | * |
| 8919 | */ |
| 8920 | |
| 8921 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8922 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8923 | |
| 8924 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8925 | lua_pushstring(L, "__index"); |
| 8926 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8927 | |
| 8928 | /* Browse existing converters and create the associated |
| 8929 | * object method. |
| 8930 | */ |
| 8931 | sc = NULL; |
| 8932 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8933 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 8934 | * by an underscore. |
| 8935 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8936 | strncpy(trash.area, sc->kw, trash.size); |
| 8937 | trash.area[trash.size - 1] = '\0'; |
| 8938 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8939 | if (*p == '.' || *p == '-' || *p == '+') |
| 8940 | *p = '_'; |
| 8941 | |
| 8942 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8943 | lua_pushstring(L, trash.area); |
| 8944 | lua_pushlightuserdata(L, sc); |
| 8945 | lua_pushcclosure(L, hlua_run_sample_conv, 1); |
| 8946 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8947 | } |
| 8948 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8949 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8950 | |
| 8951 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8952 | class_converters_ref = hlua_register_metatable(L, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8953 | |
| 8954 | /* |
| 8955 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8956 | * Register class HTTP |
| 8957 | * |
| 8958 | */ |
| 8959 | |
| 8960 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8961 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8962 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8963 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8964 | lua_pushstring(L, "__index"); |
| 8965 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8966 | |
| 8967 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8968 | hlua_class_function(L, "req_get_headers",hlua_http_req_get_headers); |
| 8969 | hlua_class_function(L, "req_del_header", hlua_http_req_del_hdr); |
| 8970 | hlua_class_function(L, "req_rep_header", hlua_http_req_rep_hdr); |
| 8971 | hlua_class_function(L, "req_rep_value", hlua_http_req_rep_val); |
| 8972 | hlua_class_function(L, "req_add_header", hlua_http_req_add_hdr); |
| 8973 | hlua_class_function(L, "req_set_header", hlua_http_req_set_hdr); |
| 8974 | hlua_class_function(L, "req_set_method", hlua_http_req_set_meth); |
| 8975 | hlua_class_function(L, "req_set_path", hlua_http_req_set_path); |
| 8976 | hlua_class_function(L, "req_set_query", hlua_http_req_set_query); |
| 8977 | hlua_class_function(L, "req_set_uri", hlua_http_req_set_uri); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8978 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8979 | hlua_class_function(L, "res_get_headers",hlua_http_res_get_headers); |
| 8980 | hlua_class_function(L, "res_del_header", hlua_http_res_del_hdr); |
| 8981 | hlua_class_function(L, "res_rep_header", hlua_http_res_rep_hdr); |
| 8982 | hlua_class_function(L, "res_rep_value", hlua_http_res_rep_val); |
| 8983 | hlua_class_function(L, "res_add_header", hlua_http_res_add_hdr); |
| 8984 | hlua_class_function(L, "res_set_header", hlua_http_res_set_hdr); |
| 8985 | hlua_class_function(L, "res_set_status", hlua_http_res_set_status); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8986 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8987 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8988 | |
| 8989 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8990 | class_http_ref = hlua_register_metatable(L, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8991 | |
| 8992 | /* |
| 8993 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8994 | * Register class AppletTCP |
| 8995 | * |
| 8996 | */ |
| 8997 | |
| 8998 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8999 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9000 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9001 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9002 | lua_pushstring(L, "__index"); |
| 9003 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9004 | |
| 9005 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9006 | hlua_class_function(L, "getline", hlua_applet_tcp_getline); |
| 9007 | hlua_class_function(L, "receive", hlua_applet_tcp_recv); |
| 9008 | hlua_class_function(L, "send", hlua_applet_tcp_send); |
| 9009 | hlua_class_function(L, "set_priv", hlua_applet_tcp_set_priv); |
| 9010 | hlua_class_function(L, "get_priv", hlua_applet_tcp_get_priv); |
| 9011 | hlua_class_function(L, "set_var", hlua_applet_tcp_set_var); |
| 9012 | hlua_class_function(L, "unset_var", hlua_applet_tcp_unset_var); |
| 9013 | hlua_class_function(L, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9014 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9015 | lua_settable(L, -3); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9016 | |
| 9017 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9018 | class_applet_tcp_ref = hlua_register_metatable(L, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9019 | |
| 9020 | /* |
| 9021 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9022 | * Register class AppletHTTP |
| 9023 | * |
| 9024 | */ |
| 9025 | |
| 9026 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9027 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9028 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9029 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9030 | lua_pushstring(L, "__index"); |
| 9031 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9032 | |
| 9033 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9034 | hlua_class_function(L, "set_priv", hlua_applet_http_set_priv); |
| 9035 | hlua_class_function(L, "get_priv", hlua_applet_http_get_priv); |
| 9036 | hlua_class_function(L, "set_var", hlua_applet_http_set_var); |
| 9037 | hlua_class_function(L, "unset_var", hlua_applet_http_unset_var); |
| 9038 | hlua_class_function(L, "get_var", hlua_applet_http_get_var); |
| 9039 | hlua_class_function(L, "getline", hlua_applet_http_getline); |
| 9040 | hlua_class_function(L, "receive", hlua_applet_http_recv); |
| 9041 | hlua_class_function(L, "send", hlua_applet_http_send); |
| 9042 | hlua_class_function(L, "add_header", hlua_applet_http_addheader); |
| 9043 | hlua_class_function(L, "set_status", hlua_applet_http_status); |
| 9044 | hlua_class_function(L, "start_response", hlua_applet_http_start_response); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9045 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9046 | lua_settable(L, -3); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9047 | |
| 9048 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9049 | class_applet_http_ref = hlua_register_metatable(L, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9050 | |
| 9051 | /* |
| 9052 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9053 | * Register class TXN |
| 9054 | * |
| 9055 | */ |
| 9056 | |
| 9057 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9058 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9059 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9060 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9061 | lua_pushstring(L, "__index"); |
| 9062 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9063 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 9064 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9065 | hlua_class_function(L, "set_priv", hlua_set_priv); |
| 9066 | hlua_class_function(L, "get_priv", hlua_get_priv); |
| 9067 | hlua_class_function(L, "set_var", hlua_set_var); |
| 9068 | hlua_class_function(L, "unset_var", hlua_unset_var); |
| 9069 | hlua_class_function(L, "get_var", hlua_get_var); |
| 9070 | hlua_class_function(L, "done", hlua_txn_done); |
| 9071 | hlua_class_function(L, "reply", hlua_txn_reply_new); |
| 9072 | hlua_class_function(L, "set_loglevel", hlua_txn_set_loglevel); |
| 9073 | hlua_class_function(L, "set_tos", hlua_txn_set_tos); |
| 9074 | hlua_class_function(L, "set_mark", hlua_txn_set_mark); |
| 9075 | hlua_class_function(L, "set_priority_class", hlua_txn_set_priority_class); |
| 9076 | hlua_class_function(L, "set_priority_offset", hlua_txn_set_priority_offset); |
| 9077 | hlua_class_function(L, "deflog", hlua_txn_deflog); |
| 9078 | hlua_class_function(L, "log", hlua_txn_log); |
| 9079 | hlua_class_function(L, "Debug", hlua_txn_log_debug); |
| 9080 | hlua_class_function(L, "Info", hlua_txn_log_info); |
| 9081 | hlua_class_function(L, "Warning", hlua_txn_log_warning); |
| 9082 | hlua_class_function(L, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 9083 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9084 | lua_rawset(L, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 9085 | |
| 9086 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9087 | class_txn_ref = hlua_register_metatable(L, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9088 | |
| 9089 | /* |
| 9090 | * |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 9091 | * Register class reply |
| 9092 | * |
| 9093 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9094 | lua_newtable(L); |
| 9095 | lua_pushstring(L, "__index"); |
| 9096 | lua_newtable(L); |
| 9097 | hlua_class_function(L, "set_status", hlua_txn_reply_set_status); |
| 9098 | hlua_class_function(L, "add_header", hlua_txn_reply_add_header); |
| 9099 | hlua_class_function(L, "del_header", hlua_txn_reply_del_header); |
| 9100 | hlua_class_function(L, "set_body", hlua_txn_reply_set_body); |
| 9101 | lua_settable(L, -3); /* Sets the __index entry. */ |
| 9102 | class_txn_reply_ref = luaL_ref(L, LUA_REGISTRYINDEX); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 9103 | |
| 9104 | |
| 9105 | /* |
| 9106 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9107 | * Register class Socket |
| 9108 | * |
| 9109 | */ |
| 9110 | |
| 9111 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9112 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9113 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9114 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9115 | lua_pushstring(L, "__index"); |
| 9116 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9117 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 9118 | #ifdef USE_OPENSSL |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9119 | hlua_class_function(L, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 9120 | #endif |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9121 | hlua_class_function(L, "connect", hlua_socket_connect); |
| 9122 | hlua_class_function(L, "send", hlua_socket_send); |
| 9123 | hlua_class_function(L, "receive", hlua_socket_receive); |
| 9124 | hlua_class_function(L, "close", hlua_socket_close); |
| 9125 | hlua_class_function(L, "getpeername", hlua_socket_getpeername); |
| 9126 | hlua_class_function(L, "getsockname", hlua_socket_getsockname); |
| 9127 | hlua_class_function(L, "setoption", hlua_socket_setoption); |
| 9128 | hlua_class_function(L, "settimeout", hlua_socket_settimeout); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9129 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9130 | 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] | 9131 | |
| 9132 | /* Register the garbage collector entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9133 | lua_pushstring(L, "__gc"); |
| 9134 | lua_pushcclosure(L, hlua_socket_gc, 0); |
| 9135 | 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] | 9136 | |
| 9137 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9138 | class_socket_ref = hlua_register_metatable(L, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9139 | |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 9140 | lua_atpanic(L, hlua_panic_safe); |
| 9141 | |
| 9142 | return L; |
| 9143 | } |
| 9144 | |
| 9145 | void hlua_init(void) { |
| 9146 | int i; |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 9147 | #ifdef USE_OPENSSL |
| 9148 | struct srv_kw *kw; |
| 9149 | int tmp_error; |
| 9150 | char *error; |
| 9151 | char *args[] = { /* SSL client configuration. */ |
| 9152 | "ssl", |
| 9153 | "verify", |
| 9154 | "none", |
| 9155 | NULL |
| 9156 | }; |
| 9157 | #endif |
| 9158 | |
| 9159 | /* Init post init function list head */ |
| 9160 | for (i = 0; i < MAX_THREADS + 1; i++) |
| 9161 | LIST_INIT(&hlua_init_functions[i]); |
| 9162 | |
| 9163 | /* Init state for common/shared lua parts */ |
| 9164 | hlua_state_id = 0; |
| 9165 | ha_set_tid(0); |
| 9166 | hlua_states[0] = hlua_init_state(0); |
| 9167 | |
| 9168 | /* Init state 1 for thread 0. We have at least one thread. */ |
| 9169 | hlua_state_id = 1; |
| 9170 | ha_set_tid(0); |
| 9171 | hlua_states[1] = hlua_init_state(1); |
| 9172 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9173 | /* Proxy and server configuration initialisation. */ |
| 9174 | memset(&socket_proxy, 0, sizeof(socket_proxy)); |
| 9175 | init_new_proxy(&socket_proxy); |
| 9176 | socket_proxy.parent = NULL; |
| 9177 | socket_proxy.last_change = now.tv_sec; |
| 9178 | socket_proxy.id = "LUA-SOCKET"; |
| 9179 | socket_proxy.cap = PR_CAP_FE | PR_CAP_BE; |
| 9180 | socket_proxy.maxconn = 0; |
| 9181 | socket_proxy.accept = NULL; |
| 9182 | socket_proxy.options2 |= PR_O2_INDEPSTR; |
| 9183 | socket_proxy.srv = NULL; |
| 9184 | socket_proxy.conn_retries = 0; |
| 9185 | socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */ |
| 9186 | |
| 9187 | /* Init TCP server: unchanged parameters */ |
| 9188 | memset(&socket_tcp, 0, sizeof(socket_tcp)); |
| 9189 | socket_tcp.next = NULL; |
| 9190 | socket_tcp.proxy = &socket_proxy; |
| 9191 | socket_tcp.obj_type = OBJ_TYPE_SERVER; |
| 9192 | LIST_INIT(&socket_tcp.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 9193 | socket_tcp.pendconns = EB_ROOT; |
Christopher Faulet | 40a007c | 2017-07-03 15:41:01 +0200 | [diff] [blame] | 9194 | socket_tcp.idle_conns = NULL; |
| 9195 | socket_tcp.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 9196 | socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9197 | socket_tcp.last_change = 0; |
| 9198 | socket_tcp.id = "LUA-TCP-CONN"; |
| 9199 | socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 9200 | socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 9201 | socket_tcp.pp_opts = 0; /* Remove proxy protocol. */ |
| 9202 | |
| 9203 | /* XXX: Copy default parameter from default server, |
| 9204 | * but the default server is not initialized. |
| 9205 | */ |
| 9206 | socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue; |
| 9207 | socket_tcp.minconn = socket_proxy.defsrv.minconn; |
| 9208 | socket_tcp.maxconn = socket_proxy.defsrv.maxconn; |
| 9209 | socket_tcp.slowstart = socket_proxy.defsrv.slowstart; |
| 9210 | socket_tcp.onerror = socket_proxy.defsrv.onerror; |
| 9211 | socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 9212 | socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 9213 | socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 9214 | socket_tcp.uweight = socket_proxy.defsrv.iweight; |
| 9215 | socket_tcp.iweight = socket_proxy.defsrv.iweight; |
| 9216 | |
| 9217 | socket_tcp.check.status = HCHK_STATUS_INI; |
| 9218 | socket_tcp.check.rise = socket_proxy.defsrv.check.rise; |
| 9219 | socket_tcp.check.fall = socket_proxy.defsrv.check.fall; |
| 9220 | socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */ |
| 9221 | socket_tcp.check.server = &socket_tcp; |
| 9222 | |
| 9223 | socket_tcp.agent.status = HCHK_STATUS_INI; |
| 9224 | socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise; |
| 9225 | socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall; |
| 9226 | socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */ |
| 9227 | socket_tcp.agent.server = &socket_tcp; |
| 9228 | |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 9229 | socket_tcp.xprt = xprt_get(XPRT_RAW); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9230 | |
| 9231 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9232 | /* Init TCP server: unchanged parameters */ |
| 9233 | memset(&socket_ssl, 0, sizeof(socket_ssl)); |
| 9234 | socket_ssl.next = NULL; |
| 9235 | socket_ssl.proxy = &socket_proxy; |
| 9236 | socket_ssl.obj_type = OBJ_TYPE_SERVER; |
| 9237 | LIST_INIT(&socket_ssl.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 9238 | socket_ssl.pendconns = EB_ROOT; |
Willy Tarreau | b784b35 | 2019-02-07 14:48:24 +0100 | [diff] [blame] | 9239 | socket_ssl.idle_conns = NULL; |
| 9240 | socket_ssl.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 9241 | socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9242 | socket_ssl.last_change = 0; |
| 9243 | socket_ssl.id = "LUA-SSL-CONN"; |
| 9244 | socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 9245 | socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 9246 | socket_ssl.pp_opts = 0; /* Remove proxy protocol. */ |
| 9247 | |
| 9248 | /* XXX: Copy default parameter from default server, |
| 9249 | * but the default server is not initialized. |
| 9250 | */ |
| 9251 | socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue; |
| 9252 | socket_ssl.minconn = socket_proxy.defsrv.minconn; |
| 9253 | socket_ssl.maxconn = socket_proxy.defsrv.maxconn; |
| 9254 | socket_ssl.slowstart = socket_proxy.defsrv.slowstart; |
| 9255 | socket_ssl.onerror = socket_proxy.defsrv.onerror; |
| 9256 | socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 9257 | socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 9258 | socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 9259 | socket_ssl.uweight = socket_proxy.defsrv.iweight; |
| 9260 | socket_ssl.iweight = socket_proxy.defsrv.iweight; |
| 9261 | |
| 9262 | socket_ssl.check.status = HCHK_STATUS_INI; |
| 9263 | socket_ssl.check.rise = socket_proxy.defsrv.check.rise; |
| 9264 | socket_ssl.check.fall = socket_proxy.defsrv.check.fall; |
| 9265 | socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */ |
| 9266 | socket_ssl.check.server = &socket_ssl; |
| 9267 | |
| 9268 | socket_ssl.agent.status = HCHK_STATUS_INI; |
| 9269 | socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise; |
| 9270 | socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall; |
| 9271 | socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */ |
| 9272 | socket_ssl.agent.server = &socket_ssl; |
| 9273 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 9274 | socket_ssl.use_ssl = 1; |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 9275 | socket_ssl.xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9276 | |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 9277 | for (i = 0; args[i] != NULL; i++) { |
| 9278 | 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] | 9279 | /* |
| 9280 | * |
| 9281 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9282 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9283 | * features like client certificates and ssl_verify. |
| 9284 | * |
| 9285 | */ |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 9286 | tmp_error = kw->parse(args, &i, &socket_proxy, &socket_ssl, &error); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9287 | if (tmp_error != 0) { |
| 9288 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 9289 | abort(); /* This must be never arrives because the command line |
| 9290 | not editable by the user. */ |
| 9291 | } |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 9292 | i += kw->skip; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9293 | } |
| 9294 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9295 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 9296 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 9297 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 9298 | |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 9299 | static void hlua_deinit() |
| 9300 | { |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 9301 | int thr; |
| 9302 | |
| 9303 | for (thr = 0; thr < MAX_THREADS+1; thr++) { |
| 9304 | if (hlua_states[thr]) |
| 9305 | lua_close(hlua_states[thr]); |
| 9306 | } |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 9307 | } |
| 9308 | |
| 9309 | REGISTER_POST_DEINIT(hlua_deinit); |
| 9310 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9311 | static void hlua_register_build_options(void) |
| 9312 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 9313 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9314 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 9315 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 9316 | hap_register_build_opts(ptr, 1); |
| 9317 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9318 | |
| 9319 | INITCALL0(STG_REGISTER, hlua_register_build_options); |