Thierry Fournier | e726b14 | 2016-02-11 17:57:57 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Lua unsafe core engine |
| 3 | * |
| 4 | * Copyright 2015-2016 Thierry Fournier <tfournier@arpalert.org> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
Bertrand Jacquin | f4c12d4 | 2021-01-21 21:14:07 +0000 | [diff] [blame] | 13 | #define _GNU_SOURCE |
| 14 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 15 | #include <ctype.h> |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 16 | #include <setjmp.h> |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 17 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 18 | #include <lauxlib.h> |
| 19 | #include <lua.h> |
| 20 | #include <lualib.h> |
| 21 | |
Thierry FOURNIER | 463119c | 2015-03-10 00:35:36 +0100 | [diff] [blame] | 22 | #if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 503 |
| 23 | #error "Requires Lua 5.3 or later." |
Cyril Bonté | dc0306e | 2015-03-02 00:08:40 +0100 | [diff] [blame] | 24 | #endif |
| 25 | |
Willy Tarreau | 8d2b777 | 2020-05-27 10:58:19 +0200 | [diff] [blame] | 26 | #include <import/ebpttree.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 27 | |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 28 | #include <haproxy/api.h> |
| 29 | #include <haproxy/applet.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 30 | #include <haproxy/arg.h> |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 31 | #include <haproxy/auth.h> |
Willy Tarreau | 6be7849 | 2020-06-05 00:00:29 +0200 | [diff] [blame] | 32 | #include <haproxy/cfgparse.h> |
Willy Tarreau | f1d32c4 | 2020-06-04 21:07:02 +0200 | [diff] [blame] | 33 | #include <haproxy/channel.h> |
Willy Tarreau | 83487a8 | 2020-06-04 20:19:54 +0200 | [diff] [blame] | 34 | #include <haproxy/cli.h> |
Willy Tarreau | 7ea393d | 2020-06-04 18:02:10 +0200 | [diff] [blame] | 35 | #include <haproxy/connection.h> |
Willy Tarreau | 5413a87 | 2020-06-02 19:33:08 +0200 | [diff] [blame] | 36 | #include <haproxy/h1.h> |
Willy Tarreau | 8641605 | 2020-06-04 09:20:54 +0200 | [diff] [blame] | 37 | #include <haproxy/hlua.h> |
Willy Tarreau | 8c79400 | 2020-06-04 10:05:25 +0200 | [diff] [blame] | 38 | #include <haproxy/hlua_fcn.h> |
Willy Tarreau | c2b1ff0 | 2020-06-04 21:21:03 +0200 | [diff] [blame] | 39 | #include <haproxy/http_ana.h> |
Willy Tarreau | 126ba3a | 2020-06-04 18:26:43 +0200 | [diff] [blame] | 40 | #include <haproxy/http_fetch.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 41 | #include <haproxy/http_htx.h> |
Willy Tarreau | c761f84 | 2020-06-04 11:40:28 +0200 | [diff] [blame] | 42 | #include <haproxy/http_rules.h> |
Willy Tarreau | 36979d9 | 2020-06-05 17:27:29 +0200 | [diff] [blame] | 43 | #include <haproxy/log.h> |
Willy Tarreau | 2cd5809 | 2020-06-04 15:10:43 +0200 | [diff] [blame] | 44 | #include <haproxy/map.h> |
Willy Tarreau | 8efbdfb | 2020-06-04 11:29:21 +0200 | [diff] [blame] | 45 | #include <haproxy/obj_type.h> |
Willy Tarreau | 225a90a | 2020-06-04 15:06:28 +0200 | [diff] [blame] | 46 | #include <haproxy/pattern.h> |
Willy Tarreau | 469509b | 2020-06-04 15:13:30 +0200 | [diff] [blame] | 47 | #include <haproxy/payload.h> |
Willy Tarreau | 3d6ee40 | 2021-05-08 20:28:07 +0200 | [diff] [blame] | 48 | #include <haproxy/proxy.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 49 | #include <haproxy/regex.h> |
Willy Tarreau | e6ce10b | 2020-06-04 15:33:47 +0200 | [diff] [blame] | 50 | #include <haproxy/sample.h> |
Willy Tarreau | 198e92a | 2021-03-05 10:23:32 +0100 | [diff] [blame] | 51 | #include <haproxy/server.h> |
Willy Tarreau | 48d25b3 | 2020-06-04 18:58:52 +0200 | [diff] [blame] | 52 | #include <haproxy/session.h> |
Willy Tarreau | 2eec9b5 | 2020-06-04 19:58:55 +0200 | [diff] [blame] | 53 | #include <haproxy/stats-t.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 54 | #include <haproxy/stream.h> |
Willy Tarreau | 5e539c9 | 2020-06-04 20:45:39 +0200 | [diff] [blame] | 55 | #include <haproxy/stream_interface.h> |
Willy Tarreau | cea0e1b | 2020-06-04 17:25:40 +0200 | [diff] [blame] | 56 | #include <haproxy/task.h> |
Willy Tarreau | 8b550af | 2020-06-04 17:42:48 +0200 | [diff] [blame] | 57 | #include <haproxy/tcp_rules.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 58 | #include <haproxy/thread.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 59 | #include <haproxy/tools.h> |
Willy Tarreau | a171892 | 2020-06-04 16:25:31 +0200 | [diff] [blame] | 60 | #include <haproxy/vars.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 61 | #include <haproxy/xref.h> |
| 62 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 63 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 64 | /* Lua uses longjmp to perform yield or throwing errors. This |
| 65 | * macro is used only for identifying the function that can |
| 66 | * not return because a longjmp is executed. |
| 67 | * __LJMP marks a prototype of hlua file that can use longjmp. |
| 68 | * WILL_LJMP() marks an lua function that will use longjmp. |
| 69 | * MAY_LJMP() marks an lua function that may use longjmp. |
| 70 | */ |
| 71 | #define __LJMP |
Willy Tarreau | 4e7cc33 | 2018-10-20 17:45:48 +0200 | [diff] [blame] | 72 | #define WILL_LJMP(func) do { func; my_unreachable(); } while(0) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 73 | #define MAY_LJMP(func) func |
| 74 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 75 | /* This couple of function executes securely some Lua calls outside of |
| 76 | * the lua runtime environment. Each Lua call can return a longjmp |
| 77 | * if it encounter a memory error. |
| 78 | * |
| 79 | * Lua documentation extract: |
| 80 | * |
| 81 | * If an error happens outside any protected environment, Lua calls |
| 82 | * a panic function (see lua_atpanic) and then calls abort, thus |
| 83 | * exiting the host application. Your panic function can avoid this |
| 84 | * exit by never returning (e.g., doing a long jump to your own |
| 85 | * recovery point outside Lua). |
| 86 | * |
| 87 | * The panic function runs as if it were a message handler (see |
| 88 | * §2.3); in particular, the error message is at the top of the |
| 89 | * stack. However, there is no guarantee about stack space. To push |
| 90 | * anything on the stack, the panic function must first check the |
| 91 | * available space (see §4.2). |
| 92 | * |
| 93 | * We must check all the Lua entry point. This includes: |
| 94 | * - The include/proto/hlua.h exported functions |
| 95 | * - the task wrapper function |
| 96 | * - The action wrapper function |
| 97 | * - The converters wrapper function |
| 98 | * - The sample-fetch wrapper functions |
| 99 | * |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 100 | * It is tolerated that the initialisation function returns an abort. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 101 | * Before each Lua abort, an error message is written on stderr. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 102 | * |
| 103 | * The macro SET_SAFE_LJMP initialise the longjmp. The Macro |
| 104 | * RESET_SAFE_LJMP reset the longjmp. These function must be macro |
| 105 | * because they must be exists in the program stack when the longjmp |
| 106 | * is called. |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 107 | * |
| 108 | * Note that the Lua processing is not really thread safe. It provides |
| 109 | * heavy system which consists to add our own lock function in the Lua |
| 110 | * code and recompile the library. This system will probably not accepted |
| 111 | * by maintainers of various distribs. |
| 112 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 113 | * Our main execution point of the Lua is the function lua_resume(). A |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 114 | * quick looking on the Lua sources displays a lua_lock() a the start |
| 115 | * of function and a lua_unlock() at the end of the function. So I |
| 116 | * conclude that the Lua thread safe mode just perform a mutex around |
| 117 | * all execution. So I prefer to do this in the HAProxy code, it will be |
| 118 | * easier for distro maintainers. |
| 119 | * |
| 120 | * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP |
| 121 | * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful |
| 122 | * to set mutex around these functions. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 123 | */ |
Willy Tarreau | 86abe44 | 2018-11-25 20:12:18 +0100 | [diff] [blame] | 124 | __decl_spinlock(hlua_global_lock); |
Thierry FOURNIER | ffbad79 | 2017-07-12 11:39:04 +0200 | [diff] [blame] | 125 | THREAD_LOCAL jmp_buf safe_ljmp_env; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 126 | static int hlua_panic_safe(lua_State *L) { return 0; } |
Willy Tarreau | 6a51090 | 2021-07-14 19:41:25 +0200 | [diff] [blame^] | 127 | static int hlua_panic_ljmp(lua_State *L) { WILL_LJMP(longjmp(safe_ljmp_env, 1)); return 0; } |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 128 | |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 129 | /* This is the chained list of struct hlua_function referenced |
| 130 | * for haproxy action, sample-fetches, converters, cli and |
| 131 | * applet bindings. It is used for a post-initialisation control. |
| 132 | */ |
| 133 | static struct list referenced_functions = LIST_HEAD_INIT(referenced_functions); |
| 134 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 135 | /* This variable is used only during initialization to identify the Lua state |
| 136 | * currently being initialized. 0 is the common lua state, 1 to n are the Lua |
| 137 | * states dedicated to each thread (in this case hlua_state_id==tid+1). |
| 138 | */ |
| 139 | static int hlua_state_id; |
| 140 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 141 | /* This is a NULL-terminated list of lua file which are referenced to load per thread */ |
| 142 | static char **per_thread_load = NULL; |
| 143 | |
| 144 | lua_State *hlua_init_state(int thread_id); |
| 145 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 146 | #define SET_SAFE_LJMP_L(__L, __HLUA) \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 147 | ({ \ |
| 148 | int ret; \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 149 | if ((__HLUA)->state_id == 0) \ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 150 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 151 | if (setjmp(safe_ljmp_env) != 0) { \ |
| 152 | lua_atpanic(__L, hlua_panic_safe); \ |
| 153 | ret = 0; \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 154 | if ((__HLUA)->state_id == 0) \ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 155 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 156 | } else { \ |
| 157 | lua_atpanic(__L, hlua_panic_ljmp); \ |
| 158 | ret = 1; \ |
| 159 | } \ |
| 160 | ret; \ |
| 161 | }) |
| 162 | |
| 163 | /* If we are the last function catching Lua errors, we |
| 164 | * must reset the panic function. |
| 165 | */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 166 | #define RESET_SAFE_LJMP_L(__L, __HLUA) \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 167 | do { \ |
| 168 | lua_atpanic(__L, hlua_panic_safe); \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 169 | if ((__HLUA)->state_id == 0) \ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 170 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 171 | } while(0) |
| 172 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 173 | #define SET_SAFE_LJMP(__HLUA) \ |
| 174 | SET_SAFE_LJMP_L((__HLUA)->T, __HLUA) |
| 175 | |
| 176 | #define RESET_SAFE_LJMP(__HLUA) \ |
| 177 | RESET_SAFE_LJMP_L((__HLUA)->T, __HLUA) |
| 178 | |
| 179 | #define SET_SAFE_LJMP_PARENT(__HLUA) \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 180 | SET_SAFE_LJMP_L(hlua_states[(__HLUA)->state_id], __HLUA) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 181 | |
| 182 | #define RESET_SAFE_LJMP_PARENT(__HLUA) \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 183 | RESET_SAFE_LJMP_L(hlua_states[(__HLUA)->state_id], __HLUA) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 184 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 185 | /* Applet status flags */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 186 | #define APPLET_DONE 0x01 /* applet processing is done. */ |
Christopher Faulet | 18c2e8d | 2019-03-01 12:02:08 +0100 | [diff] [blame] | 187 | /* unused: 0x02 */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 188 | #define APPLET_HDR_SENT 0x04 /* Response header sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 189 | /* unused: 0x08, 0x10 */ |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 190 | #define APPLET_HTTP11 0x20 /* Last chunk sent. */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 191 | #define APPLET_RSP_SENT 0x40 /* The response was fully sent */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 192 | |
Thierry Fournier | afc63e2 | 2020-11-28 17:06:51 +0100 | [diff] [blame] | 193 | /* The main Lua execution context. The 0 index is the |
| 194 | * common state shared by all threads. |
| 195 | */ |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 196 | static lua_State *hlua_states[MAX_THREADS + 1]; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 197 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 198 | /* This is the memory pool containing struct lua for applets |
| 199 | * (including cli). |
| 200 | */ |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 201 | DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua)); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 202 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 203 | /* Used for Socket connection. */ |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 204 | static struct proxy *socket_proxy; |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 205 | static struct server *socket_tcp; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 206 | #ifdef USE_OPENSSL |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 207 | static struct server *socket_ssl; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 208 | #endif |
| 209 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 210 | /* List head of the function called at the initialisation time. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 211 | struct list hlua_init_functions[MAX_THREADS + 1]; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 212 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 213 | /* The following variables contains the reference of the different |
| 214 | * Lua classes. These references are useful for identify metadata |
| 215 | * associated with an object. |
| 216 | */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 217 | static int class_txn_ref; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 218 | static int class_socket_ref; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 219 | static int class_channel_ref; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 220 | static int class_fetches_ref; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 221 | static int class_converters_ref; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 222 | static int class_http_ref; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 223 | static int class_map_ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 224 | static int class_applet_tcp_ref; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 225 | static int class_applet_http_ref; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 226 | static int class_txn_reply_ref; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 227 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 228 | /* Global Lua execution timeout. By default Lua, execution linked |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 229 | * with stream (actions, sample-fetches and converters) have a |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 230 | * short timeout. Lua linked with tasks doesn't have a timeout |
| 231 | * because a task may remain alive during all the haproxy execution. |
| 232 | */ |
| 233 | static unsigned int hlua_timeout_session = 4000; /* session timeout. */ |
| 234 | static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 235 | static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 236 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 237 | /* Interrupts the Lua processing each "hlua_nb_instruction" instructions. |
| 238 | * it is used for preventing infinite loops. |
| 239 | * |
| 240 | * I test the scheer with an infinite loop containing one incrementation |
| 241 | * and one test. I run this loop between 10 seconds, I raise a ceil of |
| 242 | * 710M loops from one interrupt each 9000 instructions, so I fix the value |
| 243 | * to one interrupt each 10 000 instructions. |
| 244 | * |
| 245 | * configured | Number of |
| 246 | * instructions | loops executed |
| 247 | * between two | in milions |
| 248 | * forced yields | |
| 249 | * ---------------+--------------- |
| 250 | * 10 | 160 |
| 251 | * 500 | 670 |
| 252 | * 1000 | 680 |
| 253 | * 5000 | 700 |
| 254 | * 7000 | 700 |
| 255 | * 8000 | 700 |
| 256 | * 9000 | 710 <- ceil |
| 257 | * 10000 | 710 |
| 258 | * 100000 | 710 |
| 259 | * 1000000 | 710 |
| 260 | * |
| 261 | */ |
| 262 | static unsigned int hlua_nb_instruction = 10000; |
| 263 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 264 | /* Descriptor for the memory allocation state. The limit is pre-initialised to |
| 265 | * 0 until it is replaced by "tune.lua.maxmem" during the config parsing, or it |
| 266 | * is replaced with ~0 during post_init after everything was loaded. This way |
| 267 | * it is guaranteed that if limit is ~0 the boot is complete and that if it's |
| 268 | * zero it's not yet limited and proper accounting is required. |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 269 | */ |
| 270 | struct hlua_mem_allocator { |
| 271 | size_t allocated; |
| 272 | size_t limit; |
| 273 | }; |
| 274 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 275 | static struct hlua_mem_allocator hlua_global_allocator THREAD_ALIGNED(64); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 276 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 277 | /* These functions converts types between HAProxy internal args or |
| 278 | * sample and LUA types. Another function permits to check if the |
| 279 | * LUA stack contains arguments according with an required ARG_T |
| 280 | * format. |
| 281 | */ |
| 282 | static int hlua_arg2lua(lua_State *L, const struct arg *arg); |
| 283 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 284 | __LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp, |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 285 | uint64_t mask, struct proxy *p); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 286 | static int hlua_smp2lua(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 287 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 288 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp); |
| 289 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 290 | __LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 291 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 292 | struct prepend_path { |
| 293 | struct list l; |
| 294 | char *type; |
| 295 | char *path; |
| 296 | }; |
| 297 | |
| 298 | static struct list prepend_path_list = LIST_HEAD_INIT(prepend_path_list); |
| 299 | |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 300 | #define SEND_ERR(__be, __fmt, __args...) \ |
| 301 | do { \ |
| 302 | send_log(__be, LOG_ERR, __fmt, ## __args); \ |
| 303 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 304 | ha_alert(__fmt, ## __args); \ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 305 | } while (0) |
| 306 | |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 307 | static inline struct hlua_function *new_hlua_function() |
| 308 | { |
| 309 | struct hlua_function *fcn; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 310 | int i; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 311 | |
| 312 | fcn = calloc(1, sizeof(*fcn)); |
| 313 | if (!fcn) |
| 314 | return NULL; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 315 | LIST_APPEND(&referenced_functions, &fcn->l); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 316 | for (i = 0; i < MAX_THREADS + 1; i++) |
| 317 | fcn->function_ref[i] = -1; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 318 | return fcn; |
| 319 | } |
| 320 | |
Christopher Faulet | dda4444 | 2021-04-12 14:05:43 +0200 | [diff] [blame] | 321 | static inline void release_hlua_function(struct hlua_function *fcn) |
| 322 | { |
| 323 | if (!fcn) |
| 324 | return; |
| 325 | if (fcn->name) |
| 326 | ha_free(&fcn->name); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 327 | LIST_DELETE(&fcn->l); |
Christopher Faulet | dda4444 | 2021-04-12 14:05:43 +0200 | [diff] [blame] | 328 | ha_free(&fcn); |
| 329 | } |
| 330 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 331 | /* If the common state is set, the stack id is 0, otherwise it is the tid + 1 */ |
| 332 | static inline int fcn_ref_to_stack_id(struct hlua_function *fcn) |
| 333 | { |
| 334 | if (fcn->function_ref[0] == -1) |
| 335 | return tid + 1; |
| 336 | return 0; |
| 337 | } |
| 338 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 339 | /* Used to check an Lua function type in the stack. It creates and |
| 340 | * returns a reference of the function. This function throws an |
| 341 | * error if the rgument is not a "function". |
| 342 | */ |
| 343 | __LJMP unsigned int hlua_checkfunction(lua_State *L, int argno) |
| 344 | { |
| 345 | if (!lua_isfunction(L, argno)) { |
Thierry FOURNIER | fd1e955 | 2018-02-23 18:41:18 +0100 | [diff] [blame] | 346 | const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, argno)); |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 347 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 348 | } |
| 349 | lua_pushvalue(L, argno); |
| 350 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 351 | } |
| 352 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 353 | /* Return the string that is of the top of the stack. */ |
| 354 | const char *hlua_get_top_error_string(lua_State *L) |
| 355 | { |
| 356 | if (lua_gettop(L) < 1) |
| 357 | return "unknown error"; |
| 358 | if (lua_type(L, -1) != LUA_TSTRING) |
| 359 | return "unknown error"; |
| 360 | return lua_tostring(L, -1); |
| 361 | } |
| 362 | |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 363 | __LJMP const char *hlua_traceback(lua_State *L, const char* sep) |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 364 | { |
| 365 | lua_Debug ar; |
| 366 | int level = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 367 | struct buffer *msg = get_trash_chunk(); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 368 | |
| 369 | while (lua_getstack(L, level++, &ar)) { |
| 370 | |
| 371 | /* Add separator */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 372 | if (b_data(msg)) |
| 373 | chunk_appendf(msg, "%s", sep); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 374 | |
| 375 | /* Fill fields: |
| 376 | * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what; |
| 377 | * 'l': fills in the field currentline; |
| 378 | * 'n': fills in the field name and namewhat; |
| 379 | * 't': fills in the field istailcall; |
| 380 | */ |
| 381 | lua_getinfo(L, "Slnt", &ar); |
| 382 | |
| 383 | /* Append code localisation */ |
| 384 | if (ar.currentline > 0) |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 385 | chunk_appendf(msg, "%s:%d: ", ar.short_src, ar.currentline); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 386 | else |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 387 | chunk_appendf(msg, "%s: ", ar.short_src); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 388 | |
| 389 | /* |
| 390 | * Get function name |
| 391 | * |
| 392 | * if namewhat is no empty, name is defined. |
| 393 | * what contains "Lua" for Lua function, "C" for C function, |
| 394 | * or "main" for main code. |
| 395 | */ |
| 396 | if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 397 | chunk_appendf(msg, "in %s '%s'", ar.namewhat, ar.name); /* use it */ |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 398 | |
| 399 | else if (*ar.what == 'm') /* "main", the code is not executed in a function */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 400 | chunk_appendf(msg, "in main chunk"); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 401 | |
| 402 | else if (*ar.what != 'C') /* for Lua functions, use <file:line> */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 403 | chunk_appendf(msg, "in function line %d", ar.linedefined); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 404 | |
| 405 | else /* nothing left... */ |
| 406 | chunk_appendf(msg, "?"); |
| 407 | |
| 408 | |
| 409 | /* Display tailed call */ |
| 410 | if (ar.istailcall) |
| 411 | chunk_appendf(msg, " ..."); |
| 412 | } |
| 413 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 414 | return msg->area; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 418 | /* This function check the number of arguments available in the |
| 419 | * stack. If the number of arguments available is not the same |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 420 | * then <nb> an error is thrown. |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 421 | */ |
| 422 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 423 | { |
| 424 | if (lua_gettop(L) == nb) |
| 425 | return; |
| 426 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 427 | } |
| 428 | |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 429 | /* This function pushes an error string prefixed by the file name |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 430 | * and the line number where the error is encountered. |
| 431 | */ |
| 432 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 433 | { |
| 434 | va_list argp; |
| 435 | va_start(argp, fmt); |
| 436 | luaL_where(L, 1); |
| 437 | lua_pushvfstring(L, fmt, argp); |
| 438 | va_end(argp); |
| 439 | lua_concat(L, 2); |
| 440 | return 1; |
| 441 | } |
| 442 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 443 | /* This functions is used with sample fetch and converters. It |
| 444 | * converts the HAProxy configuration argument in a lua stack |
| 445 | * values. |
| 446 | * |
| 447 | * It takes an array of "arg", and each entry of the array is |
| 448 | * converted and pushed in the LUA stack. |
| 449 | */ |
| 450 | static int hlua_arg2lua(lua_State *L, const struct arg *arg) |
| 451 | { |
| 452 | switch (arg->type) { |
| 453 | case ARGT_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 454 | case ARGT_TIME: |
| 455 | case ARGT_SIZE: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 456 | lua_pushinteger(L, arg->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 457 | break; |
| 458 | |
| 459 | case ARGT_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 460 | lua_pushlstring(L, arg->data.str.area, arg->data.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 461 | break; |
| 462 | |
| 463 | case ARGT_IPV4: |
| 464 | case ARGT_IPV6: |
| 465 | case ARGT_MSK4: |
| 466 | case ARGT_MSK6: |
| 467 | case ARGT_FE: |
| 468 | case ARGT_BE: |
| 469 | case ARGT_TAB: |
| 470 | case ARGT_SRV: |
| 471 | case ARGT_USR: |
| 472 | case ARGT_MAP: |
| 473 | default: |
| 474 | lua_pushnil(L); |
| 475 | break; |
| 476 | } |
| 477 | return 1; |
| 478 | } |
| 479 | |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 480 | /* This function take one entry in an LUA stack at the index "ud", |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 481 | * and try to convert it in an HAProxy argument entry. This is useful |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 482 | * with sample fetch wrappers. The input arguments are given to the |
| 483 | * lua wrapper and converted as arg list by the function. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 484 | */ |
| 485 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg) |
| 486 | { |
| 487 | switch (lua_type(L, ud)) { |
| 488 | |
| 489 | case LUA_TNUMBER: |
| 490 | case LUA_TBOOLEAN: |
| 491 | arg->type = ARGT_SINT; |
| 492 | arg->data.sint = lua_tointeger(L, ud); |
| 493 | break; |
| 494 | |
| 495 | case LUA_TSTRING: |
| 496 | arg->type = ARGT_STR; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 497 | arg->data.str.area = (char *)lua_tolstring(L, ud, &arg->data.str.data); |
Tim Duesterhus | 29d2e8a | 2019-09-29 23:03:07 +0200 | [diff] [blame] | 498 | /* We don't know the actual size of the underlying allocation, so be conservative. */ |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 499 | arg->data.str.size = arg->data.str.data+1; /* count the terminating null byte */ |
Tim Duesterhus | 29d2e8a | 2019-09-29 23:03:07 +0200 | [diff] [blame] | 500 | arg->data.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 501 | break; |
| 502 | |
| 503 | case LUA_TUSERDATA: |
| 504 | case LUA_TNIL: |
| 505 | case LUA_TTABLE: |
| 506 | case LUA_TFUNCTION: |
| 507 | case LUA_TTHREAD: |
| 508 | case LUA_TLIGHTUSERDATA: |
| 509 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 510 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 511 | break; |
| 512 | } |
| 513 | return 1; |
| 514 | } |
| 515 | |
| 516 | /* the following functions are used to convert a struct sample |
| 517 | * in Lua type. This useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 518 | * fetches or converters. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 519 | */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 520 | static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 521 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 522 | switch (smp->data.type) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 523 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 524 | case SMP_T_BOOL: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 525 | lua_pushinteger(L, smp->data.u.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 526 | break; |
| 527 | |
| 528 | case SMP_T_BIN: |
| 529 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 530 | lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 531 | break; |
| 532 | |
| 533 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 534 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 535 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 536 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 537 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 538 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 539 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 540 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 541 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 542 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 543 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 544 | lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 545 | break; |
| 546 | default: |
| 547 | lua_pushnil(L); |
| 548 | break; |
| 549 | } |
| 550 | break; |
| 551 | |
| 552 | case SMP_T_IPV4: |
| 553 | case SMP_T_IPV6: |
| 554 | case SMP_T_ADDR: /* This type is never used to qualify a sample. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 555 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 556 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 557 | lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 558 | else |
| 559 | lua_pushnil(L); |
| 560 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 561 | default: |
| 562 | lua_pushnil(L); |
| 563 | break; |
| 564 | } |
| 565 | return 1; |
| 566 | } |
| 567 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 568 | /* the following functions are used to convert a struct sample |
| 569 | * in Lua strings. This is useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 570 | * fetches or converters. |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 571 | */ |
| 572 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
| 573 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 574 | switch (smp->data.type) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 575 | |
| 576 | case SMP_T_BIN: |
| 577 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 578 | lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 579 | break; |
| 580 | |
| 581 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 582 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 583 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 584 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 585 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 586 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 587 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 588 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 589 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 590 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 591 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 592 | lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 593 | break; |
| 594 | default: |
| 595 | lua_pushstring(L, ""); |
| 596 | break; |
| 597 | } |
| 598 | break; |
| 599 | |
| 600 | case SMP_T_SINT: |
| 601 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 602 | case SMP_T_IPV4: |
| 603 | case SMP_T_IPV6: |
| 604 | case SMP_T_ADDR: /* This type is never used to qualify a sample. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 605 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 606 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 607 | lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 608 | else |
| 609 | lua_pushstring(L, ""); |
| 610 | break; |
| 611 | default: |
| 612 | lua_pushstring(L, ""); |
| 613 | break; |
| 614 | } |
| 615 | return 1; |
| 616 | } |
| 617 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 618 | /* the following functions are used to convert an Lua type in a |
| 619 | * struct sample. This is useful to provide data from a converter |
| 620 | * to the LUA code. |
| 621 | */ |
| 622 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 623 | { |
| 624 | switch (lua_type(L, ud)) { |
| 625 | |
| 626 | case LUA_TNUMBER: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 627 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 628 | smp->data.u.sint = lua_tointeger(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 629 | break; |
| 630 | |
| 631 | |
| 632 | case LUA_TBOOLEAN: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 633 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 634 | smp->data.u.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 635 | break; |
| 636 | |
| 637 | case LUA_TSTRING: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 638 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 639 | smp->flags |= SMP_F_CONST; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 640 | smp->data.u.str.area = (char *)lua_tolstring(L, ud, &smp->data.u.str.data); |
Tim Duesterhus | 29d2e8a | 2019-09-29 23:03:07 +0200 | [diff] [blame] | 641 | /* We don't know the actual size of the underlying allocation, so be conservative. */ |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 642 | smp->data.u.str.size = smp->data.u.str.data+1; /* count the terminating null byte */ |
Tim Duesterhus | 29d2e8a | 2019-09-29 23:03:07 +0200 | [diff] [blame] | 643 | smp->data.u.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 644 | break; |
| 645 | |
| 646 | case LUA_TUSERDATA: |
| 647 | case LUA_TNIL: |
| 648 | case LUA_TTABLE: |
| 649 | case LUA_TFUNCTION: |
| 650 | case LUA_TTHREAD: |
| 651 | case LUA_TLIGHTUSERDATA: |
Thierry FOURNIER | 93405e1 | 2015-08-26 14:19:03 +0200 | [diff] [blame] | 652 | case LUA_TNONE: |
| 653 | default: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 654 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 655 | smp->data.u.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 656 | break; |
| 657 | } |
| 658 | return 1; |
| 659 | } |
| 660 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 661 | /* This function check the "argp" built by another conversion function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 662 | * is in accord with the expected argp defined by the "mask". The function |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 663 | * returns true or false. It can be adjust the types if there compatibles. |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 664 | * |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 665 | * This function assumes that the argp argument contains ARGM_NBARGS + 1 |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 666 | * entries. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 667 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 668 | __LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp, |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 669 | uint64_t mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 670 | { |
| 671 | int min_arg; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 672 | int i, idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 673 | struct proxy *px; |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 674 | struct userlist *ul; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 675 | struct my_regex *reg; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 676 | const char *msg = NULL; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 677 | char *sname, *pname, *err = NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 678 | |
| 679 | idx = 0; |
| 680 | min_arg = ARGM(mask); |
| 681 | mask >>= ARGM_BITS; |
| 682 | |
| 683 | while (1) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 684 | struct buffer tmp = BUF_NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 685 | |
| 686 | /* Check oversize. */ |
| 687 | if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 688 | msg = "Malformed argument mask"; |
| 689 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | /* Check for mandatory arguments. */ |
| 693 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 694 | if (idx < min_arg) { |
| 695 | |
| 696 | /* If miss other argument than the first one, we return an error. */ |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 697 | if (idx > 0) { |
| 698 | msg = "Mandatory argument expected"; |
| 699 | goto error; |
| 700 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 701 | |
| 702 | /* If first argument have a certain type, some default values |
| 703 | * may be used. See the function smp_resolve_args(). |
| 704 | */ |
| 705 | switch (mask & ARGT_MASK) { |
| 706 | |
| 707 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 708 | if (!(p->cap & PR_CAP_FE)) { |
| 709 | msg = "Mandatory argument expected"; |
| 710 | goto error; |
| 711 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 712 | argp[idx].data.prx = p; |
| 713 | argp[idx].type = ARGT_FE; |
| 714 | argp[idx+1].type = ARGT_STOP; |
| 715 | break; |
| 716 | |
| 717 | case ARGT_BE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 718 | if (!(p->cap & PR_CAP_BE)) { |
| 719 | msg = "Mandatory argument expected"; |
| 720 | goto error; |
| 721 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 722 | argp[idx].data.prx = p; |
| 723 | argp[idx].type = ARGT_BE; |
| 724 | argp[idx+1].type = ARGT_STOP; |
| 725 | break; |
| 726 | |
| 727 | case ARGT_TAB: |
| 728 | argp[idx].data.prx = p; |
| 729 | argp[idx].type = ARGT_TAB; |
| 730 | argp[idx+1].type = ARGT_STOP; |
| 731 | break; |
| 732 | |
| 733 | default: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 734 | msg = "Mandatory argument expected"; |
| 735 | goto error; |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 736 | break; |
| 737 | } |
| 738 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 739 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 740 | } |
| 741 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 742 | /* Check for exceed the number of required argument. */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 743 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 744 | argp[idx].type != ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 745 | msg = "Last argument expected"; |
| 746 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 750 | argp[idx].type == ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 751 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 752 | } |
| 753 | |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 754 | /* Convert some argument types. All string in argp[] are for not |
| 755 | * duplicated yet. |
| 756 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 757 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 758 | case ARGT_SINT: |
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 | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 763 | argp[idx].type = ARGT_SINT; |
| 764 | break; |
| 765 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 766 | case ARGT_TIME: |
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_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 772 | break; |
| 773 | |
| 774 | case ARGT_SIZE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 775 | if (argp[idx].type != ARGT_SINT) { |
| 776 | msg = "integer expected"; |
| 777 | goto error; |
| 778 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 779 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 780 | break; |
| 781 | |
| 782 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 783 | if (argp[idx].type != ARGT_STR) { |
| 784 | msg = "string expected"; |
| 785 | goto error; |
| 786 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 787 | 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] | 788 | if (!argp[idx].data.prx) { |
| 789 | msg = "frontend doesn't exist"; |
| 790 | goto error; |
| 791 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 792 | argp[idx].type = ARGT_FE; |
| 793 | break; |
| 794 | |
| 795 | case ARGT_BE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 796 | if (argp[idx].type != ARGT_STR) { |
| 797 | msg = "string expected"; |
| 798 | goto error; |
| 799 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 800 | argp[idx].data.prx = proxy_be_by_name(argp[idx].data.str.area); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 801 | if (!argp[idx].data.prx) { |
| 802 | msg = "backend doesn't exist"; |
| 803 | goto error; |
| 804 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 805 | argp[idx].type = ARGT_BE; |
| 806 | break; |
| 807 | |
| 808 | case ARGT_TAB: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 809 | if (argp[idx].type != ARGT_STR) { |
| 810 | msg = "string expected"; |
| 811 | goto error; |
| 812 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 813 | argp[idx].data.t = stktable_find_by_name(argp[idx].data.str.area); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 814 | if (!argp[idx].data.t) { |
| 815 | msg = "table doesn't exist"; |
| 816 | goto error; |
| 817 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 818 | argp[idx].type = ARGT_TAB; |
| 819 | break; |
| 820 | |
| 821 | case ARGT_SRV: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 822 | if (argp[idx].type != ARGT_STR) { |
| 823 | msg = "string expected"; |
| 824 | goto error; |
| 825 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 826 | sname = strrchr(argp[idx].data.str.area, '/'); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 827 | if (sname) { |
| 828 | *sname++ = '\0'; |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 829 | pname = argp[idx].data.str.area; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 830 | px = proxy_be_by_name(pname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 831 | if (!px) { |
| 832 | msg = "backend doesn't exist"; |
| 833 | goto error; |
| 834 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 835 | } |
| 836 | else { |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 837 | sname = argp[idx].data.str.area; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 838 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 839 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 840 | argp[idx].data.srv = findserver(px, sname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 841 | if (!argp[idx].data.srv) { |
| 842 | msg = "server doesn't exist"; |
| 843 | goto error; |
| 844 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 845 | argp[idx].type = ARGT_SRV; |
| 846 | break; |
| 847 | |
| 848 | case ARGT_IPV4: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 849 | if (argp[idx].type != ARGT_STR) { |
| 850 | msg = "string expected"; |
| 851 | goto error; |
| 852 | } |
| 853 | if (inet_pton(AF_INET, argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 854 | msg = "invalid IPv4 address"; |
| 855 | goto error; |
| 856 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 857 | argp[idx].type = ARGT_IPV4; |
| 858 | break; |
| 859 | |
| 860 | case ARGT_MSK4: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 861 | if (argp[idx].type == ARGT_SINT) |
| 862 | len2mask4(argp[idx].data.sint, &argp[idx].data.ipv4); |
| 863 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 864 | if (!str2mask(argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 865 | msg = "invalid IPv4 mask"; |
| 866 | goto error; |
| 867 | } |
| 868 | } |
| 869 | else { |
| 870 | msg = "integer or string expected"; |
| 871 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 872 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 873 | argp[idx].type = ARGT_MSK4; |
| 874 | break; |
| 875 | |
| 876 | case ARGT_IPV6: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 877 | if (argp[idx].type != ARGT_STR) { |
| 878 | msg = "string expected"; |
| 879 | goto error; |
| 880 | } |
| 881 | if (inet_pton(AF_INET6, argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 882 | msg = "invalid IPv6 address"; |
| 883 | goto error; |
| 884 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 885 | argp[idx].type = ARGT_IPV6; |
| 886 | break; |
| 887 | |
| 888 | case ARGT_MSK6: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 889 | if (argp[idx].type == ARGT_SINT) |
| 890 | len2mask6(argp[idx].data.sint, &argp[idx].data.ipv6); |
| 891 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 892 | if (!str2mask6(argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 893 | msg = "invalid IPv6 mask"; |
| 894 | goto error; |
| 895 | } |
| 896 | } |
| 897 | else { |
| 898 | msg = "integer or string expected"; |
| 899 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 900 | } |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 901 | argp[idx].type = ARGT_MSK6; |
| 902 | break; |
| 903 | |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 904 | case ARGT_REG: |
| 905 | if (argp[idx].type != ARGT_STR) { |
| 906 | msg = "string expected"; |
| 907 | goto error; |
| 908 | } |
| 909 | reg = regex_comp(argp[idx].data.str.area, !(argp[idx].type_flags & ARGF_REG_ICASE), 1, &err); |
| 910 | if (!reg) { |
| 911 | msg = lua_pushfstring(L, "error compiling regex '%s' : '%s'", |
| 912 | argp[idx].data.str.area, err); |
| 913 | free(err); |
| 914 | goto error; |
| 915 | } |
| 916 | argp[idx].type = ARGT_REG; |
| 917 | argp[idx].data.reg = reg; |
| 918 | break; |
| 919 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 920 | case ARGT_USR: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 921 | if (argp[idx].type != ARGT_STR) { |
| 922 | msg = "string expected"; |
| 923 | goto error; |
| 924 | } |
| 925 | if (p->uri_auth && p->uri_auth->userlist && |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 926 | strcmp(p->uri_auth->userlist->name, argp[idx].data.str.area) == 0) |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 927 | ul = p->uri_auth->userlist; |
| 928 | else |
| 929 | ul = auth_find_userlist(argp[idx].data.str.area); |
| 930 | |
| 931 | if (!ul) { |
| 932 | msg = lua_pushfstring(L, "unable to find userlist '%s'", argp[idx].data.str.area); |
| 933 | goto error; |
| 934 | } |
| 935 | argp[idx].type = ARGT_USR; |
| 936 | argp[idx].data.usr = ul; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 937 | break; |
| 938 | |
| 939 | case ARGT_STR: |
| 940 | if (!chunk_dup(&tmp, &argp[idx].data.str)) { |
| 941 | msg = "unable to duplicate string arg"; |
| 942 | goto error; |
| 943 | } |
| 944 | argp[idx].data.str = tmp; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 945 | break; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 946 | |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 947 | case ARGT_MAP: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 948 | msg = "type not yet supported"; |
| 949 | goto error; |
| 950 | break; |
| 951 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 952 | } |
| 953 | |
| 954 | /* Check for type of argument. */ |
| 955 | if ((mask & ARGT_MASK) != argp[idx].type) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 956 | msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 957 | arg_type_names[(mask & ARGT_MASK)], |
| 958 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 959 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | /* Next argument. */ |
| 963 | mask >>= ARGT_BITS; |
| 964 | idx++; |
| 965 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 966 | return 0; |
| 967 | |
| 968 | error: |
| 969 | for (i = 0; i < idx; i++) { |
| 970 | if (argp[i].type == ARGT_STR) |
| 971 | chunk_destroy(&argp[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 972 | else if (argp[i].type == ARGT_REG) |
| 973 | regex_free(argp[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 974 | } |
| 975 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 976 | return 0; /* Never reached */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 977 | } |
| 978 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 979 | /* |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 980 | * The following functions are used to make correspondence between the the |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 981 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 982 | * |
| 983 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 984 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 985 | */ |
| 986 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 987 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 988 | struct hlua **hlua = lua_getextraspace(L); |
| 989 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 990 | } |
| 991 | static inline void hlua_sethlua(struct hlua *hlua) |
| 992 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 993 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 994 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 995 | } |
| 996 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 997 | /* This function is used to send logs. It try to send on screen (stderr) |
| 998 | * and on the default syslog server. |
| 999 | */ |
| 1000 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 1001 | { |
| 1002 | struct tm tm; |
| 1003 | char *p; |
| 1004 | |
| 1005 | /* Cleanup the log message. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1006 | p = trash.area; |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1007 | for (; *msg != '\0'; msg++, p++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1008 | if (p >= trash.area + trash.size - 1) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 1009 | /* Break the message if exceed the buffer size. */ |
| 1010 | *(p-4) = ' '; |
| 1011 | *(p-3) = '.'; |
| 1012 | *(p-2) = '.'; |
| 1013 | *(p-1) = '.'; |
| 1014 | break; |
| 1015 | } |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 1016 | if (isprint((unsigned char)*msg)) |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1017 | *p = *msg; |
| 1018 | else |
| 1019 | *p = '.'; |
| 1020 | } |
| 1021 | *p = '\0'; |
| 1022 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1023 | send_log(px, level, "%s\n", trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1024 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Christopher Faulet | f98d821 | 2020-10-02 18:13:52 +0200 | [diff] [blame] | 1025 | if (level == LOG_DEBUG && !(global.mode & MODE_DEBUG)) |
| 1026 | return; |
| 1027 | |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 1028 | get_localtime(date.tv_sec, &tm); |
| 1029 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1030 | 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] | 1031 | (int)getpid(), trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1032 | fflush(stderr); |
| 1033 | } |
| 1034 | } |
| 1035 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1036 | /* This function just ensure that the yield will be always |
| 1037 | * returned with a timeout and permit to set some flags |
| 1038 | */ |
| 1039 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1040 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1041 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1042 | struct hlua *hlua; |
| 1043 | |
| 1044 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1045 | hlua = hlua_gethlua(L); |
| 1046 | if (!hlua) { |
| 1047 | return; |
| 1048 | } |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1049 | |
| 1050 | /* Set the wake timeout. If timeout is required, we set |
| 1051 | * the expiration time. |
| 1052 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1053 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1054 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 1055 | hlua->flags |= flags; |
| 1056 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1057 | /* Process the yield. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1058 | MAY_LJMP(lua_yieldk(L, nresults, ctx, k)); |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1059 | } |
| 1060 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1061 | /* This function initialises the Lua environment stored in the stream. |
| 1062 | * 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] | 1063 | * 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] | 1064 | * |
| 1065 | * This function is particular. it initialises a new Lua thread. If the |
| 1066 | * initialisation fails (example: out of memory error), the lua function |
| 1067 | * throws an error (longjmp). |
| 1068 | * |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1069 | * 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] | 1070 | * environment, so we must not initialise it. While the support of |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1071 | * threads appear, the safe environment set a lock to ensure only one |
| 1072 | * Lua execution at a time. If we initialize safe environment in another |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1073 | * safe environment, we have a dead lock. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1074 | * |
| 1075 | * set "already_safe" true if the context is initialized form safe |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1076 | * Lua function. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1077 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1078 | * This function manipulates two Lua stacks: the main and the thread. Only |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 1079 | * the main stack can fail. The thread is not manipulated. This function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1080 | * MUST NOT manipulate the created thread stack state, because it is not |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1081 | * protected against errors thrown by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1082 | */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1083 | 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] | 1084 | { |
| 1085 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1086 | lua->flags = 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1087 | lua->gc_count = 0; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1088 | lua->wake_time = TICK_ETERNITY; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1089 | lua->state_id = state_id; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1090 | LIST_INIT(&lua->com); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1091 | if (!already_safe) { |
| 1092 | if (!SET_SAFE_LJMP_PARENT(lua)) { |
| 1093 | lua->Tref = LUA_REFNIL; |
| 1094 | return 0; |
| 1095 | } |
| 1096 | } |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1097 | lua->T = lua_newthread(hlua_states[state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1098 | if (!lua->T) { |
| 1099 | lua->Tref = LUA_REFNIL; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1100 | if (!already_safe) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1101 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1102 | return 0; |
| 1103 | } |
| 1104 | hlua_sethlua(lua); |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1105 | lua->Tref = luaL_ref(hlua_states[state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1106 | lua->task = task; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1107 | if (!already_safe) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1108 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1109 | return 1; |
| 1110 | } |
| 1111 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1112 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1113 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 1114 | * is not freed. |
| 1115 | */ |
| 1116 | void hlua_ctx_destroy(struct hlua *lua) |
| 1117 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1118 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 1119 | return; |
| 1120 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1121 | if (!lua->T) |
| 1122 | goto end; |
| 1123 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1124 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1125 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1126 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1127 | if (!SET_SAFE_LJMP(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1128 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1129 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1130 | RESET_SAFE_LJMP(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1131 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1132 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1133 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1134 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1135 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1136 | /* Forces a garbage collecting process. If the Lua program is finished |
| 1137 | * without error, we run the GC on the thread pointer. Its freed all |
| 1138 | * the unused memory. |
| 1139 | * If the thread is finnish with an error or is currently yielded, |
| 1140 | * it seems that the GC applied on the thread doesn't clean anything, |
| 1141 | * so e run the GC on the main thread. |
| 1142 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 1143 | * the garbage collection. |
| 1144 | */ |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1145 | if (lua->gc_count) { |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1146 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1147 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1148 | lua_gc(hlua_states[lua->state_id], LUA_GCCOLLECT, 0); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1149 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1150 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1151 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 1152 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1153 | |
| 1154 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1155 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1156 | } |
| 1157 | |
| 1158 | /* This function is used to restore the Lua context when a coroutine |
| 1159 | * fails. This function copy the common memory between old coroutine |
| 1160 | * and the new coroutine. The old coroutine is destroyed, and its |
| 1161 | * replaced by the new coroutine. |
| 1162 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 1163 | * as string error message and it is copied in the new stack. |
| 1164 | */ |
| 1165 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 1166 | { |
| 1167 | lua_State *T; |
| 1168 | int new_ref; |
| 1169 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1170 | /* New Lua coroutine. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1171 | T = lua_newthread(hlua_states[lua->state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1172 | if (!T) |
| 1173 | return 0; |
| 1174 | |
| 1175 | /* Copy last error message. */ |
| 1176 | if (keep_msg) |
| 1177 | lua_xmove(lua->T, T, 1); |
| 1178 | |
| 1179 | /* Copy data between the coroutines. */ |
| 1180 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1181 | lua_xmove(lua->T, T, 1); |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 1182 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */ |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1183 | |
| 1184 | /* Destroy old data. */ |
| 1185 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1186 | |
| 1187 | /* The thread is garbage collected by Lua. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1188 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1189 | |
| 1190 | /* Fill the struct with the new coroutine values. */ |
| 1191 | lua->Mref = new_ref; |
| 1192 | lua->T = T; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1193 | lua->Tref = luaL_ref(hlua_states[lua->state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1194 | |
| 1195 | /* Set context. */ |
| 1196 | hlua_sethlua(lua); |
| 1197 | |
| 1198 | return 1; |
| 1199 | } |
| 1200 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1201 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1202 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1203 | struct hlua *hlua; |
| 1204 | |
| 1205 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1206 | hlua = hlua_gethlua(L); |
| 1207 | if (!hlua) |
| 1208 | return; |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1209 | |
| 1210 | /* Lua cannot yield when its returning from a function, |
| 1211 | * so, we can fix the interrupt hook to 1 instruction, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1212 | * expecting that the function is finished. |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1213 | */ |
| 1214 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1215 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1216 | return; |
| 1217 | } |
| 1218 | |
| 1219 | /* restore the interrupt condition. */ |
| 1220 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1221 | |
| 1222 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1223 | * If the state is not yieldable, trying yield causes an error. |
| 1224 | */ |
| 1225 | if (lua_isyieldable(L)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1226 | MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1227 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 1228 | /* If we cannot yield, update the clock and check the timeout. */ |
| 1229 | tv_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1230 | hlua->run_time += now_ms - hlua->start_time; |
| 1231 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1232 | lua_pushfstring(L, "execution timeout"); |
| 1233 | WILL_LJMP(lua_error(L)); |
| 1234 | } |
| 1235 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1236 | /* Update the start time. */ |
| 1237 | hlua->start_time = now_ms; |
| 1238 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1239 | /* Try to interrupt the process at the end of the current |
| 1240 | * unyieldable function. |
| 1241 | */ |
| 1242 | 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] | 1243 | } |
| 1244 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1245 | /* This function start or resumes the Lua stack execution. If the flag |
| 1246 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1247 | * The function return an error. |
| 1248 | * |
| 1249 | * The function can returns 4 values: |
| 1250 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1251 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1252 | * task wakeup. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1253 | * - 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] | 1254 | * the top of the stack. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1255 | * - HLUA_E_ERR : An error has occurred without error message. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1256 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1257 | * 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] | 1258 | * LUA code. |
| 1259 | */ |
| 1260 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1261 | { |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1262 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 1263 | int nres; |
| 1264 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1265 | int ret; |
| 1266 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1267 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1268 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1269 | /* Initialise run time counter. */ |
| 1270 | if (!HLUA_IS_RUNNING(lua)) |
| 1271 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1272 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1273 | /* Lock the whole Lua execution. This lock must be before the |
| 1274 | * label "resume_execution". |
| 1275 | */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1276 | if (lua->state_id == 0) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1277 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1278 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1279 | resume_execution: |
| 1280 | |
| 1281 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1282 | * instructions. it is used for preventing infinite loops. |
| 1283 | */ |
| 1284 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1285 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1286 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1287 | HLUA_SET_RUN(lua); |
| 1288 | HLUA_CLR_CTRLYIELD(lua); |
| 1289 | HLUA_CLR_WAKERESWR(lua); |
| 1290 | HLUA_CLR_WAKEREQWR(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1291 | |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1292 | /* Update the start time and reset wake_time. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1293 | lua->start_time = now_ms; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1294 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1295 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1296 | /* Call the function. */ |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1297 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1298 | 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] | 1299 | #else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1300 | ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs); |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1301 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1302 | switch (ret) { |
| 1303 | |
| 1304 | case LUA_OK: |
| 1305 | ret = HLUA_E_OK; |
| 1306 | break; |
| 1307 | |
| 1308 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1309 | /* Check if the execution timeout is expired. It it is the case, we |
| 1310 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1311 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1312 | tv_update_date(0, 1); |
| 1313 | lua->run_time += now_ms - lua->start_time; |
| 1314 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1315 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1316 | ret = HLUA_E_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1317 | break; |
| 1318 | } |
| 1319 | /* Process the forced yield. if the general yield is not allowed or |
| 1320 | * if no task were associated this the current Lua execution |
| 1321 | * coroutine, we resume the execution. Else we want to return in the |
| 1322 | * scheduler and we want to be waked up again, to continue the |
| 1323 | * current Lua execution. So we schedule our own task. |
| 1324 | */ |
| 1325 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1326 | if (!yield_allowed || !lua->task) |
| 1327 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1328 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1329 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1330 | if (!yield_allowed) { |
| 1331 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1332 | ret = HLUA_E_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1333 | break; |
| 1334 | } |
| 1335 | ret = HLUA_E_AGAIN; |
| 1336 | break; |
| 1337 | |
| 1338 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1339 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1340 | /* Special exit case. The traditional exit is returned as an error |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1341 | * because the errors ares the only one mean to return immediately |
| 1342 | * from and lua execution. |
| 1343 | */ |
| 1344 | if (lua->flags & HLUA_EXIT) { |
| 1345 | ret = HLUA_E_OK; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 1346 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1347 | break; |
| 1348 | } |
| 1349 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1350 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1351 | if (!lua_checkstack(lua->T, 1)) { |
| 1352 | ret = HLUA_E_ERR; |
| 1353 | break; |
| 1354 | } |
| 1355 | msg = lua_tostring(lua->T, -1); |
| 1356 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1357 | lua_pop(lua->T, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 1358 | trace = hlua_traceback(lua->T, ", "); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1359 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1360 | 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] | 1361 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1362 | 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] | 1363 | ret = HLUA_E_ERRMSG; |
| 1364 | break; |
| 1365 | |
| 1366 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1367 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1368 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1369 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1370 | break; |
| 1371 | |
| 1372 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1373 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1374 | if (!lua_checkstack(lua->T, 1)) { |
| 1375 | ret = HLUA_E_ERR; |
| 1376 | break; |
| 1377 | } |
| 1378 | msg = lua_tostring(lua->T, -1); |
| 1379 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1380 | lua_pop(lua->T, 1); |
| 1381 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1382 | 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] | 1383 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1384 | 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] | 1385 | ret = HLUA_E_ERRMSG; |
| 1386 | break; |
| 1387 | |
| 1388 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1389 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1390 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1391 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1392 | break; |
| 1393 | } |
| 1394 | |
| 1395 | switch (ret) { |
| 1396 | case HLUA_E_AGAIN: |
| 1397 | break; |
| 1398 | |
| 1399 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1400 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1401 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1402 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1403 | break; |
| 1404 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1405 | case HLUA_E_ETMOUT: |
| 1406 | case HLUA_E_NOMEM: |
| 1407 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1408 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1409 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1410 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1411 | hlua_ctx_renew(lua, 0); |
| 1412 | break; |
| 1413 | |
| 1414 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1415 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1416 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1417 | break; |
| 1418 | } |
| 1419 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1420 | /* This is the main exit point, remove the Lua lock. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1421 | if (lua->state_id == 0) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1422 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1423 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1424 | return ret; |
| 1425 | } |
| 1426 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1427 | /* This function exit the current code. */ |
| 1428 | __LJMP static int hlua_done(lua_State *L) |
| 1429 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1430 | struct hlua *hlua; |
| 1431 | |
| 1432 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1433 | hlua = hlua_gethlua(L); |
| 1434 | if (!hlua) |
| 1435 | return 0; |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1436 | |
| 1437 | hlua->flags |= HLUA_EXIT; |
| 1438 | WILL_LJMP(lua_error(L)); |
| 1439 | |
| 1440 | return 0; |
| 1441 | } |
| 1442 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1443 | /* This function is an LUA binding. It provides a function |
| 1444 | * for deleting ACL from a referenced ACL file. |
| 1445 | */ |
| 1446 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1447 | { |
| 1448 | const char *name; |
| 1449 | const char *key; |
| 1450 | struct pat_ref *ref; |
| 1451 | |
| 1452 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1453 | |
| 1454 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1455 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1456 | |
| 1457 | ref = pat_ref_lookup(name); |
| 1458 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1459 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1460 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1461 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1462 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1463 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1464 | return 0; |
| 1465 | } |
| 1466 | |
| 1467 | /* This function is an LUA binding. It provides a function |
| 1468 | * for deleting map entry from a referenced map file. |
| 1469 | */ |
| 1470 | static int hlua_del_map(lua_State *L) |
| 1471 | { |
| 1472 | const char *name; |
| 1473 | const char *key; |
| 1474 | struct pat_ref *ref; |
| 1475 | |
| 1476 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1477 | |
| 1478 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1479 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1480 | |
| 1481 | ref = pat_ref_lookup(name); |
| 1482 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1483 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1484 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1485 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1486 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1487 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1488 | return 0; |
| 1489 | } |
| 1490 | |
| 1491 | /* This function is an LUA binding. It provides a function |
| 1492 | * for adding ACL pattern from a referenced ACL file. |
| 1493 | */ |
| 1494 | static int hlua_add_acl(lua_State *L) |
| 1495 | { |
| 1496 | const char *name; |
| 1497 | const char *key; |
| 1498 | struct pat_ref *ref; |
| 1499 | |
| 1500 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1501 | |
| 1502 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1503 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1504 | |
| 1505 | ref = pat_ref_lookup(name); |
| 1506 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1507 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1508 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1509 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1510 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1511 | pat_ref_add(ref, key, NULL, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1512 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1513 | return 0; |
| 1514 | } |
| 1515 | |
| 1516 | /* This function is an LUA binding. It provides a function |
| 1517 | * for setting map pattern and sample from a referenced map |
| 1518 | * file. |
| 1519 | */ |
| 1520 | static int hlua_set_map(lua_State *L) |
| 1521 | { |
| 1522 | const char *name; |
| 1523 | const char *key; |
| 1524 | const char *value; |
| 1525 | struct pat_ref *ref; |
| 1526 | |
| 1527 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1528 | |
| 1529 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1530 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1531 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1532 | |
| 1533 | ref = pat_ref_lookup(name); |
| 1534 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1535 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1536 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1537 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1538 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1539 | pat_ref_set(ref, key, value, NULL); |
| 1540 | else |
| 1541 | pat_ref_add(ref, key, value, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1542 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1543 | return 0; |
| 1544 | } |
| 1545 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1546 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1547 | * 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] | 1548 | * metatable have an original version registered in the global context with |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1549 | * the name of the object (_G[<name>] = <metable> ). |
| 1550 | * |
| 1551 | * A metable is a table that modify the standard behavior of a standard |
| 1552 | * access to the associated data. The entries of this new metatable are |
| 1553 | * defined as is: |
| 1554 | * |
| 1555 | * http://lua-users.org/wiki/MetatableEvents |
| 1556 | * |
| 1557 | * __index |
| 1558 | * |
| 1559 | * we access an absent field in a table, the result is nil. This is |
| 1560 | * true, but it is not the whole truth. Actually, such access triggers |
| 1561 | * the interpreter to look for an __index metamethod: If there is no |
| 1562 | * such method, as usually happens, then the access results in nil; |
| 1563 | * otherwise, the metamethod will provide the result. |
| 1564 | * |
| 1565 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1566 | * the key does not appear in the table, but the metatable has an __index |
| 1567 | * property: |
| 1568 | * |
| 1569 | * - if the value is a function, the function is called, passing in the |
| 1570 | * table and the key; the return value of that function is returned as |
| 1571 | * the result. |
| 1572 | * |
| 1573 | * - if the value is another table, the value of the key in that table is |
| 1574 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1575 | * table's metatable has an __index property, then it continues on up) |
| 1576 | * |
| 1577 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1578 | * |
| 1579 | * http://www.lua.org/pil/13.4.1.html |
| 1580 | * |
| 1581 | * __newindex |
| 1582 | * |
| 1583 | * Like __index, but control property assignment. |
| 1584 | * |
| 1585 | * __mode - Control weak references. A string value with one or both |
| 1586 | * of the characters 'k' and 'v' which specifies that the the |
| 1587 | * keys and/or values in the table are weak references. |
| 1588 | * |
| 1589 | * __call - Treat a table like a function. When a table is followed by |
| 1590 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1591 | * a __call key pointing to a function, that function is invoked |
| 1592 | * (passing any specified arguments) and the return value is |
| 1593 | * returned. |
| 1594 | * |
| 1595 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1596 | * called, if the metatable for myTable has a __metatable |
| 1597 | * key, the value of that key is returned instead of the |
| 1598 | * actual metatable. |
| 1599 | * |
| 1600 | * __tostring - Control string representation. When the builtin |
| 1601 | * "tostring( myTable )" function is called, if the metatable |
| 1602 | * for myTable has a __tostring property set to a function, |
| 1603 | * that function is invoked (passing myTable to it) and the |
| 1604 | * return value is used as the string representation. |
| 1605 | * |
| 1606 | * __len - Control table length. When the table length is requested using |
| 1607 | * the length operator ( '#' ), if the metatable for myTable has |
| 1608 | * a __len key pointing to a function, that function is invoked |
| 1609 | * (passing myTable to it) and the return value used as the value |
| 1610 | * of "#myTable". |
| 1611 | * |
| 1612 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1613 | * collected, if the metatable has a __gc field pointing to a |
| 1614 | * function, that function is first invoked, passing the userdata |
| 1615 | * to it. The __gc metamethod is not called for tables. |
| 1616 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1617 | * |
| 1618 | * Special metamethods for redefining standard operators: |
| 1619 | * http://www.lua.org/pil/13.1.html |
| 1620 | * |
| 1621 | * __add "+" |
| 1622 | * __sub "-" |
| 1623 | * __mul "*" |
| 1624 | * __div "/" |
| 1625 | * __unm "!" |
| 1626 | * __pow "^" |
| 1627 | * __concat ".." |
| 1628 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1629 | * Special methods for redefining standard relations |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1630 | * http://www.lua.org/pil/13.2.html |
| 1631 | * |
| 1632 | * __eq "==" |
| 1633 | * __lt "<" |
| 1634 | * __le "<=" |
| 1635 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1636 | |
| 1637 | /* |
| 1638 | * |
| 1639 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1640 | * Class Map |
| 1641 | * |
| 1642 | * |
| 1643 | */ |
| 1644 | |
| 1645 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1646 | * a class session, otherwise it throws an error. |
| 1647 | */ |
| 1648 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1649 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1650 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1651 | } |
| 1652 | |
| 1653 | /* This function is the map constructor. It don't need |
| 1654 | * the class Map object. It creates and return a new Map |
| 1655 | * object. It must be called only during "body" or "init" |
| 1656 | * context because it process some filesystem accesses. |
| 1657 | */ |
| 1658 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1659 | { |
| 1660 | const char *fn; |
| 1661 | int match = PAT_MATCH_STR; |
| 1662 | struct sample_conv conv; |
| 1663 | const char *file = ""; |
| 1664 | int line = 0; |
| 1665 | lua_Debug ar; |
| 1666 | char *err = NULL; |
| 1667 | struct arg args[2]; |
| 1668 | |
| 1669 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1670 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1671 | |
| 1672 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1673 | |
| 1674 | if (lua_gettop(L) >= 2) { |
| 1675 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1676 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1677 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1678 | } |
| 1679 | |
| 1680 | /* Get Lua filename and line number. */ |
| 1681 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1682 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1683 | if (ar.currentline > 0) { /* is there info? */ |
| 1684 | file = ar.short_src; |
| 1685 | line = ar.currentline; |
| 1686 | } |
| 1687 | } |
| 1688 | |
| 1689 | /* fill fake sample_conv struct. */ |
| 1690 | conv.kw = ""; /* unused. */ |
| 1691 | conv.process = NULL; /* unused. */ |
| 1692 | conv.arg_mask = 0; /* unused. */ |
| 1693 | conv.val_args = NULL; /* unused. */ |
| 1694 | conv.out_type = SMP_T_STR; |
| 1695 | conv.private = (void *)(long)match; |
| 1696 | switch (match) { |
| 1697 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1698 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1699 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1700 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1701 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1702 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1703 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1704 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1705 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1706 | default: |
| 1707 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1708 | } |
| 1709 | |
| 1710 | /* fill fake args. */ |
| 1711 | args[0].type = ARGT_STR; |
Christopher Faulet | 73292e9 | 2020-08-06 08:40:09 +0200 | [diff] [blame] | 1712 | args[0].data.str.area = strdup(fn); |
| 1713 | args[0].data.str.data = strlen(fn); |
| 1714 | args[0].data.str.size = args[0].data.str.data+1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1715 | args[1].type = ARGT_STOP; |
| 1716 | |
| 1717 | /* load the map. */ |
| 1718 | if (!sample_load_map(args, &conv, file, line, &err)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1719 | /* error case: we can't use luaL_error because we must |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1720 | * free the err variable. |
| 1721 | */ |
| 1722 | luaL_where(L, 1); |
| 1723 | lua_pushfstring(L, "'new': %s.", err); |
| 1724 | lua_concat(L, 2); |
| 1725 | free(err); |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 1726 | chunk_destroy(&args[0].data.str); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1727 | WILL_LJMP(lua_error(L)); |
| 1728 | } |
| 1729 | |
| 1730 | /* create the lua object. */ |
| 1731 | lua_newtable(L); |
| 1732 | lua_pushlightuserdata(L, args[0].data.map); |
| 1733 | lua_rawseti(L, -2, 0); |
| 1734 | |
| 1735 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1736 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1737 | lua_setmetatable(L, -2); |
| 1738 | |
| 1739 | |
| 1740 | return 1; |
| 1741 | } |
| 1742 | |
| 1743 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1744 | { |
| 1745 | struct map_descriptor *desc; |
| 1746 | struct pattern *pat; |
| 1747 | struct sample smp; |
| 1748 | |
| 1749 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1750 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1751 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1752 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1753 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1754 | } |
| 1755 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1756 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1757 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1758 | 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] | 1759 | smp.data.u.str.size = smp.data.u.str.data + 1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1760 | } |
| 1761 | |
| 1762 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1763 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1764 | if (str) |
| 1765 | lua_pushstring(L, ""); |
| 1766 | else |
| 1767 | lua_pushnil(L); |
| 1768 | return 1; |
| 1769 | } |
| 1770 | |
| 1771 | /* 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] | 1772 | 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] | 1773 | return 1; |
| 1774 | } |
| 1775 | |
| 1776 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1777 | { |
| 1778 | return _hlua_map_lookup(L, 0); |
| 1779 | } |
| 1780 | |
| 1781 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1782 | { |
| 1783 | return _hlua_map_lookup(L, 1); |
| 1784 | } |
| 1785 | |
| 1786 | /* |
| 1787 | * |
| 1788 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1789 | * Class Socket |
| 1790 | * |
| 1791 | * |
| 1792 | */ |
| 1793 | |
| 1794 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1795 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1796 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1797 | } |
| 1798 | |
| 1799 | /* 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] | 1800 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1801 | * received. |
| 1802 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1803 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1804 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1805 | struct stream_interface *si = appctx->owner; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1806 | |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1807 | if (appctx->ctx.hlua_cosocket.die) { |
| 1808 | si_shutw(si); |
| 1809 | si_shutr(si); |
| 1810 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1811 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1812 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1813 | stream_shutdown(si_strm(si), SF_ERR_KILLED); |
| 1814 | } |
| 1815 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1816 | /* If we can't write, wakeup the pending write signals. */ |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1817 | if (channel_output_closed(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1818 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1819 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1820 | /* If we can't read, wakeup the pending read signals. */ |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1821 | if (channel_input_closed(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1822 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1823 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1824 | /* if the connection is not established, inform the stream that we want |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1825 | * to be notified whenever the connection completes. |
| 1826 | */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1827 | if (si_opposite(si)->state < SI_ST_EST) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 1828 | si_cant_get(si); |
Willy Tarreau | 12c2423 | 2018-12-06 15:29:50 +0100 | [diff] [blame] | 1829 | si_rx_conn_blk(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1830 | si_rx_endp_more(si); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1831 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1832 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1833 | |
| 1834 | /* This function is called after the connect. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1835 | appctx->ctx.hlua_cosocket.connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1836 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1837 | /* 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] | 1838 | if (channel_may_recv(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1839 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1840 | |
| 1841 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1842 | if (!channel_is_empty(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1843 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1844 | |
| 1845 | /* Some data were injected in the buffer, notify the stream |
| 1846 | * interface. |
| 1847 | */ |
| 1848 | if (!channel_is_empty(si_ic(si))) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 1849 | si_update(si); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1850 | |
| 1851 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1852 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1853 | */ |
| 1854 | if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write)) |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1855 | si_rx_endp_more(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1856 | } |
| 1857 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1858 | /* This function is called when the "struct stream" is destroyed. |
| 1859 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1860 | * Wake all the pending signals. |
| 1861 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1862 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1863 | { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1864 | struct xref *peer; |
| 1865 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1866 | /* Remove my link in the original object. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1867 | peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref); |
| 1868 | if (peer) |
| 1869 | xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1870 | |
| 1871 | /* Wake all the task waiting for me. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1872 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1873 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1874 | } |
| 1875 | |
| 1876 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1877 | * uses this object. If the stream does not exists, just quit. |
| 1878 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1879 | * pending signal can rest in the read and write lists. destroy |
| 1880 | * it. |
| 1881 | */ |
| 1882 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1883 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1884 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1885 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1886 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1887 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1888 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 1889 | |
| 1890 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1891 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1892 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1893 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1894 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1895 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1896 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1897 | appctx->ctx.hlua_cosocket.die = 1; |
| 1898 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1899 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1900 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1901 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1902 | return 0; |
| 1903 | } |
| 1904 | |
| 1905 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1906 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1907 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1908 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1909 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1910 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1911 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1912 | struct xref *peer; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1913 | struct hlua *hlua; |
| 1914 | |
| 1915 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1916 | hlua = hlua_gethlua(L); |
| 1917 | if (!hlua) |
| 1918 | return 0; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1919 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1920 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1921 | |
| 1922 | /* Check if we run on the same thread than the xreator thread. |
| 1923 | * We cannot access to the socket if the thread is different. |
| 1924 | */ |
| 1925 | if (socket->tid != tid) |
| 1926 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1927 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1928 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1929 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1930 | return 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1931 | |
| 1932 | hlua->gc_count--; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1933 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1934 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1935 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1936 | appctx->ctx.hlua_cosocket.die = 1; |
| 1937 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1938 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1939 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1940 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1941 | return 0; |
| 1942 | } |
| 1943 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1944 | /* The close function calls close_helper. |
| 1945 | */ |
| 1946 | __LJMP static int hlua_socket_close(lua_State *L) |
| 1947 | { |
| 1948 | MAY_LJMP(check_args(L, 1, "close")); |
| 1949 | return hlua_socket_close_helper(L); |
| 1950 | } |
| 1951 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1952 | /* This Lua function assumes that the stack contain three parameters. |
| 1953 | * 1 - USERDATA containing a struct socket |
| 1954 | * 2 - INTEGER with values of the macro defined below |
| 1955 | * If the integer is -1, we must read at most one line. |
| 1956 | * If the integer is -2, we ust read all the data until the |
| 1957 | * end of the stream. |
| 1958 | * If the integer is positive value, we must read a number of |
| 1959 | * bytes corresponding to this value. |
| 1960 | */ |
| 1961 | #define HLSR_READ_LINE (-1) |
| 1962 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1963 | __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] | 1964 | { |
| 1965 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1966 | int wanted = lua_tointeger(L, 2); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1967 | struct hlua *hlua; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1968 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1969 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1970 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1971 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1972 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1973 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1974 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1975 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1976 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1977 | struct stream_interface *si; |
| 1978 | struct stream *s; |
| 1979 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1980 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1981 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1982 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1983 | hlua = hlua_gethlua(L); |
| 1984 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1985 | /* Check if this lua stack is schedulable. */ |
| 1986 | if (!hlua || !hlua->task) |
| 1987 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 1988 | "'frontend', 'backend' or 'task'")); |
| 1989 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1990 | /* Check if we run on the same thread than the xreator thread. |
| 1991 | * We cannot access to the socket if the thread is different. |
| 1992 | */ |
| 1993 | if (socket->tid != tid) |
| 1994 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1995 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1996 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1997 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1998 | if (!peer) |
| 1999 | goto no_peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2000 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2001 | si = appctx->owner; |
| 2002 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2003 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2004 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2005 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2006 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2007 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2008 | if (nblk < 0) /* Connection close. */ |
| 2009 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2010 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2011 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 2012 | |
| 2013 | /* remove final \r\n. */ |
| 2014 | if (nblk == 1) { |
| 2015 | if (blk1[len1-1] == '\n') { |
| 2016 | len1--; |
| 2017 | skip_at_end++; |
| 2018 | if (blk1[len1-1] == '\r') { |
| 2019 | len1--; |
| 2020 | skip_at_end++; |
| 2021 | } |
| 2022 | } |
| 2023 | } |
| 2024 | else { |
| 2025 | if (blk2[len2-1] == '\n') { |
| 2026 | len2--; |
| 2027 | skip_at_end++; |
| 2028 | if (blk2[len2-1] == '\r') { |
| 2029 | len2--; |
| 2030 | skip_at_end++; |
| 2031 | } |
| 2032 | } |
| 2033 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2034 | } |
| 2035 | |
| 2036 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2037 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2038 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2039 | if (nblk < 0) /* Connection close. */ |
| 2040 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2041 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2042 | goto connection_empty; |
| 2043 | } |
| 2044 | |
| 2045 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2046 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2047 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2048 | if (nblk < 0) /* Connection close. */ |
| 2049 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2050 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2051 | goto connection_empty; |
| 2052 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2053 | missing_bytes = wanted - socket->b.n; |
| 2054 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2055 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2056 | len1 = missing_bytes; |
| 2057 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 2058 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2059 | } |
| 2060 | |
| 2061 | len = len1; |
| 2062 | |
| 2063 | luaL_addlstring(&socket->b, blk1, len1); |
| 2064 | if (nblk == 2) { |
| 2065 | len += len2; |
| 2066 | luaL_addlstring(&socket->b, blk2, len2); |
| 2067 | } |
| 2068 | |
| 2069 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2070 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2071 | |
| 2072 | /* Don't wait anything. */ |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 2073 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2074 | |
| 2075 | /* If the pattern reclaim to read all the data |
| 2076 | * in the connection, got out. |
| 2077 | */ |
| 2078 | if (wanted == HLSR_READ_ALL) |
| 2079 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2080 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2081 | goto connection_empty; |
| 2082 | |
| 2083 | /* Return result. */ |
| 2084 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2085 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2086 | return 1; |
| 2087 | |
| 2088 | connection_closed: |
| 2089 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2090 | xref_unlock(&socket->xref, peer); |
| 2091 | |
| 2092 | no_peer: |
| 2093 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2094 | /* If the buffer containds data. */ |
| 2095 | if (socket->b.n > 0) { |
| 2096 | luaL_pushresult(&socket->b); |
| 2097 | return 1; |
| 2098 | } |
| 2099 | lua_pushnil(L); |
| 2100 | lua_pushstring(L, "connection closed."); |
| 2101 | return 2; |
| 2102 | |
| 2103 | connection_empty: |
| 2104 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2105 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) { |
| 2106 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2107 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2108 | } |
| 2109 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2110 | 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] | 2111 | return 0; |
| 2112 | } |
| 2113 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2114 | /* This Lua function gets two parameters. The first one can be string |
| 2115 | * or a number. If the string is "*l", the user requires one line. If |
| 2116 | * 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] | 2117 | * If the value is a number, the user require a number of bytes equal |
| 2118 | * to the value. The default value is "*l" (a line). |
| 2119 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2120 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2121 | * integer takes this values: |
| 2122 | * -1 : read a line |
| 2123 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2124 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2125 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2126 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2127 | * concatenated with the read data. |
| 2128 | */ |
| 2129 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 2130 | { |
| 2131 | int wanted = HLSR_READ_LINE; |
| 2132 | const char *pattern; |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2133 | int lastarg, type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2134 | char *error; |
| 2135 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2136 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2137 | |
| 2138 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 2139 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 2140 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2141 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2142 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2143 | /* Check if we run on the same thread than the xreator thread. |
| 2144 | * We cannot access to the socket if the thread is different. |
| 2145 | */ |
| 2146 | if (socket->tid != tid) |
| 2147 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2148 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2149 | /* check for pattern. */ |
| 2150 | if (lua_gettop(L) >= 2) { |
| 2151 | type = lua_type(L, 2); |
| 2152 | if (type == LUA_TSTRING) { |
| 2153 | pattern = lua_tostring(L, 2); |
| 2154 | if (strcmp(pattern, "*a") == 0) |
| 2155 | wanted = HLSR_READ_ALL; |
| 2156 | else if (strcmp(pattern, "*l") == 0) |
| 2157 | wanted = HLSR_READ_LINE; |
| 2158 | else { |
| 2159 | wanted = strtoll(pattern, &error, 10); |
| 2160 | if (*error != '\0') |
| 2161 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 2162 | } |
| 2163 | } |
| 2164 | else if (type == LUA_TNUMBER) { |
| 2165 | wanted = lua_tointeger(L, 2); |
| 2166 | if (wanted < 0) |
| 2167 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 2168 | } |
| 2169 | } |
| 2170 | |
| 2171 | /* Set pattern. */ |
| 2172 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 2173 | |
| 2174 | /* Check if we would replace the top by itself. */ |
| 2175 | if (lua_gettop(L) != 2) |
| 2176 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2177 | |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2178 | /* Save index of the top of the stack because since buffers are used, it |
| 2179 | * may change |
| 2180 | */ |
| 2181 | lastarg = lua_gettop(L); |
| 2182 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2183 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2184 | luaL_buffinit(L, &socket->b); |
| 2185 | |
| 2186 | /* Check prefix. */ |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2187 | if (lastarg >= 3) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2188 | if (lua_type(L, 3) != LUA_TSTRING) |
| 2189 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 2190 | pattern = lua_tolstring(L, 3, &len); |
| 2191 | luaL_addlstring(&socket->b, pattern, len); |
| 2192 | } |
| 2193 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2194 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2195 | } |
| 2196 | |
| 2197 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 2198 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2199 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2200 | 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] | 2201 | { |
| 2202 | struct hlua_socket *socket; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2203 | struct hlua *hlua; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2204 | struct appctx *appctx; |
| 2205 | size_t buf_len; |
| 2206 | const char *buf; |
| 2207 | int len; |
| 2208 | int send_len; |
| 2209 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2210 | struct xref *peer; |
| 2211 | struct stream_interface *si; |
| 2212 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2213 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2214 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2215 | hlua = hlua_gethlua(L); |
| 2216 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2217 | /* Check if this lua stack is schedulable. */ |
| 2218 | if (!hlua || !hlua->task) |
| 2219 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 2220 | "'frontend', 'backend' or 'task'")); |
| 2221 | |
| 2222 | /* Get object */ |
| 2223 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2224 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2225 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2226 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2227 | /* Check if we run on the same thread than the xreator thread. |
| 2228 | * We cannot access to the socket if the thread is different. |
| 2229 | */ |
| 2230 | if (socket->tid != tid) |
| 2231 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2232 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2233 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2234 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2235 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2236 | lua_pushinteger(L, -1); |
| 2237 | return 1; |
| 2238 | } |
| 2239 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2240 | si = appctx->owner; |
| 2241 | s = si_strm(si); |
| 2242 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2243 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2244 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2245 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2246 | lua_pushinteger(L, -1); |
| 2247 | return 1; |
| 2248 | } |
| 2249 | |
| 2250 | /* Update the input buffer data. */ |
| 2251 | buf += sent; |
| 2252 | send_len = buf_len - sent; |
| 2253 | |
| 2254 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2255 | if (sent >= buf_len) { |
| 2256 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2257 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2258 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2259 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2260 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2261 | * the request buffer if its not required. |
| 2262 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2263 | if (s->req.buf.size == 0) { |
Willy Tarreau | 581abd3 | 2018-10-25 10:21:41 +0200 | [diff] [blame] | 2264 | if (!si_alloc_ibuf(si, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2265 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2266 | } |
| 2267 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2268 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2269 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2270 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2271 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2272 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2273 | |
| 2274 | /* send data */ |
| 2275 | if (len < send_len) |
| 2276 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2277 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2278 | |
| 2279 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2280 | * the data" (-2) are not expected because the available length |
| 2281 | * is tested. |
| 2282 | * Other unknown error are also not expected. |
| 2283 | */ |
| 2284 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2285 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2286 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2287 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2288 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2289 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2290 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2291 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2292 | return 1; |
| 2293 | } |
| 2294 | |
| 2295 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2296 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2297 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2298 | s->req.rex = TICK_ETERNITY; |
| 2299 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2300 | |
| 2301 | /* Update length sent. */ |
| 2302 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2303 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2304 | |
| 2305 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2306 | if (sent + len >= buf_len) { |
| 2307 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2308 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2309 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2310 | |
| 2311 | hlua_socket_write_yield_return: |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2312 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2313 | xref_unlock(&socket->xref, peer); |
| 2314 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2315 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2316 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2317 | 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] | 2318 | return 0; |
| 2319 | } |
| 2320 | |
| 2321 | /* This function initiate the send of data. It just check the input |
| 2322 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2323 | * 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] | 2324 | * "hlua_socket_write_yield" that can yield. |
| 2325 | * |
| 2326 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2327 | * the associated object. The second is a string buffer. The third is |
| 2328 | * a facultative integer that represents where is the buffer position |
| 2329 | * of the start of the data that can send. The first byte is the |
| 2330 | * position "1". The default value is "1". The fourth argument is a |
| 2331 | * facultative integer that represents where is the buffer position |
| 2332 | * of the end of the data that can send. The default is the last byte. |
| 2333 | */ |
| 2334 | static int hlua_socket_send(struct lua_State *L) |
| 2335 | { |
| 2336 | int i; |
| 2337 | int j; |
| 2338 | const char *buf; |
| 2339 | size_t buf_len; |
| 2340 | |
| 2341 | /* Check number of arguments. */ |
| 2342 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2343 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2344 | |
| 2345 | /* Get the string. */ |
| 2346 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2347 | |
| 2348 | /* Get and check j. */ |
| 2349 | if (lua_gettop(L) == 4) { |
| 2350 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2351 | if (j < 0) |
| 2352 | j = buf_len + j + 1; |
| 2353 | if (j > buf_len) |
| 2354 | j = buf_len + 1; |
| 2355 | lua_pop(L, 1); |
| 2356 | } |
| 2357 | else |
| 2358 | j = buf_len; |
| 2359 | |
| 2360 | /* Get and check i. */ |
| 2361 | if (lua_gettop(L) == 3) { |
| 2362 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2363 | if (i < 0) |
| 2364 | i = buf_len + i + 1; |
| 2365 | if (i > buf_len) |
| 2366 | i = buf_len + 1; |
| 2367 | lua_pop(L, 1); |
| 2368 | } else |
| 2369 | i = 1; |
| 2370 | |
| 2371 | /* Check bth i and j. */ |
| 2372 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2373 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2374 | return 1; |
| 2375 | } |
| 2376 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2377 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2378 | return 1; |
| 2379 | } |
| 2380 | if (i == 0) |
| 2381 | i = 1; |
| 2382 | if (j == 0) |
| 2383 | j = 1; |
| 2384 | |
| 2385 | /* Pop the string. */ |
| 2386 | lua_pop(L, 1); |
| 2387 | |
| 2388 | /* Update the buffer length. */ |
| 2389 | buf += i - 1; |
| 2390 | buf_len = j - i + 1; |
| 2391 | lua_pushlstring(L, buf, buf_len); |
| 2392 | |
| 2393 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2394 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2395 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2396 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2397 | } |
| 2398 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2399 | #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] | 2400 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 2401 | { |
| 2402 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2403 | int ret; |
| 2404 | int len; |
| 2405 | char *p; |
| 2406 | |
| 2407 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2408 | if (ret <= 0) { |
| 2409 | lua_pushnil(L); |
| 2410 | return 1; |
| 2411 | } |
| 2412 | |
| 2413 | if (ret == AF_UNIX) { |
| 2414 | lua_pushstring(L, buffer+1); |
| 2415 | return 1; |
| 2416 | } |
| 2417 | else if (ret == AF_INET6) { |
| 2418 | buffer[0] = '['; |
| 2419 | len = strlen(buffer); |
| 2420 | buffer[len] = ']'; |
| 2421 | len++; |
| 2422 | buffer[len] = ':'; |
| 2423 | len++; |
| 2424 | p = buffer; |
| 2425 | } |
| 2426 | else if (ret == AF_INET) { |
| 2427 | p = buffer + 1; |
| 2428 | len = strlen(p); |
| 2429 | p[len] = ':'; |
| 2430 | len++; |
| 2431 | } |
| 2432 | else { |
| 2433 | lua_pushnil(L); |
| 2434 | return 1; |
| 2435 | } |
| 2436 | |
| 2437 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2438 | lua_pushnil(L); |
| 2439 | return 1; |
| 2440 | } |
| 2441 | |
| 2442 | lua_pushstring(L, p); |
| 2443 | return 1; |
| 2444 | } |
| 2445 | |
| 2446 | /* Returns information about the peer of the connection. */ |
| 2447 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2448 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2449 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2450 | struct xref *peer; |
| 2451 | struct appctx *appctx; |
| 2452 | struct stream_interface *si; |
| 2453 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2454 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2455 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2456 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2457 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2458 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2459 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2460 | /* Check if we run on the same thread than the xreator thread. |
| 2461 | * We cannot access to the socket if the thread is different. |
| 2462 | */ |
| 2463 | if (socket->tid != tid) |
| 2464 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2465 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2466 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2467 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2468 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2469 | lua_pushnil(L); |
| 2470 | return 1; |
| 2471 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2472 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2473 | si = appctx->owner; |
| 2474 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2475 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2476 | if (!s->target_addr) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2477 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2478 | lua_pushnil(L); |
| 2479 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2480 | } |
| 2481 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2482 | ret = MAY_LJMP(hlua_socket_info(L, s->target_addr)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2483 | xref_unlock(&socket->xref, peer); |
| 2484 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2485 | } |
| 2486 | |
| 2487 | /* Returns information about my connection side. */ |
| 2488 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2489 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2490 | struct hlua_socket *socket; |
| 2491 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2492 | struct appctx *appctx; |
| 2493 | struct xref *peer; |
| 2494 | struct stream_interface *si; |
| 2495 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2496 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2497 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2498 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2499 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2500 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2501 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2502 | /* Check if we run on the same thread than the xreator thread. |
| 2503 | * We cannot access to the socket if the thread is different. |
| 2504 | */ |
| 2505 | if (socket->tid != tid) |
| 2506 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2507 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2508 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2509 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2510 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2511 | lua_pushnil(L); |
| 2512 | return 1; |
| 2513 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2514 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2515 | si = appctx->owner; |
| 2516 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2517 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2518 | conn = cs_conn(objt_cs(s->si[1].end)); |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2519 | if (!conn || !conn_get_src(conn)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2520 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2521 | lua_pushnil(L); |
| 2522 | return 1; |
| 2523 | } |
| 2524 | |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2525 | ret = hlua_socket_info(L, conn->src); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2526 | xref_unlock(&socket->xref, peer); |
| 2527 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2528 | } |
| 2529 | |
| 2530 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2531 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2532 | .obj_type = OBJ_TYPE_APPLET, |
| 2533 | .name = "<LUA_TCP>", |
| 2534 | .fct = hlua_socket_handler, |
| 2535 | .release = hlua_socket_release, |
| 2536 | }; |
| 2537 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2538 | __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] | 2539 | { |
| 2540 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2541 | struct hlua *hlua; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2542 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2543 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2544 | struct stream_interface *si; |
| 2545 | struct stream *s; |
| 2546 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2547 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2548 | hlua = hlua_gethlua(L); |
| 2549 | if (!hlua) |
| 2550 | return 0; |
| 2551 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2552 | /* Check if we run on the same thread than the xreator thread. |
| 2553 | * We cannot access to the socket if the thread is different. |
| 2554 | */ |
| 2555 | if (socket->tid != tid) |
| 2556 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2557 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2558 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2559 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2560 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2561 | lua_pushnil(L); |
| 2562 | lua_pushstring(L, "Can't connect"); |
| 2563 | return 2; |
| 2564 | } |
| 2565 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2566 | si = appctx->owner; |
| 2567 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2568 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2569 | /* Check if we run on the same thread than the xreator thread. |
| 2570 | * We cannot access to the socket if the thread is different. |
| 2571 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2572 | if (socket->tid != tid) { |
| 2573 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2574 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2575 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2576 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2577 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2578 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2579 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2580 | lua_pushnil(L); |
| 2581 | lua_pushstring(L, "Can't connect"); |
| 2582 | return 2; |
| 2583 | } |
| 2584 | |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2585 | appctx = __objt_appctx(s->si[0].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2586 | |
| 2587 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2588 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2589 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2590 | lua_pushinteger(L, 1); |
| 2591 | return 1; |
| 2592 | } |
| 2593 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2594 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2595 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2596 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2597 | } |
| 2598 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2599 | 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] | 2600 | return 0; |
| 2601 | } |
| 2602 | |
| 2603 | /* This function fail or initite the connection. */ |
| 2604 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2605 | { |
| 2606 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2607 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2608 | const char *ip; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2609 | struct hlua *hlua; |
| 2610 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2611 | int low, high; |
| 2612 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2613 | struct xref *peer; |
| 2614 | struct stream_interface *si; |
| 2615 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2616 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2617 | if (lua_gettop(L) < 2) |
| 2618 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2619 | |
| 2620 | /* Get args. */ |
| 2621 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2622 | |
| 2623 | /* Check if we run on the same thread than the xreator thread. |
| 2624 | * We cannot access to the socket if the thread is different. |
| 2625 | */ |
| 2626 | if (socket->tid != tid) |
| 2627 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2628 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2629 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2630 | if (lua_gettop(L) >= 3) { |
| 2631 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2632 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2633 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2634 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2635 | * that are not enclosed within square brackets. |
| 2636 | */ |
| 2637 | if (port > 0) { |
| 2638 | luaL_buffinit(L, &b); |
| 2639 | luaL_addstring(&b, ip); |
| 2640 | luaL_addchar(&b, ':'); |
| 2641 | luaL_pushresult(&b); |
| 2642 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2643 | } |
| 2644 | } |
| 2645 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2646 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2647 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2648 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2649 | lua_pushnil(L); |
| 2650 | return 1; |
| 2651 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2652 | |
| 2653 | /* Parse ip address. */ |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 2654 | 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] | 2655 | if (!addr) { |
| 2656 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2657 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2658 | } |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2659 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2660 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2661 | if (low == 0) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2662 | if (addr->ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2663 | if (port == -1) { |
| 2664 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2665 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2666 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2667 | ((struct sockaddr_in *)addr)->sin_port = htons(port); |
| 2668 | } else if (addr->ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2669 | if (port == -1) { |
| 2670 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2671 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2672 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2673 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(port); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2674 | } |
| 2675 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2676 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2677 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2678 | si = appctx->owner; |
| 2679 | s = si_strm(si); |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2680 | |
Willy Tarreau | 9b7587a | 2020-10-15 07:32:10 +0200 | [diff] [blame] | 2681 | if (!sockaddr_alloc(&s->target_addr, addr, sizeof(*addr))) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2682 | xref_unlock(&socket->xref, peer); |
| 2683 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
| 2684 | } |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2685 | s->flags |= SF_ADDR_SET; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2686 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2687 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2688 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2689 | if (!hlua) |
| 2690 | return 0; |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2691 | |
| 2692 | /* inform the stream that we want to be notified whenever the |
| 2693 | * connection completes. |
| 2694 | */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 2695 | si_cant_get(&s->si[0]); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2696 | si_rx_endp_more(&s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2697 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2698 | |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2699 | hlua->gc_count++; |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2700 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2701 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2702 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2703 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2704 | } |
| 2705 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2706 | |
| 2707 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2708 | /* Return yield waiting for connection. */ |
| 2709 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2710 | 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] | 2711 | |
| 2712 | return 0; |
| 2713 | } |
| 2714 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2715 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2716 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2717 | { |
| 2718 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2719 | struct xref *peer; |
| 2720 | struct appctx *appctx; |
| 2721 | struct stream_interface *si; |
| 2722 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2723 | |
| 2724 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2725 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2726 | |
| 2727 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2728 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2729 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2730 | lua_pushnil(L); |
| 2731 | return 1; |
| 2732 | } |
| 2733 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2734 | si = appctx->owner; |
| 2735 | s = si_strm(si); |
| 2736 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 2737 | s->target = &socket_ssl->obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2738 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2739 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2740 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2741 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2742 | |
| 2743 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2744 | { |
| 2745 | return 0; |
| 2746 | } |
| 2747 | |
| 2748 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2749 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2750 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2751 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2752 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2753 | struct xref *peer; |
| 2754 | struct appctx *appctx; |
| 2755 | struct stream_interface *si; |
| 2756 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2757 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2758 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2759 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2760 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2761 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2762 | /* convert the timeout to millis */ |
| 2763 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2764 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2765 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2766 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2767 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2768 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2769 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2770 | 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] | 2771 | |
| 2772 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2773 | if (tmout == 0) |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2774 | tmout++; /* very small timeouts are adjusted to a minimum of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2775 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2776 | /* Check if we run on the same thread than the xreator thread. |
| 2777 | * We cannot access to the socket if the thread is different. |
| 2778 | */ |
| 2779 | if (socket->tid != tid) |
| 2780 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2781 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2782 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2783 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2784 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2785 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2786 | WILL_LJMP(lua_error(L)); |
| 2787 | return 0; |
| 2788 | } |
| 2789 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2790 | si = appctx->owner; |
| 2791 | s = si_strm(si); |
| 2792 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2793 | s->sess->fe->timeout.connect = tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2794 | s->req.rto = tmout; |
| 2795 | s->req.wto = tmout; |
| 2796 | s->res.rto = tmout; |
| 2797 | s->res.wto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2798 | s->req.rex = tick_add_ifset(now_ms, tmout); |
| 2799 | s->req.wex = tick_add_ifset(now_ms, tmout); |
| 2800 | s->res.rex = tick_add_ifset(now_ms, tmout); |
| 2801 | s->res.wex = tick_add_ifset(now_ms, tmout); |
| 2802 | |
| 2803 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 2804 | task_queue(s->task); |
| 2805 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2806 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2807 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 2808 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 2809 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2810 | } |
| 2811 | |
| 2812 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2813 | { |
| 2814 | struct hlua_socket *socket; |
| 2815 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2816 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2817 | struct stream *strm; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2818 | |
| 2819 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2820 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2821 | hlua_pusherror(L, "socket: full stack"); |
| 2822 | goto out_fail_conf; |
| 2823 | } |
| 2824 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2825 | /* Create the object: obj[0] = userdata. */ |
| 2826 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2827 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2828 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2829 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2830 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2831 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2832 | /* Check if the various memory pools are initialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2833 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2834 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2835 | goto out_fail_conf; |
| 2836 | } |
| 2837 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2838 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2839 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2840 | lua_setmetatable(L, -2); |
| 2841 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2842 | /* Create the applet context */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 2843 | appctx = appctx_new(&update_applet, tid_bit); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2844 | if (!appctx) { |
| 2845 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2846 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2847 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2848 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2849 | appctx->ctx.hlua_cosocket.connected = 0; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2850 | appctx->ctx.hlua_cosocket.die = 0; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2851 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 2852 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2853 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2854 | /* Now create a session, task and stream for this applet */ |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 2855 | sess = session_new(socket_proxy, NULL, &appctx->obj_type); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2856 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2857 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2858 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2859 | } |
| 2860 | |
Christopher Faulet | 26256f8 | 2020-09-14 11:40:13 +0200 | [diff] [blame] | 2861 | strm = stream_new(sess, &appctx->obj_type, &BUF_NULL); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2862 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2863 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2864 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2865 | } |
| 2866 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2867 | /* Initialise cross reference between stream and Lua socket object. */ |
| 2868 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2869 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2870 | /* Configure "right" stream interface. this "si" is used to connect |
| 2871 | * and retrieve data from the server. The connection is initialized |
| 2872 | * with the "struct server". |
| 2873 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2874 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2875 | |
| 2876 | /* Force destination server. */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2877 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED; |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 2878 | strm->target = &socket_tcp->obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2879 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2880 | return 1; |
| 2881 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2882 | out_fail_stream: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 2883 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2884 | out_fail_sess: |
| 2885 | appctx_free(appctx); |
| 2886 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2887 | WILL_LJMP(lua_error(L)); |
| 2888 | return 0; |
| 2889 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2890 | |
| 2891 | /* |
| 2892 | * |
| 2893 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2894 | * Class Channel |
| 2895 | * |
| 2896 | * |
| 2897 | */ |
| 2898 | |
| 2899 | /* Returns the struct hlua_channel join to the class channel in the |
| 2900 | * stack entry "ud" or throws an argument error. |
| 2901 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2902 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2903 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2904 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2905 | } |
| 2906 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2907 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 2908 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2909 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 2910 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2911 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2912 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2913 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2914 | return 0; |
| 2915 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2916 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2917 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2918 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2919 | |
| 2920 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 2921 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 2922 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2923 | return 1; |
| 2924 | } |
| 2925 | |
| 2926 | /* Duplicate all the data present in the input channel and put it |
| 2927 | * in a string LUA variables. Returns -1 and push a nil value in |
| 2928 | * the stack if the channel is closed and all the data are consumed, |
| 2929 | * returns 0 if no data are available, otherwise it returns the length |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 2930 | * of the built string. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2931 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2932 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2933 | { |
| 2934 | char *blk1; |
| 2935 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2936 | size_t len1; |
| 2937 | size_t len2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2938 | int ret; |
| 2939 | luaL_Buffer b; |
| 2940 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2941 | ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2942 | if (unlikely(ret == 0)) |
| 2943 | return 0; |
| 2944 | |
| 2945 | if (unlikely(ret < 0)) { |
| 2946 | lua_pushnil(L); |
| 2947 | return -1; |
| 2948 | } |
| 2949 | |
| 2950 | luaL_buffinit(L, &b); |
| 2951 | luaL_addlstring(&b, blk1, len1); |
| 2952 | if (unlikely(ret == 2)) |
| 2953 | luaL_addlstring(&b, blk2, len2); |
| 2954 | luaL_pushresult(&b); |
| 2955 | |
| 2956 | if (unlikely(ret == 2)) |
| 2957 | return len1 + len2; |
| 2958 | return len1; |
| 2959 | } |
| 2960 | |
| 2961 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2962 | * a yield. This function keep the data in the buffer. |
| 2963 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2964 | __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] | 2965 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2966 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2967 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2968 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2969 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 2970 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2971 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2972 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2973 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2974 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2975 | if (_hlua_channel_dup(chn, L) == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2976 | 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] | 2977 | return 1; |
| 2978 | } |
| 2979 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2980 | /* Check arguments for the function "hlua_channel_dup_yield". */ |
| 2981 | __LJMP static int hlua_channel_dup(lua_State *L) |
| 2982 | { |
| 2983 | MAY_LJMP(check_args(L, 1, "dup")); |
| 2984 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2985 | return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0)); |
| 2986 | } |
| 2987 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2988 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2989 | * a yield. This function consumes the data in the buffer. It returns |
| 2990 | * a string containing the data or a nil pointer if no data are available |
| 2991 | * and the channel is closed. |
| 2992 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2993 | __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] | 2994 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2995 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2996 | int ret; |
| 2997 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2998 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2999 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3000 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3001 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3002 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3003 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3004 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3005 | ret = _hlua_channel_dup(chn, L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3006 | if (unlikely(ret == 0)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3007 | 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] | 3008 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3009 | if (unlikely(ret == -1)) |
| 3010 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3011 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3012 | b_sub(&chn->buf, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3013 | return 1; |
| 3014 | } |
| 3015 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3016 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3017 | __LJMP static int hlua_channel_get(lua_State *L) |
| 3018 | { |
| 3019 | MAY_LJMP(check_args(L, 1, "get")); |
| 3020 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3021 | return MAY_LJMP(hlua_channel_get_yield(L, 0, 0)); |
| 3022 | } |
| 3023 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3024 | /* This functions consumes and returns one line. If the channel is closed, |
| 3025 | * 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] | 3026 | * 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] | 3027 | * value. |
| 3028 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3029 | __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] | 3030 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3031 | char *blk1; |
| 3032 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3033 | size_t len1; |
| 3034 | size_t len2; |
| 3035 | size_t len; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3036 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3037 | int ret; |
| 3038 | luaL_Buffer b; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3039 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3040 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3041 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3042 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3043 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3044 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3045 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3046 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3047 | ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3048 | if (ret == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3049 | 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] | 3050 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3051 | if (ret == -1) { |
| 3052 | lua_pushnil(L); |
| 3053 | return 1; |
| 3054 | } |
| 3055 | |
| 3056 | luaL_buffinit(L, &b); |
| 3057 | luaL_addlstring(&b, blk1, len1); |
| 3058 | len = len1; |
| 3059 | if (unlikely(ret == 2)) { |
| 3060 | luaL_addlstring(&b, blk2, len2); |
| 3061 | len += len2; |
| 3062 | } |
| 3063 | luaL_pushresult(&b); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3064 | 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] | 3065 | return 1; |
| 3066 | } |
| 3067 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3068 | /* Check arguments for the function "hlua_channel_getline_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3069 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 3070 | { |
| 3071 | MAY_LJMP(check_args(L, 1, "getline")); |
| 3072 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3073 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 3074 | } |
| 3075 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3076 | /* This function takes a string as input, and append it at the |
| 3077 | * input side of channel. If the data is too big, but a space |
| 3078 | * is probably available after sending some data, the function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3079 | * yields. If the data is bigger than the buffer, or if the |
| 3080 | * channel is closed, it returns -1. Otherwise, it returns the |
| 3081 | * amount of data written. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3082 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3083 | __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] | 3084 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3085 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3086 | size_t len; |
| 3087 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3088 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3089 | int ret; |
| 3090 | int max; |
| 3091 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3092 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3093 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3094 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3095 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3096 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3097 | /* Check if the buffer is available because HAProxy doesn't allocate |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 3098 | * the request buffer if its not required. |
| 3099 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3100 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 3101 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3102 | 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] | 3103 | } |
| 3104 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3105 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3106 | if (max > len - l) |
| 3107 | max = len - l; |
| 3108 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3109 | ret = ci_putblk(chn, str + l, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3110 | if (ret == -2 || ret == -3) { |
| 3111 | lua_pushinteger(L, -1); |
| 3112 | return 1; |
| 3113 | } |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 3114 | if (ret == -1) { |
| 3115 | chn->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3116 | 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] | 3117 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3118 | l += ret; |
| 3119 | lua_pop(L, 1); |
| 3120 | lua_pushinteger(L, l); |
| 3121 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3122 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3123 | if (max == 0 && co_data(chn) == 0) { |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3124 | /* There are no space available, and the output buffer is empty. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3125 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3126 | * we return the amount of copied data. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3127 | */ |
| 3128 | return 1; |
| 3129 | } |
| 3130 | if (l < len) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3131 | 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] | 3132 | return 1; |
| 3133 | } |
| 3134 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3135 | /* Just a wrapper of "hlua_channel_append_yield". It returns the length |
| 3136 | * 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] | 3137 | * buffer size is too little for the data. |
| 3138 | */ |
| 3139 | __LJMP static int hlua_channel_append(lua_State *L) |
| 3140 | { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3141 | size_t len; |
| 3142 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3143 | MAY_LJMP(check_args(L, 2, "append")); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3144 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3145 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3146 | MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3147 | lua_pushinteger(L, 0); |
| 3148 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3149 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3150 | } |
| 3151 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3152 | /* Just a wrapper of "hlua_channel_append_yield". This wrapper starts |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3153 | * his process by cleaning the buffer. The result is a replacement |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3154 | * of the current data. It returns the length of the written string, |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3155 | * or -1 if the channel is closed or if the buffer size is too |
| 3156 | * little for the data. |
| 3157 | */ |
| 3158 | __LJMP static int hlua_channel_set(lua_State *L) |
| 3159 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3160 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3161 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3162 | MAY_LJMP(check_args(L, 2, "set")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3163 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3164 | lua_pushinteger(L, 0); |
| 3165 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3166 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3167 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3168 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3169 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3170 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3171 | b_set_data(&chn->buf, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3172 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3173 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3174 | } |
| 3175 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3176 | /* Append data in the output side of the buffer. This data is immediately |
| 3177 | * sent. The function returns the amount of data written. If the buffer |
| 3178 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3179 | * if the channel is closed. |
| 3180 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3181 | __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] | 3182 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3183 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3184 | size_t len; |
| 3185 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3186 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3187 | int max; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3188 | struct hlua *hlua; |
| 3189 | |
| 3190 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3191 | hlua = hlua_gethlua(L); |
| 3192 | if (!hlua) { |
| 3193 | lua_pushnil(L); |
| 3194 | return 1; |
| 3195 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3196 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3197 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3198 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3199 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3200 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3201 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3202 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3203 | lua_pushinteger(L, -1); |
| 3204 | return 1; |
| 3205 | } |
| 3206 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3207 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 3208 | * the request buffer if its not required. |
| 3209 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3210 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 3211 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3212 | 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] | 3213 | } |
| 3214 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3215 | /* The written data will be immediately sent, so we can check |
| 3216 | * the available space without taking in account the reserve. |
| 3217 | * The reserve is guaranteed for the processing of incoming |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3218 | * data, because the buffer will be flushed. |
| 3219 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3220 | max = b_room(&chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3221 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3222 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3223 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3224 | * we return the amount of copied data. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3225 | */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3226 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3227 | return 1; |
| 3228 | |
| 3229 | /* Adjust the real required length. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3230 | if (max > len - l) |
| 3231 | max = len - l; |
| 3232 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3233 | /* The buffer available size may be not contiguous. This test |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3234 | * detects a non contiguous buffer and realign it. |
| 3235 | */ |
Willy Tarreau | 3f67999 | 2018-06-15 15:06:42 +0200 | [diff] [blame] | 3236 | if (ci_space_for_replace(chn) < max) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3237 | channel_slow_realign(chn, trash.area); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3238 | |
| 3239 | /* Copy input data in the buffer. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3240 | 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] | 3241 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3242 | /* buffer replace considers that the input part is filled. |
| 3243 | * so, I must forward these new data in the output part. |
| 3244 | */ |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 3245 | c_adv(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3246 | |
| 3247 | l += max; |
| 3248 | lua_pop(L, 1); |
| 3249 | lua_pushinteger(L, l); |
| 3250 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3251 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3252 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3253 | * we return the amount of copied data. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3254 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3255 | max = b_room(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3256 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3257 | return 1; |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3258 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3259 | if (l < len) { |
| 3260 | /* If we are waiting for space in the response buffer, we |
| 3261 | * must set the flag WAKERESWR. This flag required the task |
| 3262 | * wake up if any activity is detected on the response buffer. |
| 3263 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3264 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3265 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3266 | else |
| 3267 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3268 | 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] | 3269 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3270 | |
| 3271 | return 1; |
| 3272 | } |
| 3273 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3274 | /* Just a wrapper of "_hlua_channel_send". This wrapper permits |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3275 | * yield the LUA process, and resume it without checking the |
| 3276 | * input arguments. |
| 3277 | */ |
| 3278 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3279 | { |
| 3280 | MAY_LJMP(check_args(L, 2, "send")); |
| 3281 | lua_pushinteger(L, 0); |
| 3282 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3283 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3284 | } |
| 3285 | |
| 3286 | /* This function forward and amount of butes. The data pass from |
| 3287 | * the input side of the buffer to the output side, and can be |
| 3288 | * forwarded. This function never fails. |
| 3289 | * |
| 3290 | * The Lua function takes an amount of bytes to be forwarded in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3291 | * input. It returns the number of bytes forwarded. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3292 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3293 | __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] | 3294 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3295 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3296 | int len; |
| 3297 | int l; |
| 3298 | int max; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3299 | struct hlua *hlua; |
| 3300 | |
| 3301 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3302 | hlua = hlua_gethlua(L); |
| 3303 | if (!hlua) |
| 3304 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3305 | |
| 3306 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3307 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3308 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3309 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3310 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3311 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3312 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3313 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3314 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3315 | |
| 3316 | max = len - l; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3317 | if (max > ci_data(chn)) |
| 3318 | max = ci_data(chn); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3319 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3320 | l += max; |
| 3321 | |
| 3322 | lua_pop(L, 1); |
| 3323 | lua_pushinteger(L, l); |
| 3324 | |
| 3325 | /* Check if it miss bytes to forward. */ |
| 3326 | if (l < len) { |
| 3327 | /* The the input channel or the output channel are closed, we |
| 3328 | * must return the amount of data forwarded. |
| 3329 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3330 | if (channel_input_closed(chn) || channel_output_closed(chn)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3331 | return 1; |
| 3332 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3333 | /* If we are waiting for space data in the response buffer, we |
| 3334 | * must set the flag WAKERESWR. This flag required the task |
| 3335 | * wake up if any activity is detected on the response buffer. |
| 3336 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3337 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3338 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3339 | else |
| 3340 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3341 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3342 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3343 | 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] | 3344 | } |
| 3345 | |
| 3346 | return 1; |
| 3347 | } |
| 3348 | |
| 3349 | /* Just check the input and prepare the stack for the previous |
| 3350 | * function "hlua_channel_forward_yield" |
| 3351 | */ |
| 3352 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3353 | { |
| 3354 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3355 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3356 | MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3357 | |
| 3358 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3359 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3360 | } |
| 3361 | |
| 3362 | /* Just returns the number of bytes available in the input |
| 3363 | * side of the buffer. This function never fails. |
| 3364 | */ |
| 3365 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3366 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3367 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3368 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3369 | MAY_LJMP(check_args(L, 1, "get_in_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3370 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3371 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3372 | struct htx *htx = htxbuf(&chn->buf); |
| 3373 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 3374 | } |
| 3375 | else |
| 3376 | lua_pushinteger(L, ci_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3377 | return 1; |
| 3378 | } |
| 3379 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3380 | /* Returns true if the channel is full. */ |
| 3381 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3382 | { |
| 3383 | struct channel *chn; |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3384 | |
| 3385 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3386 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 0ec740e | 2020-02-26 11:59:19 +0100 | [diff] [blame] | 3387 | /* ignore the reserve, we are not on a producer side (ie in an |
| 3388 | * applet). |
| 3389 | */ |
| 3390 | lua_pushboolean(L, channel_full(chn, 0)); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3391 | return 1; |
| 3392 | } |
| 3393 | |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 3394 | /* Returns true if the channel is the response channel. */ |
| 3395 | __LJMP static int hlua_channel_is_resp(lua_State *L) |
| 3396 | { |
| 3397 | struct channel *chn; |
| 3398 | |
| 3399 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 3400 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3401 | |
| 3402 | lua_pushboolean(L, !!(chn->flags & CF_ISRESP)); |
| 3403 | return 1; |
| 3404 | } |
| 3405 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3406 | /* Just returns the number of bytes available in the output |
| 3407 | * side of the buffer. This function never fails. |
| 3408 | */ |
| 3409 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 3410 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3411 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3412 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3413 | MAY_LJMP(check_args(L, 1, "get_out_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3414 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3415 | lua_pushinteger(L, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3416 | return 1; |
| 3417 | } |
| 3418 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3419 | /* |
| 3420 | * |
| 3421 | * |
| 3422 | * Class Fetches |
| 3423 | * |
| 3424 | * |
| 3425 | */ |
| 3426 | |
| 3427 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3428 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3429 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3430 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3431 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3432 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3433 | } |
| 3434 | |
| 3435 | /* This function creates and push in the stack a fetch object according |
| 3436 | * with a current TXN. |
| 3437 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3438 | 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] | 3439 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3440 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3441 | |
| 3442 | /* Check stack size. */ |
| 3443 | if (!lua_checkstack(L, 3)) |
| 3444 | return 0; |
| 3445 | |
| 3446 | /* Create the object: obj[0] = userdata. |
| 3447 | * Note that the base of the Fetches object is the |
| 3448 | * transaction object. |
| 3449 | */ |
| 3450 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3451 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3452 | lua_rawseti(L, -2, 0); |
| 3453 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3454 | hsmp->s = txn->s; |
| 3455 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3456 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3457 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3458 | |
| 3459 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3460 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 3461 | lua_setmetatable(L, -2); |
| 3462 | |
| 3463 | return 1; |
| 3464 | } |
| 3465 | |
| 3466 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 3467 | * It uses closure argument to store the associated sample-fetch. It |
| 3468 | * returns only one argument or throws an error. An error is thrown |
| 3469 | * only if an error is encountered during the argument parsing. If |
| 3470 | * the "sample-fetch" function fails, nil is returned. |
| 3471 | */ |
| 3472 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 3473 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3474 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3475 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3476 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3477 | int i; |
| 3478 | struct sample smp; |
| 3479 | |
| 3480 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3481 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3482 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3483 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3484 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3485 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 3486 | /* Check execution authorization. */ |
| 3487 | if (f->use & SMP_USE_HTTP_ANY && |
| 3488 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 3489 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 3490 | "is not available in Lua services", f->kw); |
| 3491 | WILL_LJMP(lua_error(L)); |
| 3492 | } |
| 3493 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3494 | /* Get extra arguments. */ |
| 3495 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 3496 | if (i >= ARGM_NBARGS) |
| 3497 | break; |
| 3498 | hlua_lua2arg(L, i + 2, &args[i]); |
| 3499 | } |
| 3500 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3501 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3502 | |
| 3503 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3504 | 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] | 3505 | |
| 3506 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3507 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3508 | lua_pushfstring(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3509 | goto error; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3510 | } |
| 3511 | |
| 3512 | /* Initialise the sample. */ |
| 3513 | memset(&smp, 0, sizeof(smp)); |
| 3514 | |
| 3515 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3516 | 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] | 3517 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3518 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3519 | lua_pushstring(L, ""); |
| 3520 | else |
| 3521 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3522 | goto end; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3523 | } |
| 3524 | |
| 3525 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3526 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3527 | hlua_smp2lua_str(L, &smp); |
| 3528 | else |
| 3529 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3530 | |
| 3531 | end: |
| 3532 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3533 | if (args[i].type == ARGT_STR) |
| 3534 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3535 | else if (args[i].type == ARGT_REG) |
| 3536 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3537 | } |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3538 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3539 | |
| 3540 | error: |
| 3541 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3542 | if (args[i].type == ARGT_STR) |
| 3543 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3544 | else if (args[i].type == ARGT_REG) |
| 3545 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3546 | } |
| 3547 | WILL_LJMP(lua_error(L)); |
| 3548 | return 0; /* Never reached */ |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3549 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3550 | |
| 3551 | /* |
| 3552 | * |
| 3553 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3554 | * Class Converters |
| 3555 | * |
| 3556 | * |
| 3557 | */ |
| 3558 | |
| 3559 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3560 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3561 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3562 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3563 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3564 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3565 | } |
| 3566 | |
| 3567 | /* This function creates and push in the stack a Converters object |
| 3568 | * according with a current TXN. |
| 3569 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3570 | 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] | 3571 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3572 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3573 | |
| 3574 | /* Check stack size. */ |
| 3575 | if (!lua_checkstack(L, 3)) |
| 3576 | return 0; |
| 3577 | |
| 3578 | /* Create the object: obj[0] = userdata. |
| 3579 | * Note that the base of the Converters object is the |
| 3580 | * same than the TXN object. |
| 3581 | */ |
| 3582 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3583 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3584 | lua_rawseti(L, -2, 0); |
| 3585 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3586 | hsmp->s = txn->s; |
| 3587 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3588 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3589 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3590 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3591 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3592 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 3593 | lua_setmetatable(L, -2); |
| 3594 | |
| 3595 | return 1; |
| 3596 | } |
| 3597 | |
| 3598 | /* This function is an LUA binding. It is called with each converter. |
| 3599 | * It uses closure argument to store the associated converter. It |
| 3600 | * returns only one argument or throws an error. An error is thrown |
| 3601 | * only if an error is encountered during the argument parsing. If |
| 3602 | * the converter function function fails, nil is returned. |
| 3603 | */ |
| 3604 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 3605 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3606 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3607 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3608 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3609 | int i; |
| 3610 | struct sample smp; |
| 3611 | |
| 3612 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3613 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3614 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3615 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3616 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3617 | |
| 3618 | /* Get extra arguments. */ |
| 3619 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 3620 | if (i >= ARGM_NBARGS) |
| 3621 | break; |
| 3622 | hlua_lua2arg(L, i + 3, &args[i]); |
| 3623 | } |
| 3624 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3625 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3626 | |
| 3627 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3628 | 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] | 3629 | |
| 3630 | /* Run the special args checker. */ |
| 3631 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 3632 | hlua_pusherror(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3633 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3634 | } |
| 3635 | |
| 3636 | /* Initialise the sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 3637 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3638 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 3639 | hlua_pusherror(L, "error in the input argument"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3640 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3641 | } |
| 3642 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3643 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 3644 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3645 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3646 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3647 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3648 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3649 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3650 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3651 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 3652 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3653 | hlua_pusherror(L, "error during the input argument casting"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3654 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3655 | } |
| 3656 | |
| 3657 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3658 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3659 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3660 | lua_pushstring(L, ""); |
| 3661 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3662 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3663 | goto end; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3664 | } |
| 3665 | |
| 3666 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3667 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3668 | hlua_smp2lua_str(L, &smp); |
| 3669 | else |
| 3670 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3671 | end: |
| 3672 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3673 | if (args[i].type == ARGT_STR) |
| 3674 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3675 | else if (args[i].type == ARGT_REG) |
| 3676 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3677 | } |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3678 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3679 | |
| 3680 | error: |
| 3681 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3682 | if (args[i].type == ARGT_STR) |
| 3683 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3684 | else if (args[i].type == ARGT_REG) |
| 3685 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3686 | } |
| 3687 | WILL_LJMP(lua_error(L)); |
| 3688 | return 0; /* Never reached */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3689 | } |
| 3690 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3691 | /* |
| 3692 | * |
| 3693 | * |
| 3694 | * Class AppletTCP |
| 3695 | * |
| 3696 | * |
| 3697 | */ |
| 3698 | |
| 3699 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 3700 | * a class stream, otherwise it throws an error. |
| 3701 | */ |
| 3702 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 3703 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3704 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3705 | } |
| 3706 | |
| 3707 | /* This function creates and push in the stack an Applet object |
| 3708 | * according with a current TXN. |
| 3709 | */ |
| 3710 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 3711 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3712 | struct hlua_appctx *luactx; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3713 | struct stream_interface *si = ctx->owner; |
| 3714 | struct stream *s = si_strm(si); |
| 3715 | struct proxy *p = s->be; |
| 3716 | |
| 3717 | /* Check stack size. */ |
| 3718 | if (!lua_checkstack(L, 3)) |
| 3719 | return 0; |
| 3720 | |
| 3721 | /* Create the object: obj[0] = userdata. |
| 3722 | * Note that the base of the Converters object is the |
| 3723 | * same than the TXN object. |
| 3724 | */ |
| 3725 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3726 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3727 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3728 | luactx->appctx = ctx; |
| 3729 | luactx->htxn.s = s; |
| 3730 | luactx->htxn.p = p; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3731 | |
| 3732 | /* Create the "f" field that contains a list of fetches. */ |
| 3733 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3734 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3735 | return 0; |
| 3736 | lua_settable(L, -3); |
| 3737 | |
| 3738 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3739 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3740 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3741 | return 0; |
| 3742 | lua_settable(L, -3); |
| 3743 | |
| 3744 | /* Create the "c" field that contains a list of converters. */ |
| 3745 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3746 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3747 | return 0; |
| 3748 | lua_settable(L, -3); |
| 3749 | |
| 3750 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3751 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3752 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3753 | return 0; |
| 3754 | lua_settable(L, -3); |
| 3755 | |
| 3756 | /* Pop a class stream metatable and affect it to the table. */ |
| 3757 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 3758 | lua_setmetatable(L, -2); |
| 3759 | |
| 3760 | return 1; |
| 3761 | } |
| 3762 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3763 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 3764 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3765 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3766 | struct stream *s; |
| 3767 | const char *name; |
| 3768 | size_t len; |
| 3769 | struct sample smp; |
| 3770 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3771 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 3772 | 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] | 3773 | |
| 3774 | /* It is useles to retrieve the stream, but this function |
| 3775 | * runs only in a stream context. |
| 3776 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3777 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3778 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3779 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3780 | |
| 3781 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 3782 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3783 | hlua_lua2smp(L, 3, &smp); |
| 3784 | |
| 3785 | /* Store the sample in a variable. */ |
| 3786 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3787 | |
| 3788 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 3789 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 3790 | else |
| 3791 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 3792 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 3793 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3794 | } |
| 3795 | |
| 3796 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 3797 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3798 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3799 | struct stream *s; |
| 3800 | const char *name; |
| 3801 | size_t len; |
| 3802 | struct sample smp; |
| 3803 | |
| 3804 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3805 | |
| 3806 | /* It is useles to retrieve the stream, but this function |
| 3807 | * runs only in a stream context. |
| 3808 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3809 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3810 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3811 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3812 | |
| 3813 | /* Unset the variable. */ |
| 3814 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 3815 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 3816 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3817 | } |
| 3818 | |
| 3819 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 3820 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3821 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3822 | struct stream *s; |
| 3823 | const char *name; |
| 3824 | size_t len; |
| 3825 | struct sample smp; |
| 3826 | |
| 3827 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3828 | |
| 3829 | /* It is useles to retrieve the stream, but this function |
| 3830 | * runs only in a stream context. |
| 3831 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3832 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3833 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3834 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3835 | |
| 3836 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3837 | if (!vars_get_by_name(name, len, &smp)) { |
| 3838 | lua_pushnil(L); |
| 3839 | return 1; |
| 3840 | } |
| 3841 | |
| 3842 | return hlua_smp2lua(L, &smp); |
| 3843 | } |
| 3844 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3845 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 3846 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3847 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3848 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3849 | struct hlua *hlua; |
| 3850 | |
| 3851 | /* 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] | 3852 | if (!s->hlua) |
| 3853 | return 0; |
| 3854 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3855 | |
| 3856 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3857 | |
| 3858 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 3859 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3860 | |
| 3861 | /* Get and store new value. */ |
| 3862 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3863 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 3864 | |
| 3865 | return 0; |
| 3866 | } |
| 3867 | |
| 3868 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 3869 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3870 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3871 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3872 | struct hlua *hlua; |
| 3873 | |
| 3874 | /* 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] | 3875 | if (!s->hlua) { |
| 3876 | lua_pushnil(L); |
| 3877 | return 1; |
| 3878 | } |
| 3879 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3880 | |
| 3881 | /* Push configuration index in the stack. */ |
| 3882 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 3883 | |
| 3884 | return 1; |
| 3885 | } |
| 3886 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3887 | /* If expected data not yet available, it returns a yield. This function |
| 3888 | * consumes the data in the buffer. It returns a string containing the |
| 3889 | * data. This string can be empty. |
| 3890 | */ |
| 3891 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 3892 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3893 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3894 | struct stream_interface *si = luactx->appctx->owner; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3895 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3896 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3897 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3898 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3899 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3900 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3901 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3902 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3903 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3904 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3905 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3906 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3907 | 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] | 3908 | } |
| 3909 | |
| 3910 | /* End of data: commit the total strings and return. */ |
| 3911 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3912 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3913 | return 1; |
| 3914 | } |
| 3915 | |
| 3916 | /* Ensure that the block 2 length is usable. */ |
| 3917 | if (ret == 1) |
| 3918 | len2 = 0; |
| 3919 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 3920 | /* don't check the max length read and don't check. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3921 | luaL_addlstring(&luactx->b, blk1, len1); |
| 3922 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3923 | |
| 3924 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3925 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3926 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3927 | return 1; |
| 3928 | } |
| 3929 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3930 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3931 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 3932 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3933 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3934 | |
| 3935 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3936 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3937 | |
| 3938 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 3939 | } |
| 3940 | |
| 3941 | /* If expected data not yet available, it returns a yield. This function |
| 3942 | * consumes the data in the buffer. It returns a string containing the |
| 3943 | * data. This string can be empty. |
| 3944 | */ |
| 3945 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 3946 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3947 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3948 | struct stream_interface *si = luactx->appctx->owner; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3949 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3950 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3951 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3952 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3953 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3954 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3955 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3956 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3957 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3958 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3959 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3960 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3961 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3962 | 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] | 3963 | } |
| 3964 | |
| 3965 | /* End of data: commit the total strings and return. */ |
| 3966 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3967 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3968 | return 1; |
| 3969 | } |
| 3970 | |
| 3971 | /* Ensure that the block 2 length is usable. */ |
| 3972 | if (ret == 1) |
| 3973 | len2 = 0; |
| 3974 | |
| 3975 | if (len == -1) { |
| 3976 | |
| 3977 | /* If len == -1, catenate all the data avalaile and |
| 3978 | * yield because we want to get all the data until |
| 3979 | * the end of data stream. |
| 3980 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3981 | luaL_addlstring(&luactx->b, blk1, len1); |
| 3982 | luaL_addlstring(&luactx->b, blk2, len2); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3983 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3984 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3985 | 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] | 3986 | |
| 3987 | } else { |
| 3988 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3989 | /* Copy the first block caping to the length required. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3990 | if (len1 > len) |
| 3991 | len1 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3992 | luaL_addlstring(&luactx->b, blk1, len1); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3993 | len -= len1; |
| 3994 | |
| 3995 | /* Copy the second block. */ |
| 3996 | if (len2 > len) |
| 3997 | len2 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3998 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3999 | len -= len2; |
| 4000 | |
| 4001 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4002 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4003 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4004 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4005 | if (len > 0) { |
| 4006 | lua_pushinteger(L, len); |
| 4007 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4008 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4009 | 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] | 4010 | } |
| 4011 | |
| 4012 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4013 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4014 | return 1; |
| 4015 | } |
| 4016 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4017 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4018 | hlua_pusherror(L, "Lua: internal error"); |
| 4019 | WILL_LJMP(lua_error(L)); |
| 4020 | return 0; |
| 4021 | } |
| 4022 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4023 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4024 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 4025 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4026 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4027 | int len = -1; |
| 4028 | |
| 4029 | if (lua_gettop(L) > 2) |
| 4030 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4031 | if (lua_gettop(L) >= 2) { |
| 4032 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4033 | lua_pop(L, 1); |
| 4034 | } |
| 4035 | |
| 4036 | /* Confirm or set the required length */ |
| 4037 | lua_pushinteger(L, len); |
| 4038 | |
| 4039 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4040 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4041 | |
| 4042 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 4043 | } |
| 4044 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4045 | /* Append data in the output side of the buffer. This data is immediately |
| 4046 | * sent. The function returns the amount of data written. If the buffer |
| 4047 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4048 | * if the channel is closed. |
| 4049 | */ |
| 4050 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4051 | { |
| 4052 | size_t len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4053 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4054 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4055 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4056 | struct stream_interface *si = luactx->appctx->owner; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4057 | struct channel *chn = si_ic(si); |
| 4058 | int max; |
| 4059 | |
| 4060 | /* Get the max amount of data which can write as input in the channel. */ |
| 4061 | max = channel_recv_max(chn); |
| 4062 | if (max > (len - l)) |
| 4063 | max = len - l; |
| 4064 | |
| 4065 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4066 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4067 | |
| 4068 | /* update counters. */ |
| 4069 | l += max; |
| 4070 | lua_pop(L, 1); |
| 4071 | lua_pushinteger(L, l); |
| 4072 | |
| 4073 | /* If some data is not send, declares the situation to the |
| 4074 | * applet, and returns a yield. |
| 4075 | */ |
| 4076 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 4077 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4078 | 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] | 4079 | } |
| 4080 | |
| 4081 | return 1; |
| 4082 | } |
| 4083 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4084 | /* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4085 | * yield the LUA process, and resume it without checking the |
| 4086 | * input arguments. |
| 4087 | */ |
| 4088 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 4089 | { |
| 4090 | MAY_LJMP(check_args(L, 2, "send")); |
| 4091 | lua_pushinteger(L, 0); |
| 4092 | |
| 4093 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 4094 | } |
| 4095 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4096 | /* |
| 4097 | * |
| 4098 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4099 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4100 | * |
| 4101 | * |
| 4102 | */ |
| 4103 | |
| 4104 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4105 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4106 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4107 | __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] | 4108 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4109 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4110 | } |
| 4111 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4112 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4113 | * according with a current TXN. |
| 4114 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4115 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4116 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4117 | struct hlua_appctx *luactx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 4118 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4119 | struct stream_interface *si = ctx->owner; |
| 4120 | struct stream *s = si_strm(si); |
| 4121 | struct proxy *px = s->be; |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4122 | struct htx *htx; |
| 4123 | struct htx_blk *blk; |
| 4124 | struct htx_sl *sl; |
| 4125 | struct ist path; |
| 4126 | unsigned long long len = 0; |
| 4127 | int32_t pos; |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 4128 | struct http_uri_parser parser; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4129 | |
| 4130 | /* Check stack size. */ |
| 4131 | if (!lua_checkstack(L, 3)) |
| 4132 | return 0; |
| 4133 | |
| 4134 | /* Create the object: obj[0] = userdata. |
| 4135 | * Note that the base of the Converters object is the |
| 4136 | * same than the TXN object. |
| 4137 | */ |
| 4138 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4139 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4140 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4141 | luactx->appctx = ctx; |
| 4142 | luactx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
| 4143 | luactx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */ |
| 4144 | luactx->htxn.s = s; |
| 4145 | luactx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4146 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4147 | /* Create the "f" field that contains a list of fetches. */ |
| 4148 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4149 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4150 | return 0; |
| 4151 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4152 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4153 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4154 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4155 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4156 | return 0; |
| 4157 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4158 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4159 | /* Create the "c" field that contains a list of converters. */ |
| 4160 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4161 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4162 | return 0; |
| 4163 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4164 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4165 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4166 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4167 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4168 | return 0; |
| 4169 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4170 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4171 | htx = htxbuf(&s->req.buf); |
| 4172 | blk = htx_get_first_blk(htx); |
Christopher Faulet | ea00973 | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 4173 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4174 | sl = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4175 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4176 | /* Stores the request method. */ |
| 4177 | lua_pushstring(L, "method"); |
| 4178 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 4179 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4180 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4181 | /* Stores the http version. */ |
| 4182 | lua_pushstring(L, "version"); |
| 4183 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 4184 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4185 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4186 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 4187 | * the array on the top of the stack. |
| 4188 | */ |
| 4189 | lua_pushstring(L, "headers"); |
| 4190 | htxn.s = s; |
| 4191 | htxn.p = px; |
| 4192 | htxn.dir = SMP_OPT_DIR_REQ; |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4193 | if (!hlua_http_get_headers(L, &htxn.s->txn->req)) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4194 | return 0; |
| 4195 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4196 | |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 4197 | parser = http_uri_parser_init(htx_sl_req_uri(sl)); |
| 4198 | path = http_parse_path(&parser); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 4199 | if (isttest(path)) { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4200 | char *p, *q, *end; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4201 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4202 | p = path.ptr; |
| 4203 | end = path.ptr + path.len; |
| 4204 | q = p; |
| 4205 | while (q < end && *q != '?') |
| 4206 | q++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4207 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4208 | /* Stores the request path. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4209 | lua_pushstring(L, "path"); |
| 4210 | lua_pushlstring(L, p, q - p); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4211 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4212 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4213 | /* Stores the query string. */ |
| 4214 | lua_pushstring(L, "qs"); |
| 4215 | if (*q == '?') |
| 4216 | q++; |
| 4217 | lua_pushlstring(L, q, end - q); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4218 | lua_settable(L, -3); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4219 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4220 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4221 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4222 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4223 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4224 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4225 | if (type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4226 | break; |
| 4227 | if (type == HTX_BLK_DATA) |
| 4228 | len += htx_get_blksz(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4229 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4230 | if (htx->extra != ULLONG_MAX) |
| 4231 | len += htx->extra; |
| 4232 | |
| 4233 | /* Stores the request path. */ |
| 4234 | lua_pushstring(L, "length"); |
| 4235 | lua_pushinteger(L, len); |
| 4236 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4237 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4238 | /* Create an empty array of HTTP request headers. */ |
| 4239 | lua_pushstring(L, "response"); |
| 4240 | lua_newtable(L); |
| 4241 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4242 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4243 | /* Pop a class stream metatable and affect it to the table. */ |
| 4244 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 4245 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4246 | |
| 4247 | return 1; |
| 4248 | } |
| 4249 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4250 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 4251 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4252 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4253 | struct stream *s; |
| 4254 | const char *name; |
| 4255 | size_t len; |
| 4256 | struct sample smp; |
| 4257 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4258 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 4259 | 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] | 4260 | |
| 4261 | /* It is useles to retrieve the stream, but this function |
| 4262 | * runs only in a stream context. |
| 4263 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4264 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4265 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4266 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4267 | |
| 4268 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4269 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4270 | hlua_lua2smp(L, 3, &smp); |
| 4271 | |
| 4272 | /* Store the sample in a variable. */ |
| 4273 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4274 | |
| 4275 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 4276 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 4277 | else |
| 4278 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 4279 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4280 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4281 | } |
| 4282 | |
| 4283 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 4284 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4285 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4286 | struct stream *s; |
| 4287 | const char *name; |
| 4288 | size_t len; |
| 4289 | struct sample smp; |
| 4290 | |
| 4291 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4292 | |
| 4293 | /* It is useles to retrieve the stream, but this function |
| 4294 | * runs only in a stream context. |
| 4295 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4296 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4297 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4298 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4299 | |
| 4300 | /* Unset the variable. */ |
| 4301 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4302 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4303 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4304 | } |
| 4305 | |
| 4306 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4307 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4308 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4309 | struct stream *s; |
| 4310 | const char *name; |
| 4311 | size_t len; |
| 4312 | struct sample smp; |
| 4313 | |
| 4314 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4315 | |
| 4316 | /* It is useles to retrieve the stream, but this function |
| 4317 | * runs only in a stream context. |
| 4318 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4319 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4320 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4321 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4322 | |
| 4323 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4324 | if (!vars_get_by_name(name, len, &smp)) { |
| 4325 | lua_pushnil(L); |
| 4326 | return 1; |
| 4327 | } |
| 4328 | |
| 4329 | return hlua_smp2lua(L, &smp); |
| 4330 | } |
| 4331 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4332 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 4333 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4334 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4335 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4336 | struct hlua *hlua; |
| 4337 | |
| 4338 | /* 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] | 4339 | if (!s->hlua) |
| 4340 | return 0; |
| 4341 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4342 | |
| 4343 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4344 | |
| 4345 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4346 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4347 | |
| 4348 | /* Get and store new value. */ |
| 4349 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4350 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4351 | |
| 4352 | return 0; |
| 4353 | } |
| 4354 | |
| 4355 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4356 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4357 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4358 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4359 | struct hlua *hlua; |
| 4360 | |
| 4361 | /* 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] | 4362 | if (!s->hlua) { |
| 4363 | lua_pushnil(L); |
| 4364 | return 1; |
| 4365 | } |
| 4366 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4367 | |
| 4368 | /* Push configuration index in the stack. */ |
| 4369 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4370 | |
| 4371 | return 1; |
| 4372 | } |
| 4373 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4374 | /* If expected data not yet available, it returns a yield. This function |
| 4375 | * consumes the data in the buffer. It returns a string containing the |
| 4376 | * data. This string can be empty. |
| 4377 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4378 | __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] | 4379 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4380 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4381 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4382 | struct channel *req = si_oc(si); |
| 4383 | struct htx *htx; |
| 4384 | struct htx_blk *blk; |
| 4385 | size_t count; |
| 4386 | int stop = 0; |
| 4387 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4388 | htx = htx_from_buf(&req->buf); |
| 4389 | count = co_data(req); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4390 | blk = htx_get_first_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 4391 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4392 | while (count && !stop && blk) { |
| 4393 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4394 | uint32_t sz = htx_get_blksz(blk); |
| 4395 | struct ist v; |
| 4396 | uint32_t vlen; |
| 4397 | char *nl; |
| 4398 | |
| 4399 | vlen = sz; |
| 4400 | if (vlen > count) { |
| 4401 | if (type != HTX_BLK_DATA) |
| 4402 | break; |
| 4403 | vlen = count; |
| 4404 | } |
| 4405 | |
| 4406 | switch (type) { |
| 4407 | case HTX_BLK_UNUSED: |
| 4408 | break; |
| 4409 | |
| 4410 | case HTX_BLK_DATA: |
| 4411 | v = htx_get_blk_value(htx, blk); |
| 4412 | v.len = vlen; |
| 4413 | nl = istchr(v, '\n'); |
| 4414 | if (nl != NULL) { |
| 4415 | stop = 1; |
| 4416 | vlen = nl - v.ptr + 1; |
| 4417 | } |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4418 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4419 | break; |
| 4420 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4421 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4422 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4423 | stop = 1; |
| 4424 | break; |
| 4425 | |
| 4426 | default: |
| 4427 | break; |
| 4428 | } |
| 4429 | |
| 4430 | co_set_data(req, co_data(req) - vlen); |
| 4431 | count -= vlen; |
| 4432 | if (sz == vlen) |
| 4433 | blk = htx_remove_blk(htx, blk); |
| 4434 | else { |
| 4435 | htx_cut_data_blk(htx, blk, vlen); |
| 4436 | break; |
| 4437 | } |
| 4438 | } |
| 4439 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 4440 | /* The message was fully consumed and no more data are expected |
| 4441 | * (EOM flag set). |
| 4442 | */ |
| 4443 | if (htx_is_empty(htx) && (htx->flags & HTX_FL_EOM)) |
| 4444 | stop = 1; |
| 4445 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4446 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4447 | if (!stop) { |
| 4448 | si_cant_get(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4449 | 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] | 4450 | } |
| 4451 | |
| 4452 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4453 | luaL_pushresult(&luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4454 | return 1; |
| 4455 | } |
| 4456 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4457 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4458 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4459 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4460 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4461 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4462 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4463 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4464 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4465 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4466 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4467 | } |
| 4468 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4469 | /* If expected data not yet available, it returns a yield. This function |
| 4470 | * consumes the data in the buffer. It returns a string containing the |
| 4471 | * data. This string can be empty. |
| 4472 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4473 | __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] | 4474 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4475 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4476 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4477 | struct channel *req = si_oc(si); |
| 4478 | struct htx *htx; |
| 4479 | struct htx_blk *blk; |
| 4480 | size_t count; |
| 4481 | int len; |
| 4482 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4483 | htx = htx_from_buf(&req->buf); |
| 4484 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4485 | count = co_data(req); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 4486 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4487 | while (count && len && blk) { |
| 4488 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4489 | uint32_t sz = htx_get_blksz(blk); |
| 4490 | struct ist v; |
| 4491 | uint32_t vlen; |
| 4492 | |
| 4493 | vlen = sz; |
| 4494 | if (len > 0 && vlen > len) |
| 4495 | vlen = len; |
| 4496 | if (vlen > count) { |
| 4497 | if (type != HTX_BLK_DATA) |
| 4498 | break; |
| 4499 | vlen = count; |
| 4500 | } |
| 4501 | |
| 4502 | switch (type) { |
| 4503 | case HTX_BLK_UNUSED: |
| 4504 | break; |
| 4505 | |
| 4506 | case HTX_BLK_DATA: |
| 4507 | v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4508 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4509 | break; |
| 4510 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4511 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4512 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4513 | len = 0; |
| 4514 | break; |
| 4515 | |
| 4516 | default: |
| 4517 | break; |
| 4518 | } |
| 4519 | |
| 4520 | co_set_data(req, co_data(req) - vlen); |
| 4521 | count -= vlen; |
| 4522 | if (len > 0) |
| 4523 | len -= vlen; |
| 4524 | if (sz == vlen) |
| 4525 | blk = htx_remove_blk(htx, blk); |
| 4526 | else { |
| 4527 | htx_cut_data_blk(htx, blk, vlen); |
| 4528 | break; |
| 4529 | } |
| 4530 | } |
| 4531 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 4532 | /* The message was fully consumed and no more data are expected |
| 4533 | * (EOM flag set). |
| 4534 | */ |
| 4535 | if (htx_is_empty(htx) && (htx->flags & HTX_FL_EOM)) |
| 4536 | len = 0; |
| 4537 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4538 | htx_to_buf(htx, &req->buf); |
| 4539 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4540 | /* If we are no other data available, yield waiting for new data. */ |
| 4541 | if (len) { |
| 4542 | if (len > 0) { |
| 4543 | lua_pushinteger(L, len); |
| 4544 | lua_replace(L, 2); |
| 4545 | } |
| 4546 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4547 | 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] | 4548 | } |
| 4549 | |
| 4550 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4551 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4552 | return 1; |
| 4553 | } |
| 4554 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4555 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4556 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 4557 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4558 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4559 | int len = -1; |
| 4560 | |
| 4561 | /* Check arguments. */ |
| 4562 | if (lua_gettop(L) > 2) |
| 4563 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4564 | if (lua_gettop(L) >= 2) { |
| 4565 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4566 | lua_pop(L, 1); |
| 4567 | } |
| 4568 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4569 | lua_pushinteger(L, len); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4570 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4571 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4572 | luaL_buffinit(L, &luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4573 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4574 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4575 | } |
| 4576 | |
| 4577 | /* Append data in the output side of the buffer. This data is immediately |
| 4578 | * sent. The function returns the amount of data written. If the buffer |
| 4579 | * cannot contain the data, the function yields. The function returns -1 |
| 4580 | * if the channel is closed. |
| 4581 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4582 | __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] | 4583 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4584 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4585 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4586 | struct channel *res = si_ic(si); |
| 4587 | struct htx *htx = htx_from_buf(&res->buf); |
| 4588 | const char *data; |
| 4589 | size_t len; |
| 4590 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4591 | int max; |
| 4592 | |
Christopher Faulet | 9060fc0 | 2019-07-03 11:39:30 +0200 | [diff] [blame] | 4593 | max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx)); |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4594 | if (!max) |
| 4595 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4596 | |
| 4597 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4598 | |
| 4599 | /* Get the max amount of data which can write as input in the channel. */ |
| 4600 | if (max > (len - l)) |
| 4601 | max = len - l; |
| 4602 | |
| 4603 | /* Copy data. */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 4604 | max = htx_add_data(htx, ist2(data + l, max)); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4605 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4606 | |
| 4607 | /* update counters. */ |
| 4608 | l += max; |
| 4609 | lua_pop(L, 1); |
| 4610 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4611 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4612 | /* If some data is not send, declares the situation to the |
| 4613 | * applet, and returns a yield. |
| 4614 | */ |
| 4615 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4616 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4617 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4618 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4619 | 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] | 4620 | } |
| 4621 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4622 | htx_to_buf(htx, &res->buf); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4623 | return 1; |
| 4624 | } |
| 4625 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4626 | /* Just a wrapper of "hlua_applet_send_yield". This wrapper permits |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4627 | * yield the LUA process, and resume it without checking the |
| 4628 | * input arguments. |
| 4629 | */ |
| 4630 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 4631 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4632 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4633 | |
| 4634 | /* We want to send some data. Headers must be sent. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4635 | if (!(luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4636 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 4637 | WILL_LJMP(lua_error(L)); |
| 4638 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4639 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4640 | /* This integer is used for followinf the amount of data sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4641 | lua_pushinteger(L, 0); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4642 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4643 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4644 | } |
| 4645 | |
| 4646 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 4647 | { |
| 4648 | const char *name; |
| 4649 | int ret; |
| 4650 | |
| 4651 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4652 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4653 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 4654 | |
| 4655 | /* Push in the stack the "response" entry. */ |
| 4656 | ret = lua_getfield(L, 1, "response"); |
| 4657 | if (ret != LUA_TTABLE) { |
| 4658 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 4659 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 4660 | WILL_LJMP(lua_error(L)); |
| 4661 | } |
| 4662 | |
| 4663 | /* check if the header is already registered if it is not |
| 4664 | * the case, register it. |
| 4665 | */ |
| 4666 | ret = lua_getfield(L, -1, name); |
| 4667 | if (ret == LUA_TNIL) { |
| 4668 | |
| 4669 | /* Entry not found. */ |
| 4670 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 4671 | |
| 4672 | /* Insert the new header name in the array in the top of the stack. |
| 4673 | * It left the new array in the top of the stack. |
| 4674 | */ |
| 4675 | lua_newtable(L); |
| 4676 | lua_pushvalue(L, 2); |
| 4677 | lua_pushvalue(L, -2); |
| 4678 | lua_settable(L, -4); |
| 4679 | |
| 4680 | } else if (ret != LUA_TTABLE) { |
| 4681 | |
| 4682 | /* corruption error. */ |
| 4683 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 4684 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 4685 | WILL_LJMP(lua_error(L)); |
| 4686 | } |
| 4687 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 4688 | /* Now the top of thestack is an array of values. We push |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4689 | * the header value as new entry. |
| 4690 | */ |
| 4691 | lua_pushvalue(L, 3); |
| 4692 | ret = lua_rawlen(L, -2); |
| 4693 | lua_rawseti(L, -2, ret + 1); |
| 4694 | lua_pushboolean(L, 1); |
| 4695 | return 1; |
| 4696 | } |
| 4697 | |
| 4698 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 4699 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4700 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4701 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4702 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4703 | |
| 4704 | if (status < 100 || status > 599) { |
| 4705 | lua_pushboolean(L, 0); |
| 4706 | return 1; |
| 4707 | } |
| 4708 | |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4709 | luactx->appctx->ctx.hlua_apphttp.status = status; |
| 4710 | luactx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4711 | lua_pushboolean(L, 1); |
| 4712 | return 1; |
| 4713 | } |
| 4714 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4715 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4716 | __LJMP static int hlua_applet_http_send_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4717 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4718 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4719 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4720 | struct channel *res = si_ic(si); |
| 4721 | struct htx *htx; |
| 4722 | struct htx_sl *sl; |
| 4723 | struct h1m h1m; |
| 4724 | const char *status, *reason; |
| 4725 | const char *name, *value; |
| 4726 | size_t nlen, vlen; |
| 4727 | unsigned int flags; |
| 4728 | |
| 4729 | /* Send the message at once. */ |
| 4730 | htx = htx_from_buf(&res->buf); |
| 4731 | h1m_init_res(&h1m); |
| 4732 | |
| 4733 | /* Use the same http version than the request. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4734 | status = ultoa_r(luactx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size); |
| 4735 | reason = luactx->appctx->ctx.hlua_apphttp.reason; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4736 | if (reason == NULL) |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4737 | reason = http_get_reason(luactx->appctx->ctx.hlua_apphttp.status); |
| 4738 | if (luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4739 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 4740 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 4741 | } |
| 4742 | else { |
| 4743 | flags = HTX_SL_F_IS_RESP; |
| 4744 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 4745 | } |
| 4746 | if (!sl) { |
| 4747 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4748 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4749 | WILL_LJMP(lua_error(L)); |
| 4750 | } |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4751 | sl->info.res.status = luactx->appctx->ctx.hlua_apphttp.status; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4752 | |
| 4753 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 4754 | lua_pushvalue(L, 0); |
| 4755 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 4756 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4757 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4758 | WILL_LJMP(lua_error(L)); |
| 4759 | } |
| 4760 | |
| 4761 | /* Browse the list of headers. */ |
| 4762 | lua_pushnil(L); |
| 4763 | while(lua_next(L, -2) != 0) { |
| 4764 | /* We expect a string as -2. */ |
| 4765 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 4766 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4767 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4768 | lua_typename(L, lua_type(L, -2))); |
| 4769 | WILL_LJMP(lua_error(L)); |
| 4770 | } |
| 4771 | name = lua_tolstring(L, -2, &nlen); |
| 4772 | |
| 4773 | /* We expect an array as -1. */ |
| 4774 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 4775 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4776 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4777 | name, |
| 4778 | lua_typename(L, lua_type(L, -1))); |
| 4779 | WILL_LJMP(lua_error(L)); |
| 4780 | } |
| 4781 | |
| 4782 | /* Browse the table who is on the top of the stack. */ |
| 4783 | lua_pushnil(L); |
| 4784 | while(lua_next(L, -2) != 0) { |
| 4785 | int id; |
| 4786 | |
| 4787 | /* We expect a number as -2. */ |
| 4788 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 4789 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4790 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4791 | name, |
| 4792 | lua_typename(L, lua_type(L, -2))); |
| 4793 | WILL_LJMP(lua_error(L)); |
| 4794 | } |
| 4795 | id = lua_tointeger(L, -2); |
| 4796 | |
| 4797 | /* We expect a string as -2. */ |
| 4798 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 4799 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4800 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4801 | name, id, |
| 4802 | lua_typename(L, lua_type(L, -1))); |
| 4803 | WILL_LJMP(lua_error(L)); |
| 4804 | } |
| 4805 | value = lua_tolstring(L, -1, &vlen); |
| 4806 | |
| 4807 | /* Simple Protocol checks. */ |
| 4808 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 4809 | h1_parse_xfer_enc_header(&h1m, ist2(value, vlen)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4810 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 4811 | struct ist v = ist2(value, vlen); |
| 4812 | int ret; |
| 4813 | |
| 4814 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 4815 | if (ret < 0) { |
| 4816 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4817 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4818 | name); |
| 4819 | WILL_LJMP(lua_error(L)); |
| 4820 | } |
| 4821 | else if (ret == 0) |
| 4822 | goto next; /* Skip it */ |
| 4823 | } |
| 4824 | |
| 4825 | /* Add a new header */ |
| 4826 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 4827 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4828 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4829 | name); |
| 4830 | WILL_LJMP(lua_error(L)); |
| 4831 | } |
| 4832 | next: |
| 4833 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4834 | lua_pop(L, 1); |
| 4835 | } |
| 4836 | |
| 4837 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4838 | lua_pop(L, 1); |
| 4839 | } |
| 4840 | |
| 4841 | if (h1m.flags & H1_MF_CHNK) |
| 4842 | h1m.flags &= ~H1_MF_CLEN; |
| 4843 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 4844 | h1m.flags |= H1_MF_XFER_LEN; |
| 4845 | |
| 4846 | /* Uset HTX start-line flags */ |
| 4847 | if (h1m.flags & H1_MF_XFER_ENC) |
| 4848 | flags |= HTX_SL_F_XFER_ENC; |
| 4849 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 4850 | flags |= HTX_SL_F_XFER_LEN; |
| 4851 | if (h1m.flags & H1_MF_CHNK) |
| 4852 | flags |= HTX_SL_F_CHNK; |
| 4853 | else if (h1m.flags & H1_MF_CLEN) |
| 4854 | flags |= HTX_SL_F_CLEN; |
| 4855 | if (h1m.body_len == 0) |
| 4856 | flags |= HTX_SL_F_BODYLESS; |
| 4857 | } |
| 4858 | sl->flags |= flags; |
| 4859 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 4860 | /* 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] | 4861 | * and the status code implies the presence of a message body, we must |
| 4862 | * announce a transfer encoding chunked. This is required by haproxy |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4863 | * for the keepalive compliance. If the applet announces a transfer-encoding |
| 4864 | * chunked itself, don't do anything. |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4865 | */ |
| 4866 | if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 && |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4867 | luactx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 4868 | luactx->appctx->ctx.hlua_apphttp.status != 204 && |
| 4869 | luactx->appctx->ctx.hlua_apphttp.status != 304) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4870 | /* Add a new header */ |
| 4871 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 4872 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 4873 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4874 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4875 | WILL_LJMP(lua_error(L)); |
| 4876 | } |
| 4877 | } |
| 4878 | |
| 4879 | /* Finalize headers. */ |
| 4880 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 4881 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4882 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4883 | WILL_LJMP(lua_error(L)); |
| 4884 | } |
| 4885 | |
| 4886 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 4887 | b_reset(&res->buf); |
| 4888 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 4889 | WILL_LJMP(lua_error(L)); |
| 4890 | } |
| 4891 | |
| 4892 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4893 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4894 | |
| 4895 | /* Headers sent, set the flag. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4896 | luactx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4897 | return 0; |
| 4898 | |
| 4899 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4900 | /* We will build the status line and the headers of the HTTP response. |
| 4901 | * We will try send at once if its not possible, we give back the hand |
| 4902 | * waiting for more room. |
| 4903 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4904 | __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] | 4905 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4906 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4907 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4908 | struct channel *res = si_ic(si); |
| 4909 | |
| 4910 | if (co_data(res)) { |
| 4911 | si_rx_room_blk(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4912 | 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] | 4913 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4914 | return MAY_LJMP(hlua_applet_http_send_response(L)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4915 | } |
| 4916 | |
| 4917 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4918 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4919 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4920 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4921 | } |
| 4922 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4923 | /* |
| 4924 | * |
| 4925 | * |
| 4926 | * Class HTTP |
| 4927 | * |
| 4928 | * |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4929 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4930 | |
| 4931 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4932 | * a class stream, otherwise it throws an error. |
| 4933 | */ |
| 4934 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4935 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4936 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
| 4937 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4938 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4939 | /* This function creates and push in the stack a HTTP object |
| 4940 | * according with a current TXN. |
| 4941 | */ |
| 4942 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 4943 | { |
| 4944 | struct hlua_txn *htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4945 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4946 | /* Check stack size. */ |
| 4947 | if (!lua_checkstack(L, 3)) |
| 4948 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4949 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4950 | /* Create the object: obj[0] = userdata. |
| 4951 | * Note that the base of the Converters object is the |
| 4952 | * same than the TXN object. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4953 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4954 | lua_newtable(L); |
| 4955 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 4956 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4957 | |
| 4958 | htxn->s = txn->s; |
| 4959 | htxn->p = txn->p; |
Christopher Faulet | 256b69a | 2019-05-23 11:14:21 +0200 | [diff] [blame] | 4960 | htxn->dir = txn->dir; |
| 4961 | htxn->flags = txn->flags; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4962 | |
| 4963 | /* Pop a class stream metatable and affect it to the table. */ |
| 4964 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 4965 | lua_setmetatable(L, -2); |
| 4966 | |
| 4967 | return 1; |
| 4968 | } |
| 4969 | |
| 4970 | /* This function creates ans returns an array of HTTP headers. |
| 4971 | * This function does not fails. It is used as wrapper with the |
| 4972 | * 2 following functions. |
| 4973 | */ |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4974 | __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] | 4975 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4976 | struct htx *htx; |
| 4977 | int32_t pos; |
| 4978 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4979 | /* Create the table. */ |
| 4980 | lua_newtable(L); |
| 4981 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4982 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4983 | htx = htxbuf(&msg->chn->buf); |
| 4984 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4985 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4986 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4987 | struct ist n, v; |
| 4988 | int len; |
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 | if (type == HTX_BLK_HDR) { |
| 4991 | n = htx_get_blk_name(htx,blk); |
| 4992 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4993 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4994 | else if (type == HTX_BLK_EOH) |
| 4995 | break; |
| 4996 | else |
| 4997 | continue; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4998 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4999 | /* Check for existing entry: |
| 5000 | * assume that the table is on the top of the stack, and |
| 5001 | * push the key in the stack, the function lua_gettable() |
| 5002 | * perform the lookup. |
| 5003 | */ |
| 5004 | lua_pushlstring(L, n.ptr, n.len); |
| 5005 | lua_gettable(L, -2); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5006 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5007 | switch (lua_type(L, -1)) { |
| 5008 | case LUA_TNIL: |
| 5009 | /* Table not found, create it. */ |
| 5010 | lua_pop(L, 1); /* remove the nil value. */ |
| 5011 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 5012 | lua_newtable(L); /* create and push empty table. */ |
| 5013 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5014 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 5015 | 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] | 5016 | break; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5017 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5018 | case LUA_TTABLE: |
| 5019 | /* Entry found: push the value in the table. */ |
| 5020 | len = lua_rawlen(L, -1); |
| 5021 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5022 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 5023 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 5024 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5025 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5026 | default: |
| 5027 | /* Other cases are errors. */ |
| 5028 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 5029 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5030 | } |
| 5031 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5032 | return 1; |
| 5033 | } |
| 5034 | |
| 5035 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 5036 | { |
| 5037 | struct hlua_txn *htxn; |
| 5038 | |
| 5039 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 5040 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5041 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5042 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5043 | WILL_LJMP(lua_error(L)); |
| 5044 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5045 | return hlua_http_get_headers(L, &htxn->s->txn->req); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5046 | } |
| 5047 | |
| 5048 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 5049 | { |
| 5050 | struct hlua_txn *htxn; |
| 5051 | |
| 5052 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 5053 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5054 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5055 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5056 | WILL_LJMP(lua_error(L)); |
| 5057 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5058 | return hlua_http_get_headers(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5059 | } |
| 5060 | |
| 5061 | /* This function replace full header, or just a value in |
| 5062 | * the request or in the response. It is a wrapper fir the |
| 5063 | * 4 following functions. |
| 5064 | */ |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5065 | __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] | 5066 | { |
| 5067 | size_t name_len; |
| 5068 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5069 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5070 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5071 | struct htx *htx; |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5072 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5073 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5074 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5075 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 5076 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5077 | htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5078 | 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] | 5079 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5080 | return 0; |
| 5081 | } |
| 5082 | |
| 5083 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 5084 | { |
| 5085 | struct hlua_txn *htxn; |
| 5086 | |
| 5087 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5088 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5089 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5090 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5091 | WILL_LJMP(lua_error(L)); |
| 5092 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5093 | 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] | 5094 | } |
| 5095 | |
| 5096 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 5097 | { |
| 5098 | struct hlua_txn *htxn; |
| 5099 | |
| 5100 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 5101 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5102 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5103 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5104 | WILL_LJMP(lua_error(L)); |
| 5105 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5106 | 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] | 5107 | } |
| 5108 | |
| 5109 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 5110 | { |
| 5111 | struct hlua_txn *htxn; |
| 5112 | |
| 5113 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5114 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5115 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5116 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5117 | WILL_LJMP(lua_error(L)); |
| 5118 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5119 | 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] | 5120 | } |
| 5121 | |
| 5122 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 5123 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5124 | struct hlua_txn *htxn; |
| 5125 | |
| 5126 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 5127 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5128 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5129 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5130 | WILL_LJMP(lua_error(L)); |
| 5131 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5132 | 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] | 5133 | } |
| 5134 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5135 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5136 | * It is a wrapper for the 2 following functions. |
| 5137 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5138 | __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] | 5139 | { |
| 5140 | size_t len; |
| 5141 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5142 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5143 | struct http_hdr_ctx ctx; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5144 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5145 | ctx.blk = NULL; |
| 5146 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 5147 | http_remove_header(htx, &ctx); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5148 | return 0; |
| 5149 | } |
| 5150 | |
| 5151 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 5152 | { |
| 5153 | struct hlua_txn *htxn; |
| 5154 | |
| 5155 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 5156 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5157 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5158 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5159 | WILL_LJMP(lua_error(L)); |
| 5160 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5161 | return hlua_http_del_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5162 | } |
| 5163 | |
| 5164 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 5165 | { |
| 5166 | struct hlua_txn *htxn; |
| 5167 | |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5168 | MAY_LJMP(check_args(L, 2, "res_del_hdr")); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5169 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5170 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5171 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5172 | WILL_LJMP(lua_error(L)); |
| 5173 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5174 | return hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5175 | } |
| 5176 | |
| 5177 | /* This function adds an header. It is a wrapper used by |
| 5178 | * the 2 following functions. |
| 5179 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5180 | __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] | 5181 | { |
| 5182 | size_t name_len; |
| 5183 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5184 | size_t value_len; |
| 5185 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5186 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5187 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5188 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 5189 | ist2(value, value_len))); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5190 | return 0; |
| 5191 | } |
| 5192 | |
| 5193 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 5194 | { |
| 5195 | struct hlua_txn *htxn; |
| 5196 | |
| 5197 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 5198 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5199 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5200 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5201 | WILL_LJMP(lua_error(L)); |
| 5202 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5203 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5204 | } |
| 5205 | |
| 5206 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 5207 | { |
| 5208 | struct hlua_txn *htxn; |
| 5209 | |
| 5210 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 5211 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5212 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5213 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5214 | WILL_LJMP(lua_error(L)); |
| 5215 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5216 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5217 | } |
| 5218 | |
| 5219 | static int hlua_http_req_set_hdr(lua_State *L) |
| 5220 | { |
| 5221 | struct hlua_txn *htxn; |
| 5222 | |
| 5223 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 5224 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5225 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5226 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5227 | WILL_LJMP(lua_error(L)); |
| 5228 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5229 | hlua_http_del_hdr(L, &htxn->s->txn->req); |
| 5230 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5231 | } |
| 5232 | |
| 5233 | static int hlua_http_res_set_hdr(lua_State *L) |
| 5234 | { |
| 5235 | struct hlua_txn *htxn; |
| 5236 | |
| 5237 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 5238 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5239 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5240 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5241 | WILL_LJMP(lua_error(L)); |
| 5242 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5243 | hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
| 5244 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5245 | } |
| 5246 | |
| 5247 | /* This function set the method. */ |
| 5248 | static int hlua_http_req_set_meth(lua_State *L) |
| 5249 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5250 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5251 | size_t name_len; |
| 5252 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5253 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5254 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5255 | WILL_LJMP(lua_error(L)); |
| 5256 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5257 | lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5258 | return 1; |
| 5259 | } |
| 5260 | |
| 5261 | /* This function set the method. */ |
| 5262 | static int hlua_http_req_set_path(lua_State *L) |
| 5263 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5264 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5265 | size_t name_len; |
| 5266 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 5267 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5268 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5269 | WILL_LJMP(lua_error(L)); |
| 5270 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5271 | lua_pushboolean(L, http_req_replace_stline(1, name, name_len, htxn->p, htxn->s) != -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5272 | return 1; |
| 5273 | } |
| 5274 | |
| 5275 | /* This function set the query-string. */ |
| 5276 | static int hlua_http_req_set_query(lua_State *L) |
| 5277 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5278 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5279 | size_t name_len; |
| 5280 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5281 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5282 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5283 | WILL_LJMP(lua_error(L)); |
| 5284 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5285 | /* Check length. */ |
| 5286 | if (name_len > trash.size - 1) { |
| 5287 | lua_pushboolean(L, 0); |
| 5288 | return 1; |
| 5289 | } |
| 5290 | |
| 5291 | /* Add the mark question as prefix. */ |
| 5292 | chunk_reset(&trash); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5293 | trash.area[trash.data++] = '?'; |
| 5294 | memcpy(trash.area + trash.data, name, name_len); |
| 5295 | trash.data += name_len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5296 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5297 | lua_pushboolean(L, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5298 | 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] | 5299 | return 1; |
| 5300 | } |
| 5301 | |
| 5302 | /* This function set the uri. */ |
| 5303 | static int hlua_http_req_set_uri(lua_State *L) |
| 5304 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5305 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5306 | size_t name_len; |
| 5307 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5308 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5309 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5310 | WILL_LJMP(lua_error(L)); |
| 5311 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5312 | 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] | 5313 | return 1; |
| 5314 | } |
| 5315 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5316 | /* This function set the response code & optionally reason. */ |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5317 | static int hlua_http_res_set_status(lua_State *L) |
| 5318 | { |
| 5319 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5320 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 5321 | const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 5322 | const struct ist reason = ist2(str, (str ? strlen(str) : 0)); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5323 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5324 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5325 | WILL_LJMP(lua_error(L)); |
| 5326 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5327 | http_res_set_status(code, reason, htxn->s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5328 | return 0; |
| 5329 | } |
| 5330 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5331 | /* |
| 5332 | * |
| 5333 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5334 | * Class TXN |
| 5335 | * |
| 5336 | * |
| 5337 | */ |
| 5338 | |
| 5339 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5340 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5341 | */ |
| 5342 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 5343 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5344 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5345 | } |
| 5346 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5347 | __LJMP static int hlua_set_var(lua_State *L) |
| 5348 | { |
| 5349 | struct hlua_txn *htxn; |
| 5350 | const char *name; |
| 5351 | size_t len; |
| 5352 | struct sample smp; |
| 5353 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 5354 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 5355 | 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] | 5356 | |
| 5357 | /* It is useles to retrieve the stream, but this function |
| 5358 | * runs only in a stream context. |
| 5359 | */ |
| 5360 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5361 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5362 | |
| 5363 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 5364 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5365 | hlua_lua2smp(L, 3, &smp); |
| 5366 | |
| 5367 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5368 | 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] | 5369 | |
| 5370 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 5371 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 5372 | else |
| 5373 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 5374 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 5375 | return 1; |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5376 | } |
| 5377 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5378 | __LJMP static int hlua_unset_var(lua_State *L) |
| 5379 | { |
| 5380 | struct hlua_txn *htxn; |
| 5381 | const char *name; |
| 5382 | size_t len; |
| 5383 | struct sample smp; |
| 5384 | |
| 5385 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 5386 | |
| 5387 | /* It is useles to retrieve the stream, but this function |
| 5388 | * runs only in a stream context. |
| 5389 | */ |
| 5390 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5391 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5392 | |
| 5393 | /* Unset the variable. */ |
| 5394 | 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] | 5395 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 5396 | return 1; |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5397 | } |
| 5398 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5399 | __LJMP static int hlua_get_var(lua_State *L) |
| 5400 | { |
| 5401 | struct hlua_txn *htxn; |
| 5402 | const char *name; |
| 5403 | size_t len; |
| 5404 | struct sample smp; |
| 5405 | |
| 5406 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 5407 | |
| 5408 | /* It is useles to retrieve the stream, but this function |
| 5409 | * runs only in a stream context. |
| 5410 | */ |
| 5411 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5412 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5413 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5414 | 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] | 5415 | if (!vars_get_by_name(name, len, &smp)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5416 | lua_pushnil(L); |
| 5417 | return 1; |
| 5418 | } |
| 5419 | |
| 5420 | return hlua_smp2lua(L, &smp); |
| 5421 | } |
| 5422 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5423 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5424 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5425 | struct hlua *hlua; |
| 5426 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5427 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 5428 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5429 | /* It is useles to retrieve the stream, but this function |
| 5430 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5431 | */ |
| 5432 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5433 | |
| 5434 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5435 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5436 | if (!hlua) |
| 5437 | return 0; |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5438 | |
| 5439 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5440 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5441 | |
| 5442 | /* Get and store new value. */ |
| 5443 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5444 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5445 | |
| 5446 | return 0; |
| 5447 | } |
| 5448 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5449 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5450 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5451 | struct hlua *hlua; |
| 5452 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5453 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 5454 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5455 | /* It is useles to retrieve the stream, but this function |
| 5456 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5457 | */ |
| 5458 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5459 | |
| 5460 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5461 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5462 | if (!hlua) { |
| 5463 | lua_pushnil(L); |
| 5464 | return 1; |
| 5465 | } |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5466 | |
| 5467 | /* Push configuration index in the stack. */ |
| 5468 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5469 | |
| 5470 | return 1; |
| 5471 | } |
| 5472 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5473 | /* Create stack entry containing a class TXN. This function |
| 5474 | * return 0 if the stack does not contains free slots, |
| 5475 | * otherwise it returns 1. |
| 5476 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5477 | 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] | 5478 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5479 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5480 | |
| 5481 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5482 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5483 | return 0; |
| 5484 | |
| 5485 | /* NOTE: The allocation never fails. The failure |
| 5486 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5487 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5488 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5489 | /* Create the object: obj[0] = userdata. */ |
| 5490 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5491 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5492 | lua_rawseti(L, -2, 0); |
| 5493 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5494 | htxn->s = s; |
| 5495 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 5496 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5497 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5498 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5499 | /* Create the "f" field that contains a list of fetches. */ |
| 5500 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5501 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5502 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5503 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5504 | |
| 5505 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 5506 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5507 | 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] | 5508 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5509 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5510 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5511 | /* Create the "c" field that contains a list of converters. */ |
| 5512 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5513 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5514 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5515 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5516 | |
| 5517 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 5518 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 5519 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5520 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5521 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5522 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5523 | /* Create the "req" field that contains the request channel object. */ |
| 5524 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5525 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5526 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5527 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5528 | |
| 5529 | /* Create the "res" field that contains the response channel object. */ |
| 5530 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5531 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5532 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5533 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5534 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5535 | /* Creates the HTTP object is the current proxy allows http. */ |
| 5536 | lua_pushstring(L, "http"); |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 5537 | if (IS_HTX_STRM(s)) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5538 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5539 | return 0; |
| 5540 | } |
| 5541 | else |
| 5542 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5543 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5544 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5545 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5546 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 5547 | lua_setmetatable(L, -2); |
| 5548 | |
| 5549 | return 1; |
| 5550 | } |
| 5551 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5552 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 5553 | { |
| 5554 | const char *msg; |
| 5555 | struct hlua_txn *htxn; |
| 5556 | |
| 5557 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 5558 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5559 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5560 | |
| 5561 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 5562 | return 0; |
| 5563 | } |
| 5564 | |
| 5565 | __LJMP static int hlua_txn_log(lua_State *L) |
| 5566 | { |
| 5567 | int level; |
| 5568 | const char *msg; |
| 5569 | struct hlua_txn *htxn; |
| 5570 | |
| 5571 | MAY_LJMP(check_args(L, 3, "log")); |
| 5572 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5573 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5574 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5575 | |
| 5576 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5577 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5578 | |
| 5579 | hlua_sendlog(htxn->s->be, level, msg); |
| 5580 | return 0; |
| 5581 | } |
| 5582 | |
| 5583 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 5584 | { |
| 5585 | const char *msg; |
| 5586 | struct hlua_txn *htxn; |
| 5587 | |
| 5588 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 5589 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5590 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5591 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 5592 | return 0; |
| 5593 | } |
| 5594 | |
| 5595 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 5596 | { |
| 5597 | const char *msg; |
| 5598 | struct hlua_txn *htxn; |
| 5599 | |
| 5600 | MAY_LJMP(check_args(L, 2, "Info")); |
| 5601 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5602 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5603 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 5604 | return 0; |
| 5605 | } |
| 5606 | |
| 5607 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 5608 | { |
| 5609 | const char *msg; |
| 5610 | struct hlua_txn *htxn; |
| 5611 | |
| 5612 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 5613 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5614 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5615 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 5616 | return 0; |
| 5617 | } |
| 5618 | |
| 5619 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 5620 | { |
| 5621 | const char *msg; |
| 5622 | struct hlua_txn *htxn; |
| 5623 | |
| 5624 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 5625 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5626 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5627 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 5628 | return 0; |
| 5629 | } |
| 5630 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5631 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 5632 | { |
| 5633 | struct hlua_txn *htxn; |
| 5634 | int ll; |
| 5635 | |
| 5636 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 5637 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5638 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5639 | |
| 5640 | if (ll < 0 || ll > 7) |
| 5641 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 5642 | |
| 5643 | htxn->s->logs.level = ll; |
| 5644 | return 0; |
| 5645 | } |
| 5646 | |
| 5647 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 5648 | { |
| 5649 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5650 | int tos; |
| 5651 | |
| 5652 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 5653 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5654 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5655 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 5656 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5657 | return 0; |
| 5658 | } |
| 5659 | |
| 5660 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 5661 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5662 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5663 | int mark; |
| 5664 | |
| 5665 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 5666 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5667 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5668 | |
Lukas Tribus | 579e3e3 | 2019-08-11 18:03:45 +0200 | [diff] [blame] | 5669 | conn_set_mark(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5670 | return 0; |
| 5671 | } |
| 5672 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 5673 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 5674 | { |
| 5675 | struct hlua_txn *htxn; |
| 5676 | |
| 5677 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 5678 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5679 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5680 | return 0; |
| 5681 | } |
| 5682 | |
| 5683 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 5684 | { |
| 5685 | struct hlua_txn *htxn; |
| 5686 | |
| 5687 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 5688 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5689 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5690 | return 0; |
| 5691 | } |
| 5692 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5693 | /* 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] | 5694 | * 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] | 5695 | * message and terminate the transaction. It returns 1 on success and 0 on |
| 5696 | * error. The Reply must be on top of the stack. |
| 5697 | */ |
| 5698 | __LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s) |
| 5699 | { |
| 5700 | struct htx *htx; |
| 5701 | struct htx_sl *sl; |
| 5702 | struct h1m h1m; |
| 5703 | const char *status, *reason, *body; |
| 5704 | size_t status_len, reason_len, body_len; |
| 5705 | int ret, code, flags; |
| 5706 | |
| 5707 | code = 200; |
| 5708 | status = "200"; |
| 5709 | status_len = 3; |
| 5710 | ret = lua_getfield(L, -1, "status"); |
| 5711 | if (ret == LUA_TNUMBER) { |
| 5712 | code = lua_tointeger(L, -1); |
| 5713 | status = lua_tolstring(L, -1, &status_len); |
| 5714 | } |
| 5715 | lua_pop(L, 1); |
| 5716 | |
| 5717 | reason = http_get_reason(code); |
| 5718 | reason_len = strlen(reason); |
| 5719 | ret = lua_getfield(L, -1, "reason"); |
| 5720 | if (ret == LUA_TSTRING) |
| 5721 | reason = lua_tolstring(L, -1, &reason_len); |
| 5722 | lua_pop(L, 1); |
| 5723 | |
| 5724 | body = NULL; |
| 5725 | body_len = 0; |
| 5726 | ret = lua_getfield(L, -1, "body"); |
| 5727 | if (ret == LUA_TSTRING) |
| 5728 | body = lua_tolstring(L, -1, &body_len); |
| 5729 | lua_pop(L, 1); |
| 5730 | |
| 5731 | /* Prepare the response before inserting the headers */ |
| 5732 | h1m_init_res(&h1m); |
| 5733 | htx = htx_from_buf(&s->res.buf); |
| 5734 | channel_htx_truncate(&s->res, htx); |
| 5735 | if (s->txn->req.flags & HTTP_MSGF_VER_11) { |
| 5736 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 5737 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), |
| 5738 | ist2(status, status_len), ist2(reason, reason_len)); |
| 5739 | } |
| 5740 | else { |
| 5741 | flags = HTX_SL_F_IS_RESP; |
| 5742 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), |
| 5743 | ist2(status, status_len), ist2(reason, reason_len)); |
| 5744 | } |
| 5745 | if (!sl) |
| 5746 | goto fail; |
| 5747 | sl->info.res.status = code; |
| 5748 | |
| 5749 | /* Push in the stack the "headers" entry. */ |
| 5750 | ret = lua_getfield(L, -1, "headers"); |
| 5751 | if (ret != LUA_TTABLE) |
| 5752 | goto skip_headers; |
| 5753 | |
| 5754 | lua_pushnil(L); |
| 5755 | while (lua_next(L, -2) != 0) { |
| 5756 | struct ist name, value; |
| 5757 | const char *n, *v; |
| 5758 | size_t nlen, vlen; |
| 5759 | |
| 5760 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 5761 | /* Skip element if the key is not a string or if the value is not a table */ |
| 5762 | goto next_hdr; |
| 5763 | } |
| 5764 | |
| 5765 | n = lua_tolstring(L, -2, &nlen); |
| 5766 | name = ist2(n, nlen); |
| 5767 | if (isteqi(name, ist("content-length"))) { |
| 5768 | /* Always skip content-length header. It will be added |
| 5769 | * later with the correct len |
| 5770 | */ |
| 5771 | goto next_hdr; |
| 5772 | } |
| 5773 | |
| 5774 | /* Loop on header's values */ |
| 5775 | lua_pushnil(L); |
| 5776 | while (lua_next(L, -2)) { |
| 5777 | if (!lua_isstring(L, -1)) { |
| 5778 | /* Skip the value if it is not a string */ |
| 5779 | goto next_value; |
| 5780 | } |
| 5781 | |
| 5782 | v = lua_tolstring(L, -1, &vlen); |
| 5783 | value = ist2(v, vlen); |
| 5784 | |
| 5785 | if (isteqi(name, ist("transfer-encoding"))) |
| 5786 | h1_parse_xfer_enc_header(&h1m, value); |
| 5787 | if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen))) |
| 5788 | goto fail; |
| 5789 | |
| 5790 | next_value: |
| 5791 | lua_pop(L, 1); |
| 5792 | } |
| 5793 | |
| 5794 | next_hdr: |
| 5795 | lua_pop(L, 1); |
| 5796 | } |
| 5797 | skip_headers: |
| 5798 | lua_pop(L, 1); |
| 5799 | |
| 5800 | /* Update h1m flags: CLEN is set if CHNK is not present */ |
| 5801 | if (!(h1m.flags & H1_MF_CHNK)) { |
| 5802 | const char *clen = ultoa(body_len); |
| 5803 | |
| 5804 | h1m.flags |= H1_MF_CLEN; |
| 5805 | if (!htx_add_header(htx, ist("content-length"), ist(clen))) |
| 5806 | goto fail; |
| 5807 | } |
| 5808 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 5809 | h1m.flags |= H1_MF_XFER_LEN; |
| 5810 | |
| 5811 | /* Update HTX start-line flags */ |
| 5812 | if (h1m.flags & H1_MF_XFER_ENC) |
| 5813 | flags |= HTX_SL_F_XFER_ENC; |
| 5814 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 5815 | flags |= HTX_SL_F_XFER_LEN; |
| 5816 | if (h1m.flags & H1_MF_CHNK) |
| 5817 | flags |= HTX_SL_F_CHNK; |
| 5818 | else if (h1m.flags & H1_MF_CLEN) |
| 5819 | flags |= HTX_SL_F_CLEN; |
| 5820 | if (h1m.body_len == 0) |
| 5821 | flags |= HTX_SL_F_BODYLESS; |
| 5822 | } |
| 5823 | sl->flags |= flags; |
| 5824 | |
| 5825 | |
| 5826 | if (!htx_add_endof(htx, HTX_BLK_EOH) || |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5827 | (body_len && !htx_add_data_atonce(htx, ist2(body, body_len)))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5828 | goto fail; |
| 5829 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5830 | htx->flags |= HTX_FL_EOM; |
| 5831 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5832 | /* Now, forward the response and terminate the transaction */ |
| 5833 | s->txn->status = code; |
| 5834 | htx_to_buf(htx, &s->res.buf); |
| 5835 | if (!http_forward_proxy_resp(s, 1)) |
| 5836 | goto fail; |
| 5837 | |
| 5838 | return 1; |
| 5839 | |
| 5840 | fail: |
| 5841 | channel_htx_truncate(&s->res, htx); |
| 5842 | return 0; |
| 5843 | } |
| 5844 | |
| 5845 | /* Terminate a transaction if called from a lua action. For TCP streams, |
| 5846 | * processing is just aborted. Nothing is returned to the client and all |
| 5847 | * arguments are ignored. For HTTP streams, if a reply is passed as argument, it |
| 5848 | * is forwarded to the client before terminating the transaction. On success, |
| 5849 | * the function exits with ACT_RET_DONE code. If an error occurred, it exits |
| 5850 | * with ACT_RET_ERR code. If this function is not called from a lua action, it |
| 5851 | * just exits without any processing. |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5852 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5853 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5854 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5855 | struct hlua_txn *htxn; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5856 | struct stream *s; |
| 5857 | int finst; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5858 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5859 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5860 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5861 | /* If the flags NOTERM is set, we cannot terminate the session, so we |
| 5862 | * just end the execution of the current lua code. */ |
| 5863 | if (htxn->flags & HLUA_TXN_NOTERM) |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5864 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5865 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5866 | s = htxn->s; |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5867 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5868 | struct channel *req = &s->req; |
| 5869 | struct channel *res = &s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5870 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5871 | channel_auto_read(req); |
| 5872 | channel_abort(req); |
| 5873 | channel_auto_close(req); |
| 5874 | channel_erase(req); |
| 5875 | |
| 5876 | res->wex = tick_add_ifset(now_ms, res->wto); |
| 5877 | channel_auto_read(res); |
| 5878 | channel_auto_close(res); |
| 5879 | channel_shutr_now(res); |
| 5880 | |
| 5881 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D); |
| 5882 | goto done; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5883 | } |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 5884 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5885 | if (lua_gettop(L) == 1 || !lua_istable(L, 2)) { |
| 5886 | /* No reply or invalid reply */ |
| 5887 | s->txn->status = 0; |
| 5888 | http_reply_and_close(s, 0, NULL); |
| 5889 | } |
| 5890 | else { |
| 5891 | /* Remove extra args to have the reply on top of the stack */ |
| 5892 | if (lua_gettop(L) > 2) |
| 5893 | lua_pop(L, lua_gettop(L) - 2); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5894 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5895 | if (!hlua_txn_forward_reply(L, s)) { |
| 5896 | if (!(s->flags & SF_ERR_MASK)) |
| 5897 | s->flags |= SF_ERR_PRXCOND; |
| 5898 | lua_pushinteger(L, ACT_RET_ERR); |
| 5899 | WILL_LJMP(hlua_done(L)); |
| 5900 | return 0; /* Never reached */ |
| 5901 | } |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 5902 | } |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5903 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5904 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H); |
| 5905 | if (htxn->dir == SMP_OPT_DIR_REQ) { |
| 5906 | /* let's log the request time */ |
| 5907 | s->logs.tv_request = now; |
| 5908 | if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */ |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 5909 | _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5910 | } |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5911 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5912 | done: |
| 5913 | if (!(s->flags & SF_ERR_MASK)) |
| 5914 | s->flags |= SF_ERR_LOCAL; |
| 5915 | if (!(s->flags & SF_FINST_MASK)) |
| 5916 | s->flags |= finst; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5917 | |
Christopher Faulet | 4ad7310 | 2020-03-05 11:07:31 +0100 | [diff] [blame] | 5918 | lua_pushinteger(L, ACT_RET_ABRT); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5919 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5920 | return 0; |
| 5921 | } |
| 5922 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5923 | /* |
| 5924 | * |
| 5925 | * |
| 5926 | * Class REPLY |
| 5927 | * |
| 5928 | * |
| 5929 | */ |
| 5930 | |
| 5931 | /* Pushes the TXN reply onto the top of the stack. If the stask does not have a |
| 5932 | * free slots, the function fails and returns 0; |
| 5933 | */ |
| 5934 | static int hlua_txn_reply_new(lua_State *L) |
| 5935 | { |
| 5936 | struct hlua_txn *htxn; |
| 5937 | const char *reason, *body = NULL; |
| 5938 | int ret, status; |
| 5939 | |
| 5940 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5941 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5942 | hlua_pusherror(L, "txn object is not an HTTP transaction."); |
| 5943 | WILL_LJMP(lua_error(L)); |
| 5944 | } |
| 5945 | |
| 5946 | /* Default value */ |
| 5947 | status = 200; |
| 5948 | reason = http_get_reason(status); |
| 5949 | |
| 5950 | if (lua_istable(L, 2)) { |
| 5951 | /* load status and reason from the table argument at index 2 */ |
| 5952 | ret = lua_getfield(L, 2, "status"); |
| 5953 | if (ret == LUA_TNIL) |
| 5954 | goto reason; |
| 5955 | else if (ret != LUA_TNUMBER) { |
| 5956 | /* invalid status: ignore the reason */ |
| 5957 | goto body; |
| 5958 | } |
| 5959 | status = lua_tointeger(L, -1); |
| 5960 | |
| 5961 | reason: |
| 5962 | lua_pop(L, 1); /* restore the stack: remove status */ |
| 5963 | ret = lua_getfield(L, 2, "reason"); |
| 5964 | if (ret == LUA_TSTRING) |
| 5965 | reason = lua_tostring(L, -1); |
| 5966 | |
| 5967 | body: |
| 5968 | lua_pop(L, 1); /* restore the stack: remove invalid status or reason */ |
| 5969 | ret = lua_getfield(L, 2, "body"); |
| 5970 | if (ret == LUA_TSTRING) |
| 5971 | body = lua_tostring(L, -1); |
| 5972 | lua_pop(L, 1); /* restore the stack: remove body */ |
| 5973 | } |
| 5974 | |
| 5975 | /* Create the Reply table */ |
| 5976 | lua_newtable(L); |
| 5977 | |
| 5978 | /* Add status element */ |
| 5979 | lua_pushstring(L, "status"); |
| 5980 | lua_pushinteger(L, status); |
| 5981 | lua_settable(L, -3); |
| 5982 | |
| 5983 | /* Add reason element */ |
| 5984 | reason = http_get_reason(status); |
| 5985 | lua_pushstring(L, "reason"); |
| 5986 | lua_pushstring(L, reason); |
| 5987 | lua_settable(L, -3); |
| 5988 | |
| 5989 | /* Add body element, nil if undefined */ |
| 5990 | lua_pushstring(L, "body"); |
| 5991 | if (body) |
| 5992 | lua_pushstring(L, body); |
| 5993 | else |
| 5994 | lua_pushnil(L); |
| 5995 | lua_settable(L, -3); |
| 5996 | |
| 5997 | /* Add headers element */ |
| 5998 | lua_pushstring(L, "headers"); |
| 5999 | lua_newtable(L); |
| 6000 | |
| 6001 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 6002 | if (lua_istable(L, 2)) { |
| 6003 | /* load headers from the table argument at index 2. If it is a table, copy it. */ |
| 6004 | ret = lua_getfield(L, 2, "headers"); |
| 6005 | if (ret == LUA_TTABLE) { |
| 6006 | /* stack: [ ... <headers:table>, <table> ] */ |
| 6007 | lua_pushnil(L); |
| 6008 | while (lua_next(L, -2) != 0) { |
| 6009 | /* stack: [ ... <headers:table>, <table>, k, v] */ |
| 6010 | if (!lua_isstring(L, -1) && !lua_istable(L, -1)) { |
| 6011 | /* invalid value type, skip it */ |
| 6012 | lua_pop(L, 1); |
| 6013 | continue; |
| 6014 | } |
| 6015 | |
| 6016 | |
| 6017 | /* Duplicate the key and swap it with the value. */ |
| 6018 | lua_pushvalue(L, -2); |
| 6019 | lua_insert(L, -2); |
| 6020 | /* stack: [ ... <headers:table>, <table>, k, k, v ] */ |
| 6021 | |
| 6022 | lua_newtable(L); |
| 6023 | lua_insert(L, -2); |
| 6024 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */ |
| 6025 | |
| 6026 | if (lua_isstring(L, -1)) { |
| 6027 | /* push the value in the inner table */ |
| 6028 | lua_rawseti(L, -2, 1); |
| 6029 | } |
| 6030 | else { /* table */ |
| 6031 | lua_pushnil(L); |
| 6032 | while (lua_next(L, -2) != 0) { |
| 6033 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */ |
| 6034 | if (!lua_isstring(L, -1)) { |
| 6035 | /* invalid value type, skip it*/ |
| 6036 | lua_pop(L, 1); |
| 6037 | continue; |
| 6038 | } |
| 6039 | /* push the value in the inner table */ |
| 6040 | lua_rawseti(L, -4, lua_rawlen(L, -4) + 1); |
| 6041 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */ |
| 6042 | } |
| 6043 | lua_pop(L, 1); |
| 6044 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */ |
| 6045 | } |
| 6046 | |
| 6047 | /* push (k,v) on the stack in the headers table: |
| 6048 | * stack: [ ... <headers:table>, <table>, k, k, v ] |
| 6049 | */ |
| 6050 | lua_settable(L, -5); |
| 6051 | /* stack: [ ... <headers:table>, <table>, k ] */ |
| 6052 | } |
| 6053 | } |
| 6054 | lua_pop(L, 1); |
| 6055 | } |
| 6056 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 6057 | lua_settable(L, -3); |
| 6058 | /* stack: [ txn, <Arg:table>, <Reply:table> ] */ |
| 6059 | |
| 6060 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 6061 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref); |
| 6062 | lua_setmetatable(L, -2); |
| 6063 | return 1; |
| 6064 | } |
| 6065 | |
| 6066 | /* Set the reply status code, and optionally the reason. If no reason is |
| 6067 | * provided, the default one corresponding to the status code is used. |
| 6068 | */ |
| 6069 | __LJMP static int hlua_txn_reply_set_status(lua_State *L) |
| 6070 | { |
| 6071 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6072 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 6073 | |
| 6074 | /* First argument (self) must be a table */ |
| 6075 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6076 | |
| 6077 | if (status < 100 || status > 599) { |
| 6078 | lua_pushboolean(L, 0); |
| 6079 | return 1; |
| 6080 | } |
| 6081 | if (!reason) |
| 6082 | reason = http_get_reason(status); |
| 6083 | |
| 6084 | lua_pushinteger(L, status); |
| 6085 | lua_setfield(L, 1, "status"); |
| 6086 | |
| 6087 | lua_pushstring(L, reason); |
| 6088 | lua_setfield(L, 1, "reason"); |
| 6089 | |
| 6090 | lua_pushboolean(L, 1); |
| 6091 | return 1; |
| 6092 | } |
| 6093 | |
| 6094 | /* Add a header into the reply object. Each header name is associated to an |
| 6095 | * array of values in the "headers" table. If the header name is not found, a |
| 6096 | * new entry is created. |
| 6097 | */ |
| 6098 | __LJMP static int hlua_txn_reply_add_header(lua_State *L) |
| 6099 | { |
| 6100 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6101 | const char *value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 6102 | int ret; |
| 6103 | |
| 6104 | /* First argument (self) must be a table */ |
| 6105 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6106 | |
| 6107 | /* Push in the stack the "headers" entry. */ |
| 6108 | ret = lua_getfield(L, 1, "headers"); |
| 6109 | if (ret != LUA_TTABLE) { |
| 6110 | hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret)); |
| 6111 | WILL_LJMP(lua_error(L)); |
| 6112 | } |
| 6113 | |
| 6114 | /* check if the header is already registered. If not, register it. */ |
| 6115 | ret = lua_getfield(L, -1, name); |
| 6116 | if (ret == LUA_TNIL) { |
| 6117 | /* Entry not found. */ |
| 6118 | lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */ |
| 6119 | |
| 6120 | /* Insert the new header name in the array in the top of the stack. |
| 6121 | * It left the new array in the top of the stack. |
| 6122 | */ |
| 6123 | lua_newtable(L); |
| 6124 | lua_pushstring(L, name); |
| 6125 | lua_pushvalue(L, -2); |
| 6126 | lua_settable(L, -4); |
| 6127 | } |
| 6128 | else if (ret != LUA_TTABLE) { |
| 6129 | hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret)); |
| 6130 | WILL_LJMP(lua_error(L)); |
| 6131 | } |
| 6132 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 6133 | /* Now the top of thestack is an array of values. We push |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6134 | * the header value as new entry. |
| 6135 | */ |
| 6136 | lua_pushstring(L, value); |
| 6137 | ret = lua_rawlen(L, -2); |
| 6138 | lua_rawseti(L, -2, ret + 1); |
| 6139 | |
| 6140 | lua_pushboolean(L, 1); |
| 6141 | return 1; |
| 6142 | } |
| 6143 | |
| 6144 | /* Remove all occurrences of a given header name. */ |
| 6145 | __LJMP static int hlua_txn_reply_del_header(lua_State *L) |
| 6146 | { |
| 6147 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6148 | int ret; |
| 6149 | |
| 6150 | /* First argument (self) must be a table */ |
| 6151 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6152 | |
| 6153 | /* Push in the stack the "headers" entry. */ |
| 6154 | ret = lua_getfield(L, 1, "headers"); |
| 6155 | if (ret != LUA_TTABLE) { |
| 6156 | hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret)); |
| 6157 | WILL_LJMP(lua_error(L)); |
| 6158 | } |
| 6159 | |
| 6160 | lua_pushstring(L, name); |
| 6161 | lua_pushnil(L); |
| 6162 | lua_settable(L, -3); |
| 6163 | |
| 6164 | lua_pushboolean(L, 1); |
| 6165 | return 1; |
| 6166 | } |
| 6167 | |
| 6168 | /* Set the reply's body. Overwrite any existing entry. */ |
| 6169 | __LJMP static int hlua_txn_reply_set_body(lua_State *L) |
| 6170 | { |
| 6171 | const char *payload = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6172 | |
| 6173 | /* First argument (self) must be a table */ |
| 6174 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6175 | |
| 6176 | lua_pushstring(L, payload); |
| 6177 | lua_setfield(L, 1, "body"); |
| 6178 | |
| 6179 | lua_pushboolean(L, 1); |
| 6180 | return 1; |
| 6181 | } |
| 6182 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 6183 | __LJMP static int hlua_log(lua_State *L) |
| 6184 | { |
| 6185 | int level; |
| 6186 | const char *msg; |
| 6187 | |
| 6188 | MAY_LJMP(check_args(L, 2, "log")); |
| 6189 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 6190 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6191 | |
| 6192 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 6193 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 6194 | |
| 6195 | hlua_sendlog(NULL, level, msg); |
| 6196 | return 0; |
| 6197 | } |
| 6198 | |
| 6199 | __LJMP static int hlua_log_debug(lua_State *L) |
| 6200 | { |
| 6201 | const char *msg; |
| 6202 | |
| 6203 | MAY_LJMP(check_args(L, 1, "debug")); |
| 6204 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6205 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 6206 | return 0; |
| 6207 | } |
| 6208 | |
| 6209 | __LJMP static int hlua_log_info(lua_State *L) |
| 6210 | { |
| 6211 | const char *msg; |
| 6212 | |
| 6213 | MAY_LJMP(check_args(L, 1, "info")); |
| 6214 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6215 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 6216 | return 0; |
| 6217 | } |
| 6218 | |
| 6219 | __LJMP static int hlua_log_warning(lua_State *L) |
| 6220 | { |
| 6221 | const char *msg; |
| 6222 | |
| 6223 | MAY_LJMP(check_args(L, 1, "warning")); |
| 6224 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6225 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 6226 | return 0; |
| 6227 | } |
| 6228 | |
| 6229 | __LJMP static int hlua_log_alert(lua_State *L) |
| 6230 | { |
| 6231 | const char *msg; |
| 6232 | |
| 6233 | MAY_LJMP(check_args(L, 1, "alert")); |
| 6234 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6235 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6236 | return 0; |
| 6237 | } |
| 6238 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6239 | __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] | 6240 | { |
| 6241 | int wakeup_ms = lua_tointeger(L, -1); |
| 6242 | if (now_ms < wakeup_ms) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6243 | 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] | 6244 | return 0; |
| 6245 | } |
| 6246 | |
| 6247 | __LJMP static int hlua_sleep(lua_State *L) |
| 6248 | { |
| 6249 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6250 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6251 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6252 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6253 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6254 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6255 | wakeup_ms = tick_add(now_ms, delay); |
| 6256 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6257 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6258 | 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] | 6259 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6260 | } |
| 6261 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6262 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6263 | { |
| 6264 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6265 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6266 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6267 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6268 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6269 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6270 | wakeup_ms = tick_add(now_ms, delay); |
| 6271 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6272 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6273 | 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] | 6274 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6275 | } |
| 6276 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6277 | /* This functionis an LUA binding. it permits to give back |
| 6278 | * the hand at the HAProxy scheduler. It is used when the |
| 6279 | * LUA processing consumes a lot of time. |
| 6280 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6281 | __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] | 6282 | { |
| 6283 | return 0; |
| 6284 | } |
| 6285 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6286 | __LJMP static int hlua_yield(lua_State *L) |
| 6287 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6288 | 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] | 6289 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6290 | } |
| 6291 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6292 | /* This function change the nice of the currently executed |
| 6293 | * task. It is used set low or high priority at the current |
| 6294 | * task. |
| 6295 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 6296 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6297 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6298 | struct hlua *hlua; |
| 6299 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6300 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6301 | MAY_LJMP(check_args(L, 1, "set_nice")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6302 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6303 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 6304 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 6305 | hlua = hlua_gethlua(L); |
| 6306 | |
| 6307 | /* If the task is not set, I'm in a start mode. */ |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6308 | if (!hlua || !hlua->task) |
| 6309 | return 0; |
| 6310 | |
| 6311 | if (nice < -1024) |
| 6312 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6313 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6314 | nice = 1024; |
| 6315 | |
| 6316 | hlua->task->nice = nice; |
| 6317 | return 0; |
| 6318 | } |
| 6319 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6320 | /* 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] | 6321 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 6322 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 6323 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6324 | * |
| 6325 | * Task wrapper are longjmp safe because the only one Lua code |
| 6326 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6327 | */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 6328 | struct task *hlua_process_task(struct task *task, void *context, unsigned int state) |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6329 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6330 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6331 | enum hlua_exec status; |
| 6332 | |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 6333 | if (task->thread_mask == MAX_THREADS_MASK) |
| 6334 | task_set_affinity(task, tid_bit); |
| 6335 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6336 | /* If it is the first call to the task, we must initialize the |
| 6337 | * execution timeouts. |
| 6338 | */ |
| 6339 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6340 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6341 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6342 | /* Execute the Lua code. */ |
| 6343 | status = hlua_ctx_resume(hlua, 1); |
| 6344 | |
| 6345 | switch (status) { |
| 6346 | /* finished or yield */ |
| 6347 | case HLUA_E_OK: |
| 6348 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6349 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 6350 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6351 | break; |
| 6352 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6353 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 6354 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 6355 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6356 | break; |
| 6357 | |
| 6358 | /* finished with error. */ |
| 6359 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6360 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6361 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6362 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6363 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6364 | break; |
| 6365 | |
| 6366 | case HLUA_E_ERR: |
| 6367 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6368 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6369 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6370 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6371 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6372 | break; |
| 6373 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6374 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6375 | } |
| 6376 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6377 | /* This function is an LUA binding that register LUA function to be |
| 6378 | * executed after the HAProxy configuration parsing and before the |
| 6379 | * HAProxy scheduler starts. This function expect only one LUA |
| 6380 | * argument that is a function. This function returns nothing, but |
| 6381 | * throws if an error is encountered. |
| 6382 | */ |
| 6383 | __LJMP static int hlua_register_init(lua_State *L) |
| 6384 | { |
| 6385 | struct hlua_init_function *init; |
| 6386 | int ref; |
| 6387 | |
| 6388 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 6389 | |
| 6390 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6391 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6392 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6393 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6394 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6395 | |
| 6396 | init->function_ref = ref; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 6397 | LIST_APPEND(&hlua_init_functions[hlua_state_id], &init->l); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6398 | return 0; |
| 6399 | } |
| 6400 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6401 | /* This functio is an LUA binding. It permits to register a task |
| 6402 | * executed in parallel of the main HAroxy activity. The task is |
| 6403 | * created and it is set in the HAProxy scheduler. It can be called |
| 6404 | * from the "init" section, "post init" or during the runtime. |
| 6405 | * |
| 6406 | * Lua prototype: |
| 6407 | * |
| 6408 | * <none> core.register_task(<function>) |
| 6409 | */ |
| 6410 | static int hlua_register_task(lua_State *L) |
| 6411 | { |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6412 | struct hlua *hlua = NULL; |
| 6413 | struct task *task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6414 | int ref; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 6415 | int state_id; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6416 | |
| 6417 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 6418 | |
| 6419 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6420 | |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 6421 | /* Get the reference state. If the reference is NULL, L is the master |
| 6422 | * state, otherwise hlua->T is. |
| 6423 | */ |
| 6424 | hlua = hlua_gethlua(L); |
| 6425 | if (hlua) |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 6426 | /* we are in runtime processing */ |
| 6427 | state_id = hlua->state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 6428 | else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 6429 | /* we are in initialization mode */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6430 | state_id = hlua_state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 6431 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6432 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6433 | if (!hlua) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6434 | goto alloc_error; |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6435 | HLUA_INIT(hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6436 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 6437 | /* We are in the common lua state, execute the task anywhere, |
| 6438 | * otherwise, inherit the current thread identifier |
| 6439 | */ |
| 6440 | if (state_id == 0) |
| 6441 | task = task_new(MAX_THREADS_MASK); |
| 6442 | else |
| 6443 | task = task_new(tid_bit); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 6444 | if (!task) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6445 | goto alloc_error; |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 6446 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6447 | task->context = hlua; |
| 6448 | task->process = hlua_process_task; |
| 6449 | |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 6450 | if (!hlua_ctx_init(hlua, state_id, task, 1)) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6451 | goto alloc_error; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6452 | |
| 6453 | /* Restore the function in the stack. */ |
| 6454 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 6455 | hlua->nargs = 0; |
| 6456 | |
| 6457 | /* Schedule task. */ |
| 6458 | task_schedule(task, now_ms); |
| 6459 | |
| 6460 | return 0; |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6461 | |
| 6462 | alloc_error: |
| 6463 | task_destroy(task); |
| 6464 | hlua_ctx_destroy(hlua); |
| 6465 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 6466 | return 0; /* Never reached */ |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6467 | } |
| 6468 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6469 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 6470 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 6471 | * resume converters. |
| 6472 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6473 | 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] | 6474 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6475 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6476 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6477 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6478 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6479 | if (!stream) |
| 6480 | return 0; |
| 6481 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6482 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6483 | * Lua context can be not initialized. This behavior |
| 6484 | * permits to save performances because a systematic |
| 6485 | * Lua initialization cause 5% performances loss. |
| 6486 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6487 | if (!stream->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6488 | struct hlua *hlua; |
| 6489 | |
| 6490 | hlua = pool_alloc(pool_head_hlua); |
| 6491 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6492 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6493 | return 0; |
| 6494 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6495 | HLUA_INIT(hlua); |
| 6496 | stream->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6497 | 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] | 6498 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6499 | return 0; |
| 6500 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6501 | } |
| 6502 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6503 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6504 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6505 | |
| 6506 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6507 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6508 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6509 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6510 | else |
| 6511 | error = "critical error"; |
| 6512 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6513 | return 0; |
| 6514 | } |
| 6515 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6516 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6517 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6518 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6519 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6520 | return 0; |
| 6521 | } |
| 6522 | |
| 6523 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6524 | 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] | 6525 | |
| 6526 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6527 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6528 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6529 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6530 | return 0; |
| 6531 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6532 | hlua_smp2lua(stream->hlua->T, smp); |
| 6533 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6534 | |
| 6535 | /* push keywords in the stack. */ |
| 6536 | if (arg_p) { |
| 6537 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6538 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6539 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6540 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6541 | return 0; |
| 6542 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6543 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6544 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6545 | } |
| 6546 | } |
| 6547 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6548 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6549 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6550 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6551 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6552 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6553 | } |
| 6554 | |
| 6555 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6556 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6557 | /* finished. */ |
| 6558 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6559 | /* If the stack is empty, the function fails. */ |
| 6560 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6561 | return 0; |
| 6562 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6563 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6564 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6565 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6566 | return 1; |
| 6567 | |
| 6568 | /* yield. */ |
| 6569 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6570 | 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] | 6571 | return 0; |
| 6572 | |
| 6573 | /* finished with error. */ |
| 6574 | case HLUA_E_ERRMSG: |
| 6575 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6576 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6577 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6578 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6579 | return 0; |
| 6580 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6581 | case HLUA_E_ETMOUT: |
| 6582 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 6583 | return 0; |
| 6584 | |
| 6585 | case HLUA_E_NOMEM: |
| 6586 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 6587 | return 0; |
| 6588 | |
| 6589 | case HLUA_E_YIELD: |
| 6590 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 6591 | return 0; |
| 6592 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6593 | case HLUA_E_ERR: |
| 6594 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6595 | 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] | 6596 | /* fall through */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6597 | |
| 6598 | default: |
| 6599 | return 0; |
| 6600 | } |
| 6601 | } |
| 6602 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6603 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 6604 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6605 | * resume sample-fetches. This function will be called by the sample |
| 6606 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6607 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6608 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 6609 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6610 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6611 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6612 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6613 | const char *error; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6614 | unsigned int hflags = HLUA_TXN_NOTERM; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6615 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6616 | if (!stream) |
| 6617 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 6618 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6619 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6620 | * Lua context can be not initialized. This behavior |
| 6621 | * permits to save performances because a systematic |
| 6622 | * Lua initialization cause 5% performances loss. |
| 6623 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6624 | if (!stream->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6625 | struct hlua *hlua; |
| 6626 | |
| 6627 | hlua = pool_alloc(pool_head_hlua); |
| 6628 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6629 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6630 | return 0; |
| 6631 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6632 | hlua->T = NULL; |
| 6633 | stream->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6634 | 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] | 6635 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6636 | return 0; |
| 6637 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6638 | } |
| 6639 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6640 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6641 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6642 | |
| 6643 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6644 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6645 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6646 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6647 | else |
| 6648 | error = "critical error"; |
| 6649 | 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] | 6650 | return 0; |
| 6651 | } |
| 6652 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6653 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6654 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6655 | 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] | 6656 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6657 | return 0; |
| 6658 | } |
| 6659 | |
| 6660 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6661 | 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] | 6662 | |
| 6663 | /* push arguments in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6664 | 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] | 6665 | 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] | 6666 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6667 | return 0; |
| 6668 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6669 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6670 | |
| 6671 | /* push keywords in the stack. */ |
| 6672 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 6673 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6674 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6675 | 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] | 6676 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6677 | return 0; |
| 6678 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6679 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6680 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6681 | } |
| 6682 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6683 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6684 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6685 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6686 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6687 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6688 | } |
| 6689 | |
| 6690 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6691 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6692 | /* finished. */ |
| 6693 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6694 | /* If the stack is empty, the function fails. */ |
| 6695 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6696 | return 0; |
| 6697 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6698 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6699 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6700 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6701 | |
| 6702 | /* Set the end of execution flag. */ |
| 6703 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 6704 | return 1; |
| 6705 | |
| 6706 | /* yield. */ |
| 6707 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6708 | 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] | 6709 | return 0; |
| 6710 | |
| 6711 | /* finished with error. */ |
| 6712 | case HLUA_E_ERRMSG: |
| 6713 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6714 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6715 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6716 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6717 | return 0; |
| 6718 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6719 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6720 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 6721 | return 0; |
| 6722 | |
| 6723 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6724 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 6725 | return 0; |
| 6726 | |
| 6727 | case HLUA_E_YIELD: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6728 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 6729 | return 0; |
| 6730 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6731 | case HLUA_E_ERR: |
| 6732 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6733 | 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] | 6734 | /* fall through */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6735 | |
| 6736 | default: |
| 6737 | return 0; |
| 6738 | } |
| 6739 | } |
| 6740 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6741 | /* This function is an LUA binding used for registering |
| 6742 | * "sample-conv" functions. It expects a converter name used |
| 6743 | * in the haproxy configuration file, and an LUA function. |
| 6744 | */ |
| 6745 | __LJMP static int hlua_register_converters(lua_State *L) |
| 6746 | { |
| 6747 | struct sample_conv_kw_list *sck; |
| 6748 | const char *name; |
| 6749 | int ref; |
| 6750 | int len; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6751 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6752 | struct sample_conv *sc; |
| 6753 | struct buffer *trash; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6754 | |
| 6755 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 6756 | |
| 6757 | /* First argument : converter name. */ |
| 6758 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6759 | |
| 6760 | /* Second argument : lua function. */ |
| 6761 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6762 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6763 | /* Check if the converter is already registered */ |
| 6764 | trash = get_trash_chunk(); |
| 6765 | chunk_printf(trash, "lua.%s", name); |
| 6766 | sc = find_sample_conv(trash->area, trash->data); |
| 6767 | if (sc != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 6768 | fcn = sc->private; |
| 6769 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 6770 | ha_warning("Trying to register converter 'lua.%s' more than once. " |
| 6771 | "This will become a hard error in version 2.5.\n", name); |
| 6772 | } |
| 6773 | fcn->function_ref[hlua_state_id] = ref; |
| 6774 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6775 | } |
| 6776 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6777 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6778 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6779 | if (!sck) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6780 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 6781 | fcn = new_hlua_function(); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6782 | if (!fcn) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6783 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6784 | |
| 6785 | /* Fill fcn. */ |
| 6786 | fcn->name = strdup(name); |
| 6787 | if (!fcn->name) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6788 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6789 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6790 | |
| 6791 | /* List head */ |
| 6792 | sck->list.n = sck->list.p = NULL; |
| 6793 | |
| 6794 | /* converter keyword. */ |
| 6795 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6796 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6797 | if (!sck->kw[0].kw) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6798 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6799 | |
| 6800 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 6801 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6802 | 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] | 6803 | sck->kw[0].val_args = NULL; |
| 6804 | sck->kw[0].in_type = SMP_T_STR; |
| 6805 | sck->kw[0].out_type = SMP_T_STR; |
| 6806 | sck->kw[0].private = fcn; |
| 6807 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6808 | /* Register this new converter */ |
| 6809 | sample_register_convs(sck); |
| 6810 | |
| 6811 | return 0; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6812 | |
| 6813 | alloc_error: |
| 6814 | release_hlua_function(fcn); |
| 6815 | ha_free(&sck); |
| 6816 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 6817 | return 0; /* Never reached */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6818 | } |
| 6819 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6820 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6821 | * "sample-fetch" functions. It expects a converter name used |
| 6822 | * in the haproxy configuration file, and an LUA function. |
| 6823 | */ |
| 6824 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 6825 | { |
| 6826 | const char *name; |
| 6827 | int ref; |
| 6828 | int len; |
| 6829 | struct sample_fetch_kw_list *sfk; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 6830 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6831 | struct sample_fetch *sf; |
| 6832 | struct buffer *trash; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6833 | |
| 6834 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 6835 | |
| 6836 | /* First argument : sample-fetch name. */ |
| 6837 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6838 | |
| 6839 | /* Second argument : lua function. */ |
| 6840 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6841 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6842 | /* Check if the sample-fetch is already registered */ |
| 6843 | trash = get_trash_chunk(); |
| 6844 | chunk_printf(trash, "lua.%s", name); |
| 6845 | sf = find_sample_fetch(trash->area, trash->data); |
| 6846 | if (sf != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 6847 | fcn = sf->private; |
| 6848 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 6849 | ha_warning("Trying to register sample-fetch 'lua.%s' more than once. " |
| 6850 | "This will become a hard error in version 2.5.\n", name); |
| 6851 | } |
| 6852 | fcn->function_ref[hlua_state_id] = ref; |
| 6853 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6854 | } |
| 6855 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6856 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6857 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6858 | if (!sfk) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 6859 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 6860 | fcn = new_hlua_function(); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6861 | if (!fcn) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 6862 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6863 | |
| 6864 | /* Fill fcn. */ |
| 6865 | fcn->name = strdup(name); |
| 6866 | if (!fcn->name) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 6867 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6868 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6869 | |
| 6870 | /* List head */ |
| 6871 | sfk->list.n = sfk->list.p = NULL; |
| 6872 | |
| 6873 | /* sample-fetch keyword. */ |
| 6874 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6875 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6876 | if (!sfk->kw[0].kw) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 6877 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6878 | |
| 6879 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 6880 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6881 | 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] | 6882 | sfk->kw[0].val_args = NULL; |
| 6883 | sfk->kw[0].out_type = SMP_T_STR; |
| 6884 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 6885 | sfk->kw[0].val = 0; |
| 6886 | sfk->kw[0].private = fcn; |
| 6887 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6888 | /* Register this new fetch. */ |
| 6889 | sample_register_fetches(sfk); |
| 6890 | |
| 6891 | return 0; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 6892 | |
| 6893 | alloc_error: |
| 6894 | release_hlua_function(fcn); |
| 6895 | ha_free(&sfk); |
| 6896 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 6897 | return 0; /* Never reached */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6898 | } |
| 6899 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 6900 | /* This function is a lua binding to set the wake_time. |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6901 | */ |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 6902 | __LJMP static int hlua_set_wake_time(lua_State *L) |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6903 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 6904 | struct hlua *hlua; |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6905 | unsigned int delay; |
| 6906 | unsigned int wakeup_ms; |
| 6907 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 6908 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 6909 | hlua = hlua_gethlua(L); |
| 6910 | if (!hlua) { |
| 6911 | return 0; |
| 6912 | } |
| 6913 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6914 | MAY_LJMP(check_args(L, 1, "wake_time")); |
| 6915 | |
| 6916 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 6917 | wakeup_ms = tick_add(now_ms, delay); |
| 6918 | hlua->wake_time = wakeup_ms; |
| 6919 | return 0; |
| 6920 | } |
| 6921 | |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6922 | /* This function is a wrapper to execute each LUA function declared as an action |
| 6923 | * wrapper during the initialisation period. This function may return any |
| 6924 | * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is |
| 6925 | * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the |
| 6926 | * return value is the first element on the stack. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6927 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6928 | 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] | 6929 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6930 | { |
| 6931 | char **arg; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6932 | unsigned int hflags = 0; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6933 | int dir, act_ret = ACT_RET_CONT; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6934 | const char *error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6935 | |
| 6936 | switch (rule->from) { |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 6937 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 6938 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 6939 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 6940 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6941 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6942 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6943 | goto end; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6944 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6945 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6946 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6947 | * Lua context can be not initialized. This behavior |
| 6948 | * permits to save performances because a systematic |
| 6949 | * Lua initialization cause 5% performances loss. |
| 6950 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6951 | if (!s->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6952 | struct hlua *hlua; |
| 6953 | |
| 6954 | hlua = pool_alloc(pool_head_hlua); |
| 6955 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6956 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6957 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6958 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6959 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6960 | HLUA_INIT(hlua); |
| 6961 | s->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6962 | 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] | 6963 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6964 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6965 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6966 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6967 | } |
| 6968 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6969 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6970 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6971 | |
| 6972 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6973 | if (!SET_SAFE_LJMP(s->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6974 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 6975 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6976 | else |
| 6977 | error = "critical error"; |
| 6978 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6979 | rule->arg.hlua_rule->fcn->name, error); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6980 | goto end; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6981 | } |
| 6982 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6983 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6984 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6985 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6986 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6987 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6988 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6989 | } |
| 6990 | |
| 6991 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6992 | 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] | 6993 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6994 | /* Create and and push object stream in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6995 | if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6996 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6997 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6998 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6999 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7000 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7001 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7002 | |
| 7003 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7004 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7005 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7006 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7007 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7008 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7009 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7010 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7011 | lua_pushstring(s->hlua->T, *arg); |
| 7012 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7013 | } |
| 7014 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7015 | /* Now the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7016 | RESET_SAFE_LJMP(s->hlua); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7017 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7018 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7019 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7020 | } |
| 7021 | |
| 7022 | /* Execute the function. */ |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 7023 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7024 | /* finished. */ |
| 7025 | case HLUA_E_OK: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7026 | /* Catch the return value */ |
| 7027 | if (lua_gettop(s->hlua->T) > 0) |
| 7028 | act_ret = lua_tointeger(s->hlua->T, -1); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7029 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 7030 | /* Set timeout in the required channel. */ |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 7031 | if (act_ret == ACT_RET_YIELD) { |
| 7032 | if (flags & ACT_OPT_FINAL) |
| 7033 | goto err_yield; |
| 7034 | |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 7035 | if (dir == SMP_OPT_DIR_REQ) |
| 7036 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 7037 | s->hlua->wake_time); |
| 7038 | else |
| 7039 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 7040 | s->hlua->wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 7041 | } |
| 7042 | goto end; |
| 7043 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7044 | /* yield. */ |
| 7045 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 7046 | /* Set timeout in the required channel. */ |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 7047 | if (dir == SMP_OPT_DIR_REQ) |
| 7048 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 7049 | s->hlua->wake_time); |
| 7050 | else |
| 7051 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 7052 | s->hlua->wake_time); |
| 7053 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7054 | /* Some actions can be wake up when a "write" event |
| 7055 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7056 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7057 | */ |
Christopher Faulet | 51fa358 | 2019-07-26 14:54:52 +0200 | [diff] [blame] | 7058 | if (HLUA_IS_WAKERESWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7059 | s->res.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7060 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7061 | s->req.flags |= CF_WAKE_WRITE; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7062 | act_ret = ACT_RET_YIELD; |
| 7063 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7064 | |
| 7065 | /* finished with error. */ |
| 7066 | case HLUA_E_ERRMSG: |
| 7067 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7068 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7069 | rule->arg.hlua_rule->fcn->name, lua_tostring(s->hlua->T, -1)); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7070 | lua_pop(s->hlua->T, 1); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7071 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7072 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7073 | case HLUA_E_ETMOUT: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7074 | 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] | 7075 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7076 | |
| 7077 | case HLUA_E_NOMEM: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7078 | 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] | 7079 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7080 | |
| 7081 | case HLUA_E_YIELD: |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 7082 | err_yield: |
| 7083 | act_ret = ACT_RET_CONT; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7084 | 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] | 7085 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7086 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7087 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7088 | case HLUA_E_ERR: |
| 7089 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7090 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7091 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7092 | |
| 7093 | default: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7094 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7095 | } |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7096 | |
| 7097 | end: |
Christopher Faulet | 2361fd9 | 2020-07-30 10:40:58 +0200 | [diff] [blame] | 7098 | if (act_ret != ACT_RET_YIELD && s->hlua) |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 7099 | s->hlua->wake_time = TICK_ETERNITY; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7100 | return act_ret; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7101 | } |
| 7102 | |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 7103 | struct task *hlua_applet_wakeup(struct task *t, void *context, unsigned int state) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7104 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 7105 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7106 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7107 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 7108 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 7109 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7110 | } |
| 7111 | |
| 7112 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 7113 | { |
| 7114 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7115 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7116 | struct task *task; |
| 7117 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7118 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7119 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7120 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7121 | if (!hlua) { |
| 7122 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7123 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7124 | return 0; |
| 7125 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7126 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7127 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7128 | ctx->ctx.hlua_apptcp.flags = 0; |
| 7129 | |
| 7130 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7131 | task = task_new(tid_bit); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7132 | if (!task) { |
| 7133 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7134 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7135 | return 0; |
| 7136 | } |
| 7137 | task->nice = 0; |
| 7138 | task->context = ctx; |
| 7139 | task->process = hlua_applet_wakeup; |
| 7140 | ctx->ctx.hlua_apptcp.task = task; |
| 7141 | |
| 7142 | /* In the execution wrappers linked with a stream, the |
| 7143 | * Lua context can be not initialized. This behavior |
| 7144 | * permits to save performances because a systematic |
| 7145 | * Lua initialization cause 5% performances loss. |
| 7146 | */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7147 | 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] | 7148 | 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] | 7149 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7150 | return 0; |
| 7151 | } |
| 7152 | |
| 7153 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 7154 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7155 | |
| 7156 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7157 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7158 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7159 | error = lua_tostring(hlua->T, -1); |
| 7160 | else |
| 7161 | error = "critical error"; |
| 7162 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7163 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7164 | return 0; |
| 7165 | } |
| 7166 | |
| 7167 | /* Check stack available size. */ |
| 7168 | if (!lua_checkstack(hlua->T, 1)) { |
| 7169 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7170 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7171 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7172 | return 0; |
| 7173 | } |
| 7174 | |
| 7175 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7176 | 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] | 7177 | |
| 7178 | /* Create and and push object stream in the stack. */ |
| 7179 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 7180 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7181 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7182 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7183 | return 0; |
| 7184 | } |
| 7185 | hlua->nargs = 1; |
| 7186 | |
| 7187 | /* push keywords in the stack. */ |
| 7188 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 7189 | if (!lua_checkstack(hlua->T, 1)) { |
| 7190 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7191 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7192 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7193 | return 0; |
| 7194 | } |
| 7195 | lua_pushstring(hlua->T, *arg); |
| 7196 | hlua->nargs++; |
| 7197 | } |
| 7198 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7199 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7200 | |
| 7201 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7202 | si_cant_get(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 7203 | si_rx_endp_more(si); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7204 | |
| 7205 | return 1; |
| 7206 | } |
| 7207 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7208 | void hlua_applet_tcp_fct(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7209 | { |
| 7210 | struct stream_interface *si = ctx->owner; |
| 7211 | struct stream *strm = si_strm(si); |
| 7212 | struct channel *res = si_ic(si); |
| 7213 | struct act_rule *rule = ctx->rule; |
| 7214 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7215 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7216 | |
| 7217 | /* The applet execution is already done. */ |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 7218 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) { |
| 7219 | /* eat the whole request */ |
| 7220 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7221 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 7222 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7223 | |
| 7224 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7225 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7226 | return; |
| 7227 | |
| 7228 | /* Execute the function. */ |
| 7229 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7230 | /* finished. */ |
| 7231 | case HLUA_E_OK: |
| 7232 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 7233 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7234 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 7235 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7236 | res->flags |= CF_READ_NULL; |
| 7237 | si_shutr(si); |
| 7238 | return; |
| 7239 | |
| 7240 | /* yield. */ |
| 7241 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 7242 | if (hlua->wake_time != TICK_ETERNITY) |
| 7243 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7244 | return; |
| 7245 | |
| 7246 | /* finished with error. */ |
| 7247 | case HLUA_E_ERRMSG: |
| 7248 | /* Display log. */ |
| 7249 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7250 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7251 | lua_pop(hlua->T, 1); |
| 7252 | goto error; |
| 7253 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7254 | case HLUA_E_ETMOUT: |
| 7255 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7256 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7257 | goto error; |
| 7258 | |
| 7259 | case HLUA_E_NOMEM: |
| 7260 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7261 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7262 | goto error; |
| 7263 | |
| 7264 | case HLUA_E_YIELD: /* unexpected */ |
| 7265 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7266 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7267 | goto error; |
| 7268 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7269 | case HLUA_E_ERR: |
| 7270 | /* Display log. */ |
| 7271 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7272 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7273 | goto error; |
| 7274 | |
| 7275 | default: |
| 7276 | goto error; |
| 7277 | } |
| 7278 | |
| 7279 | error: |
| 7280 | |
| 7281 | /* For all other cases, just close the stream. */ |
| 7282 | si_shutw(si); |
| 7283 | si_shutr(si); |
| 7284 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 7285 | } |
| 7286 | |
| 7287 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 7288 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7289 | task_destroy(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7290 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7291 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7292 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7293 | } |
| 7294 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7295 | /* The function returns 1 if the initialisation is complete, 0 if |
| 7296 | * an errors occurs and -1 if more data are required for initializing |
| 7297 | * the applet. |
| 7298 | */ |
| 7299 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 7300 | { |
| 7301 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7302 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7303 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7304 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7305 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7306 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7307 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7308 | txn = strm->txn; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7309 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7310 | if (!hlua) { |
| 7311 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7312 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7313 | return 0; |
| 7314 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7315 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7316 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7317 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 7318 | ctx->ctx.hlua_apphttp.flags = 0; |
| 7319 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 7320 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 7321 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 7322 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7323 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7324 | task = task_new(tid_bit); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7325 | if (!task) { |
| 7326 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7327 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7328 | return 0; |
| 7329 | } |
| 7330 | task->nice = 0; |
| 7331 | task->context = ctx; |
| 7332 | task->process = hlua_applet_wakeup; |
| 7333 | ctx->ctx.hlua_apphttp.task = task; |
| 7334 | |
| 7335 | /* In the execution wrappers linked with a stream, the |
| 7336 | * Lua context can be not initialized. This behavior |
| 7337 | * permits to save performances because a systematic |
| 7338 | * Lua initialization cause 5% performances loss. |
| 7339 | */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7340 | 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] | 7341 | 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] | 7342 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7343 | return 0; |
| 7344 | } |
| 7345 | |
| 7346 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 7347 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7348 | |
| 7349 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7350 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7351 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7352 | error = lua_tostring(hlua->T, -1); |
| 7353 | else |
| 7354 | error = "critical error"; |
| 7355 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7356 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7357 | return 0; |
| 7358 | } |
| 7359 | |
| 7360 | /* Check stack available size. */ |
| 7361 | if (!lua_checkstack(hlua->T, 1)) { |
| 7362 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7363 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7364 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7365 | return 0; |
| 7366 | } |
| 7367 | |
| 7368 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7369 | 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] | 7370 | |
| 7371 | /* Create and and push object stream in the stack. */ |
| 7372 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 7373 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7374 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7375 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7376 | return 0; |
| 7377 | } |
| 7378 | hlua->nargs = 1; |
| 7379 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7380 | /* push keywords in the stack. */ |
| 7381 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 7382 | if (!lua_checkstack(hlua->T, 1)) { |
| 7383 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7384 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7385 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7386 | return 0; |
| 7387 | } |
| 7388 | lua_pushstring(hlua->T, *arg); |
| 7389 | hlua->nargs++; |
| 7390 | } |
| 7391 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7392 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7393 | |
| 7394 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7395 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7396 | |
| 7397 | return 1; |
| 7398 | } |
| 7399 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7400 | void hlua_applet_http_fct(struct appctx *ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7401 | { |
| 7402 | struct stream_interface *si = ctx->owner; |
| 7403 | struct stream *strm = si_strm(si); |
| 7404 | struct channel *req = si_oc(si); |
| 7405 | struct channel *res = si_ic(si); |
| 7406 | struct act_rule *rule = ctx->rule; |
| 7407 | struct proxy *px = strm->be; |
| 7408 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
| 7409 | struct htx *req_htx, *res_htx; |
| 7410 | |
| 7411 | res_htx = htx_from_buf(&res->buf); |
| 7412 | |
| 7413 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7414 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7415 | goto out; |
| 7416 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7417 | /* Check if the input buffer is available. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7418 | if (!b_size(&res->buf)) { |
| 7419 | si_rx_room_blk(si); |
| 7420 | goto out; |
| 7421 | } |
| 7422 | /* check that the output is not closed */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7423 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7424 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7425 | |
| 7426 | /* Set the currently running flag. */ |
| 7427 | if (!HLUA_IS_RUNNING(hlua) && |
| 7428 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
Christopher Faulet | bd878d2 | 2021-04-28 10:50:21 +0200 | [diff] [blame] | 7429 | if (!co_data(req)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7430 | si_cant_get(si); |
| 7431 | goto out; |
| 7432 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7433 | } |
| 7434 | |
| 7435 | /* Executes The applet if it is not done. */ |
| 7436 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7437 | |
| 7438 | /* Execute the function. */ |
| 7439 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7440 | /* finished. */ |
| 7441 | case HLUA_E_OK: |
| 7442 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7443 | break; |
| 7444 | |
| 7445 | /* yield. */ |
| 7446 | case HLUA_E_AGAIN: |
| 7447 | if (hlua->wake_time != TICK_ETERNITY) |
| 7448 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7449 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7450 | |
| 7451 | /* finished with error. */ |
| 7452 | case HLUA_E_ERRMSG: |
| 7453 | /* Display log. */ |
| 7454 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7455 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7456 | lua_pop(hlua->T, 1); |
| 7457 | goto error; |
| 7458 | |
| 7459 | case HLUA_E_ETMOUT: |
| 7460 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7461 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7462 | goto error; |
| 7463 | |
| 7464 | case HLUA_E_NOMEM: |
| 7465 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7466 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7467 | goto error; |
| 7468 | |
| 7469 | case HLUA_E_YIELD: /* unexpected */ |
| 7470 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7471 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7472 | goto error; |
| 7473 | |
| 7474 | case HLUA_E_ERR: |
| 7475 | /* Display log. */ |
| 7476 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7477 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7478 | goto error; |
| 7479 | |
| 7480 | default: |
| 7481 | goto error; |
| 7482 | } |
| 7483 | } |
| 7484 | |
| 7485 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7486 | if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT) |
| 7487 | goto done; |
| 7488 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7489 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) |
| 7490 | goto error; |
| 7491 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 7492 | /* no more data are expected. Don't add TLR because mux-h1 will take care of it */ |
| 7493 | res_htx->flags |= HTX_FL_EOM; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7494 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 7495 | ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7496 | } |
| 7497 | |
| 7498 | done: |
| 7499 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7500 | if (!(res->flags & CF_SHUTR)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7501 | res->flags |= CF_READ_NULL; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7502 | si_shutr(si); |
| 7503 | } |
| 7504 | |
| 7505 | /* eat the whole request */ |
| 7506 | if (co_data(req)) { |
| 7507 | req_htx = htx_from_buf(&req->buf); |
| 7508 | co_htx_skip(req, req_htx, co_data(req)); |
| 7509 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7510 | } |
| 7511 | } |
| 7512 | |
| 7513 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7514 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7515 | return; |
| 7516 | |
| 7517 | error: |
| 7518 | |
| 7519 | /* If we are in HTTP mode, and we are not send any |
| 7520 | * data, return a 500 server error in best effort: |
| 7521 | * if there is no room available in the buffer, |
| 7522 | * just close the connection. |
| 7523 | */ |
| 7524 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 7525 | struct buffer *err = &http_err_chunks[HTTP_ERR_500]; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7526 | |
| 7527 | channel_erase(res); |
| 7528 | res->buf.data = b_data(err); |
| 7529 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 7530 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 7531 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7532 | } |
| 7533 | if (!(strm->flags & SF_ERR_MASK)) |
| 7534 | strm->flags |= SF_ERR_RESOURCE; |
| 7535 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7536 | goto done; |
| 7537 | } |
| 7538 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7539 | static void hlua_applet_http_release(struct appctx *ctx) |
| 7540 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7541 | task_destroy(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7542 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7543 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7544 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7545 | } |
| 7546 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7547 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7548 | * success case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7549 | * |
| 7550 | * This function can fail with an abort() due to an Lua critical error. |
| 7551 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7552 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7553 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7554 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 7555 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7556 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7557 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7558 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7559 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7560 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7561 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7562 | if (!rule->arg.hlua_rule) { |
| 7563 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7564 | goto error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7565 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7566 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7567 | /* Memory for arguments. */ |
Tim Duesterhus | e52b6e5 | 2020-09-12 20:26:43 +0200 | [diff] [blame] | 7568 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, |
| 7569 | sizeof(*rule->arg.hlua_rule->args)); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7570 | if (!rule->arg.hlua_rule->args) { |
| 7571 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7572 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7573 | } |
| 7574 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7575 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7576 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7577 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7578 | /* Expect some arguments */ |
| 7579 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7580 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7581 | memprintf(err, "expect %d arguments", fcn->nargs); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7582 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7583 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7584 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7585 | if (!rule->arg.hlua_rule->args[i]) { |
| 7586 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7587 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7588 | } |
| 7589 | (*cur_arg)++; |
| 7590 | } |
| 7591 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7592 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 7593 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7594 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7595 | return ACT_RET_PRS_OK; |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7596 | |
| 7597 | error: |
| 7598 | if (rule->arg.hlua_rule) { |
| 7599 | if (rule->arg.hlua_rule->args) { |
| 7600 | for (i = 0; i < fcn->nargs; i++) |
| 7601 | ha_free(&rule->arg.hlua_rule->args[i]); |
| 7602 | ha_free(&rule->arg.hlua_rule->args); |
| 7603 | } |
| 7604 | ha_free(&rule->arg.hlua_rule); |
| 7605 | } |
| 7606 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7607 | } |
| 7608 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7609 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 7610 | struct act_rule *rule, char **err) |
| 7611 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7612 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7613 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7614 | /* HTTP applets are forbidden in tcp-request rules. |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7615 | * HTTP applet request requires everything initialized by |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7616 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7617 | * The applet will be immediately initialized, but its before |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7618 | * the call of this analyzer. |
| 7619 | */ |
| 7620 | if (rule->from != ACT_F_HTTP_REQ) { |
| 7621 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 7622 | return ACT_RET_PRS_ERR; |
| 7623 | } |
| 7624 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7625 | /* Memory for the rule. */ |
| 7626 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7627 | if (!rule->arg.hlua_rule) { |
| 7628 | memprintf(err, "out of memory error"); |
| 7629 | return ACT_RET_PRS_ERR; |
| 7630 | } |
| 7631 | |
| 7632 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7633 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7634 | |
| 7635 | /* TODO: later accept arguments. */ |
| 7636 | rule->arg.hlua_rule->args = NULL; |
| 7637 | |
| 7638 | /* Add applet pointer in the rule. */ |
| 7639 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7640 | rule->applet.name = fcn->name; |
| 7641 | rule->applet.init = hlua_applet_http_init; |
| 7642 | rule->applet.fct = hlua_applet_http_fct; |
| 7643 | rule->applet.release = hlua_applet_http_release; |
| 7644 | rule->applet.timeout = hlua_timeout_applet; |
| 7645 | |
| 7646 | return ACT_RET_PRS_OK; |
| 7647 | } |
| 7648 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7649 | /* This function is an LUA binding used for registering |
| 7650 | * "sample-conv" functions. It expects a converter name used |
| 7651 | * in the haproxy configuration file, and an LUA function. |
| 7652 | */ |
| 7653 | __LJMP static int hlua_register_action(lua_State *L) |
| 7654 | { |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7655 | struct action_kw_list *akl = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7656 | const char *name; |
| 7657 | int ref; |
| 7658 | int len; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7659 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7660 | int nargs; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7661 | struct buffer *trash; |
| 7662 | struct action_kw *akw; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7663 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7664 | /* Initialise the number of expected arguments at 0. */ |
| 7665 | nargs = 0; |
| 7666 | |
| 7667 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 7668 | 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] | 7669 | |
| 7670 | /* First argument : converter name. */ |
| 7671 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7672 | |
| 7673 | /* Second argument : environment. */ |
| 7674 | if (lua_type(L, 2) != LUA_TTABLE) |
| 7675 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7676 | |
| 7677 | /* Third argument : lua function. */ |
| 7678 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7679 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7680 | /* 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] | 7681 | if (lua_gettop(L) >= 4) |
| 7682 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 7683 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7684 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7685 | lua_pushnil(L); |
| 7686 | while (lua_next(L, 2) != 0) { |
| 7687 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7688 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7689 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7690 | /* Check if action exists */ |
| 7691 | trash = get_trash_chunk(); |
| 7692 | chunk_printf(trash, "lua.%s", name); |
| 7693 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) { |
| 7694 | akw = tcp_req_cont_action(trash->area); |
| 7695 | } else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) { |
| 7696 | akw = tcp_res_cont_action(trash->area); |
| 7697 | } else if (strcmp(lua_tostring(L, -1), "http-req") == 0) { |
| 7698 | akw = action_http_req_custom(trash->area); |
| 7699 | } else if (strcmp(lua_tostring(L, -1), "http-res") == 0) { |
| 7700 | akw = action_http_res_custom(trash->area); |
| 7701 | } else { |
| 7702 | akw = NULL; |
| 7703 | } |
| 7704 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 7705 | fcn = akw->private; |
| 7706 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 7707 | ha_warning("Trying to register action 'lua.%s' more than once. " |
| 7708 | "This will become a hard error in version 2.5.\n", name); |
| 7709 | } |
| 7710 | fcn->function_ref[hlua_state_id] = ref; |
| 7711 | |
| 7712 | /* pop the environment string. */ |
| 7713 | lua_pop(L, 1); |
| 7714 | continue; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7715 | } |
| 7716 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7717 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 7718 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7719 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7720 | if (!akl) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7721 | goto alloc_error;; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 7722 | fcn = new_hlua_function(); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7723 | if (!fcn) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7724 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7725 | |
| 7726 | /* Fill fcn. */ |
| 7727 | fcn->name = strdup(name); |
| 7728 | if (!fcn->name) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7729 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7730 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7731 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 7732 | /* Set the expected number of arguments. */ |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7733 | fcn->nargs = nargs; |
| 7734 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7735 | /* List head */ |
| 7736 | akl->list.n = akl->list.p = NULL; |
| 7737 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7738 | /* action keyword. */ |
| 7739 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7740 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7741 | if (!akl->kw[0].kw) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7742 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7743 | |
| 7744 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7745 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 7746 | akl->kw[0].flags = 0; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7747 | akl->kw[0].private = fcn; |
| 7748 | akl->kw[0].parse = action_register_lua; |
| 7749 | |
| 7750 | /* select the action registering point. */ |
| 7751 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 7752 | tcp_req_cont_keywords_register(akl); |
| 7753 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 7754 | tcp_res_cont_keywords_register(akl); |
| 7755 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 7756 | http_req_keywords_register(akl); |
| 7757 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 7758 | http_res_keywords_register(akl); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7759 | else { |
| 7760 | release_hlua_function(fcn); |
| 7761 | if (akl) |
| 7762 | ha_free((char **)&(akl->kw[0].kw)); |
| 7763 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7764 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7765 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 7766 | "are expected.", lua_tostring(L, -1))); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7767 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7768 | |
| 7769 | /* pop the environment string. */ |
| 7770 | lua_pop(L, 1); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7771 | |
| 7772 | /* reset for next loop */ |
| 7773 | akl = NULL; |
| 7774 | fcn = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7775 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7776 | return ACT_RET_PRS_OK; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7777 | |
| 7778 | alloc_error: |
| 7779 | release_hlua_function(fcn); |
| 7780 | ha_free(&akl); |
| 7781 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 7782 | return 0; /* Never reached */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7783 | } |
| 7784 | |
| 7785 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 7786 | struct act_rule *rule, char **err) |
| 7787 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7788 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7789 | |
Christopher Faulet | 280f85b | 2019-07-15 15:02:04 +0200 | [diff] [blame] | 7790 | if (px->mode == PR_MODE_HTTP) { |
| 7791 | memprintf(err, "Lua TCP services cannot be used on HTTP proxies"); |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 7792 | return ACT_RET_PRS_ERR; |
| 7793 | } |
| 7794 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7795 | /* Memory for the rule. */ |
| 7796 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7797 | if (!rule->arg.hlua_rule) { |
| 7798 | memprintf(err, "out of memory error"); |
| 7799 | return ACT_RET_PRS_ERR; |
| 7800 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7801 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7802 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7803 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7804 | |
| 7805 | /* TODO: later accept arguments. */ |
| 7806 | rule->arg.hlua_rule->args = NULL; |
| 7807 | |
| 7808 | /* Add applet pointer in the rule. */ |
| 7809 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7810 | rule->applet.name = fcn->name; |
| 7811 | rule->applet.init = hlua_applet_tcp_init; |
| 7812 | rule->applet.fct = hlua_applet_tcp_fct; |
| 7813 | rule->applet.release = hlua_applet_tcp_release; |
| 7814 | rule->applet.timeout = hlua_timeout_applet; |
| 7815 | |
| 7816 | return 0; |
| 7817 | } |
| 7818 | |
| 7819 | /* This function is an LUA binding used for registering |
| 7820 | * "sample-conv" functions. It expects a converter name used |
| 7821 | * in the haproxy configuration file, and an LUA function. |
| 7822 | */ |
| 7823 | __LJMP static int hlua_register_service(lua_State *L) |
| 7824 | { |
| 7825 | struct action_kw_list *akl; |
| 7826 | const char *name; |
| 7827 | const char *env; |
| 7828 | int ref; |
| 7829 | int len; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 7830 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7831 | struct buffer *trash; |
| 7832 | struct action_kw *akw; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7833 | |
| 7834 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 7835 | |
| 7836 | /* First argument : converter name. */ |
| 7837 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7838 | |
| 7839 | /* Second argument : environment. */ |
| 7840 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7841 | |
| 7842 | /* Third argument : lua function. */ |
| 7843 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7844 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7845 | /* Check for service already registered */ |
| 7846 | trash = get_trash_chunk(); |
| 7847 | chunk_printf(trash, "lua.%s", name); |
| 7848 | akw = service_find(trash->area); |
| 7849 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 7850 | fcn = akw->private; |
| 7851 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 7852 | ha_warning("Trying to register service 'lua.%s' more than once. " |
| 7853 | "This will become a hard error in version 2.5.\n", name); |
| 7854 | } |
| 7855 | fcn->function_ref[hlua_state_id] = ref; |
| 7856 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7857 | } |
| 7858 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7859 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7860 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 7861 | if (!akl) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 7862 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 7863 | fcn = new_hlua_function(); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7864 | if (!fcn) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 7865 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7866 | |
| 7867 | /* Fill fcn. */ |
| 7868 | len = strlen("<lua.>") + strlen(name) + 1; |
| 7869 | fcn->name = calloc(1, len); |
| 7870 | if (!fcn->name) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 7871 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7872 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7873 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7874 | |
| 7875 | /* List head */ |
| 7876 | akl->list.n = akl->list.p = NULL; |
| 7877 | |
| 7878 | /* converter keyword. */ |
| 7879 | len = strlen("lua.") + strlen(name) + 1; |
| 7880 | akl->kw[0].kw = calloc(1, len); |
| 7881 | if (!akl->kw[0].kw) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 7882 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7883 | |
| 7884 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7885 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 7886 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7887 | if (strcmp(env, "tcp") == 0) |
| 7888 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7889 | else if (strcmp(env, "http") == 0) |
| 7890 | akl->kw[0].parse = action_register_service_http; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 7891 | else { |
| 7892 | release_hlua_function(fcn); |
| 7893 | if (akl) |
| 7894 | ha_free((char **)&(akl->kw[0].kw)); |
| 7895 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7896 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 7897 | "'tcp' or 'http' are expected.", env)); |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 7898 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7899 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 7900 | akl->kw[0].flags = 0; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7901 | akl->kw[0].private = fcn; |
| 7902 | |
| 7903 | /* End of array. */ |
| 7904 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 7905 | |
| 7906 | /* Register this new converter */ |
| 7907 | service_keywords_register(akl); |
| 7908 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7909 | return 0; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 7910 | |
| 7911 | alloc_error: |
| 7912 | release_hlua_function(fcn); |
| 7913 | ha_free(&akl); |
| 7914 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 7915 | return 0; /* Never reached */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7916 | } |
| 7917 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7918 | /* This function initialises Lua cli handler. It copies the |
| 7919 | * arguments in the Lua stack and create channel IO objects. |
| 7920 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 7921 | 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] | 7922 | { |
| 7923 | struct hlua *hlua; |
| 7924 | struct hlua_function *fcn; |
| 7925 | int i; |
| 7926 | const char *error; |
| 7927 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7928 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7929 | appctx->ctx.hlua_cli.fcn = private; |
| 7930 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7931 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7932 | if (!hlua) { |
| 7933 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7934 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7935 | } |
| 7936 | HLUA_INIT(hlua); |
| 7937 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7938 | |
| 7939 | /* Create task used by signal to wakeup applets. |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 7940 | * 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] | 7941 | * applet_http. It is absolutely compatible. |
| 7942 | */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7943 | appctx->ctx.hlua_cli.task = task_new(tid_bit); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7944 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 7945 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7946 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7947 | } |
| 7948 | appctx->ctx.hlua_cli.task->nice = 0; |
| 7949 | appctx->ctx.hlua_cli.task->context = appctx; |
| 7950 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 7951 | |
| 7952 | /* Initialises the Lua context */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7953 | 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] | 7954 | 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] | 7955 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7956 | } |
| 7957 | |
| 7958 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7959 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7960 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7961 | error = lua_tostring(hlua->T, -1); |
| 7962 | else |
| 7963 | error = "critical error"; |
| 7964 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 7965 | goto error; |
| 7966 | } |
| 7967 | |
| 7968 | /* Check stack available size. */ |
| 7969 | if (!lua_checkstack(hlua->T, 2)) { |
| 7970 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7971 | goto error; |
| 7972 | } |
| 7973 | |
| 7974 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7975 | 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] | 7976 | |
| 7977 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 7978 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7979 | */ |
| 7980 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 7981 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7982 | goto error; |
| 7983 | } |
| 7984 | hlua->nargs = 1; |
| 7985 | |
| 7986 | /* push keywords in the stack. */ |
| 7987 | for (i = 0; *args[i]; i++) { |
| 7988 | /* Check stack available size. */ |
| 7989 | if (!lua_checkstack(hlua->T, 1)) { |
| 7990 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7991 | goto error; |
| 7992 | } |
| 7993 | lua_pushstring(hlua->T, args[i]); |
| 7994 | hlua->nargs++; |
| 7995 | } |
| 7996 | |
| 7997 | /* We must initialize the execution timeouts. */ |
| 7998 | hlua->max_time = hlua_timeout_session; |
| 7999 | |
| 8000 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8001 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8002 | |
| 8003 | /* It's ok */ |
| 8004 | return 0; |
| 8005 | |
| 8006 | /* It's not ok. */ |
| 8007 | error: |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8008 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8009 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 8010 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8011 | return 1; |
| 8012 | } |
| 8013 | |
| 8014 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 8015 | { |
| 8016 | struct hlua *hlua; |
| 8017 | struct stream_interface *si; |
| 8018 | struct hlua_function *fcn; |
| 8019 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8020 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8021 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 8022 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8023 | |
| 8024 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 8025 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 8026 | return 1; |
| 8027 | |
| 8028 | /* Execute the function. */ |
| 8029 | switch (hlua_ctx_resume(hlua, 1)) { |
| 8030 | |
| 8031 | /* finished. */ |
| 8032 | case HLUA_E_OK: |
| 8033 | return 1; |
| 8034 | |
| 8035 | /* yield. */ |
| 8036 | case HLUA_E_AGAIN: |
| 8037 | /* We want write. */ |
| 8038 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 8039 | si_rx_room_blk(si); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8040 | /* Set the timeout. */ |
| 8041 | if (hlua->wake_time != TICK_ETERNITY) |
| 8042 | task_schedule(hlua->task, hlua->wake_time); |
| 8043 | return 0; |
| 8044 | |
| 8045 | /* finished with error. */ |
| 8046 | case HLUA_E_ERRMSG: |
| 8047 | /* Display log. */ |
| 8048 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 8049 | fcn->name, lua_tostring(hlua->T, -1)); |
| 8050 | lua_pop(hlua->T, 1); |
| 8051 | return 1; |
| 8052 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8053 | case HLUA_E_ETMOUT: |
| 8054 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 8055 | fcn->name); |
| 8056 | return 1; |
| 8057 | |
| 8058 | case HLUA_E_NOMEM: |
| 8059 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 8060 | fcn->name); |
| 8061 | return 1; |
| 8062 | |
| 8063 | case HLUA_E_YIELD: /* unexpected */ |
| 8064 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 8065 | fcn->name); |
| 8066 | return 1; |
| 8067 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8068 | case HLUA_E_ERR: |
| 8069 | /* Display log. */ |
| 8070 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 8071 | fcn->name); |
| 8072 | return 1; |
| 8073 | |
| 8074 | default: |
| 8075 | return 1; |
| 8076 | } |
| 8077 | |
| 8078 | return 1; |
| 8079 | } |
| 8080 | |
| 8081 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 8082 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8083 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8084 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8085 | } |
| 8086 | |
| 8087 | /* This function is an LUA binding used for registering |
| 8088 | * new keywords in the cli. It expects a list of keywords |
| 8089 | * which are the "path". It is limited to 5 keywords. A |
| 8090 | * description of the command, a function to be executed |
| 8091 | * for the parsing and a function for io handlers. |
| 8092 | */ |
| 8093 | __LJMP static int hlua_register_cli(lua_State *L) |
| 8094 | { |
| 8095 | struct cli_kw_list *cli_kws; |
| 8096 | const char *message; |
| 8097 | int ref_io; |
| 8098 | int len; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8099 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8100 | int index; |
| 8101 | int i; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8102 | struct buffer *trash; |
| 8103 | const char *kw[5]; |
| 8104 | struct cli_kw *cli_kw; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8105 | const char *errmsg; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8106 | |
| 8107 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 8108 | |
| 8109 | /* First argument : an array of maximum 5 keywords. */ |
| 8110 | if (!lua_istable(L, 1)) |
| 8111 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 8112 | |
| 8113 | /* Second argument : string with contextual message. */ |
| 8114 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8115 | |
| 8116 | /* Third and fourth argument : lua function. */ |
| 8117 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 8118 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8119 | /* Check for CLI service already registered */ |
| 8120 | trash = get_trash_chunk(); |
| 8121 | index = 0; |
| 8122 | lua_pushnil(L); |
| 8123 | memset(kw, 0, sizeof(kw)); |
| 8124 | while (lua_next(L, 1) != 0) { |
| 8125 | if (index >= CLI_PREFIX_KW_NB) |
| 8126 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 8127 | if (lua_type(L, -1) != LUA_TSTRING) |
| 8128 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 8129 | kw[index] = lua_tostring(L, -1); |
| 8130 | if (index == 0) |
| 8131 | chunk_printf(trash, "%s", kw[index]); |
| 8132 | else |
| 8133 | chunk_appendf(trash, " %s", kw[index]); |
| 8134 | index++; |
| 8135 | lua_pop(L, 1); |
| 8136 | } |
| 8137 | cli_kw = cli_find_kw_exact((char **)kw); |
| 8138 | if (cli_kw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8139 | fcn = cli_kw->private; |
| 8140 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 8141 | ha_warning("Trying to register CLI keyword 'lua.%s' more than once. " |
| 8142 | "This will become a hard error in version 2.5.\n", trash->area); |
| 8143 | } |
| 8144 | fcn->function_ref[hlua_state_id] = ref_io; |
| 8145 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8146 | } |
| 8147 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8148 | /* Allocate and fill the sample fetch keyword struct. */ |
| 8149 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8150 | if (!cli_kws) { |
| 8151 | errmsg = "Lua out of memory error."; |
| 8152 | goto error; |
| 8153 | } |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 8154 | fcn = new_hlua_function(); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8155 | if (!fcn) { |
| 8156 | errmsg = "Lua out of memory error."; |
| 8157 | goto error; |
| 8158 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8159 | |
| 8160 | /* Fill path. */ |
| 8161 | index = 0; |
| 8162 | lua_pushnil(L); |
| 8163 | while(lua_next(L, 1) != 0) { |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8164 | if (index >= 5) { |
| 8165 | errmsg = "1st argument must be a table with a maximum of 5 entries"; |
| 8166 | goto error; |
| 8167 | } |
| 8168 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 8169 | errmsg = "1st argument must be a table filled with strings"; |
| 8170 | goto error; |
| 8171 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8172 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8173 | if (!cli_kws->kw[0].str_kw[index]) { |
| 8174 | errmsg = "Lua out of memory error."; |
| 8175 | goto error; |
| 8176 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8177 | index++; |
| 8178 | lua_pop(L, 1); |
| 8179 | } |
| 8180 | |
| 8181 | /* Copy help message. */ |
| 8182 | cli_kws->kw[0].usage = strdup(message); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8183 | if (!cli_kws->kw[0].usage) { |
| 8184 | errmsg = "Lua out of memory error."; |
| 8185 | goto error; |
| 8186 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8187 | |
| 8188 | /* Fill fcn io handler. */ |
| 8189 | len = strlen("<lua.cli>") + 1; |
| 8190 | for (i = 0; i < index; i++) |
| 8191 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 8192 | fcn->name = calloc(1, len); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8193 | if (!fcn->name) { |
| 8194 | errmsg = "Lua out of memory error."; |
| 8195 | goto error; |
| 8196 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8197 | strncat((char *)fcn->name, "<lua.cli", len); |
| 8198 | for (i = 0; i < index; i++) { |
| 8199 | strncat((char *)fcn->name, ".", len); |
| 8200 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 8201 | } |
| 8202 | strncat((char *)fcn->name, ">", len); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8203 | fcn->function_ref[hlua_state_id] = ref_io; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8204 | |
| 8205 | /* Fill last entries. */ |
| 8206 | cli_kws->kw[0].private = fcn; |
| 8207 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 8208 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 8209 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 8210 | |
| 8211 | /* Register this new converter */ |
| 8212 | cli_register_kw(cli_kws); |
| 8213 | |
| 8214 | return 0; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8215 | |
| 8216 | error: |
| 8217 | release_hlua_function(fcn); |
| 8218 | if (cli_kws) { |
| 8219 | for (i = 0; i < index; i++) |
| 8220 | ha_free((char **)&(cli_kws->kw[0].str_kw[i])); |
| 8221 | ha_free((char **)&(cli_kws->kw[0].usage)); |
| 8222 | } |
| 8223 | ha_free(&cli_kws); |
| 8224 | WILL_LJMP(luaL_error(L, errmsg)); |
| 8225 | return 0; /* Never reached */ |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8226 | } |
| 8227 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8228 | static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 8229 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8230 | char **err, unsigned int *timeout) |
| 8231 | { |
| 8232 | const char *error; |
| 8233 | |
| 8234 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 8235 | if (error == PARSE_TIME_OVER) { |
| 8236 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 8237 | args[1], args[0]); |
| 8238 | return -1; |
| 8239 | } |
| 8240 | else if (error == PARSE_TIME_UNDER) { |
| 8241 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 8242 | args[1], args[0]); |
| 8243 | return -1; |
| 8244 | } |
| 8245 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8246 | memprintf(err, "%s: invalid timeout", args[0]); |
| 8247 | return -1; |
| 8248 | } |
| 8249 | return 0; |
| 8250 | } |
| 8251 | |
| 8252 | static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 8253 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8254 | char **err) |
| 8255 | { |
| 8256 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8257 | file, line, err, &hlua_timeout_session); |
| 8258 | } |
| 8259 | |
| 8260 | static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 8261 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8262 | char **err) |
| 8263 | { |
| 8264 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8265 | file, line, err, &hlua_timeout_task); |
| 8266 | } |
| 8267 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8268 | static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 8269 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8270 | char **err) |
| 8271 | { |
| 8272 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8273 | file, line, err, &hlua_timeout_applet); |
| 8274 | } |
| 8275 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8276 | static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 8277 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8278 | char **err) |
| 8279 | { |
| 8280 | char *error; |
| 8281 | |
| 8282 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 8283 | if (*error != '\0') { |
| 8284 | memprintf(err, "%s: invalid number", args[0]); |
| 8285 | return -1; |
| 8286 | } |
| 8287 | return 0; |
| 8288 | } |
| 8289 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8290 | static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 8291 | const struct proxy *defpx, const char *file, int line, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8292 | char **err) |
| 8293 | { |
| 8294 | char *error; |
| 8295 | |
| 8296 | if (*(args[1]) == 0) { |
| 8297 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 8298 | return -1; |
| 8299 | } |
| 8300 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 8301 | if (*error != '\0') { |
| 8302 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 8303 | return -1; |
| 8304 | } |
| 8305 | return 0; |
| 8306 | } |
| 8307 | |
| 8308 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8309 | /* This function is called by the main configuration key "lua-load". It loads and |
| 8310 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 8311 | * the main lua entry point. |
| 8312 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8313 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 8314 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8315 | * |
| 8316 | * In some error case, LUA set an error message in top of the stack. This function |
| 8317 | * 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] | 8318 | * |
| 8319 | * This function can fail with an abort() due to an Lua critical error. |
| 8320 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 8321 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8322 | */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8323 | static int hlua_load_state(char *filename, lua_State *L, char **err) |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8324 | { |
| 8325 | int error; |
| 8326 | |
| 8327 | /* Just load and compile the file. */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8328 | error = luaL_loadfile(L, filename); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8329 | if (error) { |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8330 | memprintf(err, "error in Lua file '%s': %s", filename, lua_tostring(L, -1)); |
| 8331 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8332 | return -1; |
| 8333 | } |
| 8334 | |
| 8335 | /* If no syntax error where detected, execute the code. */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8336 | error = lua_pcall(L, 0, LUA_MULTRET, 0); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8337 | switch (error) { |
| 8338 | case LUA_OK: |
| 8339 | break; |
| 8340 | case LUA_ERRRUN: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8341 | memprintf(err, "Lua runtime error: %s\n", lua_tostring(L, -1)); |
| 8342 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8343 | return -1; |
| 8344 | case LUA_ERRMEM: |
Thierry Fournier | de6145f | 2020-11-29 00:55:53 +0100 | [diff] [blame] | 8345 | memprintf(err, "Lua out of memory error\n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8346 | return -1; |
| 8347 | case LUA_ERRERR: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8348 | memprintf(err, "Lua message handler error: %s\n", lua_tostring(L, -1)); |
| 8349 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8350 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 8351 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8352 | case LUA_ERRGCMM: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8353 | memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(L, -1)); |
| 8354 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8355 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 8356 | #endif |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8357 | default: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8358 | memprintf(err, "Lua unknown error: %s\n", lua_tostring(L, -1)); |
| 8359 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8360 | return -1; |
| 8361 | } |
| 8362 | |
| 8363 | return 0; |
| 8364 | } |
| 8365 | |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8366 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 8367 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8368 | char **err) |
| 8369 | { |
| 8370 | if (*(args[1]) == 0) { |
| 8371 | memprintf(err, "'%s' expects a file name as parameter.\n", args[0]); |
| 8372 | return -1; |
| 8373 | } |
| 8374 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8375 | /* loading for global state */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8376 | hlua_state_id = 0; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8377 | ha_set_tid(0); |
Thierry Fournier | afc63e2 | 2020-11-28 17:06:51 +0100 | [diff] [blame] | 8378 | return hlua_load_state(args[1], hlua_states[0], err); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8379 | } |
| 8380 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8381 | static int hlua_load_per_thread(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 8382 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8383 | char **err) |
| 8384 | { |
| 8385 | int len; |
| 8386 | |
| 8387 | if (*(args[1]) == 0) { |
| 8388 | memprintf(err, "'%s' expects a file as parameter.\n", args[0]); |
| 8389 | return -1; |
| 8390 | } |
| 8391 | |
| 8392 | if (per_thread_load == NULL) { |
| 8393 | /* allocate the first entry large enough to store the final NULL */ |
| 8394 | per_thread_load = calloc(1, sizeof(*per_thread_load)); |
| 8395 | if (per_thread_load == NULL) { |
| 8396 | memprintf(err, "out of memory error"); |
| 8397 | return -1; |
| 8398 | } |
| 8399 | } |
| 8400 | |
| 8401 | /* count used entries */ |
| 8402 | for (len = 0; per_thread_load[len] != NULL; len++) |
| 8403 | ; |
| 8404 | |
| 8405 | per_thread_load = realloc(per_thread_load, (len + 2) * sizeof(*per_thread_load)); |
| 8406 | if (per_thread_load == NULL) { |
| 8407 | memprintf(err, "out of memory error"); |
| 8408 | return -1; |
| 8409 | } |
| 8410 | |
| 8411 | per_thread_load[len] = strdup(args[1]); |
| 8412 | per_thread_load[len + 1] = NULL; |
| 8413 | |
| 8414 | if (per_thread_load[len] == NULL) { |
| 8415 | memprintf(err, "out of memory error"); |
| 8416 | return -1; |
| 8417 | } |
| 8418 | |
| 8419 | /* loading for thread 1 only */ |
| 8420 | hlua_state_id = 1; |
| 8421 | ha_set_tid(0); |
| 8422 | return hlua_load_state(args[1], hlua_states[1], err); |
| 8423 | } |
| 8424 | |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 8425 | /* Prepend the given <path> followed by a semicolon to the `package.<type>` variable |
| 8426 | * in the given <ctx>. |
| 8427 | */ |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 8428 | static int hlua_prepend_path(lua_State *L, char *type, char *path) |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 8429 | { |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 8430 | lua_getglobal(L, "package"); /* push package variable */ |
| 8431 | lua_pushstring(L, path); /* push given path */ |
| 8432 | lua_pushstring(L, ";"); /* push semicolon */ |
| 8433 | lua_getfield(L, -3, type); /* push old path */ |
| 8434 | lua_concat(L, 3); /* concatenate to new path */ |
| 8435 | lua_setfield(L, -2, type); /* store new path */ |
| 8436 | lua_pop(L, 1); /* pop package variable */ |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 8437 | |
| 8438 | return 0; |
| 8439 | } |
| 8440 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8441 | static int hlua_config_prepend_path(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 8442 | const struct proxy *defpx, const char *file, int line, |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8443 | char **err) |
| 8444 | { |
| 8445 | char *path; |
| 8446 | char *type = "path"; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8447 | struct prepend_path *p = NULL; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8448 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8449 | if (too_many_args(2, args, err, NULL)) { |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8450 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8451 | } |
| 8452 | |
| 8453 | if (!(*args[1])) { |
| 8454 | memprintf(err, "'%s' expects to receive a <path> as argument", args[0]); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8455 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8456 | } |
| 8457 | path = args[1]; |
| 8458 | |
| 8459 | if (*args[2]) { |
| 8460 | if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) { |
| 8461 | 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] | 8462 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8463 | } |
| 8464 | type = args[2]; |
| 8465 | } |
| 8466 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8467 | p = calloc(1, sizeof(*p)); |
| 8468 | if (p == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 8469 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8470 | goto err; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8471 | } |
| 8472 | p->path = strdup(path); |
| 8473 | if (p->path == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 8474 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8475 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8476 | } |
| 8477 | p->type = strdup(type); |
| 8478 | if (p->type == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 8479 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8480 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8481 | } |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 8482 | LIST_APPEND(&prepend_path_list, &p->l); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8483 | |
| 8484 | hlua_prepend_path(hlua_states[0], type, path); |
| 8485 | hlua_prepend_path(hlua_states[1], type, path); |
| 8486 | return 0; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8487 | |
| 8488 | err2: |
| 8489 | free(p->type); |
| 8490 | free(p->path); |
| 8491 | err: |
| 8492 | free(p); |
| 8493 | return -1; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8494 | } |
| 8495 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8496 | /* configuration keywords declaration */ |
| 8497 | static struct cfg_kw_list cfg_kws = {{ },{ |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8498 | { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8499 | { CFG_GLOBAL, "lua-load", hlua_load }, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8500 | { CFG_GLOBAL, "lua-load-per-thread", hlua_load_per_thread }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8501 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 8502 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 8503 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8504 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8505 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8506 | { 0, NULL, NULL }, |
| 8507 | }}; |
| 8508 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 8509 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 8510 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 8511 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8512 | /* This function can fail with an abort() due to an Lua critical error. |
| 8513 | * We are in the initialisation process of HAProxy, this abort() is |
| 8514 | * tolerated. |
| 8515 | */ |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8516 | int hlua_post_init_state(lua_State *L) |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8517 | { |
| 8518 | struct hlua_init_function *init; |
| 8519 | const char *msg; |
| 8520 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8521 | const char *error; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8522 | const char *kind; |
| 8523 | const char *trace; |
| 8524 | int return_status = 1; |
| 8525 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 8526 | int nres; |
| 8527 | #endif |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8528 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8529 | /* disable memory limit checks if limit is not set */ |
| 8530 | if (!hlua_global_allocator.limit) |
| 8531 | hlua_global_allocator.limit = ~hlua_global_allocator.limit; |
| 8532 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 8533 | /* Call post initialisation function in safe environment. */ |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8534 | if (setjmp(safe_ljmp_env) != 0) { |
| 8535 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8536 | if (lua_type(L, -1) == LUA_TSTRING) |
| 8537 | error = lua_tostring(L, -1); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8538 | else |
| 8539 | error = "critical error"; |
| 8540 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 8541 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8542 | } else { |
| 8543 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8544 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 8545 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8546 | hlua_fcn_post_init(L); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8547 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8548 | list_for_each_entry(init, &hlua_init_functions[hlua_state_id], l) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8549 | lua_rawgeti(L, LUA_REGISTRYINDEX, init->function_ref); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8550 | |
| 8551 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8552 | ret = lua_resume(L, L, 0, &nres); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8553 | #else |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8554 | ret = lua_resume(L, L, 0); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8555 | #endif |
| 8556 | kind = NULL; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8557 | switch (ret) { |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8558 | |
| 8559 | case LUA_OK: |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8560 | lua_pop(L, -1); |
Thierry Fournier | 13d08b7 | 2020-11-28 11:02:58 +0100 | [diff] [blame] | 8561 | break; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8562 | |
| 8563 | case LUA_ERRERR: |
| 8564 | kind = "message handler error"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8565 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8566 | case LUA_ERRRUN: |
| 8567 | if (!kind) |
| 8568 | kind = "runtime error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8569 | msg = lua_tostring(L, -1); |
| 8570 | lua_settop(L, 0); /* Empty the stack. */ |
| 8571 | lua_pop(L, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 8572 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8573 | if (msg) |
| 8574 | ha_alert("Lua init: %s: '%s' from %s\n", kind, msg, trace); |
| 8575 | else |
| 8576 | ha_alert("Lua init: unknown %s from %s\n", kind, trace); |
| 8577 | return_status = 0; |
| 8578 | break; |
| 8579 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8580 | default: |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8581 | /* Unknown error */ |
| 8582 | kind = "Unknown error"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8583 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8584 | case LUA_YIELD: |
| 8585 | /* yield is not configured at this step, this state doesn't happen */ |
| 8586 | if (!kind) |
| 8587 | kind = "yield not allowed"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8588 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8589 | case LUA_ERRMEM: |
| 8590 | if (!kind) |
| 8591 | kind = "out of memory error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8592 | lua_settop(L, 0); |
| 8593 | lua_pop(L, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 8594 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8595 | ha_alert("Lua init: %s: %s\n", kind, trace); |
| 8596 | return_status = 0; |
| 8597 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8598 | } |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8599 | if (!return_status) |
| 8600 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8601 | } |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8602 | |
| 8603 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8604 | return return_status; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8605 | } |
| 8606 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8607 | int hlua_post_init() |
| 8608 | { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8609 | int ret; |
| 8610 | int i; |
| 8611 | int errors; |
| 8612 | char *err = NULL; |
| 8613 | struct hlua_function *fcn; |
| 8614 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8615 | #if USE_OPENSSL |
| 8616 | /* Initialize SSL server. */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 8617 | if (socket_ssl->xprt->prepare_srv) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8618 | int saved_used_backed = global.ssl_used_backend; |
| 8619 | // don't affect maxconn automatic computation |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 8620 | socket_ssl->xprt->prepare_srv(socket_ssl); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8621 | global.ssl_used_backend = saved_used_backed; |
| 8622 | } |
| 8623 | #endif |
| 8624 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8625 | /* Perform post init of common thread */ |
| 8626 | hlua_state_id = 0; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8627 | ha_set_tid(0); |
| 8628 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 8629 | if (ret == 0) |
| 8630 | return 0; |
| 8631 | |
| 8632 | /* init remaining lua states and load files */ |
| 8633 | for (hlua_state_id = 2; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 8634 | |
| 8635 | /* set thread context */ |
| 8636 | ha_set_tid(hlua_state_id - 1); |
| 8637 | |
| 8638 | /* Init lua state */ |
| 8639 | hlua_states[hlua_state_id] = hlua_init_state(hlua_state_id); |
| 8640 | |
| 8641 | /* Load lua files */ |
| 8642 | for (i = 0; per_thread_load && per_thread_load[i]; i++) { |
| 8643 | ret = hlua_load_state(per_thread_load[i], hlua_states[hlua_state_id], &err); |
| 8644 | if (ret != 0) { |
| 8645 | ha_alert("Lua init: %s\n", err); |
| 8646 | return 0; |
| 8647 | } |
| 8648 | } |
| 8649 | } |
| 8650 | |
| 8651 | /* Reset thread context */ |
| 8652 | ha_set_tid(0); |
| 8653 | |
| 8654 | /* Execute post init for all states */ |
| 8655 | for (hlua_state_id = 1; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 8656 | |
| 8657 | /* set thread context */ |
| 8658 | ha_set_tid(hlua_state_id - 1); |
| 8659 | |
| 8660 | /* run post init */ |
| 8661 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 8662 | if (ret == 0) |
| 8663 | return 0; |
| 8664 | } |
| 8665 | |
| 8666 | /* Reset thread context */ |
| 8667 | ha_set_tid(0); |
| 8668 | |
| 8669 | /* control functions registering. Each function must have: |
| 8670 | * - only the function_ref[0] set positive and all other to -1 |
| 8671 | * - only the function_ref[0] set to -1 and all other positive |
| 8672 | * This ensure a same reference is not used both in shared |
| 8673 | * lua state and thread dedicated lua state. Note: is the case |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 8674 | * reach, the shared state is priority, but the bug will be |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8675 | * complicated to found for the end user. |
| 8676 | */ |
| 8677 | errors = 0; |
| 8678 | list_for_each_entry(fcn, &referenced_functions, l) { |
| 8679 | ret = 0; |
| 8680 | for (i = 1; i < global.nbthread + 1; i++) { |
| 8681 | if (fcn->function_ref[i] == -1) |
| 8682 | ret--; |
| 8683 | else |
| 8684 | ret++; |
| 8685 | } |
| 8686 | if (abs(ret) != global.nbthread) { |
| 8687 | ha_alert("Lua function '%s' is not referenced in all thread. " |
| 8688 | "Expect function in all thread or in none thread.\n", fcn->name); |
| 8689 | errors++; |
| 8690 | continue; |
| 8691 | } |
| 8692 | |
| 8693 | if ((fcn->function_ref[0] == -1) == (ret < 0)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 8694 | ha_alert("Lua function '%s' is referenced both ins shared Lua context (through lua-load) " |
| 8695 | "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] | 8696 | "exclusive.\n", fcn->name); |
| 8697 | errors++; |
| 8698 | } |
| 8699 | } |
| 8700 | |
| 8701 | if (errors > 0) |
| 8702 | return 0; |
| 8703 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 8704 | /* 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] | 8705 | * -1 in order to have probably a segfault if someone use it |
| 8706 | */ |
| 8707 | hlua_state_id = -1; |
| 8708 | |
| 8709 | return 1; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8710 | } |
| 8711 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8712 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 8713 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 8714 | * 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] | 8715 | * allocation. <nsize> is the requested new size. A new allocation is |
| 8716 | * indicated by <ptr> being NULL. A free is indicated by <nsize> being |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8717 | * zero. This one verifies that the limits are respected but is optimized |
| 8718 | * 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] | 8719 | */ |
| 8720 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 8721 | { |
| 8722 | struct hlua_mem_allocator *zone = ud; |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8723 | size_t limit, old, new; |
| 8724 | |
Tim Duesterhus | 2258652 | 2021-01-08 10:35:33 +0100 | [diff] [blame] | 8725 | if (unlikely(!ptr && !nsize)) |
| 8726 | return NULL; |
| 8727 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8728 | /* a limit of ~0 means unlimited and boot complete, so there's no need |
| 8729 | * for accounting anymore. |
| 8730 | */ |
Christopher Faulet | cc2c4f8 | 2021-03-24 14:52:24 +0100 | [diff] [blame] | 8731 | if (likely(~zone->limit == 0)) |
| 8732 | return realloc(ptr, nsize); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8733 | |
Willy Tarreau | d36c7fa | 2020-12-02 12:26:29 +0100 | [diff] [blame] | 8734 | if (!ptr) |
| 8735 | osize = 0; |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8736 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8737 | /* enforce strict limits across all threads */ |
| 8738 | limit = zone->limit; |
| 8739 | old = _HA_ATOMIC_LOAD(&zone->allocated); |
| 8740 | do { |
| 8741 | new = old + nsize - osize; |
| 8742 | if (unlikely(nsize && limit && new > limit)) |
| 8743 | return NULL; |
| 8744 | } while (!_HA_ATOMIC_CAS(&zone->allocated, &old, new)); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8745 | |
| 8746 | ptr = realloc(ptr, nsize); |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8747 | |
| 8748 | if (unlikely(!ptr && nsize)) // failed |
| 8749 | _HA_ATOMIC_SUB(&zone->allocated, nsize - osize); |
| 8750 | |
| 8751 | __ha_barrier_atomic_store(); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8752 | return ptr; |
| 8753 | } |
| 8754 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 8755 | /* 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] | 8756 | * We are in the initialisation process of HAProxy, this abort() is |
| 8757 | * tolerated. |
| 8758 | */ |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 8759 | lua_State *hlua_init_state(int thread_num) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8760 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8761 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8762 | int idx; |
| 8763 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8764 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8765 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8766 | const char *error_msg; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8767 | void **context; |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8768 | lua_State *L; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8769 | struct prepend_path *pp; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8770 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8771 | /* Init main lua stack. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8772 | L = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8773 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8774 | /* Initialise Lua context to NULL */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8775 | context = lua_getextraspace(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8776 | *context = NULL; |
| 8777 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8778 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8779 | * the Lua function can fail with an abort. We are in the initialisation |
| 8780 | * process of HAProxy, this abort() is tolerated. |
| 8781 | */ |
| 8782 | |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8783 | /* Call post initialisation function in safe environment. */ |
| 8784 | if (setjmp(safe_ljmp_env) != 0) { |
| 8785 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8786 | if (lua_type(L, -1) == LUA_TSTRING) |
| 8787 | error_msg = lua_tostring(L, -1); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 8788 | else |
| 8789 | error_msg = "critical error"; |
| 8790 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
| 8791 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8792 | } else { |
| 8793 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 8794 | } |
| 8795 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8796 | /* Initialise lua. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8797 | luaL_openlibs(L); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 8798 | #define HLUA_PREPEND_PATH_TOSTRING1(x) #x |
| 8799 | #define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x) |
| 8800 | #ifdef HLUA_PREPEND_PATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8801 | hlua_prepend_path(L, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 8802 | #endif |
| 8803 | #ifdef HLUA_PREPEND_CPATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8804 | hlua_prepend_path(L, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 8805 | #endif |
| 8806 | #undef HLUA_PREPEND_PATH_TOSTRING |
| 8807 | #undef HLUA_PREPEND_PATH_TOSTRING1 |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8808 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8809 | /* Apply configured prepend path */ |
| 8810 | list_for_each_entry(pp, &prepend_path_list, l) |
| 8811 | hlua_prepend_path(L, pp->type, pp->path); |
| 8812 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8813 | /* |
| 8814 | * |
| 8815 | * Create "core" object. |
| 8816 | * |
| 8817 | */ |
| 8818 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 8819 | /* This table entry is the object "core" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8820 | lua_newtable(L); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8821 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 8822 | /* set the thread id */ |
| 8823 | hlua_class_const_int(L, "thread", thread_num); |
| 8824 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8825 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8826 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8827 | hlua_class_const_int(L, log_levels[i], i); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8828 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8829 | /* Register special functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8830 | hlua_class_function(L, "register_init", hlua_register_init); |
| 8831 | hlua_class_function(L, "register_task", hlua_register_task); |
| 8832 | hlua_class_function(L, "register_fetches", hlua_register_fetches); |
| 8833 | hlua_class_function(L, "register_converters", hlua_register_converters); |
| 8834 | hlua_class_function(L, "register_action", hlua_register_action); |
| 8835 | hlua_class_function(L, "register_service", hlua_register_service); |
| 8836 | hlua_class_function(L, "register_cli", hlua_register_cli); |
| 8837 | hlua_class_function(L, "yield", hlua_yield); |
| 8838 | hlua_class_function(L, "set_nice", hlua_set_nice); |
| 8839 | hlua_class_function(L, "sleep", hlua_sleep); |
| 8840 | hlua_class_function(L, "msleep", hlua_msleep); |
| 8841 | hlua_class_function(L, "add_acl", hlua_add_acl); |
| 8842 | hlua_class_function(L, "del_acl", hlua_del_acl); |
| 8843 | hlua_class_function(L, "set_map", hlua_set_map); |
| 8844 | hlua_class_function(L, "del_map", hlua_del_map); |
| 8845 | hlua_class_function(L, "tcp", hlua_socket_new); |
| 8846 | hlua_class_function(L, "log", hlua_log); |
| 8847 | hlua_class_function(L, "Debug", hlua_log_debug); |
| 8848 | hlua_class_function(L, "Info", hlua_log_info); |
| 8849 | hlua_class_function(L, "Warning", hlua_log_warning); |
| 8850 | hlua_class_function(L, "Alert", hlua_log_alert); |
| 8851 | hlua_class_function(L, "done", hlua_done); |
| 8852 | hlua_fcn_reg_core_fcn(L); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8853 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8854 | lua_setglobal(L, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8855 | |
| 8856 | /* |
| 8857 | * |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8858 | * Create "act" object. |
| 8859 | * |
| 8860 | */ |
| 8861 | |
| 8862 | /* This table entry is the object "act" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8863 | lua_newtable(L); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8864 | |
| 8865 | /* push action return constants */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8866 | hlua_class_const_int(L, "CONTINUE", ACT_RET_CONT); |
| 8867 | hlua_class_const_int(L, "STOP", ACT_RET_STOP); |
| 8868 | hlua_class_const_int(L, "YIELD", ACT_RET_YIELD); |
| 8869 | hlua_class_const_int(L, "ERROR", ACT_RET_ERR); |
| 8870 | hlua_class_const_int(L, "DONE", ACT_RET_DONE); |
| 8871 | hlua_class_const_int(L, "DENY", ACT_RET_DENY); |
| 8872 | hlua_class_const_int(L, "ABORT", ACT_RET_ABRT); |
| 8873 | hlua_class_const_int(L, "INVALID", ACT_RET_INV); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8874 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8875 | hlua_class_function(L, "wake_time", hlua_set_wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8876 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8877 | lua_setglobal(L, "act"); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8878 | |
| 8879 | /* |
| 8880 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8881 | * Register class Map |
| 8882 | * |
| 8883 | */ |
| 8884 | |
| 8885 | /* This table entry is the object "Map" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8886 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8887 | |
| 8888 | /* register pattern types. */ |
| 8889 | for (i=0; i<PAT_MATCH_NUM; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8890 | hlua_class_const_int(L, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8891 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8892 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8893 | hlua_class_const_int(L, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8894 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8895 | |
| 8896 | /* register constructor. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8897 | hlua_class_function(L, "new", hlua_map_new); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8898 | |
| 8899 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8900 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8901 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8902 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8903 | lua_pushstring(L, "__index"); |
| 8904 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8905 | |
| 8906 | /* Register . */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8907 | hlua_class_function(L, "lookup", hlua_map_lookup); |
| 8908 | hlua_class_function(L, "slookup", hlua_map_slookup); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8909 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8910 | lua_rawset(L, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8911 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8912 | /* Register previous table in the registry with reference and named entry. |
| 8913 | * The function hlua_register_metatable() pops the stack, so we |
| 8914 | * previously create a copy of the table. |
| 8915 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8916 | lua_pushvalue(L, -1); /* Copy the -1 entry and push it on the stack. */ |
| 8917 | class_map_ref = hlua_register_metatable(L, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8918 | |
| 8919 | /* Assign the metatable to the mai Map object. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8920 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8921 | |
| 8922 | /* Set a name to the table. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8923 | lua_setglobal(L, "Map"); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8924 | |
| 8925 | /* |
| 8926 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8927 | * Register class Channel |
| 8928 | * |
| 8929 | */ |
| 8930 | |
| 8931 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8932 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8933 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8934 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8935 | lua_pushstring(L, "__index"); |
| 8936 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8937 | |
| 8938 | /* Register . */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8939 | hlua_class_function(L, "get", hlua_channel_get); |
| 8940 | hlua_class_function(L, "dup", hlua_channel_dup); |
| 8941 | hlua_class_function(L, "getline", hlua_channel_getline); |
| 8942 | hlua_class_function(L, "set", hlua_channel_set); |
| 8943 | hlua_class_function(L, "append", hlua_channel_append); |
| 8944 | hlua_class_function(L, "send", hlua_channel_send); |
| 8945 | hlua_class_function(L, "forward", hlua_channel_forward); |
| 8946 | hlua_class_function(L, "get_in_len", hlua_channel_get_in_len); |
| 8947 | hlua_class_function(L, "get_out_len", hlua_channel_get_out_len); |
| 8948 | hlua_class_function(L, "is_full", hlua_channel_is_full); |
| 8949 | hlua_class_function(L, "is_resp", hlua_channel_is_resp); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8950 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8951 | lua_rawset(L, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8952 | |
| 8953 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8954 | class_channel_ref = hlua_register_metatable(L, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8955 | |
| 8956 | /* |
| 8957 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8958 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8959 | * |
| 8960 | */ |
| 8961 | |
| 8962 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8963 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8964 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8965 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8966 | lua_pushstring(L, "__index"); |
| 8967 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8968 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8969 | /* Browse existing fetches and create the associated |
| 8970 | * object method. |
| 8971 | */ |
| 8972 | sf = NULL; |
| 8973 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8974 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 8975 | * by an underscore. |
| 8976 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8977 | strncpy(trash.area, sf->kw, trash.size); |
| 8978 | trash.area[trash.size - 1] = '\0'; |
| 8979 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8980 | if (*p == '.' || *p == '-' || *p == '+') |
| 8981 | *p = '_'; |
| 8982 | |
| 8983 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8984 | lua_pushstring(L, trash.area); |
| 8985 | lua_pushlightuserdata(L, sf); |
| 8986 | lua_pushcclosure(L, hlua_run_sample_fetch, 1); |
| 8987 | lua_rawset(L, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8988 | } |
| 8989 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8990 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8991 | |
| 8992 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8993 | class_fetches_ref = hlua_register_metatable(L, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8994 | |
| 8995 | /* |
| 8996 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8997 | * Register class Converters |
| 8998 | * |
| 8999 | */ |
| 9000 | |
| 9001 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9002 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9003 | |
| 9004 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9005 | lua_pushstring(L, "__index"); |
| 9006 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9007 | |
| 9008 | /* Browse existing converters and create the associated |
| 9009 | * object method. |
| 9010 | */ |
| 9011 | sc = NULL; |
| 9012 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9013 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 9014 | * by an underscore. |
| 9015 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 9016 | strncpy(trash.area, sc->kw, trash.size); |
| 9017 | trash.area[trash.size - 1] = '\0'; |
| 9018 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9019 | if (*p == '.' || *p == '-' || *p == '+') |
| 9020 | *p = '_'; |
| 9021 | |
| 9022 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9023 | lua_pushstring(L, trash.area); |
| 9024 | lua_pushlightuserdata(L, sc); |
| 9025 | lua_pushcclosure(L, hlua_run_sample_conv, 1); |
| 9026 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9027 | } |
| 9028 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9029 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9030 | |
| 9031 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9032 | class_converters_ref = hlua_register_metatable(L, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 9033 | |
| 9034 | /* |
| 9035 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9036 | * Register class HTTP |
| 9037 | * |
| 9038 | */ |
| 9039 | |
| 9040 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9041 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9042 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9043 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9044 | lua_pushstring(L, "__index"); |
| 9045 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9046 | |
| 9047 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9048 | hlua_class_function(L, "req_get_headers",hlua_http_req_get_headers); |
| 9049 | hlua_class_function(L, "req_del_header", hlua_http_req_del_hdr); |
| 9050 | hlua_class_function(L, "req_rep_header", hlua_http_req_rep_hdr); |
| 9051 | hlua_class_function(L, "req_rep_value", hlua_http_req_rep_val); |
| 9052 | hlua_class_function(L, "req_add_header", hlua_http_req_add_hdr); |
| 9053 | hlua_class_function(L, "req_set_header", hlua_http_req_set_hdr); |
| 9054 | hlua_class_function(L, "req_set_method", hlua_http_req_set_meth); |
| 9055 | hlua_class_function(L, "req_set_path", hlua_http_req_set_path); |
| 9056 | hlua_class_function(L, "req_set_query", hlua_http_req_set_query); |
| 9057 | hlua_class_function(L, "req_set_uri", hlua_http_req_set_uri); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9058 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9059 | hlua_class_function(L, "res_get_headers",hlua_http_res_get_headers); |
| 9060 | hlua_class_function(L, "res_del_header", hlua_http_res_del_hdr); |
| 9061 | hlua_class_function(L, "res_rep_header", hlua_http_res_rep_hdr); |
| 9062 | hlua_class_function(L, "res_rep_value", hlua_http_res_rep_val); |
| 9063 | hlua_class_function(L, "res_add_header", hlua_http_res_add_hdr); |
| 9064 | hlua_class_function(L, "res_set_header", hlua_http_res_set_hdr); |
| 9065 | hlua_class_function(L, "res_set_status", hlua_http_res_set_status); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9066 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9067 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9068 | |
| 9069 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9070 | class_http_ref = hlua_register_metatable(L, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9071 | |
| 9072 | /* |
| 9073 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9074 | * Register class AppletTCP |
| 9075 | * |
| 9076 | */ |
| 9077 | |
| 9078 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9079 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9080 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9081 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9082 | lua_pushstring(L, "__index"); |
| 9083 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9084 | |
| 9085 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9086 | hlua_class_function(L, "getline", hlua_applet_tcp_getline); |
| 9087 | hlua_class_function(L, "receive", hlua_applet_tcp_recv); |
| 9088 | hlua_class_function(L, "send", hlua_applet_tcp_send); |
| 9089 | hlua_class_function(L, "set_priv", hlua_applet_tcp_set_priv); |
| 9090 | hlua_class_function(L, "get_priv", hlua_applet_tcp_get_priv); |
| 9091 | hlua_class_function(L, "set_var", hlua_applet_tcp_set_var); |
| 9092 | hlua_class_function(L, "unset_var", hlua_applet_tcp_unset_var); |
| 9093 | hlua_class_function(L, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9094 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9095 | lua_settable(L, -3); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9096 | |
| 9097 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9098 | class_applet_tcp_ref = hlua_register_metatable(L, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9099 | |
| 9100 | /* |
| 9101 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9102 | * Register class AppletHTTP |
| 9103 | * |
| 9104 | */ |
| 9105 | |
| 9106 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9107 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9108 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9109 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9110 | lua_pushstring(L, "__index"); |
| 9111 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9112 | |
| 9113 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9114 | hlua_class_function(L, "set_priv", hlua_applet_http_set_priv); |
| 9115 | hlua_class_function(L, "get_priv", hlua_applet_http_get_priv); |
| 9116 | hlua_class_function(L, "set_var", hlua_applet_http_set_var); |
| 9117 | hlua_class_function(L, "unset_var", hlua_applet_http_unset_var); |
| 9118 | hlua_class_function(L, "get_var", hlua_applet_http_get_var); |
| 9119 | hlua_class_function(L, "getline", hlua_applet_http_getline); |
| 9120 | hlua_class_function(L, "receive", hlua_applet_http_recv); |
| 9121 | hlua_class_function(L, "send", hlua_applet_http_send); |
| 9122 | hlua_class_function(L, "add_header", hlua_applet_http_addheader); |
| 9123 | hlua_class_function(L, "set_status", hlua_applet_http_status); |
| 9124 | hlua_class_function(L, "start_response", hlua_applet_http_start_response); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9125 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9126 | lua_settable(L, -3); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9127 | |
| 9128 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9129 | class_applet_http_ref = hlua_register_metatable(L, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9130 | |
| 9131 | /* |
| 9132 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9133 | * Register class TXN |
| 9134 | * |
| 9135 | */ |
| 9136 | |
| 9137 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9138 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9139 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9140 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9141 | lua_pushstring(L, "__index"); |
| 9142 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9143 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 9144 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9145 | hlua_class_function(L, "set_priv", hlua_set_priv); |
| 9146 | hlua_class_function(L, "get_priv", hlua_get_priv); |
| 9147 | hlua_class_function(L, "set_var", hlua_set_var); |
| 9148 | hlua_class_function(L, "unset_var", hlua_unset_var); |
| 9149 | hlua_class_function(L, "get_var", hlua_get_var); |
| 9150 | hlua_class_function(L, "done", hlua_txn_done); |
| 9151 | hlua_class_function(L, "reply", hlua_txn_reply_new); |
| 9152 | hlua_class_function(L, "set_loglevel", hlua_txn_set_loglevel); |
| 9153 | hlua_class_function(L, "set_tos", hlua_txn_set_tos); |
| 9154 | hlua_class_function(L, "set_mark", hlua_txn_set_mark); |
| 9155 | hlua_class_function(L, "set_priority_class", hlua_txn_set_priority_class); |
| 9156 | hlua_class_function(L, "set_priority_offset", hlua_txn_set_priority_offset); |
| 9157 | hlua_class_function(L, "deflog", hlua_txn_deflog); |
| 9158 | hlua_class_function(L, "log", hlua_txn_log); |
| 9159 | hlua_class_function(L, "Debug", hlua_txn_log_debug); |
| 9160 | hlua_class_function(L, "Info", hlua_txn_log_info); |
| 9161 | hlua_class_function(L, "Warning", hlua_txn_log_warning); |
| 9162 | hlua_class_function(L, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 9163 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9164 | lua_rawset(L, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 9165 | |
| 9166 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9167 | class_txn_ref = hlua_register_metatable(L, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9168 | |
| 9169 | /* |
| 9170 | * |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 9171 | * Register class reply |
| 9172 | * |
| 9173 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9174 | lua_newtable(L); |
| 9175 | lua_pushstring(L, "__index"); |
| 9176 | lua_newtable(L); |
| 9177 | hlua_class_function(L, "set_status", hlua_txn_reply_set_status); |
| 9178 | hlua_class_function(L, "add_header", hlua_txn_reply_add_header); |
| 9179 | hlua_class_function(L, "del_header", hlua_txn_reply_del_header); |
| 9180 | hlua_class_function(L, "set_body", hlua_txn_reply_set_body); |
| 9181 | lua_settable(L, -3); /* Sets the __index entry. */ |
| 9182 | class_txn_reply_ref = luaL_ref(L, LUA_REGISTRYINDEX); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 9183 | |
| 9184 | |
| 9185 | /* |
| 9186 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9187 | * Register class Socket |
| 9188 | * |
| 9189 | */ |
| 9190 | |
| 9191 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9192 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9193 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9194 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9195 | lua_pushstring(L, "__index"); |
| 9196 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9197 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 9198 | #ifdef USE_OPENSSL |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9199 | hlua_class_function(L, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 9200 | #endif |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9201 | hlua_class_function(L, "connect", hlua_socket_connect); |
| 9202 | hlua_class_function(L, "send", hlua_socket_send); |
| 9203 | hlua_class_function(L, "receive", hlua_socket_receive); |
| 9204 | hlua_class_function(L, "close", hlua_socket_close); |
| 9205 | hlua_class_function(L, "getpeername", hlua_socket_getpeername); |
| 9206 | hlua_class_function(L, "getsockname", hlua_socket_getsockname); |
| 9207 | hlua_class_function(L, "setoption", hlua_socket_setoption); |
| 9208 | hlua_class_function(L, "settimeout", hlua_socket_settimeout); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9209 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9210 | 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] | 9211 | |
| 9212 | /* Register the garbage collector entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9213 | lua_pushstring(L, "__gc"); |
| 9214 | lua_pushcclosure(L, hlua_socket_gc, 0); |
| 9215 | 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] | 9216 | |
| 9217 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9218 | class_socket_ref = hlua_register_metatable(L, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9219 | |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 9220 | lua_atpanic(L, hlua_panic_safe); |
| 9221 | |
| 9222 | return L; |
| 9223 | } |
| 9224 | |
| 9225 | void hlua_init(void) { |
| 9226 | int i; |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 9227 | char *errmsg; |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 9228 | #ifdef USE_OPENSSL |
| 9229 | struct srv_kw *kw; |
| 9230 | int tmp_error; |
| 9231 | char *error; |
| 9232 | char *args[] = { /* SSL client configuration. */ |
| 9233 | "ssl", |
| 9234 | "verify", |
| 9235 | "none", |
| 9236 | NULL |
| 9237 | }; |
| 9238 | #endif |
| 9239 | |
| 9240 | /* Init post init function list head */ |
| 9241 | for (i = 0; i < MAX_THREADS + 1; i++) |
| 9242 | LIST_INIT(&hlua_init_functions[i]); |
| 9243 | |
| 9244 | /* Init state for common/shared lua parts */ |
| 9245 | hlua_state_id = 0; |
| 9246 | ha_set_tid(0); |
| 9247 | hlua_states[0] = hlua_init_state(0); |
| 9248 | |
| 9249 | /* Init state 1 for thread 0. We have at least one thread. */ |
| 9250 | hlua_state_id = 1; |
| 9251 | ha_set_tid(0); |
| 9252 | hlua_states[1] = hlua_init_state(1); |
| 9253 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9254 | /* Proxy and server configuration initialisation. */ |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 9255 | socket_proxy = alloc_new_proxy("LUA-SOCKET", PR_CAP_FE|PR_CAP_BE|PR_CAP_LUA, &errmsg); |
| 9256 | if (!socket_proxy) { |
| 9257 | fprintf(stderr, "Lua init: %s\n", errmsg); |
| 9258 | exit(1); |
| 9259 | } |
| 9260 | proxy_preset_defaults(socket_proxy); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9261 | |
| 9262 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9263 | socket_tcp = new_server(socket_proxy); |
| 9264 | if (!socket_tcp) { |
| 9265 | fprintf(stderr, "Lua init: failed to allocate tcp server socket\n"); |
| 9266 | exit(1); |
| 9267 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9268 | |
| 9269 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9270 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9271 | socket_ssl = new_server(socket_proxy); |
| 9272 | if (!socket_ssl) { |
| 9273 | fprintf(stderr, "Lua init: failed to allocate ssl server socket\n"); |
| 9274 | exit(1); |
| 9275 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9276 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9277 | socket_ssl->use_ssl = 1; |
| 9278 | socket_ssl->xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9279 | |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 9280 | for (i = 0; args[i] != NULL; i++) { |
| 9281 | 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] | 9282 | /* |
| 9283 | * |
| 9284 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9285 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9286 | * features like client certificates and ssl_verify. |
| 9287 | * |
| 9288 | */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9289 | tmp_error = kw->parse(args, &i, socket_proxy, socket_ssl, &error); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9290 | if (tmp_error != 0) { |
| 9291 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 9292 | abort(); /* This must be never arrives because the command line |
| 9293 | not editable by the user. */ |
| 9294 | } |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 9295 | i += kw->skip; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9296 | } |
| 9297 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9298 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 9299 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 9300 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 9301 | |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 9302 | static void hlua_deinit() |
| 9303 | { |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 9304 | int thr; |
| 9305 | |
| 9306 | for (thr = 0; thr < MAX_THREADS+1; thr++) { |
| 9307 | if (hlua_states[thr]) |
| 9308 | lua_close(hlua_states[thr]); |
| 9309 | } |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 9310 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9311 | free_server(socket_tcp); |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 9312 | |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 9313 | #ifdef USE_OPENSSL |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9314 | free_server(socket_ssl); |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 9315 | #endif |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 9316 | |
| 9317 | free_proxy(socket_proxy); |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 9318 | } |
| 9319 | |
| 9320 | REGISTER_POST_DEINIT(hlua_deinit); |
| 9321 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9322 | static void hlua_register_build_options(void) |
| 9323 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 9324 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9325 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 9326 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 9327 | hap_register_build_opts(ptr, 1); |
| 9328 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9329 | |
| 9330 | INITCALL0(STG_REGISTER, hlua_register_build_options); |