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 | |
Christopher Faulet | ba9e21d | 2020-02-25 10:20:04 +0100 | [diff] [blame^] | 353 | /* Used to check an Lua table type in the stack. It creates and |
| 354 | * returns a reference of the table. This function throws an |
| 355 | * error if the rgument is not a "table". |
| 356 | */ |
| 357 | __LJMP unsigned int hlua_checktable(lua_State *L, int argno) |
| 358 | { |
| 359 | if (!lua_istable(L, argno)) { |
| 360 | const char *msg = lua_pushfstring(L, "table expected, got %s", luaL_typename(L, argno)); |
| 361 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 362 | } |
| 363 | lua_pushvalue(L, argno); |
| 364 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 365 | } |
| 366 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 367 | /* Return the string that is of the top of the stack. */ |
| 368 | const char *hlua_get_top_error_string(lua_State *L) |
| 369 | { |
| 370 | if (lua_gettop(L) < 1) |
| 371 | return "unknown error"; |
| 372 | if (lua_type(L, -1) != LUA_TSTRING) |
| 373 | return "unknown error"; |
| 374 | return lua_tostring(L, -1); |
| 375 | } |
| 376 | |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 377 | __LJMP const char *hlua_traceback(lua_State *L, const char* sep) |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 378 | { |
| 379 | lua_Debug ar; |
| 380 | int level = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 381 | struct buffer *msg = get_trash_chunk(); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 382 | |
| 383 | while (lua_getstack(L, level++, &ar)) { |
| 384 | |
| 385 | /* Add separator */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 386 | if (b_data(msg)) |
| 387 | chunk_appendf(msg, "%s", sep); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 388 | |
| 389 | /* Fill fields: |
| 390 | * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what; |
| 391 | * 'l': fills in the field currentline; |
| 392 | * 'n': fills in the field name and namewhat; |
| 393 | * 't': fills in the field istailcall; |
| 394 | */ |
| 395 | lua_getinfo(L, "Slnt", &ar); |
| 396 | |
| 397 | /* Append code localisation */ |
| 398 | if (ar.currentline > 0) |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 399 | chunk_appendf(msg, "%s:%d: ", ar.short_src, ar.currentline); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 400 | else |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 401 | chunk_appendf(msg, "%s: ", ar.short_src); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 402 | |
| 403 | /* |
| 404 | * Get function name |
| 405 | * |
| 406 | * if namewhat is no empty, name is defined. |
| 407 | * what contains "Lua" for Lua function, "C" for C function, |
| 408 | * or "main" for main code. |
| 409 | */ |
| 410 | 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] | 411 | chunk_appendf(msg, "in %s '%s'", ar.namewhat, ar.name); /* use it */ |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 412 | |
| 413 | 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] | 414 | chunk_appendf(msg, "in main chunk"); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 415 | |
| 416 | else if (*ar.what != 'C') /* for Lua functions, use <file:line> */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 417 | chunk_appendf(msg, "in function line %d", ar.linedefined); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 418 | |
| 419 | else /* nothing left... */ |
| 420 | chunk_appendf(msg, "?"); |
| 421 | |
| 422 | |
| 423 | /* Display tailed call */ |
| 424 | if (ar.istailcall) |
| 425 | chunk_appendf(msg, " ..."); |
| 426 | } |
| 427 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 428 | return msg->area; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 432 | /* This function check the number of arguments available in the |
| 433 | * stack. If the number of arguments available is not the same |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 434 | * then <nb> an error is thrown. |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 435 | */ |
| 436 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 437 | { |
| 438 | if (lua_gettop(L) == nb) |
| 439 | return; |
| 440 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 441 | } |
| 442 | |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 443 | /* This function pushes an error string prefixed by the file name |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 444 | * and the line number where the error is encountered. |
| 445 | */ |
| 446 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 447 | { |
| 448 | va_list argp; |
| 449 | va_start(argp, fmt); |
| 450 | luaL_where(L, 1); |
| 451 | lua_pushvfstring(L, fmt, argp); |
| 452 | va_end(argp); |
| 453 | lua_concat(L, 2); |
| 454 | return 1; |
| 455 | } |
| 456 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 457 | /* This functions is used with sample fetch and converters. It |
| 458 | * converts the HAProxy configuration argument in a lua stack |
| 459 | * values. |
| 460 | * |
| 461 | * It takes an array of "arg", and each entry of the array is |
| 462 | * converted and pushed in the LUA stack. |
| 463 | */ |
| 464 | static int hlua_arg2lua(lua_State *L, const struct arg *arg) |
| 465 | { |
| 466 | switch (arg->type) { |
| 467 | case ARGT_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 468 | case ARGT_TIME: |
| 469 | case ARGT_SIZE: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 470 | lua_pushinteger(L, arg->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 471 | break; |
| 472 | |
| 473 | case ARGT_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 474 | lua_pushlstring(L, arg->data.str.area, arg->data.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 475 | break; |
| 476 | |
| 477 | case ARGT_IPV4: |
| 478 | case ARGT_IPV6: |
| 479 | case ARGT_MSK4: |
| 480 | case ARGT_MSK6: |
| 481 | case ARGT_FE: |
| 482 | case ARGT_BE: |
| 483 | case ARGT_TAB: |
| 484 | case ARGT_SRV: |
| 485 | case ARGT_USR: |
| 486 | case ARGT_MAP: |
| 487 | default: |
| 488 | lua_pushnil(L); |
| 489 | break; |
| 490 | } |
| 491 | return 1; |
| 492 | } |
| 493 | |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 494 | /* 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] | 495 | * 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] | 496 | * with sample fetch wrappers. The input arguments are given to the |
| 497 | * lua wrapper and converted as arg list by the function. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 498 | */ |
| 499 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg) |
| 500 | { |
| 501 | switch (lua_type(L, ud)) { |
| 502 | |
| 503 | case LUA_TNUMBER: |
| 504 | case LUA_TBOOLEAN: |
| 505 | arg->type = ARGT_SINT; |
| 506 | arg->data.sint = lua_tointeger(L, ud); |
| 507 | break; |
| 508 | |
| 509 | case LUA_TSTRING: |
| 510 | arg->type = ARGT_STR; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 511 | 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] | 512 | /* 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] | 513 | 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] | 514 | arg->data.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 515 | break; |
| 516 | |
| 517 | case LUA_TUSERDATA: |
| 518 | case LUA_TNIL: |
| 519 | case LUA_TTABLE: |
| 520 | case LUA_TFUNCTION: |
| 521 | case LUA_TTHREAD: |
| 522 | case LUA_TLIGHTUSERDATA: |
| 523 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 524 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 525 | break; |
| 526 | } |
| 527 | return 1; |
| 528 | } |
| 529 | |
| 530 | /* the following functions are used to convert a struct sample |
| 531 | * in Lua type. This useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 532 | * fetches or converters. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 533 | */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 534 | static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 535 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 536 | switch (smp->data.type) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 537 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 538 | case SMP_T_BOOL: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 539 | lua_pushinteger(L, smp->data.u.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 540 | break; |
| 541 | |
| 542 | case SMP_T_BIN: |
| 543 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 544 | 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] | 545 | break; |
| 546 | |
| 547 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 548 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 549 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 550 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 551 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 552 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 553 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 554 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 555 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 556 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 557 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 558 | 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] | 559 | break; |
| 560 | default: |
| 561 | lua_pushnil(L); |
| 562 | break; |
| 563 | } |
| 564 | break; |
| 565 | |
| 566 | case SMP_T_IPV4: |
| 567 | case SMP_T_IPV6: |
| 568 | 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] | 569 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 570 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 571 | 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] | 572 | else |
| 573 | lua_pushnil(L); |
| 574 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 575 | default: |
| 576 | lua_pushnil(L); |
| 577 | break; |
| 578 | } |
| 579 | return 1; |
| 580 | } |
| 581 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 582 | /* the following functions are used to convert a struct sample |
| 583 | * in Lua strings. This is useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 584 | * fetches or converters. |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 585 | */ |
| 586 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
| 587 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 588 | switch (smp->data.type) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 589 | |
| 590 | case SMP_T_BIN: |
| 591 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 592 | 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] | 593 | break; |
| 594 | |
| 595 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 596 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 597 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 598 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 599 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 600 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 601 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 602 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 603 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 604 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 605 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 606 | 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] | 607 | break; |
| 608 | default: |
| 609 | lua_pushstring(L, ""); |
| 610 | break; |
| 611 | } |
| 612 | break; |
| 613 | |
| 614 | case SMP_T_SINT: |
| 615 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 616 | case SMP_T_IPV4: |
| 617 | case SMP_T_IPV6: |
| 618 | 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] | 619 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 620 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 621 | 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] | 622 | else |
| 623 | lua_pushstring(L, ""); |
| 624 | break; |
| 625 | default: |
| 626 | lua_pushstring(L, ""); |
| 627 | break; |
| 628 | } |
| 629 | return 1; |
| 630 | } |
| 631 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 632 | /* the following functions are used to convert an Lua type in a |
| 633 | * struct sample. This is useful to provide data from a converter |
| 634 | * to the LUA code. |
| 635 | */ |
| 636 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 637 | { |
| 638 | switch (lua_type(L, ud)) { |
| 639 | |
| 640 | case LUA_TNUMBER: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 641 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 642 | smp->data.u.sint = lua_tointeger(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 643 | break; |
| 644 | |
| 645 | |
| 646 | case LUA_TBOOLEAN: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 647 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 648 | smp->data.u.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 649 | break; |
| 650 | |
| 651 | case LUA_TSTRING: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 652 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 653 | smp->flags |= SMP_F_CONST; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 654 | 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] | 655 | /* 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] | 656 | 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] | 657 | smp->data.u.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 658 | break; |
| 659 | |
| 660 | case LUA_TUSERDATA: |
| 661 | case LUA_TNIL: |
| 662 | case LUA_TTABLE: |
| 663 | case LUA_TFUNCTION: |
| 664 | case LUA_TTHREAD: |
| 665 | case LUA_TLIGHTUSERDATA: |
Thierry FOURNIER | 93405e1 | 2015-08-26 14:19:03 +0200 | [diff] [blame] | 666 | case LUA_TNONE: |
| 667 | default: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 668 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 669 | smp->data.u.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 670 | break; |
| 671 | } |
| 672 | return 1; |
| 673 | } |
| 674 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 675 | /* This function check the "argp" built by another conversion function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 676 | * 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] | 677 | * 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] | 678 | * |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 679 | * This function assumes that the argp argument contains ARGM_NBARGS + 1 |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 680 | * entries and that there is at least one stop at the last position. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 681 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 682 | __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] | 683 | uint64_t mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 684 | { |
| 685 | int min_arg; |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 686 | int idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 687 | struct proxy *px; |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 688 | struct userlist *ul; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 689 | struct my_regex *reg; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 690 | const char *msg = NULL; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 691 | char *sname, *pname, *err = NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 692 | |
| 693 | idx = 0; |
| 694 | min_arg = ARGM(mask); |
| 695 | mask >>= ARGM_BITS; |
| 696 | |
| 697 | while (1) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 698 | struct buffer tmp = BUF_NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 699 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 700 | /* Check for mandatory arguments. */ |
| 701 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 702 | if (idx < min_arg) { |
| 703 | |
| 704 | /* If miss other argument than the first one, we return an error. */ |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 705 | if (idx > 0) { |
| 706 | msg = "Mandatory argument expected"; |
| 707 | goto error; |
| 708 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 709 | |
| 710 | /* If first argument have a certain type, some default values |
| 711 | * may be used. See the function smp_resolve_args(). |
| 712 | */ |
| 713 | switch (mask & ARGT_MASK) { |
| 714 | |
| 715 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 716 | if (!(p->cap & PR_CAP_FE)) { |
| 717 | msg = "Mandatory argument expected"; |
| 718 | goto error; |
| 719 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 720 | argp[idx].data.prx = p; |
| 721 | argp[idx].type = ARGT_FE; |
| 722 | argp[idx+1].type = ARGT_STOP; |
| 723 | break; |
| 724 | |
| 725 | case ARGT_BE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 726 | if (!(p->cap & PR_CAP_BE)) { |
| 727 | msg = "Mandatory argument expected"; |
| 728 | goto error; |
| 729 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 730 | argp[idx].data.prx = p; |
| 731 | argp[idx].type = ARGT_BE; |
| 732 | argp[idx+1].type = ARGT_STOP; |
| 733 | break; |
| 734 | |
| 735 | case ARGT_TAB: |
| 736 | argp[idx].data.prx = p; |
| 737 | argp[idx].type = ARGT_TAB; |
| 738 | argp[idx+1].type = ARGT_STOP; |
| 739 | break; |
| 740 | |
| 741 | default: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 742 | msg = "Mandatory argument expected"; |
| 743 | goto error; |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 744 | break; |
| 745 | } |
| 746 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 747 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 748 | } |
| 749 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 750 | /* Check for exceed the number of required argument. */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 751 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 752 | argp[idx].type != ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 753 | msg = "Last argument expected"; |
| 754 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 758 | argp[idx].type == ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 759 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 760 | } |
| 761 | |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 762 | /* Convert some argument types. All string in argp[] are for not |
| 763 | * duplicated yet. |
| 764 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 765 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 766 | case ARGT_SINT: |
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 | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 771 | argp[idx].type = ARGT_SINT; |
| 772 | break; |
| 773 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 774 | case ARGT_TIME: |
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_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 780 | break; |
| 781 | |
| 782 | case ARGT_SIZE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 783 | if (argp[idx].type != ARGT_SINT) { |
| 784 | msg = "integer expected"; |
| 785 | goto error; |
| 786 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 787 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 788 | break; |
| 789 | |
| 790 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 791 | if (argp[idx].type != ARGT_STR) { |
| 792 | msg = "string expected"; |
| 793 | goto error; |
| 794 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 795 | 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] | 796 | if (!argp[idx].data.prx) { |
| 797 | msg = "frontend doesn't exist"; |
| 798 | goto error; |
| 799 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 800 | argp[idx].type = ARGT_FE; |
| 801 | break; |
| 802 | |
| 803 | case ARGT_BE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 804 | if (argp[idx].type != ARGT_STR) { |
| 805 | msg = "string expected"; |
| 806 | goto error; |
| 807 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 808 | 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] | 809 | if (!argp[idx].data.prx) { |
| 810 | msg = "backend doesn't exist"; |
| 811 | goto error; |
| 812 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 813 | argp[idx].type = ARGT_BE; |
| 814 | break; |
| 815 | |
| 816 | case ARGT_TAB: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 817 | if (argp[idx].type != ARGT_STR) { |
| 818 | msg = "string expected"; |
| 819 | goto error; |
| 820 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 821 | 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] | 822 | if (!argp[idx].data.t) { |
| 823 | msg = "table doesn't exist"; |
| 824 | goto error; |
| 825 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 826 | argp[idx].type = ARGT_TAB; |
| 827 | break; |
| 828 | |
| 829 | case ARGT_SRV: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 830 | if (argp[idx].type != ARGT_STR) { |
| 831 | msg = "string expected"; |
| 832 | goto error; |
| 833 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 834 | sname = strrchr(argp[idx].data.str.area, '/'); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 835 | if (sname) { |
| 836 | *sname++ = '\0'; |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 837 | pname = argp[idx].data.str.area; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 838 | px = proxy_be_by_name(pname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 839 | if (!px) { |
| 840 | msg = "backend doesn't exist"; |
| 841 | goto error; |
| 842 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 843 | } |
| 844 | else { |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 845 | sname = argp[idx].data.str.area; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 846 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 847 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 848 | argp[idx].data.srv = findserver(px, sname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 849 | if (!argp[idx].data.srv) { |
| 850 | msg = "server doesn't exist"; |
| 851 | goto error; |
| 852 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 853 | argp[idx].type = ARGT_SRV; |
| 854 | break; |
| 855 | |
| 856 | case ARGT_IPV4: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 857 | if (argp[idx].type != ARGT_STR) { |
| 858 | msg = "string expected"; |
| 859 | goto error; |
| 860 | } |
| 861 | if (inet_pton(AF_INET, argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 862 | msg = "invalid IPv4 address"; |
| 863 | goto error; |
| 864 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 865 | argp[idx].type = ARGT_IPV4; |
| 866 | break; |
| 867 | |
| 868 | case ARGT_MSK4: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 869 | if (argp[idx].type == ARGT_SINT) |
| 870 | len2mask4(argp[idx].data.sint, &argp[idx].data.ipv4); |
| 871 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 872 | if (!str2mask(argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 873 | msg = "invalid IPv4 mask"; |
| 874 | goto error; |
| 875 | } |
| 876 | } |
| 877 | else { |
| 878 | msg = "integer or string expected"; |
| 879 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 880 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 881 | argp[idx].type = ARGT_MSK4; |
| 882 | break; |
| 883 | |
| 884 | case ARGT_IPV6: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 885 | if (argp[idx].type != ARGT_STR) { |
| 886 | msg = "string expected"; |
| 887 | goto error; |
| 888 | } |
| 889 | if (inet_pton(AF_INET6, argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 890 | msg = "invalid IPv6 address"; |
| 891 | goto error; |
| 892 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 893 | argp[idx].type = ARGT_IPV6; |
| 894 | break; |
| 895 | |
| 896 | case ARGT_MSK6: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 897 | if (argp[idx].type == ARGT_SINT) |
| 898 | len2mask6(argp[idx].data.sint, &argp[idx].data.ipv6); |
| 899 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 900 | if (!str2mask6(argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 901 | msg = "invalid IPv6 mask"; |
| 902 | goto error; |
| 903 | } |
| 904 | } |
| 905 | else { |
| 906 | msg = "integer or string expected"; |
| 907 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 908 | } |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 909 | argp[idx].type = ARGT_MSK6; |
| 910 | break; |
| 911 | |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 912 | case ARGT_REG: |
| 913 | if (argp[idx].type != ARGT_STR) { |
| 914 | msg = "string expected"; |
| 915 | goto error; |
| 916 | } |
| 917 | reg = regex_comp(argp[idx].data.str.area, !(argp[idx].type_flags & ARGF_REG_ICASE), 1, &err); |
| 918 | if (!reg) { |
| 919 | msg = lua_pushfstring(L, "error compiling regex '%s' : '%s'", |
| 920 | argp[idx].data.str.area, err); |
| 921 | free(err); |
| 922 | goto error; |
| 923 | } |
| 924 | argp[idx].type = ARGT_REG; |
| 925 | argp[idx].data.reg = reg; |
| 926 | break; |
| 927 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 928 | case ARGT_USR: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 929 | if (argp[idx].type != ARGT_STR) { |
| 930 | msg = "string expected"; |
| 931 | goto error; |
| 932 | } |
| 933 | if (p->uri_auth && p->uri_auth->userlist && |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 934 | strcmp(p->uri_auth->userlist->name, argp[idx].data.str.area) == 0) |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 935 | ul = p->uri_auth->userlist; |
| 936 | else |
| 937 | ul = auth_find_userlist(argp[idx].data.str.area); |
| 938 | |
| 939 | if (!ul) { |
| 940 | msg = lua_pushfstring(L, "unable to find userlist '%s'", argp[idx].data.str.area); |
| 941 | goto error; |
| 942 | } |
| 943 | argp[idx].type = ARGT_USR; |
| 944 | argp[idx].data.usr = ul; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 945 | break; |
| 946 | |
| 947 | case ARGT_STR: |
| 948 | if (!chunk_dup(&tmp, &argp[idx].data.str)) { |
| 949 | msg = "unable to duplicate string arg"; |
| 950 | goto error; |
| 951 | } |
| 952 | argp[idx].data.str = tmp; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 953 | break; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 954 | |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 955 | case ARGT_MAP: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 956 | msg = "type not yet supported"; |
| 957 | goto error; |
| 958 | break; |
| 959 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | /* Check for type of argument. */ |
| 963 | if ((mask & ARGT_MASK) != argp[idx].type) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 964 | msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 965 | arg_type_names[(mask & ARGT_MASK)], |
| 966 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 967 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 968 | } |
| 969 | |
| 970 | /* Next argument. */ |
| 971 | mask >>= ARGT_BITS; |
| 972 | idx++; |
| 973 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 974 | return 0; |
| 975 | |
| 976 | error: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 977 | free_args(argp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 978 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 979 | return 0; /* Never reached */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 980 | } |
| 981 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 982 | /* |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 983 | * The following functions are used to make correspondence between the the |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 984 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 985 | * |
| 986 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 987 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 988 | */ |
| 989 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 990 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 991 | struct hlua **hlua = lua_getextraspace(L); |
| 992 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 993 | } |
| 994 | static inline void hlua_sethlua(struct hlua *hlua) |
| 995 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 996 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 997 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 998 | } |
| 999 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1000 | /* This function is used to send logs. It try to send on screen (stderr) |
| 1001 | * and on the default syslog server. |
| 1002 | */ |
| 1003 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 1004 | { |
| 1005 | struct tm tm; |
| 1006 | char *p; |
| 1007 | |
| 1008 | /* Cleanup the log message. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1009 | p = trash.area; |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1010 | for (; *msg != '\0'; msg++, p++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1011 | if (p >= trash.area + trash.size - 1) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 1012 | /* Break the message if exceed the buffer size. */ |
| 1013 | *(p-4) = ' '; |
| 1014 | *(p-3) = '.'; |
| 1015 | *(p-2) = '.'; |
| 1016 | *(p-1) = '.'; |
| 1017 | break; |
| 1018 | } |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 1019 | if (isprint((unsigned char)*msg)) |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1020 | *p = *msg; |
| 1021 | else |
| 1022 | *p = '.'; |
| 1023 | } |
| 1024 | *p = '\0'; |
| 1025 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1026 | send_log(px, level, "%s\n", trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1027 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Christopher Faulet | f98d821 | 2020-10-02 18:13:52 +0200 | [diff] [blame] | 1028 | if (level == LOG_DEBUG && !(global.mode & MODE_DEBUG)) |
| 1029 | return; |
| 1030 | |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 1031 | get_localtime(date.tv_sec, &tm); |
| 1032 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1033 | 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] | 1034 | (int)getpid(), trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1035 | fflush(stderr); |
| 1036 | } |
| 1037 | } |
| 1038 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1039 | /* This function just ensure that the yield will be always |
| 1040 | * returned with a timeout and permit to set some flags |
| 1041 | */ |
| 1042 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1043 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1044 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1045 | struct hlua *hlua; |
| 1046 | |
| 1047 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1048 | hlua = hlua_gethlua(L); |
| 1049 | if (!hlua) { |
| 1050 | return; |
| 1051 | } |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1052 | |
| 1053 | /* Set the wake timeout. If timeout is required, we set |
| 1054 | * the expiration time. |
| 1055 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1056 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1057 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 1058 | hlua->flags |= flags; |
| 1059 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1060 | /* Process the yield. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1061 | MAY_LJMP(lua_yieldk(L, nresults, ctx, k)); |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1062 | } |
| 1063 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1064 | /* This function initialises the Lua environment stored in the stream. |
| 1065 | * 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] | 1066 | * 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] | 1067 | * |
| 1068 | * This function is particular. it initialises a new Lua thread. If the |
| 1069 | * initialisation fails (example: out of memory error), the lua function |
| 1070 | * throws an error (longjmp). |
| 1071 | * |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1072 | * 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] | 1073 | * environment, so we must not initialise it. While the support of |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1074 | * threads appear, the safe environment set a lock to ensure only one |
| 1075 | * Lua execution at a time. If we initialize safe environment in another |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1076 | * safe environment, we have a dead lock. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1077 | * |
| 1078 | * set "already_safe" true if the context is initialized form safe |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1079 | * Lua function. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1080 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1081 | * This function manipulates two Lua stacks: the main and the thread. Only |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 1082 | * the main stack can fail. The thread is not manipulated. This function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1083 | * MUST NOT manipulate the created thread stack state, because it is not |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1084 | * protected against errors thrown by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1085 | */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1086 | 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] | 1087 | { |
| 1088 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1089 | lua->flags = 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1090 | lua->gc_count = 0; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1091 | lua->wake_time = TICK_ETERNITY; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1092 | lua->state_id = state_id; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1093 | LIST_INIT(&lua->com); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1094 | if (!already_safe) { |
| 1095 | if (!SET_SAFE_LJMP_PARENT(lua)) { |
| 1096 | lua->Tref = LUA_REFNIL; |
| 1097 | return 0; |
| 1098 | } |
| 1099 | } |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1100 | lua->T = lua_newthread(hlua_states[state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1101 | if (!lua->T) { |
| 1102 | lua->Tref = LUA_REFNIL; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1103 | if (!already_safe) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1104 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1105 | return 0; |
| 1106 | } |
| 1107 | hlua_sethlua(lua); |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1108 | lua->Tref = luaL_ref(hlua_states[state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1109 | lua->task = task; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1110 | if (!already_safe) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1111 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1112 | return 1; |
| 1113 | } |
| 1114 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1115 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1116 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 1117 | * is not freed. |
| 1118 | */ |
| 1119 | void hlua_ctx_destroy(struct hlua *lua) |
| 1120 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1121 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 1122 | return; |
| 1123 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1124 | if (!lua->T) |
| 1125 | goto end; |
| 1126 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1127 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1128 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1129 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1130 | if (!SET_SAFE_LJMP(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1131 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1132 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1133 | RESET_SAFE_LJMP(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1134 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1135 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1136 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1137 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1138 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1139 | /* Forces a garbage collecting process. If the Lua program is finished |
| 1140 | * without error, we run the GC on the thread pointer. Its freed all |
| 1141 | * the unused memory. |
| 1142 | * If the thread is finnish with an error or is currently yielded, |
| 1143 | * it seems that the GC applied on the thread doesn't clean anything, |
| 1144 | * so e run the GC on the main thread. |
| 1145 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 1146 | * the garbage collection. |
| 1147 | */ |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1148 | if (lua->gc_count) { |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1149 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1150 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1151 | lua_gc(hlua_states[lua->state_id], LUA_GCCOLLECT, 0); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1152 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1153 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1154 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 1155 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1156 | |
| 1157 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1158 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1159 | } |
| 1160 | |
| 1161 | /* This function is used to restore the Lua context when a coroutine |
| 1162 | * fails. This function copy the common memory between old coroutine |
| 1163 | * and the new coroutine. The old coroutine is destroyed, and its |
| 1164 | * replaced by the new coroutine. |
| 1165 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 1166 | * as string error message and it is copied in the new stack. |
| 1167 | */ |
| 1168 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 1169 | { |
| 1170 | lua_State *T; |
| 1171 | int new_ref; |
| 1172 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1173 | /* New Lua coroutine. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1174 | T = lua_newthread(hlua_states[lua->state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1175 | if (!T) |
| 1176 | return 0; |
| 1177 | |
| 1178 | /* Copy last error message. */ |
| 1179 | if (keep_msg) |
| 1180 | lua_xmove(lua->T, T, 1); |
| 1181 | |
| 1182 | /* Copy data between the coroutines. */ |
| 1183 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1184 | lua_xmove(lua->T, T, 1); |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 1185 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */ |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1186 | |
| 1187 | /* Destroy old data. */ |
| 1188 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1189 | |
| 1190 | /* The thread is garbage collected by Lua. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1191 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1192 | |
| 1193 | /* Fill the struct with the new coroutine values. */ |
| 1194 | lua->Mref = new_ref; |
| 1195 | lua->T = T; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1196 | lua->Tref = luaL_ref(hlua_states[lua->state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1197 | |
| 1198 | /* Set context. */ |
| 1199 | hlua_sethlua(lua); |
| 1200 | |
| 1201 | return 1; |
| 1202 | } |
| 1203 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1204 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1205 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1206 | struct hlua *hlua; |
| 1207 | |
| 1208 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1209 | hlua = hlua_gethlua(L); |
| 1210 | if (!hlua) |
| 1211 | return; |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1212 | |
| 1213 | /* Lua cannot yield when its returning from a function, |
| 1214 | * so, we can fix the interrupt hook to 1 instruction, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1215 | * expecting that the function is finished. |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1216 | */ |
| 1217 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1218 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1219 | return; |
| 1220 | } |
| 1221 | |
| 1222 | /* restore the interrupt condition. */ |
| 1223 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1224 | |
| 1225 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1226 | * If the state is not yieldable, trying yield causes an error. |
| 1227 | */ |
| 1228 | if (lua_isyieldable(L)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1229 | MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1230 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 1231 | /* If we cannot yield, update the clock and check the timeout. */ |
| 1232 | tv_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1233 | hlua->run_time += now_ms - hlua->start_time; |
| 1234 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1235 | lua_pushfstring(L, "execution timeout"); |
| 1236 | WILL_LJMP(lua_error(L)); |
| 1237 | } |
| 1238 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1239 | /* Update the start time. */ |
| 1240 | hlua->start_time = now_ms; |
| 1241 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1242 | /* Try to interrupt the process at the end of the current |
| 1243 | * unyieldable function. |
| 1244 | */ |
| 1245 | 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] | 1246 | } |
| 1247 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1248 | /* This function start or resumes the Lua stack execution. If the flag |
| 1249 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1250 | * The function return an error. |
| 1251 | * |
| 1252 | * The function can returns 4 values: |
| 1253 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1254 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1255 | * task wakeup. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1256 | * - 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] | 1257 | * the top of the stack. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1258 | * - HLUA_E_ERR : An error has occurred without error message. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1259 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1260 | * 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] | 1261 | * LUA code. |
| 1262 | */ |
| 1263 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1264 | { |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1265 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 1266 | int nres; |
| 1267 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1268 | int ret; |
| 1269 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1270 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1271 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1272 | /* Initialise run time counter. */ |
| 1273 | if (!HLUA_IS_RUNNING(lua)) |
| 1274 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1275 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1276 | /* Lock the whole Lua execution. This lock must be before the |
| 1277 | * label "resume_execution". |
| 1278 | */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1279 | if (lua->state_id == 0) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1280 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1281 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1282 | resume_execution: |
| 1283 | |
| 1284 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1285 | * instructions. it is used for preventing infinite loops. |
| 1286 | */ |
| 1287 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1288 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1289 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1290 | HLUA_SET_RUN(lua); |
| 1291 | HLUA_CLR_CTRLYIELD(lua); |
| 1292 | HLUA_CLR_WAKERESWR(lua); |
| 1293 | HLUA_CLR_WAKEREQWR(lua); |
Christopher Faulet | 1f43a34 | 2021-08-04 17:58:21 +0200 | [diff] [blame] | 1294 | HLUA_CLR_NOYIELD(lua); |
| 1295 | if (!yield_allowed) |
| 1296 | HLUA_SET_NOYIELD(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1297 | |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1298 | /* Update the start time and reset wake_time. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1299 | lua->start_time = now_ms; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1300 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1301 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1302 | /* Call the function. */ |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1303 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1304 | 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] | 1305 | #else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1306 | ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs); |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1307 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1308 | switch (ret) { |
| 1309 | |
| 1310 | case LUA_OK: |
| 1311 | ret = HLUA_E_OK; |
| 1312 | break; |
| 1313 | |
| 1314 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1315 | /* Check if the execution timeout is expired. It it is the case, we |
| 1316 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1317 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1318 | tv_update_date(0, 1); |
| 1319 | lua->run_time += now_ms - lua->start_time; |
| 1320 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1321 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1322 | ret = HLUA_E_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1323 | break; |
| 1324 | } |
| 1325 | /* Process the forced yield. if the general yield is not allowed or |
| 1326 | * if no task were associated this the current Lua execution |
| 1327 | * coroutine, we resume the execution. Else we want to return in the |
| 1328 | * scheduler and we want to be waked up again, to continue the |
| 1329 | * current Lua execution. So we schedule our own task. |
| 1330 | */ |
| 1331 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1332 | if (!yield_allowed || !lua->task) |
| 1333 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1334 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1335 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1336 | if (!yield_allowed) { |
| 1337 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1338 | ret = HLUA_E_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1339 | break; |
| 1340 | } |
| 1341 | ret = HLUA_E_AGAIN; |
| 1342 | break; |
| 1343 | |
| 1344 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1345 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1346 | /* Special exit case. The traditional exit is returned as an error |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1347 | * because the errors ares the only one mean to return immediately |
| 1348 | * from and lua execution. |
| 1349 | */ |
| 1350 | if (lua->flags & HLUA_EXIT) { |
| 1351 | ret = HLUA_E_OK; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 1352 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1353 | break; |
| 1354 | } |
| 1355 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1356 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1357 | if (!lua_checkstack(lua->T, 1)) { |
| 1358 | ret = HLUA_E_ERR; |
| 1359 | break; |
| 1360 | } |
| 1361 | msg = lua_tostring(lua->T, -1); |
| 1362 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1363 | lua_pop(lua->T, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 1364 | trace = hlua_traceback(lua->T, ", "); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1365 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1366 | 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] | 1367 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1368 | 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] | 1369 | ret = HLUA_E_ERRMSG; |
| 1370 | break; |
| 1371 | |
| 1372 | case LUA_ERRMEM: |
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 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1375 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1376 | break; |
| 1377 | |
| 1378 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1379 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1380 | if (!lua_checkstack(lua->T, 1)) { |
| 1381 | ret = HLUA_E_ERR; |
| 1382 | break; |
| 1383 | } |
| 1384 | msg = lua_tostring(lua->T, -1); |
| 1385 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1386 | lua_pop(lua->T, 1); |
| 1387 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1388 | 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] | 1389 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1390 | 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] | 1391 | ret = HLUA_E_ERRMSG; |
| 1392 | break; |
| 1393 | |
| 1394 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1395 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1396 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1397 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1398 | break; |
| 1399 | } |
| 1400 | |
| 1401 | switch (ret) { |
| 1402 | case HLUA_E_AGAIN: |
| 1403 | break; |
| 1404 | |
| 1405 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1406 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1407 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1408 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1409 | break; |
| 1410 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1411 | case HLUA_E_ETMOUT: |
| 1412 | case HLUA_E_NOMEM: |
| 1413 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1414 | case HLUA_E_ERR: |
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 | hlua_ctx_renew(lua, 0); |
| 1418 | break; |
| 1419 | |
| 1420 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1421 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1422 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1423 | break; |
| 1424 | } |
| 1425 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1426 | /* This is the main exit point, remove the Lua lock. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1427 | if (lua->state_id == 0) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1428 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1429 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1430 | return ret; |
| 1431 | } |
| 1432 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1433 | /* This function exit the current code. */ |
| 1434 | __LJMP static int hlua_done(lua_State *L) |
| 1435 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1436 | struct hlua *hlua; |
| 1437 | |
| 1438 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1439 | hlua = hlua_gethlua(L); |
| 1440 | if (!hlua) |
| 1441 | return 0; |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1442 | |
| 1443 | hlua->flags |= HLUA_EXIT; |
| 1444 | WILL_LJMP(lua_error(L)); |
| 1445 | |
| 1446 | return 0; |
| 1447 | } |
| 1448 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1449 | /* This function is an LUA binding. It provides a function |
| 1450 | * for deleting ACL from a referenced ACL file. |
| 1451 | */ |
| 1452 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1453 | { |
| 1454 | const char *name; |
| 1455 | const char *key; |
| 1456 | struct pat_ref *ref; |
| 1457 | |
| 1458 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1459 | |
| 1460 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1461 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1462 | |
| 1463 | ref = pat_ref_lookup(name); |
| 1464 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1465 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1466 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1467 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1468 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1469 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1470 | return 0; |
| 1471 | } |
| 1472 | |
| 1473 | /* This function is an LUA binding. It provides a function |
| 1474 | * for deleting map entry from a referenced map file. |
| 1475 | */ |
| 1476 | static int hlua_del_map(lua_State *L) |
| 1477 | { |
| 1478 | const char *name; |
| 1479 | const char *key; |
| 1480 | struct pat_ref *ref; |
| 1481 | |
| 1482 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1483 | |
| 1484 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1485 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1486 | |
| 1487 | ref = pat_ref_lookup(name); |
| 1488 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1489 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1490 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1491 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1492 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1493 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1494 | return 0; |
| 1495 | } |
| 1496 | |
| 1497 | /* This function is an LUA binding. It provides a function |
| 1498 | * for adding ACL pattern from a referenced ACL file. |
| 1499 | */ |
| 1500 | static int hlua_add_acl(lua_State *L) |
| 1501 | { |
| 1502 | const char *name; |
| 1503 | const char *key; |
| 1504 | struct pat_ref *ref; |
| 1505 | |
| 1506 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1507 | |
| 1508 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1509 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1510 | |
| 1511 | ref = pat_ref_lookup(name); |
| 1512 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1513 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1514 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1515 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1516 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1517 | pat_ref_add(ref, key, NULL, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1518 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1519 | return 0; |
| 1520 | } |
| 1521 | |
| 1522 | /* This function is an LUA binding. It provides a function |
| 1523 | * for setting map pattern and sample from a referenced map |
| 1524 | * file. |
| 1525 | */ |
| 1526 | static int hlua_set_map(lua_State *L) |
| 1527 | { |
| 1528 | const char *name; |
| 1529 | const char *key; |
| 1530 | const char *value; |
| 1531 | struct pat_ref *ref; |
| 1532 | |
| 1533 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1534 | |
| 1535 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1536 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1537 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1538 | |
| 1539 | ref = pat_ref_lookup(name); |
| 1540 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1541 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1542 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1543 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1544 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1545 | pat_ref_set(ref, key, value, NULL); |
| 1546 | else |
| 1547 | pat_ref_add(ref, key, value, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1548 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1549 | return 0; |
| 1550 | } |
| 1551 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1552 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1553 | * 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] | 1554 | * metatable have an original version registered in the global context with |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1555 | * the name of the object (_G[<name>] = <metable> ). |
| 1556 | * |
| 1557 | * A metable is a table that modify the standard behavior of a standard |
| 1558 | * access to the associated data. The entries of this new metatable are |
| 1559 | * defined as is: |
| 1560 | * |
| 1561 | * http://lua-users.org/wiki/MetatableEvents |
| 1562 | * |
| 1563 | * __index |
| 1564 | * |
| 1565 | * we access an absent field in a table, the result is nil. This is |
| 1566 | * true, but it is not the whole truth. Actually, such access triggers |
| 1567 | * the interpreter to look for an __index metamethod: If there is no |
| 1568 | * such method, as usually happens, then the access results in nil; |
| 1569 | * otherwise, the metamethod will provide the result. |
| 1570 | * |
| 1571 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1572 | * the key does not appear in the table, but the metatable has an __index |
| 1573 | * property: |
| 1574 | * |
| 1575 | * - if the value is a function, the function is called, passing in the |
| 1576 | * table and the key; the return value of that function is returned as |
| 1577 | * the result. |
| 1578 | * |
| 1579 | * - if the value is another table, the value of the key in that table is |
| 1580 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1581 | * table's metatable has an __index property, then it continues on up) |
| 1582 | * |
| 1583 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1584 | * |
| 1585 | * http://www.lua.org/pil/13.4.1.html |
| 1586 | * |
| 1587 | * __newindex |
| 1588 | * |
| 1589 | * Like __index, but control property assignment. |
| 1590 | * |
| 1591 | * __mode - Control weak references. A string value with one or both |
| 1592 | * of the characters 'k' and 'v' which specifies that the the |
| 1593 | * keys and/or values in the table are weak references. |
| 1594 | * |
| 1595 | * __call - Treat a table like a function. When a table is followed by |
| 1596 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1597 | * a __call key pointing to a function, that function is invoked |
| 1598 | * (passing any specified arguments) and the return value is |
| 1599 | * returned. |
| 1600 | * |
| 1601 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1602 | * called, if the metatable for myTable has a __metatable |
| 1603 | * key, the value of that key is returned instead of the |
| 1604 | * actual metatable. |
| 1605 | * |
| 1606 | * __tostring - Control string representation. When the builtin |
| 1607 | * "tostring( myTable )" function is called, if the metatable |
| 1608 | * for myTable has a __tostring property set to a function, |
| 1609 | * that function is invoked (passing myTable to it) and the |
| 1610 | * return value is used as the string representation. |
| 1611 | * |
| 1612 | * __len - Control table length. When the table length is requested using |
| 1613 | * the length operator ( '#' ), if the metatable for myTable has |
| 1614 | * a __len key pointing to a function, that function is invoked |
| 1615 | * (passing myTable to it) and the return value used as the value |
| 1616 | * of "#myTable". |
| 1617 | * |
| 1618 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1619 | * collected, if the metatable has a __gc field pointing to a |
| 1620 | * function, that function is first invoked, passing the userdata |
| 1621 | * to it. The __gc metamethod is not called for tables. |
| 1622 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1623 | * |
| 1624 | * Special metamethods for redefining standard operators: |
| 1625 | * http://www.lua.org/pil/13.1.html |
| 1626 | * |
| 1627 | * __add "+" |
| 1628 | * __sub "-" |
| 1629 | * __mul "*" |
| 1630 | * __div "/" |
| 1631 | * __unm "!" |
| 1632 | * __pow "^" |
| 1633 | * __concat ".." |
| 1634 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1635 | * Special methods for redefining standard relations |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1636 | * http://www.lua.org/pil/13.2.html |
| 1637 | * |
| 1638 | * __eq "==" |
| 1639 | * __lt "<" |
| 1640 | * __le "<=" |
| 1641 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1642 | |
| 1643 | /* |
| 1644 | * |
| 1645 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1646 | * Class Map |
| 1647 | * |
| 1648 | * |
| 1649 | */ |
| 1650 | |
| 1651 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1652 | * a class session, otherwise it throws an error. |
| 1653 | */ |
| 1654 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1655 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1656 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1657 | } |
| 1658 | |
| 1659 | /* This function is the map constructor. It don't need |
| 1660 | * the class Map object. It creates and return a new Map |
| 1661 | * object. It must be called only during "body" or "init" |
| 1662 | * context because it process some filesystem accesses. |
| 1663 | */ |
| 1664 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1665 | { |
| 1666 | const char *fn; |
| 1667 | int match = PAT_MATCH_STR; |
| 1668 | struct sample_conv conv; |
| 1669 | const char *file = ""; |
| 1670 | int line = 0; |
| 1671 | lua_Debug ar; |
| 1672 | char *err = NULL; |
| 1673 | struct arg args[2]; |
| 1674 | |
| 1675 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1676 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1677 | |
| 1678 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1679 | |
| 1680 | if (lua_gettop(L) >= 2) { |
| 1681 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1682 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1683 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1684 | } |
| 1685 | |
| 1686 | /* Get Lua filename and line number. */ |
| 1687 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1688 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1689 | if (ar.currentline > 0) { /* is there info? */ |
| 1690 | file = ar.short_src; |
| 1691 | line = ar.currentline; |
| 1692 | } |
| 1693 | } |
| 1694 | |
| 1695 | /* fill fake sample_conv struct. */ |
| 1696 | conv.kw = ""; /* unused. */ |
| 1697 | conv.process = NULL; /* unused. */ |
| 1698 | conv.arg_mask = 0; /* unused. */ |
| 1699 | conv.val_args = NULL; /* unused. */ |
| 1700 | conv.out_type = SMP_T_STR; |
| 1701 | conv.private = (void *)(long)match; |
| 1702 | switch (match) { |
| 1703 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1704 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1705 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1706 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1707 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1708 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1709 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1710 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1711 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1712 | default: |
| 1713 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1714 | } |
| 1715 | |
| 1716 | /* fill fake args. */ |
| 1717 | args[0].type = ARGT_STR; |
Christopher Faulet | 73292e9 | 2020-08-06 08:40:09 +0200 | [diff] [blame] | 1718 | args[0].data.str.area = strdup(fn); |
| 1719 | args[0].data.str.data = strlen(fn); |
| 1720 | args[0].data.str.size = args[0].data.str.data+1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1721 | args[1].type = ARGT_STOP; |
| 1722 | |
| 1723 | /* load the map. */ |
| 1724 | if (!sample_load_map(args, &conv, file, line, &err)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1725 | /* error case: we can't use luaL_error because we must |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1726 | * free the err variable. |
| 1727 | */ |
| 1728 | luaL_where(L, 1); |
| 1729 | lua_pushfstring(L, "'new': %s.", err); |
| 1730 | lua_concat(L, 2); |
| 1731 | free(err); |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 1732 | chunk_destroy(&args[0].data.str); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1733 | WILL_LJMP(lua_error(L)); |
| 1734 | } |
| 1735 | |
| 1736 | /* create the lua object. */ |
| 1737 | lua_newtable(L); |
| 1738 | lua_pushlightuserdata(L, args[0].data.map); |
| 1739 | lua_rawseti(L, -2, 0); |
| 1740 | |
| 1741 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1742 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1743 | lua_setmetatable(L, -2); |
| 1744 | |
| 1745 | |
| 1746 | return 1; |
| 1747 | } |
| 1748 | |
| 1749 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1750 | { |
| 1751 | struct map_descriptor *desc; |
| 1752 | struct pattern *pat; |
| 1753 | struct sample smp; |
| 1754 | |
| 1755 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1756 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1757 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1758 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1759 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1760 | } |
| 1761 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1762 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1763 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1764 | 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] | 1765 | smp.data.u.str.size = smp.data.u.str.data + 1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1766 | } |
| 1767 | |
| 1768 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1769 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1770 | if (str) |
| 1771 | lua_pushstring(L, ""); |
| 1772 | else |
| 1773 | lua_pushnil(L); |
| 1774 | return 1; |
| 1775 | } |
| 1776 | |
| 1777 | /* 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] | 1778 | 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] | 1779 | return 1; |
| 1780 | } |
| 1781 | |
| 1782 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1783 | { |
| 1784 | return _hlua_map_lookup(L, 0); |
| 1785 | } |
| 1786 | |
| 1787 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1788 | { |
| 1789 | return _hlua_map_lookup(L, 1); |
| 1790 | } |
| 1791 | |
| 1792 | /* |
| 1793 | * |
| 1794 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1795 | * Class Socket |
| 1796 | * |
| 1797 | * |
| 1798 | */ |
| 1799 | |
| 1800 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1801 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1802 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1803 | } |
| 1804 | |
| 1805 | /* 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] | 1806 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1807 | * received. |
| 1808 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1809 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1810 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1811 | struct stream_interface *si = appctx->owner; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1812 | |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1813 | if (appctx->ctx.hlua_cosocket.die) { |
| 1814 | si_shutw(si); |
| 1815 | si_shutr(si); |
| 1816 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1817 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1818 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1819 | stream_shutdown(si_strm(si), SF_ERR_KILLED); |
| 1820 | } |
| 1821 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1822 | /* If we can't write, wakeup the pending write signals. */ |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1823 | if (channel_output_closed(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1824 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1825 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1826 | /* If we can't read, wakeup the pending read signals. */ |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1827 | if (channel_input_closed(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1828 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1829 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1830 | /* if the connection is not established, inform the stream that we want |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1831 | * to be notified whenever the connection completes. |
| 1832 | */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1833 | if (si_opposite(si)->state < SI_ST_EST) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 1834 | si_cant_get(si); |
Willy Tarreau | 12c2423 | 2018-12-06 15:29:50 +0100 | [diff] [blame] | 1835 | si_rx_conn_blk(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1836 | si_rx_endp_more(si); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1837 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1838 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1839 | |
| 1840 | /* This function is called after the connect. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1841 | appctx->ctx.hlua_cosocket.connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1842 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1843 | /* 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] | 1844 | if (channel_may_recv(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1845 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1846 | |
| 1847 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1848 | if (!channel_is_empty(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1849 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1850 | |
| 1851 | /* Some data were injected in the buffer, notify the stream |
| 1852 | * interface. |
| 1853 | */ |
| 1854 | if (!channel_is_empty(si_ic(si))) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 1855 | si_update(si); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1856 | |
| 1857 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1858 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1859 | */ |
| 1860 | if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write)) |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1861 | si_rx_endp_more(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1862 | } |
| 1863 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1864 | /* This function is called when the "struct stream" is destroyed. |
| 1865 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1866 | * Wake all the pending signals. |
| 1867 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1868 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1869 | { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1870 | struct xref *peer; |
| 1871 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1872 | /* Remove my link in the original object. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1873 | peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref); |
| 1874 | if (peer) |
| 1875 | xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1876 | |
| 1877 | /* Wake all the task waiting for me. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1878 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1879 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1880 | } |
| 1881 | |
| 1882 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1883 | * uses this object. If the stream does not exists, just quit. |
| 1884 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1885 | * pending signal can rest in the read and write lists. destroy |
| 1886 | * it. |
| 1887 | */ |
| 1888 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1889 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1890 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1891 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1892 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1893 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1894 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 1895 | |
| 1896 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1897 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1898 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1899 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1900 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1901 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1902 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1903 | appctx->ctx.hlua_cosocket.die = 1; |
| 1904 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1905 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1906 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1907 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1908 | return 0; |
| 1909 | } |
| 1910 | |
| 1911 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1912 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1913 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1914 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1915 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1916 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1917 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1918 | struct xref *peer; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1919 | struct hlua *hlua; |
| 1920 | |
| 1921 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1922 | hlua = hlua_gethlua(L); |
| 1923 | if (!hlua) |
| 1924 | return 0; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1925 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1926 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1927 | |
| 1928 | /* Check if we run on the same thread than the xreator thread. |
| 1929 | * We cannot access to the socket if the thread is different. |
| 1930 | */ |
| 1931 | if (socket->tid != tid) |
| 1932 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1933 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1934 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1935 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1936 | return 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1937 | |
| 1938 | hlua->gc_count--; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1939 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1940 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1941 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1942 | appctx->ctx.hlua_cosocket.die = 1; |
| 1943 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1944 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1945 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1946 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1947 | return 0; |
| 1948 | } |
| 1949 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1950 | /* The close function calls close_helper. |
| 1951 | */ |
| 1952 | __LJMP static int hlua_socket_close(lua_State *L) |
| 1953 | { |
| 1954 | MAY_LJMP(check_args(L, 1, "close")); |
| 1955 | return hlua_socket_close_helper(L); |
| 1956 | } |
| 1957 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1958 | /* This Lua function assumes that the stack contain three parameters. |
| 1959 | * 1 - USERDATA containing a struct socket |
| 1960 | * 2 - INTEGER with values of the macro defined below |
| 1961 | * If the integer is -1, we must read at most one line. |
| 1962 | * If the integer is -2, we ust read all the data until the |
| 1963 | * end of the stream. |
| 1964 | * If the integer is positive value, we must read a number of |
| 1965 | * bytes corresponding to this value. |
| 1966 | */ |
| 1967 | #define HLSR_READ_LINE (-1) |
| 1968 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1969 | __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] | 1970 | { |
| 1971 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1972 | int wanted = lua_tointeger(L, 2); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1973 | struct hlua *hlua; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1974 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1975 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1976 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1977 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1978 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1979 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1980 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1981 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1982 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1983 | struct stream_interface *si; |
| 1984 | struct stream *s; |
| 1985 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1986 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1987 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1988 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1989 | hlua = hlua_gethlua(L); |
| 1990 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1991 | /* Check if this lua stack is schedulable. */ |
| 1992 | if (!hlua || !hlua->task) |
| 1993 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 1994 | "'frontend', 'backend' or 'task'")); |
| 1995 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1996 | /* Check if we run on the same thread than the xreator thread. |
| 1997 | * We cannot access to the socket if the thread is different. |
| 1998 | */ |
| 1999 | if (socket->tid != tid) |
| 2000 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2001 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2002 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2003 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2004 | if (!peer) |
| 2005 | goto no_peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2006 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2007 | si = appctx->owner; |
| 2008 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2009 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2010 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2011 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2012 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2013 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2014 | if (nblk < 0) /* Connection close. */ |
| 2015 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2016 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2017 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 2018 | |
| 2019 | /* remove final \r\n. */ |
| 2020 | if (nblk == 1) { |
| 2021 | if (blk1[len1-1] == '\n') { |
| 2022 | len1--; |
| 2023 | skip_at_end++; |
| 2024 | if (blk1[len1-1] == '\r') { |
| 2025 | len1--; |
| 2026 | skip_at_end++; |
| 2027 | } |
| 2028 | } |
| 2029 | } |
| 2030 | else { |
| 2031 | if (blk2[len2-1] == '\n') { |
| 2032 | len2--; |
| 2033 | skip_at_end++; |
| 2034 | if (blk2[len2-1] == '\r') { |
| 2035 | len2--; |
| 2036 | skip_at_end++; |
| 2037 | } |
| 2038 | } |
| 2039 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2040 | } |
| 2041 | |
| 2042 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2043 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2044 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2045 | if (nblk < 0) /* Connection close. */ |
| 2046 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2047 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2048 | goto connection_empty; |
| 2049 | } |
| 2050 | |
| 2051 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2052 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2053 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2054 | if (nblk < 0) /* Connection close. */ |
| 2055 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2056 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2057 | goto connection_empty; |
| 2058 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2059 | missing_bytes = wanted - socket->b.n; |
| 2060 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2061 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2062 | len1 = missing_bytes; |
| 2063 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 2064 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2065 | } |
| 2066 | |
| 2067 | len = len1; |
| 2068 | |
| 2069 | luaL_addlstring(&socket->b, blk1, len1); |
| 2070 | if (nblk == 2) { |
| 2071 | len += len2; |
| 2072 | luaL_addlstring(&socket->b, blk2, len2); |
| 2073 | } |
| 2074 | |
| 2075 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2076 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2077 | |
| 2078 | /* Don't wait anything. */ |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 2079 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2080 | |
| 2081 | /* If the pattern reclaim to read all the data |
| 2082 | * in the connection, got out. |
| 2083 | */ |
| 2084 | if (wanted == HLSR_READ_ALL) |
| 2085 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2086 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2087 | goto connection_empty; |
| 2088 | |
| 2089 | /* Return result. */ |
| 2090 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2091 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2092 | return 1; |
| 2093 | |
| 2094 | connection_closed: |
| 2095 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2096 | xref_unlock(&socket->xref, peer); |
| 2097 | |
| 2098 | no_peer: |
| 2099 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2100 | /* If the buffer containds data. */ |
| 2101 | if (socket->b.n > 0) { |
| 2102 | luaL_pushresult(&socket->b); |
| 2103 | return 1; |
| 2104 | } |
| 2105 | lua_pushnil(L); |
| 2106 | lua_pushstring(L, "connection closed."); |
| 2107 | return 2; |
| 2108 | |
| 2109 | connection_empty: |
| 2110 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2111 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) { |
| 2112 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2113 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2114 | } |
| 2115 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2116 | 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] | 2117 | return 0; |
| 2118 | } |
| 2119 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2120 | /* This Lua function gets two parameters. The first one can be string |
| 2121 | * or a number. If the string is "*l", the user requires one line. If |
| 2122 | * 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] | 2123 | * If the value is a number, the user require a number of bytes equal |
| 2124 | * to the value. The default value is "*l" (a line). |
| 2125 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2126 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2127 | * integer takes this values: |
| 2128 | * -1 : read a line |
| 2129 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2130 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2131 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2132 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2133 | * concatenated with the read data. |
| 2134 | */ |
| 2135 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 2136 | { |
| 2137 | int wanted = HLSR_READ_LINE; |
| 2138 | const char *pattern; |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2139 | int lastarg, type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2140 | char *error; |
| 2141 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2142 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2143 | |
| 2144 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 2145 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 2146 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2147 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2148 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2149 | /* Check if we run on the same thread than the xreator thread. |
| 2150 | * We cannot access to the socket if the thread is different. |
| 2151 | */ |
| 2152 | if (socket->tid != tid) |
| 2153 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2154 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2155 | /* check for pattern. */ |
| 2156 | if (lua_gettop(L) >= 2) { |
| 2157 | type = lua_type(L, 2); |
| 2158 | if (type == LUA_TSTRING) { |
| 2159 | pattern = lua_tostring(L, 2); |
| 2160 | if (strcmp(pattern, "*a") == 0) |
| 2161 | wanted = HLSR_READ_ALL; |
| 2162 | else if (strcmp(pattern, "*l") == 0) |
| 2163 | wanted = HLSR_READ_LINE; |
| 2164 | else { |
| 2165 | wanted = strtoll(pattern, &error, 10); |
| 2166 | if (*error != '\0') |
| 2167 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 2168 | } |
| 2169 | } |
| 2170 | else if (type == LUA_TNUMBER) { |
| 2171 | wanted = lua_tointeger(L, 2); |
| 2172 | if (wanted < 0) |
| 2173 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 2174 | } |
| 2175 | } |
| 2176 | |
| 2177 | /* Set pattern. */ |
| 2178 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 2179 | |
| 2180 | /* Check if we would replace the top by itself. */ |
| 2181 | if (lua_gettop(L) != 2) |
| 2182 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2183 | |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2184 | /* Save index of the top of the stack because since buffers are used, it |
| 2185 | * may change |
| 2186 | */ |
| 2187 | lastarg = lua_gettop(L); |
| 2188 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2189 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2190 | luaL_buffinit(L, &socket->b); |
| 2191 | |
| 2192 | /* Check prefix. */ |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2193 | if (lastarg >= 3) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2194 | if (lua_type(L, 3) != LUA_TSTRING) |
| 2195 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 2196 | pattern = lua_tolstring(L, 3, &len); |
| 2197 | luaL_addlstring(&socket->b, pattern, len); |
| 2198 | } |
| 2199 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2200 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2201 | } |
| 2202 | |
| 2203 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 2204 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2205 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2206 | 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] | 2207 | { |
| 2208 | struct hlua_socket *socket; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2209 | struct hlua *hlua; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2210 | struct appctx *appctx; |
| 2211 | size_t buf_len; |
| 2212 | const char *buf; |
| 2213 | int len; |
| 2214 | int send_len; |
| 2215 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2216 | struct xref *peer; |
| 2217 | struct stream_interface *si; |
| 2218 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2219 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2220 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2221 | hlua = hlua_gethlua(L); |
| 2222 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2223 | /* Check if this lua stack is schedulable. */ |
| 2224 | if (!hlua || !hlua->task) |
| 2225 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 2226 | "'frontend', 'backend' or 'task'")); |
| 2227 | |
| 2228 | /* Get object */ |
| 2229 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2230 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2231 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2232 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2233 | /* Check if we run on the same thread than the xreator thread. |
| 2234 | * We cannot access to the socket if the thread is different. |
| 2235 | */ |
| 2236 | if (socket->tid != tid) |
| 2237 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2238 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2239 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2240 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2241 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2242 | lua_pushinteger(L, -1); |
| 2243 | return 1; |
| 2244 | } |
| 2245 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2246 | si = appctx->owner; |
| 2247 | s = si_strm(si); |
| 2248 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2249 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2250 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2251 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2252 | lua_pushinteger(L, -1); |
| 2253 | return 1; |
| 2254 | } |
| 2255 | |
| 2256 | /* Update the input buffer data. */ |
| 2257 | buf += sent; |
| 2258 | send_len = buf_len - sent; |
| 2259 | |
| 2260 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2261 | if (sent >= buf_len) { |
| 2262 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2263 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2264 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2265 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2266 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2267 | * the request buffer if its not required. |
| 2268 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2269 | if (s->req.buf.size == 0) { |
Willy Tarreau | 581abd3 | 2018-10-25 10:21:41 +0200 | [diff] [blame] | 2270 | if (!si_alloc_ibuf(si, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2271 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2272 | } |
| 2273 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2274 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2275 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2276 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2277 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2278 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2279 | |
| 2280 | /* send data */ |
| 2281 | if (len < send_len) |
| 2282 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2283 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2284 | |
| 2285 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2286 | * the data" (-2) are not expected because the available length |
| 2287 | * is tested. |
| 2288 | * Other unknown error are also not expected. |
| 2289 | */ |
| 2290 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2291 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2292 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2293 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2294 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2295 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2296 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2297 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2298 | return 1; |
| 2299 | } |
| 2300 | |
| 2301 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2302 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2303 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2304 | s->req.rex = TICK_ETERNITY; |
| 2305 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2306 | |
| 2307 | /* Update length sent. */ |
| 2308 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2309 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2310 | |
| 2311 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2312 | if (sent + len >= buf_len) { |
| 2313 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2314 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2315 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2316 | |
| 2317 | hlua_socket_write_yield_return: |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2318 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2319 | xref_unlock(&socket->xref, peer); |
| 2320 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2321 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2322 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2323 | 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] | 2324 | return 0; |
| 2325 | } |
| 2326 | |
| 2327 | /* This function initiate the send of data. It just check the input |
| 2328 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2329 | * 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] | 2330 | * "hlua_socket_write_yield" that can yield. |
| 2331 | * |
| 2332 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2333 | * the associated object. The second is a string buffer. The third is |
| 2334 | * a facultative integer that represents where is the buffer position |
| 2335 | * of the start of the data that can send. The first byte is the |
| 2336 | * position "1". The default value is "1". The fourth argument is a |
| 2337 | * facultative integer that represents where is the buffer position |
| 2338 | * of the end of the data that can send. The default is the last byte. |
| 2339 | */ |
| 2340 | static int hlua_socket_send(struct lua_State *L) |
| 2341 | { |
| 2342 | int i; |
| 2343 | int j; |
| 2344 | const char *buf; |
| 2345 | size_t buf_len; |
| 2346 | |
| 2347 | /* Check number of arguments. */ |
| 2348 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2349 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2350 | |
| 2351 | /* Get the string. */ |
| 2352 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2353 | |
| 2354 | /* Get and check j. */ |
| 2355 | if (lua_gettop(L) == 4) { |
| 2356 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2357 | if (j < 0) |
| 2358 | j = buf_len + j + 1; |
| 2359 | if (j > buf_len) |
| 2360 | j = buf_len + 1; |
| 2361 | lua_pop(L, 1); |
| 2362 | } |
| 2363 | else |
| 2364 | j = buf_len; |
| 2365 | |
| 2366 | /* Get and check i. */ |
| 2367 | if (lua_gettop(L) == 3) { |
| 2368 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2369 | if (i < 0) |
| 2370 | i = buf_len + i + 1; |
| 2371 | if (i > buf_len) |
| 2372 | i = buf_len + 1; |
| 2373 | lua_pop(L, 1); |
| 2374 | } else |
| 2375 | i = 1; |
| 2376 | |
| 2377 | /* Check bth i and j. */ |
| 2378 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2379 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2380 | return 1; |
| 2381 | } |
| 2382 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2383 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2384 | return 1; |
| 2385 | } |
| 2386 | if (i == 0) |
| 2387 | i = 1; |
| 2388 | if (j == 0) |
| 2389 | j = 1; |
| 2390 | |
| 2391 | /* Pop the string. */ |
| 2392 | lua_pop(L, 1); |
| 2393 | |
| 2394 | /* Update the buffer length. */ |
| 2395 | buf += i - 1; |
| 2396 | buf_len = j - i + 1; |
| 2397 | lua_pushlstring(L, buf, buf_len); |
| 2398 | |
| 2399 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2400 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2401 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2402 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2403 | } |
| 2404 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2405 | #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] | 2406 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 2407 | { |
| 2408 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2409 | int ret; |
| 2410 | int len; |
| 2411 | char *p; |
| 2412 | |
| 2413 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2414 | if (ret <= 0) { |
| 2415 | lua_pushnil(L); |
| 2416 | return 1; |
| 2417 | } |
| 2418 | |
| 2419 | if (ret == AF_UNIX) { |
| 2420 | lua_pushstring(L, buffer+1); |
| 2421 | return 1; |
| 2422 | } |
| 2423 | else if (ret == AF_INET6) { |
| 2424 | buffer[0] = '['; |
| 2425 | len = strlen(buffer); |
| 2426 | buffer[len] = ']'; |
| 2427 | len++; |
| 2428 | buffer[len] = ':'; |
| 2429 | len++; |
| 2430 | p = buffer; |
| 2431 | } |
| 2432 | else if (ret == AF_INET) { |
| 2433 | p = buffer + 1; |
| 2434 | len = strlen(p); |
| 2435 | p[len] = ':'; |
| 2436 | len++; |
| 2437 | } |
| 2438 | else { |
| 2439 | lua_pushnil(L); |
| 2440 | return 1; |
| 2441 | } |
| 2442 | |
| 2443 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2444 | lua_pushnil(L); |
| 2445 | return 1; |
| 2446 | } |
| 2447 | |
| 2448 | lua_pushstring(L, p); |
| 2449 | return 1; |
| 2450 | } |
| 2451 | |
| 2452 | /* Returns information about the peer of the connection. */ |
| 2453 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2454 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2455 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2456 | struct xref *peer; |
| 2457 | struct appctx *appctx; |
| 2458 | struct stream_interface *si; |
| 2459 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2460 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2461 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2462 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2463 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2464 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2465 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2466 | /* Check if we run on the same thread than the xreator thread. |
| 2467 | * We cannot access to the socket if the thread is different. |
| 2468 | */ |
| 2469 | if (socket->tid != tid) |
| 2470 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2471 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2472 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2473 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2474 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2475 | lua_pushnil(L); |
| 2476 | return 1; |
| 2477 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2478 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2479 | si = appctx->owner; |
| 2480 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2481 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2482 | if (!s->target_addr) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2483 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2484 | lua_pushnil(L); |
| 2485 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2486 | } |
| 2487 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2488 | ret = MAY_LJMP(hlua_socket_info(L, s->target_addr)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2489 | xref_unlock(&socket->xref, peer); |
| 2490 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2491 | } |
| 2492 | |
| 2493 | /* Returns information about my connection side. */ |
| 2494 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2495 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2496 | struct hlua_socket *socket; |
| 2497 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2498 | struct appctx *appctx; |
| 2499 | struct xref *peer; |
| 2500 | struct stream_interface *si; |
| 2501 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2502 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2503 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2504 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2505 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2506 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2507 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2508 | /* Check if we run on the same thread than the xreator thread. |
| 2509 | * We cannot access to the socket if the thread is different. |
| 2510 | */ |
| 2511 | if (socket->tid != tid) |
| 2512 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2513 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2514 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2515 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2516 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2517 | lua_pushnil(L); |
| 2518 | return 1; |
| 2519 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2520 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2521 | si = appctx->owner; |
| 2522 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2523 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2524 | conn = cs_conn(objt_cs(s->si[1].end)); |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2525 | if (!conn || !conn_get_src(conn)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2526 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2527 | lua_pushnil(L); |
| 2528 | return 1; |
| 2529 | } |
| 2530 | |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2531 | ret = hlua_socket_info(L, conn->src); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2532 | xref_unlock(&socket->xref, peer); |
| 2533 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2534 | } |
| 2535 | |
| 2536 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2537 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2538 | .obj_type = OBJ_TYPE_APPLET, |
| 2539 | .name = "<LUA_TCP>", |
| 2540 | .fct = hlua_socket_handler, |
| 2541 | .release = hlua_socket_release, |
| 2542 | }; |
| 2543 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2544 | __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] | 2545 | { |
| 2546 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2547 | struct hlua *hlua; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2548 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2549 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2550 | struct stream_interface *si; |
| 2551 | struct stream *s; |
| 2552 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2553 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2554 | hlua = hlua_gethlua(L); |
| 2555 | if (!hlua) |
| 2556 | return 0; |
| 2557 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2558 | /* Check if we run on the same thread than the xreator thread. |
| 2559 | * We cannot access to the socket if the thread is different. |
| 2560 | */ |
| 2561 | if (socket->tid != tid) |
| 2562 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2563 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2564 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2565 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2566 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2567 | lua_pushnil(L); |
| 2568 | lua_pushstring(L, "Can't connect"); |
| 2569 | return 2; |
| 2570 | } |
| 2571 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2572 | si = appctx->owner; |
| 2573 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2574 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2575 | /* Check if we run on the same thread than the xreator thread. |
| 2576 | * We cannot access to the socket if the thread is different. |
| 2577 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2578 | if (socket->tid != tid) { |
| 2579 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2580 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2581 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2582 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2583 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2584 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2585 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2586 | lua_pushnil(L); |
| 2587 | lua_pushstring(L, "Can't connect"); |
| 2588 | return 2; |
| 2589 | } |
| 2590 | |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2591 | appctx = __objt_appctx(s->si[0].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2592 | |
| 2593 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2594 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2595 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2596 | lua_pushinteger(L, 1); |
| 2597 | return 1; |
| 2598 | } |
| 2599 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2600 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2601 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2602 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2603 | } |
| 2604 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2605 | 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] | 2606 | return 0; |
| 2607 | } |
| 2608 | |
| 2609 | /* This function fail or initite the connection. */ |
| 2610 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2611 | { |
| 2612 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2613 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2614 | const char *ip; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2615 | struct hlua *hlua; |
| 2616 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2617 | int low, high; |
| 2618 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2619 | struct xref *peer; |
| 2620 | struct stream_interface *si; |
| 2621 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2622 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2623 | if (lua_gettop(L) < 2) |
| 2624 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2625 | |
| 2626 | /* Get args. */ |
| 2627 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2628 | |
| 2629 | /* Check if we run on the same thread than the xreator thread. |
| 2630 | * We cannot access to the socket if the thread is different. |
| 2631 | */ |
| 2632 | if (socket->tid != tid) |
| 2633 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2634 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2635 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2636 | if (lua_gettop(L) >= 3) { |
| 2637 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2638 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2639 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2640 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2641 | * that are not enclosed within square brackets. |
| 2642 | */ |
| 2643 | if (port > 0) { |
| 2644 | luaL_buffinit(L, &b); |
| 2645 | luaL_addstring(&b, ip); |
| 2646 | luaL_addchar(&b, ':'); |
| 2647 | luaL_pushresult(&b); |
| 2648 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2649 | } |
| 2650 | } |
| 2651 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2652 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2653 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2654 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2655 | lua_pushnil(L); |
| 2656 | return 1; |
| 2657 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2658 | |
| 2659 | /* Parse ip address. */ |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 2660 | 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] | 2661 | if (!addr) { |
| 2662 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2663 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2664 | } |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2665 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2666 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2667 | if (low == 0) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2668 | if (addr->ss_family == AF_INET) { |
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_in *)addr)->sin_port = htons(port); |
| 2674 | } else if (addr->ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2675 | if (port == -1) { |
| 2676 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2677 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2678 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2679 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(port); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2680 | } |
| 2681 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2682 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2683 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2684 | si = appctx->owner; |
| 2685 | s = si_strm(si); |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2686 | |
Willy Tarreau | 9b7587a | 2020-10-15 07:32:10 +0200 | [diff] [blame] | 2687 | if (!sockaddr_alloc(&s->target_addr, addr, sizeof(*addr))) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2688 | xref_unlock(&socket->xref, peer); |
| 2689 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
| 2690 | } |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2691 | s->flags |= SF_ADDR_SET; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2692 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2693 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2694 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2695 | if (!hlua) |
| 2696 | return 0; |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2697 | |
| 2698 | /* inform the stream that we want to be notified whenever the |
| 2699 | * connection completes. |
| 2700 | */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 2701 | si_cant_get(&s->si[0]); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2702 | si_rx_endp_more(&s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2703 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2704 | |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2705 | hlua->gc_count++; |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2706 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2707 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2708 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2709 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2710 | } |
| 2711 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2712 | |
| 2713 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2714 | /* Return yield waiting for connection. */ |
| 2715 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2716 | 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] | 2717 | |
| 2718 | return 0; |
| 2719 | } |
| 2720 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2721 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2722 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2723 | { |
| 2724 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2725 | struct xref *peer; |
| 2726 | struct appctx *appctx; |
| 2727 | struct stream_interface *si; |
| 2728 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2729 | |
| 2730 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2731 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2732 | |
| 2733 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2734 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2735 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2736 | lua_pushnil(L); |
| 2737 | return 1; |
| 2738 | } |
| 2739 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2740 | si = appctx->owner; |
| 2741 | s = si_strm(si); |
| 2742 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 2743 | s->target = &socket_ssl->obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2744 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2745 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2746 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2747 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2748 | |
| 2749 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2750 | { |
| 2751 | return 0; |
| 2752 | } |
| 2753 | |
| 2754 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2755 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2756 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2757 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2758 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2759 | struct xref *peer; |
| 2760 | struct appctx *appctx; |
| 2761 | struct stream_interface *si; |
| 2762 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2763 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2764 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2765 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2766 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2767 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2768 | /* convert the timeout to millis */ |
| 2769 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2770 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2771 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2772 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2773 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2774 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2775 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2776 | 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] | 2777 | |
| 2778 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2779 | if (tmout == 0) |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2780 | tmout++; /* very small timeouts are adjusted to a minimum of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2781 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2782 | /* Check if we run on the same thread than the xreator thread. |
| 2783 | * We cannot access to the socket if the thread is different. |
| 2784 | */ |
| 2785 | if (socket->tid != tid) |
| 2786 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2787 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2788 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2789 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2790 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2791 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2792 | WILL_LJMP(lua_error(L)); |
| 2793 | return 0; |
| 2794 | } |
| 2795 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2796 | si = appctx->owner; |
| 2797 | s = si_strm(si); |
| 2798 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2799 | s->sess->fe->timeout.connect = tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2800 | s->req.rto = tmout; |
| 2801 | s->req.wto = tmout; |
| 2802 | s->res.rto = tmout; |
| 2803 | s->res.wto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2804 | s->req.rex = tick_add_ifset(now_ms, tmout); |
| 2805 | s->req.wex = tick_add_ifset(now_ms, tmout); |
| 2806 | s->res.rex = tick_add_ifset(now_ms, tmout); |
| 2807 | s->res.wex = tick_add_ifset(now_ms, tmout); |
| 2808 | |
| 2809 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 2810 | task_queue(s->task); |
| 2811 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2812 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2813 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 2814 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 2815 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2816 | } |
| 2817 | |
| 2818 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2819 | { |
| 2820 | struct hlua_socket *socket; |
| 2821 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2822 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2823 | struct stream *strm; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2824 | |
| 2825 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2826 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2827 | hlua_pusherror(L, "socket: full stack"); |
| 2828 | goto out_fail_conf; |
| 2829 | } |
| 2830 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2831 | /* Create the object: obj[0] = userdata. */ |
| 2832 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2833 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2834 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2835 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2836 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2837 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2838 | /* Check if the various memory pools are initialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2839 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2840 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2841 | goto out_fail_conf; |
| 2842 | } |
| 2843 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2844 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2845 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2846 | lua_setmetatable(L, -2); |
| 2847 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2848 | /* Create the applet context */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 2849 | appctx = appctx_new(&update_applet, tid_bit); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2850 | if (!appctx) { |
| 2851 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2852 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2853 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2854 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2855 | appctx->ctx.hlua_cosocket.connected = 0; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2856 | appctx->ctx.hlua_cosocket.die = 0; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2857 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 2858 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2859 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2860 | /* Now create a session, task and stream for this applet */ |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 2861 | sess = session_new(socket_proxy, NULL, &appctx->obj_type); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2862 | if (!sess) { |
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_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2865 | } |
| 2866 | |
Christopher Faulet | 26256f8 | 2020-09-14 11:40:13 +0200 | [diff] [blame] | 2867 | strm = stream_new(sess, &appctx->obj_type, &BUF_NULL); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2868 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2869 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2870 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2871 | } |
| 2872 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2873 | /* Initialise cross reference between stream and Lua socket object. */ |
| 2874 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2875 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2876 | /* Configure "right" stream interface. this "si" is used to connect |
| 2877 | * and retrieve data from the server. The connection is initialized |
| 2878 | * with the "struct server". |
| 2879 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2880 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2881 | |
| 2882 | /* Force destination server. */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2883 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED; |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 2884 | strm->target = &socket_tcp->obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2885 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2886 | return 1; |
| 2887 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2888 | out_fail_stream: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 2889 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2890 | out_fail_sess: |
| 2891 | appctx_free(appctx); |
| 2892 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2893 | WILL_LJMP(lua_error(L)); |
| 2894 | return 0; |
| 2895 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2896 | |
| 2897 | /* |
| 2898 | * |
| 2899 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2900 | * Class Channel |
| 2901 | * |
| 2902 | * |
| 2903 | */ |
| 2904 | |
| 2905 | /* Returns the struct hlua_channel join to the class channel in the |
| 2906 | * stack entry "ud" or throws an argument error. |
| 2907 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2908 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2909 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2910 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2911 | } |
| 2912 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2913 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 2914 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2915 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 2916 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2917 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2918 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2919 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2920 | return 0; |
| 2921 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2922 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2923 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2924 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2925 | |
| 2926 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 2927 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 2928 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2929 | return 1; |
| 2930 | } |
| 2931 | |
| 2932 | /* Duplicate all the data present in the input channel and put it |
| 2933 | * in a string LUA variables. Returns -1 and push a nil value in |
| 2934 | * the stack if the channel is closed and all the data are consumed, |
| 2935 | * returns 0 if no data are available, otherwise it returns the length |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 2936 | * of the built string. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2937 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2938 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2939 | { |
| 2940 | char *blk1; |
| 2941 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2942 | size_t len1; |
| 2943 | size_t len2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2944 | int ret; |
| 2945 | luaL_Buffer b; |
| 2946 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2947 | ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2); |
Christopher Faulet | 2e60aa4 | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 2948 | if (unlikely(ret <= 0)) { |
| 2949 | if (ret < 0 || HLUA_CANT_YIELD(hlua_gethlua(L))) { |
| 2950 | lua_pushnil(L); |
| 2951 | return -1; |
| 2952 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2953 | return 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2954 | } |
| 2955 | |
| 2956 | luaL_buffinit(L, &b); |
| 2957 | luaL_addlstring(&b, blk1, len1); |
| 2958 | if (unlikely(ret == 2)) |
| 2959 | luaL_addlstring(&b, blk2, len2); |
| 2960 | luaL_pushresult(&b); |
| 2961 | |
| 2962 | if (unlikely(ret == 2)) |
| 2963 | return len1 + len2; |
| 2964 | return len1; |
| 2965 | } |
| 2966 | |
| 2967 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2968 | * a yield. This function keep the data in the buffer. |
| 2969 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2970 | __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] | 2971 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2972 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2973 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2974 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2975 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 2976 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2977 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2978 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2979 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2980 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2981 | if (_hlua_channel_dup(chn, L) == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2982 | 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] | 2983 | return 1; |
| 2984 | } |
| 2985 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2986 | /* Check arguments for the function "hlua_channel_dup_yield". */ |
| 2987 | __LJMP static int hlua_channel_dup(lua_State *L) |
| 2988 | { |
| 2989 | MAY_LJMP(check_args(L, 1, "dup")); |
| 2990 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2991 | return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0)); |
| 2992 | } |
| 2993 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2994 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2995 | * a yield. This function consumes the data in the buffer. It returns |
| 2996 | * a string containing the data or a nil pointer if no data are available |
| 2997 | * and the channel is closed. |
| 2998 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2999 | __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] | 3000 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3001 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3002 | int ret; |
| 3003 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3004 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3005 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3006 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3007 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3008 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3009 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3010 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3011 | ret = _hlua_channel_dup(chn, L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3012 | if (unlikely(ret == 0)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3013 | 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] | 3014 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3015 | if (unlikely(ret == -1)) |
| 3016 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3017 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3018 | b_sub(&chn->buf, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3019 | return 1; |
| 3020 | } |
| 3021 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3022 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3023 | __LJMP static int hlua_channel_get(lua_State *L) |
| 3024 | { |
| 3025 | MAY_LJMP(check_args(L, 1, "get")); |
| 3026 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3027 | return MAY_LJMP(hlua_channel_get_yield(L, 0, 0)); |
| 3028 | } |
| 3029 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3030 | /* This functions consumes and returns one line. If the channel is closed, |
| 3031 | * 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] | 3032 | * 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] | 3033 | * value. |
| 3034 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3035 | __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] | 3036 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3037 | char *blk1; |
| 3038 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3039 | size_t len1; |
| 3040 | size_t len2; |
| 3041 | size_t len; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3042 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3043 | int ret; |
| 3044 | luaL_Buffer b; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3045 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3046 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3047 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3048 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3049 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3050 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3051 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3052 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3053 | ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2); |
Christopher Faulet | 2e60aa4 | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3054 | if (ret == 0) { |
| 3055 | if (HLUA_CANT_YIELD(hlua_gethlua(L))) { |
| 3056 | _hlua_channel_dup(chn, L); |
| 3057 | return 1; |
| 3058 | } |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3059 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0)); |
Christopher Faulet | 2e60aa4 | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3060 | } |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3061 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3062 | if (ret == -1) { |
| 3063 | lua_pushnil(L); |
| 3064 | return 1; |
| 3065 | } |
| 3066 | |
| 3067 | luaL_buffinit(L, &b); |
| 3068 | luaL_addlstring(&b, blk1, len1); |
| 3069 | len = len1; |
| 3070 | if (unlikely(ret == 2)) { |
| 3071 | luaL_addlstring(&b, blk2, len2); |
| 3072 | len += len2; |
| 3073 | } |
| 3074 | luaL_pushresult(&b); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3075 | 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] | 3076 | return 1; |
| 3077 | } |
| 3078 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3079 | /* Check arguments for the function "hlua_channel_getline_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3080 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 3081 | { |
| 3082 | MAY_LJMP(check_args(L, 1, "getline")); |
| 3083 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3084 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 3085 | } |
| 3086 | |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3087 | /* This function takes a string as argument, and append it at the input side of |
| 3088 | * channel. If data cannot be copied, because there is not enough space to do so |
| 3089 | * or because the channel is closed, it returns -1. Otherwise, it returns the |
| 3090 | * amount of data written (the string length). This function does not yield. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3091 | */ |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3092 | __LJMP static int hlua_channel_append(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3093 | { |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3094 | struct channel *chn; |
| 3095 | const char *str; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3096 | size_t len; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3097 | |
| 3098 | MAY_LJMP(check_args(L, 2, "append")); |
| 3099 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3100 | str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3101 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3102 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3103 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3104 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3105 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3106 | |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3107 | if (len > c_room(chn) || ci_putblk(chn, str, len) < 0) |
| 3108 | lua_pushinteger(L, -1); |
| 3109 | else |
| 3110 | lua_pushinteger(L, len); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3111 | return 1; |
| 3112 | } |
| 3113 | |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3114 | /* The function replaces input data from the channel by the string passed as |
| 3115 | * argument. Before clearing the buffer, we take care the copy will be |
| 3116 | * possible. It returns the amount of data written (the string length) on |
| 3117 | * success or -1 if the copy is not performed. This function does not yield. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3118 | */ |
| 3119 | __LJMP static int hlua_channel_set(lua_State *L) |
| 3120 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3121 | struct channel *chn; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3122 | const char *str; |
| 3123 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3124 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3125 | MAY_LJMP(check_args(L, 2, "set")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3126 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3127 | str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3128 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3129 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3130 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3131 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3132 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3133 | |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3134 | /* Be sure we can copied the string once input data will be removed. */ |
| 3135 | if (len > c_room(chn) + ci_data(chn) || channel_input_closed(chn)) |
| 3136 | lua_pushinteger(L, -1); |
| 3137 | else { |
| 3138 | b_set_data(&chn->buf, co_data(chn)); |
| 3139 | if (ci_putblk(chn, str, len) < 0) |
| 3140 | lua_pushinteger(L, -1); |
| 3141 | else |
| 3142 | lua_pushinteger(L, len); |
| 3143 | } |
| 3144 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3145 | } |
| 3146 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3147 | /* Append data in the output side of the buffer. This data is immediately |
| 3148 | * sent. The function returns the amount of data written. If the buffer |
| 3149 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3150 | * if the channel is closed. |
| 3151 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3152 | __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] | 3153 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3154 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3155 | size_t len; |
| 3156 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3157 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3158 | int max; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3159 | struct hlua *hlua; |
| 3160 | |
| 3161 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3162 | hlua = hlua_gethlua(L); |
| 3163 | if (!hlua) { |
| 3164 | lua_pushnil(L); |
| 3165 | return 1; |
| 3166 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3167 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3168 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3169 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3170 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3171 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3172 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3173 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3174 | lua_pushinteger(L, -1); |
| 3175 | return 1; |
| 3176 | } |
| 3177 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3178 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 3179 | * the request buffer if its not required. |
| 3180 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3181 | if (chn->buf.size == 0) { |
Christopher Faulet | 2e60aa4 | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3182 | if (HLUA_CANT_YIELD(hlua_gethlua(L))) |
| 3183 | return 1; |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 3184 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3185 | 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] | 3186 | } |
| 3187 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3188 | /* The written data will be immediately sent, so we can check |
| 3189 | * the available space without taking in account the reserve. |
| 3190 | * The reserve is guaranteed for the processing of incoming |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3191 | * data, because the buffer will be flushed. |
| 3192 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3193 | max = b_room(&chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3194 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3195 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3196 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3197 | * we return the amount of copied data. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3198 | */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3199 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3200 | return 1; |
| 3201 | |
| 3202 | /* Adjust the real required length. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3203 | if (max > len - l) |
| 3204 | max = len - l; |
| 3205 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3206 | /* The buffer available size may be not contiguous. This test |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3207 | * detects a non contiguous buffer and realign it. |
| 3208 | */ |
Willy Tarreau | 3f67999 | 2018-06-15 15:06:42 +0200 | [diff] [blame] | 3209 | if (ci_space_for_replace(chn) < max) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3210 | channel_slow_realign(chn, trash.area); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3211 | |
| 3212 | /* Copy input data in the buffer. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3213 | 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] | 3214 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3215 | /* buffer replace considers that the input part is filled. |
| 3216 | * so, I must forward these new data in the output part. |
| 3217 | */ |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 3218 | c_adv(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3219 | |
| 3220 | l += max; |
| 3221 | lua_pop(L, 1); |
| 3222 | lua_pushinteger(L, l); |
| 3223 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3224 | if (l < len) { |
Christopher Faulet | 2e60aa4 | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3225 | /* If there is no space available, and the output buffer is empty. |
| 3226 | * in this case, we cannot add more data, so we cannot yield, |
| 3227 | * we return the amount of copied data. |
| 3228 | */ |
| 3229 | max = b_room(&chn->buf); |
| 3230 | if ((max == 0 && co_data(chn) == 0) || HLUA_CANT_YIELD(hlua_gethlua(L))) |
| 3231 | return 1; |
| 3232 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3233 | /* If we are waiting for space in the response buffer, we |
| 3234 | * must set the flag WAKERESWR. This flag required the task |
| 3235 | * wake up if any activity is detected on the response buffer. |
| 3236 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3237 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3238 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3239 | else |
| 3240 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3241 | 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] | 3242 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3243 | |
| 3244 | return 1; |
| 3245 | } |
| 3246 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3247 | /* Just a wrapper of "_hlua_channel_send". This wrapper permits |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3248 | * yield the LUA process, and resume it without checking the |
| 3249 | * input arguments. |
| 3250 | */ |
| 3251 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3252 | { |
| 3253 | MAY_LJMP(check_args(L, 2, "send")); |
| 3254 | lua_pushinteger(L, 0); |
| 3255 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3256 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3257 | } |
| 3258 | |
| 3259 | /* This function forward and amount of butes. The data pass from |
| 3260 | * the input side of the buffer to the output side, and can be |
| 3261 | * forwarded. This function never fails. |
| 3262 | * |
| 3263 | * The Lua function takes an amount of bytes to be forwarded in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3264 | * input. It returns the number of bytes forwarded. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3265 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3266 | __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] | 3267 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3268 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3269 | int len; |
| 3270 | int l; |
| 3271 | int max; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3272 | struct hlua *hlua; |
| 3273 | |
| 3274 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3275 | hlua = hlua_gethlua(L); |
| 3276 | if (!hlua) |
| 3277 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3278 | |
| 3279 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3280 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3281 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3282 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3283 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3284 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3285 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3286 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3287 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3288 | |
| 3289 | max = len - l; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3290 | if (max > ci_data(chn)) |
| 3291 | max = ci_data(chn); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3292 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3293 | l += max; |
| 3294 | |
| 3295 | lua_pop(L, 1); |
| 3296 | lua_pushinteger(L, l); |
| 3297 | |
| 3298 | /* Check if it miss bytes to forward. */ |
| 3299 | if (l < len) { |
| 3300 | /* The the input channel or the output channel are closed, we |
| 3301 | * must return the amount of data forwarded. |
| 3302 | */ |
Christopher Faulet | 2e60aa4 | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3303 | if (channel_input_closed(chn) || channel_output_closed(chn) || HLUA_CANT_YIELD(hlua_gethlua(L))) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3304 | return 1; |
| 3305 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3306 | /* If we are waiting for space data in the response buffer, we |
| 3307 | * must set the flag WAKERESWR. This flag required the task |
| 3308 | * wake up if any activity is detected on the response buffer. |
| 3309 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3310 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3311 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3312 | else |
| 3313 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3314 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3315 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3316 | 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] | 3317 | } |
| 3318 | |
| 3319 | return 1; |
| 3320 | } |
| 3321 | |
| 3322 | /* Just check the input and prepare the stack for the previous |
| 3323 | * function "hlua_channel_forward_yield" |
| 3324 | */ |
| 3325 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3326 | { |
| 3327 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3328 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3329 | MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3330 | |
| 3331 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3332 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3333 | } |
| 3334 | |
| 3335 | /* Just returns the number of bytes available in the input |
| 3336 | * side of the buffer. This function never fails. |
| 3337 | */ |
| 3338 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3339 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3340 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3341 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3342 | MAY_LJMP(check_args(L, 1, "get_in_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3343 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3344 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3345 | struct htx *htx = htxbuf(&chn->buf); |
| 3346 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 3347 | } |
| 3348 | else |
| 3349 | lua_pushinteger(L, ci_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3350 | return 1; |
| 3351 | } |
| 3352 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3353 | /* Returns true if the channel is full. */ |
| 3354 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3355 | { |
| 3356 | struct channel *chn; |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3357 | |
| 3358 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3359 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 0ec740e | 2020-02-26 11:59:19 +0100 | [diff] [blame] | 3360 | /* ignore the reserve, we are not on a producer side (ie in an |
| 3361 | * applet). |
| 3362 | */ |
| 3363 | lua_pushboolean(L, channel_full(chn, 0)); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3364 | return 1; |
| 3365 | } |
| 3366 | |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 3367 | /* Returns true if the channel is the response channel. */ |
| 3368 | __LJMP static int hlua_channel_is_resp(lua_State *L) |
| 3369 | { |
| 3370 | struct channel *chn; |
| 3371 | |
| 3372 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 3373 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3374 | |
| 3375 | lua_pushboolean(L, !!(chn->flags & CF_ISRESP)); |
| 3376 | return 1; |
| 3377 | } |
| 3378 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3379 | /* Just returns the number of bytes available in the output |
| 3380 | * side of the buffer. This function never fails. |
| 3381 | */ |
| 3382 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 3383 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3384 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3385 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3386 | MAY_LJMP(check_args(L, 1, "get_out_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3387 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3388 | lua_pushinteger(L, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3389 | return 1; |
| 3390 | } |
| 3391 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3392 | /* |
| 3393 | * |
| 3394 | * |
| 3395 | * Class Fetches |
| 3396 | * |
| 3397 | * |
| 3398 | */ |
| 3399 | |
| 3400 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3401 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3402 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3403 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3404 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3405 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3406 | } |
| 3407 | |
| 3408 | /* This function creates and push in the stack a fetch object according |
| 3409 | * with a current TXN. |
| 3410 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3411 | 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] | 3412 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3413 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3414 | |
| 3415 | /* Check stack size. */ |
| 3416 | if (!lua_checkstack(L, 3)) |
| 3417 | return 0; |
| 3418 | |
| 3419 | /* Create the object: obj[0] = userdata. |
| 3420 | * Note that the base of the Fetches object is the |
| 3421 | * transaction object. |
| 3422 | */ |
| 3423 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3424 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3425 | lua_rawseti(L, -2, 0); |
| 3426 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3427 | hsmp->s = txn->s; |
| 3428 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3429 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3430 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3431 | |
| 3432 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3433 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 3434 | lua_setmetatable(L, -2); |
| 3435 | |
| 3436 | return 1; |
| 3437 | } |
| 3438 | |
| 3439 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 3440 | * It uses closure argument to store the associated sample-fetch. It |
| 3441 | * returns only one argument or throws an error. An error is thrown |
| 3442 | * only if an error is encountered during the argument parsing. If |
| 3443 | * the "sample-fetch" function fails, nil is returned. |
| 3444 | */ |
| 3445 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 3446 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3447 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3448 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3449 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3450 | int i; |
| 3451 | struct sample smp; |
| 3452 | |
| 3453 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3454 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3455 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3456 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3457 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3458 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 3459 | /* Check execution authorization. */ |
| 3460 | if (f->use & SMP_USE_HTTP_ANY && |
| 3461 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 3462 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 3463 | "is not available in Lua services", f->kw); |
| 3464 | WILL_LJMP(lua_error(L)); |
| 3465 | } |
| 3466 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3467 | /* Get extra arguments. */ |
| 3468 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 3469 | if (i >= ARGM_NBARGS) |
| 3470 | break; |
| 3471 | hlua_lua2arg(L, i + 2, &args[i]); |
| 3472 | } |
| 3473 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3474 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3475 | |
| 3476 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3477 | 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] | 3478 | |
| 3479 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3480 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3481 | lua_pushfstring(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3482 | goto error; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3483 | } |
| 3484 | |
| 3485 | /* Initialise the sample. */ |
| 3486 | memset(&smp, 0, sizeof(smp)); |
| 3487 | |
| 3488 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3489 | 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] | 3490 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3491 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3492 | lua_pushstring(L, ""); |
| 3493 | else |
| 3494 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3495 | goto end; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3496 | } |
| 3497 | |
| 3498 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3499 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3500 | hlua_smp2lua_str(L, &smp); |
| 3501 | else |
| 3502 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3503 | |
| 3504 | end: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 3505 | free_args(args); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3506 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3507 | |
| 3508 | error: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 3509 | free_args(args); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3510 | WILL_LJMP(lua_error(L)); |
| 3511 | return 0; /* Never reached */ |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3512 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3513 | |
| 3514 | /* |
| 3515 | * |
| 3516 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3517 | * Class Converters |
| 3518 | * |
| 3519 | * |
| 3520 | */ |
| 3521 | |
| 3522 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3523 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3524 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3525 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3526 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3527 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3528 | } |
| 3529 | |
| 3530 | /* This function creates and push in the stack a Converters object |
| 3531 | * according with a current TXN. |
| 3532 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3533 | 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] | 3534 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3535 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3536 | |
| 3537 | /* Check stack size. */ |
| 3538 | if (!lua_checkstack(L, 3)) |
| 3539 | return 0; |
| 3540 | |
| 3541 | /* Create the object: obj[0] = userdata. |
| 3542 | * Note that the base of the Converters object is the |
| 3543 | * same than the TXN object. |
| 3544 | */ |
| 3545 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3546 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3547 | lua_rawseti(L, -2, 0); |
| 3548 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3549 | hsmp->s = txn->s; |
| 3550 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3551 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3552 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3553 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3554 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3555 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 3556 | lua_setmetatable(L, -2); |
| 3557 | |
| 3558 | return 1; |
| 3559 | } |
| 3560 | |
| 3561 | /* This function is an LUA binding. It is called with each converter. |
| 3562 | * It uses closure argument to store the associated converter. It |
| 3563 | * returns only one argument or throws an error. An error is thrown |
| 3564 | * only if an error is encountered during the argument parsing. If |
| 3565 | * the converter function function fails, nil is returned. |
| 3566 | */ |
| 3567 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 3568 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3569 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3570 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3571 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3572 | int i; |
| 3573 | struct sample smp; |
| 3574 | |
| 3575 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3576 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3577 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3578 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3579 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3580 | |
| 3581 | /* Get extra arguments. */ |
| 3582 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 3583 | if (i >= ARGM_NBARGS) |
| 3584 | break; |
| 3585 | hlua_lua2arg(L, i + 3, &args[i]); |
| 3586 | } |
| 3587 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3588 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3589 | |
| 3590 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3591 | 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] | 3592 | |
| 3593 | /* Run the special args checker. */ |
| 3594 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 3595 | hlua_pusherror(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3596 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3597 | } |
| 3598 | |
| 3599 | /* Initialise the sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 3600 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3601 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 3602 | hlua_pusherror(L, "error in the input argument"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3603 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3604 | } |
| 3605 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3606 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 3607 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3608 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3609 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3610 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3611 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3612 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3613 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3614 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 3615 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3616 | hlua_pusherror(L, "error during the input argument casting"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3617 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3618 | } |
| 3619 | |
| 3620 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3621 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3622 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3623 | lua_pushstring(L, ""); |
| 3624 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3625 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3626 | goto end; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3627 | } |
| 3628 | |
| 3629 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3630 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3631 | hlua_smp2lua_str(L, &smp); |
| 3632 | else |
| 3633 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3634 | end: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 3635 | free_args(args); |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3636 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3637 | |
| 3638 | error: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 3639 | free_args(args); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3640 | WILL_LJMP(lua_error(L)); |
| 3641 | return 0; /* Never reached */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3642 | } |
| 3643 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3644 | /* |
| 3645 | * |
| 3646 | * |
| 3647 | * Class AppletTCP |
| 3648 | * |
| 3649 | * |
| 3650 | */ |
| 3651 | |
| 3652 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 3653 | * a class stream, otherwise it throws an error. |
| 3654 | */ |
| 3655 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 3656 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3657 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3658 | } |
| 3659 | |
| 3660 | /* This function creates and push in the stack an Applet object |
| 3661 | * according with a current TXN. |
| 3662 | */ |
| 3663 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 3664 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3665 | struct hlua_appctx *luactx; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3666 | struct stream_interface *si = ctx->owner; |
| 3667 | struct stream *s = si_strm(si); |
| 3668 | struct proxy *p = s->be; |
| 3669 | |
| 3670 | /* Check stack size. */ |
| 3671 | if (!lua_checkstack(L, 3)) |
| 3672 | return 0; |
| 3673 | |
| 3674 | /* Create the object: obj[0] = userdata. |
| 3675 | * Note that the base of the Converters object is the |
| 3676 | * same than the TXN object. |
| 3677 | */ |
| 3678 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3679 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3680 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3681 | luactx->appctx = ctx; |
| 3682 | luactx->htxn.s = s; |
| 3683 | luactx->htxn.p = p; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3684 | |
| 3685 | /* Create the "f" field that contains a list of fetches. */ |
| 3686 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3687 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3688 | return 0; |
| 3689 | lua_settable(L, -3); |
| 3690 | |
| 3691 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3692 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3693 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3694 | return 0; |
| 3695 | lua_settable(L, -3); |
| 3696 | |
| 3697 | /* Create the "c" field that contains a list of converters. */ |
| 3698 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3699 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3700 | return 0; |
| 3701 | lua_settable(L, -3); |
| 3702 | |
| 3703 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3704 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3705 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3706 | return 0; |
| 3707 | lua_settable(L, -3); |
| 3708 | |
| 3709 | /* Pop a class stream metatable and affect it to the table. */ |
| 3710 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 3711 | lua_setmetatable(L, -2); |
| 3712 | |
| 3713 | return 1; |
| 3714 | } |
| 3715 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3716 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 3717 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3718 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3719 | struct stream *s; |
| 3720 | const char *name; |
| 3721 | size_t len; |
| 3722 | struct sample smp; |
| 3723 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3724 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 3725 | 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] | 3726 | |
| 3727 | /* It is useles to retrieve the stream, but this function |
| 3728 | * runs only in a stream context. |
| 3729 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3730 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3731 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3732 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3733 | |
| 3734 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 3735 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3736 | hlua_lua2smp(L, 3, &smp); |
| 3737 | |
| 3738 | /* Store the sample in a variable. */ |
| 3739 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3740 | |
| 3741 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 3742 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 3743 | else |
| 3744 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 3745 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 3746 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3747 | } |
| 3748 | |
| 3749 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 3750 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3751 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3752 | struct stream *s; |
| 3753 | const char *name; |
| 3754 | size_t len; |
| 3755 | struct sample smp; |
| 3756 | |
| 3757 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3758 | |
| 3759 | /* It is useles to retrieve the stream, but this function |
| 3760 | * runs only in a stream context. |
| 3761 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3762 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3763 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3764 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3765 | |
| 3766 | /* Unset the variable. */ |
| 3767 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 3768 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 3769 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3770 | } |
| 3771 | |
| 3772 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 3773 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3774 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3775 | struct stream *s; |
| 3776 | const char *name; |
| 3777 | size_t len; |
| 3778 | struct sample smp; |
| 3779 | |
| 3780 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3781 | |
| 3782 | /* It is useles to retrieve the stream, but this function |
| 3783 | * runs only in a stream context. |
| 3784 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3785 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3786 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3787 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3788 | |
| 3789 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3790 | if (!vars_get_by_name(name, len, &smp)) { |
| 3791 | lua_pushnil(L); |
| 3792 | return 1; |
| 3793 | } |
| 3794 | |
| 3795 | return hlua_smp2lua(L, &smp); |
| 3796 | } |
| 3797 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3798 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 3799 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3800 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3801 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3802 | struct hlua *hlua; |
| 3803 | |
| 3804 | /* 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] | 3805 | if (!s->hlua) |
| 3806 | return 0; |
| 3807 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3808 | |
| 3809 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3810 | |
| 3811 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 3812 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3813 | |
| 3814 | /* Get and store new value. */ |
| 3815 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3816 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 3817 | |
| 3818 | return 0; |
| 3819 | } |
| 3820 | |
| 3821 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 3822 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3823 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3824 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3825 | struct hlua *hlua; |
| 3826 | |
| 3827 | /* 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] | 3828 | if (!s->hlua) { |
| 3829 | lua_pushnil(L); |
| 3830 | return 1; |
| 3831 | } |
| 3832 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3833 | |
| 3834 | /* Push configuration index in the stack. */ |
| 3835 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 3836 | |
| 3837 | return 1; |
| 3838 | } |
| 3839 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3840 | /* If expected data not yet available, it returns a yield. This function |
| 3841 | * consumes the data in the buffer. It returns a string containing the |
| 3842 | * data. This string can be empty. |
| 3843 | */ |
| 3844 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 3845 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3846 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3847 | struct stream_interface *si = luactx->appctx->owner; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3848 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3849 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3850 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3851 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3852 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3853 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3854 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3855 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3856 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3857 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3858 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3859 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3860 | 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] | 3861 | } |
| 3862 | |
| 3863 | /* End of data: commit the total strings and return. */ |
| 3864 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3865 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3866 | return 1; |
| 3867 | } |
| 3868 | |
| 3869 | /* Ensure that the block 2 length is usable. */ |
| 3870 | if (ret == 1) |
| 3871 | len2 = 0; |
| 3872 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 3873 | /* don't check the max length read and don't check. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3874 | luaL_addlstring(&luactx->b, blk1, len1); |
| 3875 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3876 | |
| 3877 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3878 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3879 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3880 | return 1; |
| 3881 | } |
| 3882 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3883 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3884 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 3885 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3886 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3887 | |
| 3888 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3889 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3890 | |
| 3891 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 3892 | } |
| 3893 | |
| 3894 | /* If expected data not yet available, it returns a yield. This function |
| 3895 | * consumes the data in the buffer. It returns a string containing the |
| 3896 | * data. This string can be empty. |
| 3897 | */ |
| 3898 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 3899 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3900 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3901 | struct stream_interface *si = luactx->appctx->owner; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3902 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3903 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3904 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3905 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3906 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3907 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3908 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3909 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3910 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3911 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3912 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3913 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3914 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3915 | 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] | 3916 | } |
| 3917 | |
| 3918 | /* End of data: commit the total strings and return. */ |
| 3919 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3920 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3921 | return 1; |
| 3922 | } |
| 3923 | |
| 3924 | /* Ensure that the block 2 length is usable. */ |
| 3925 | if (ret == 1) |
| 3926 | len2 = 0; |
| 3927 | |
| 3928 | if (len == -1) { |
| 3929 | |
| 3930 | /* If len == -1, catenate all the data avalaile and |
| 3931 | * yield because we want to get all the data until |
| 3932 | * the end of data stream. |
| 3933 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3934 | luaL_addlstring(&luactx->b, blk1, len1); |
| 3935 | luaL_addlstring(&luactx->b, blk2, len2); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3936 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3937 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3938 | 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] | 3939 | |
| 3940 | } else { |
| 3941 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3942 | /* Copy the first block caping to the length required. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3943 | if (len1 > len) |
| 3944 | len1 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3945 | luaL_addlstring(&luactx->b, blk1, len1); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3946 | len -= len1; |
| 3947 | |
| 3948 | /* Copy the second block. */ |
| 3949 | if (len2 > len) |
| 3950 | len2 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3951 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3952 | len -= len2; |
| 3953 | |
| 3954 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3955 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3956 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3957 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3958 | if (len > 0) { |
| 3959 | lua_pushinteger(L, len); |
| 3960 | lua_replace(L, 2); |
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 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3966 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3967 | return 1; |
| 3968 | } |
| 3969 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3970 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3971 | hlua_pusherror(L, "Lua: internal error"); |
| 3972 | WILL_LJMP(lua_error(L)); |
| 3973 | return 0; |
| 3974 | } |
| 3975 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3976 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3977 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 3978 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3979 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3980 | int len = -1; |
| 3981 | |
| 3982 | if (lua_gettop(L) > 2) |
| 3983 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 3984 | if (lua_gettop(L) >= 2) { |
| 3985 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3986 | lua_pop(L, 1); |
| 3987 | } |
| 3988 | |
| 3989 | /* Confirm or set the required length */ |
| 3990 | lua_pushinteger(L, len); |
| 3991 | |
| 3992 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 3993 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3994 | |
| 3995 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 3996 | } |
| 3997 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3998 | /* Append data in the output side of the buffer. This data is immediately |
| 3999 | * sent. The function returns the amount of data written. If the buffer |
| 4000 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4001 | * if the channel is closed. |
| 4002 | */ |
| 4003 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4004 | { |
| 4005 | size_t len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4006 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4007 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4008 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4009 | struct stream_interface *si = luactx->appctx->owner; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4010 | struct channel *chn = si_ic(si); |
| 4011 | int max; |
| 4012 | |
| 4013 | /* Get the max amount of data which can write as input in the channel. */ |
| 4014 | max = channel_recv_max(chn); |
| 4015 | if (max > (len - l)) |
| 4016 | max = len - l; |
| 4017 | |
| 4018 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4019 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4020 | |
| 4021 | /* update counters. */ |
| 4022 | l += max; |
| 4023 | lua_pop(L, 1); |
| 4024 | lua_pushinteger(L, l); |
| 4025 | |
| 4026 | /* If some data is not send, declares the situation to the |
| 4027 | * applet, and returns a yield. |
| 4028 | */ |
| 4029 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 4030 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4031 | 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] | 4032 | } |
| 4033 | |
| 4034 | return 1; |
| 4035 | } |
| 4036 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4037 | /* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4038 | * yield the LUA process, and resume it without checking the |
| 4039 | * input arguments. |
| 4040 | */ |
| 4041 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 4042 | { |
| 4043 | MAY_LJMP(check_args(L, 2, "send")); |
| 4044 | lua_pushinteger(L, 0); |
| 4045 | |
| 4046 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 4047 | } |
| 4048 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4049 | /* |
| 4050 | * |
| 4051 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4052 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4053 | * |
| 4054 | * |
| 4055 | */ |
| 4056 | |
| 4057 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4058 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4059 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4060 | __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] | 4061 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4062 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4063 | } |
| 4064 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4065 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4066 | * according with a current TXN. |
| 4067 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4068 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4069 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4070 | struct hlua_appctx *luactx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 4071 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4072 | struct stream_interface *si = ctx->owner; |
| 4073 | struct stream *s = si_strm(si); |
| 4074 | struct proxy *px = s->be; |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4075 | struct htx *htx; |
| 4076 | struct htx_blk *blk; |
| 4077 | struct htx_sl *sl; |
| 4078 | struct ist path; |
| 4079 | unsigned long long len = 0; |
| 4080 | int32_t pos; |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 4081 | struct http_uri_parser parser; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4082 | |
| 4083 | /* Check stack size. */ |
| 4084 | if (!lua_checkstack(L, 3)) |
| 4085 | return 0; |
| 4086 | |
| 4087 | /* Create the object: obj[0] = userdata. |
| 4088 | * Note that the base of the Converters object is the |
| 4089 | * same than the TXN object. |
| 4090 | */ |
| 4091 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4092 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4093 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4094 | luactx->appctx = ctx; |
| 4095 | luactx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
| 4096 | luactx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */ |
| 4097 | luactx->htxn.s = s; |
| 4098 | luactx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4099 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4100 | /* Create the "f" field that contains a list of fetches. */ |
| 4101 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4102 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4103 | return 0; |
| 4104 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4105 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4106 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4107 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4108 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4109 | return 0; |
| 4110 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4111 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4112 | /* Create the "c" field that contains a list of converters. */ |
| 4113 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4114 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4115 | return 0; |
| 4116 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4117 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4118 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4119 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4120 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4121 | return 0; |
| 4122 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4123 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4124 | htx = htxbuf(&s->req.buf); |
| 4125 | blk = htx_get_first_blk(htx); |
Christopher Faulet | ea00973 | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 4126 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4127 | sl = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4128 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4129 | /* Stores the request method. */ |
| 4130 | lua_pushstring(L, "method"); |
| 4131 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 4132 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4133 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4134 | /* Stores the http version. */ |
| 4135 | lua_pushstring(L, "version"); |
| 4136 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 4137 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4138 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4139 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 4140 | * the array on the top of the stack. |
| 4141 | */ |
| 4142 | lua_pushstring(L, "headers"); |
| 4143 | htxn.s = s; |
| 4144 | htxn.p = px; |
| 4145 | htxn.dir = SMP_OPT_DIR_REQ; |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4146 | if (!hlua_http_get_headers(L, &htxn.s->txn->req)) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4147 | return 0; |
| 4148 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4149 | |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 4150 | parser = http_uri_parser_init(htx_sl_req_uri(sl)); |
| 4151 | path = http_parse_path(&parser); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 4152 | if (isttest(path)) { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4153 | char *p, *q, *end; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4154 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4155 | p = path.ptr; |
| 4156 | end = path.ptr + path.len; |
| 4157 | q = p; |
| 4158 | while (q < end && *q != '?') |
| 4159 | q++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4160 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4161 | /* Stores the request path. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4162 | lua_pushstring(L, "path"); |
| 4163 | lua_pushlstring(L, p, q - p); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4164 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4165 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4166 | /* Stores the query string. */ |
| 4167 | lua_pushstring(L, "qs"); |
| 4168 | if (*q == '?') |
| 4169 | q++; |
| 4170 | lua_pushlstring(L, q, end - q); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4171 | lua_settable(L, -3); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4172 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4173 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4174 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4175 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4176 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4177 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4178 | if (type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4179 | break; |
| 4180 | if (type == HTX_BLK_DATA) |
| 4181 | len += htx_get_blksz(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4182 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4183 | if (htx->extra != ULLONG_MAX) |
| 4184 | len += htx->extra; |
| 4185 | |
| 4186 | /* Stores the request path. */ |
| 4187 | lua_pushstring(L, "length"); |
| 4188 | lua_pushinteger(L, len); |
| 4189 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4190 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4191 | /* Create an empty array of HTTP request headers. */ |
| 4192 | lua_pushstring(L, "response"); |
| 4193 | lua_newtable(L); |
| 4194 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4195 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4196 | /* Pop a class stream metatable and affect it to the table. */ |
| 4197 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 4198 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4199 | |
| 4200 | return 1; |
| 4201 | } |
| 4202 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4203 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 4204 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4205 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4206 | struct stream *s; |
| 4207 | const char *name; |
| 4208 | size_t len; |
| 4209 | struct sample smp; |
| 4210 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4211 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 4212 | 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] | 4213 | |
| 4214 | /* It is useles to retrieve the stream, but this function |
| 4215 | * runs only in a stream context. |
| 4216 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4217 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4218 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4219 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4220 | |
| 4221 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4222 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4223 | hlua_lua2smp(L, 3, &smp); |
| 4224 | |
| 4225 | /* Store the sample in a variable. */ |
| 4226 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4227 | |
| 4228 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 4229 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 4230 | else |
| 4231 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 4232 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4233 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4234 | } |
| 4235 | |
| 4236 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 4237 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4238 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4239 | struct stream *s; |
| 4240 | const char *name; |
| 4241 | size_t len; |
| 4242 | struct sample smp; |
| 4243 | |
| 4244 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4245 | |
| 4246 | /* It is useles to retrieve the stream, but this function |
| 4247 | * runs only in a stream context. |
| 4248 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4249 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4250 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4251 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4252 | |
| 4253 | /* Unset the variable. */ |
| 4254 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4255 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4256 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4257 | } |
| 4258 | |
| 4259 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4260 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4261 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4262 | struct stream *s; |
| 4263 | const char *name; |
| 4264 | size_t len; |
| 4265 | struct sample smp; |
| 4266 | |
| 4267 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4268 | |
| 4269 | /* It is useles to retrieve the stream, but this function |
| 4270 | * runs only in a stream context. |
| 4271 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4272 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4273 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4274 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4275 | |
| 4276 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4277 | if (!vars_get_by_name(name, len, &smp)) { |
| 4278 | lua_pushnil(L); |
| 4279 | return 1; |
| 4280 | } |
| 4281 | |
| 4282 | return hlua_smp2lua(L, &smp); |
| 4283 | } |
| 4284 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4285 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 4286 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4287 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4288 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4289 | struct hlua *hlua; |
| 4290 | |
| 4291 | /* 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] | 4292 | if (!s->hlua) |
| 4293 | return 0; |
| 4294 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4295 | |
| 4296 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4297 | |
| 4298 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4299 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4300 | |
| 4301 | /* Get and store new value. */ |
| 4302 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4303 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4304 | |
| 4305 | return 0; |
| 4306 | } |
| 4307 | |
| 4308 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4309 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4310 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4311 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4312 | struct hlua *hlua; |
| 4313 | |
| 4314 | /* 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] | 4315 | if (!s->hlua) { |
| 4316 | lua_pushnil(L); |
| 4317 | return 1; |
| 4318 | } |
| 4319 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4320 | |
| 4321 | /* Push configuration index in the stack. */ |
| 4322 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4323 | |
| 4324 | return 1; |
| 4325 | } |
| 4326 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4327 | /* If expected data not yet available, it returns a yield. This function |
| 4328 | * consumes the data in the buffer. It returns a string containing the |
| 4329 | * data. This string can be empty. |
| 4330 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4331 | __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] | 4332 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4333 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4334 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4335 | struct channel *req = si_oc(si); |
| 4336 | struct htx *htx; |
| 4337 | struct htx_blk *blk; |
| 4338 | size_t count; |
| 4339 | int stop = 0; |
| 4340 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4341 | htx = htx_from_buf(&req->buf); |
| 4342 | count = co_data(req); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4343 | blk = htx_get_first_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 4344 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4345 | while (count && !stop && blk) { |
| 4346 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4347 | uint32_t sz = htx_get_blksz(blk); |
| 4348 | struct ist v; |
| 4349 | uint32_t vlen; |
| 4350 | char *nl; |
| 4351 | |
| 4352 | vlen = sz; |
| 4353 | if (vlen > count) { |
| 4354 | if (type != HTX_BLK_DATA) |
| 4355 | break; |
| 4356 | vlen = count; |
| 4357 | } |
| 4358 | |
| 4359 | switch (type) { |
| 4360 | case HTX_BLK_UNUSED: |
| 4361 | break; |
| 4362 | |
| 4363 | case HTX_BLK_DATA: |
| 4364 | v = htx_get_blk_value(htx, blk); |
| 4365 | v.len = vlen; |
| 4366 | nl = istchr(v, '\n'); |
| 4367 | if (nl != NULL) { |
| 4368 | stop = 1; |
| 4369 | vlen = nl - v.ptr + 1; |
| 4370 | } |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4371 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4372 | break; |
| 4373 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4374 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4375 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4376 | stop = 1; |
| 4377 | break; |
| 4378 | |
| 4379 | default: |
| 4380 | break; |
| 4381 | } |
| 4382 | |
| 4383 | co_set_data(req, co_data(req) - vlen); |
| 4384 | count -= vlen; |
| 4385 | if (sz == vlen) |
| 4386 | blk = htx_remove_blk(htx, blk); |
| 4387 | else { |
| 4388 | htx_cut_data_blk(htx, blk, vlen); |
| 4389 | break; |
| 4390 | } |
| 4391 | } |
| 4392 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 4393 | /* The message was fully consumed and no more data are expected |
| 4394 | * (EOM flag set). |
| 4395 | */ |
| 4396 | if (htx_is_empty(htx) && (htx->flags & HTX_FL_EOM)) |
| 4397 | stop = 1; |
| 4398 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4399 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4400 | if (!stop) { |
| 4401 | si_cant_get(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4402 | 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] | 4403 | } |
| 4404 | |
| 4405 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4406 | luaL_pushresult(&luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4407 | return 1; |
| 4408 | } |
| 4409 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4410 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4411 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4412 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4413 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4414 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4415 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4416 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4417 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4418 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4419 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4420 | } |
| 4421 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4422 | /* If expected data not yet available, it returns a yield. This function |
| 4423 | * consumes the data in the buffer. It returns a string containing the |
| 4424 | * data. This string can be empty. |
| 4425 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4426 | __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] | 4427 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4428 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4429 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4430 | struct channel *req = si_oc(si); |
| 4431 | struct htx *htx; |
| 4432 | struct htx_blk *blk; |
| 4433 | size_t count; |
| 4434 | int len; |
| 4435 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4436 | htx = htx_from_buf(&req->buf); |
| 4437 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4438 | count = co_data(req); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 4439 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4440 | while (count && len && blk) { |
| 4441 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4442 | uint32_t sz = htx_get_blksz(blk); |
| 4443 | struct ist v; |
| 4444 | uint32_t vlen; |
| 4445 | |
| 4446 | vlen = sz; |
| 4447 | if (len > 0 && vlen > len) |
| 4448 | vlen = len; |
| 4449 | if (vlen > count) { |
| 4450 | if (type != HTX_BLK_DATA) |
| 4451 | break; |
| 4452 | vlen = count; |
| 4453 | } |
| 4454 | |
| 4455 | switch (type) { |
| 4456 | case HTX_BLK_UNUSED: |
| 4457 | break; |
| 4458 | |
| 4459 | case HTX_BLK_DATA: |
| 4460 | v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4461 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4462 | break; |
| 4463 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4464 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4465 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4466 | len = 0; |
| 4467 | break; |
| 4468 | |
| 4469 | default: |
| 4470 | break; |
| 4471 | } |
| 4472 | |
| 4473 | co_set_data(req, co_data(req) - vlen); |
| 4474 | count -= vlen; |
| 4475 | if (len > 0) |
| 4476 | len -= vlen; |
| 4477 | if (sz == vlen) |
| 4478 | blk = htx_remove_blk(htx, blk); |
| 4479 | else { |
| 4480 | htx_cut_data_blk(htx, blk, vlen); |
| 4481 | break; |
| 4482 | } |
| 4483 | } |
| 4484 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 4485 | /* The message was fully consumed and no more data are expected |
| 4486 | * (EOM flag set). |
| 4487 | */ |
| 4488 | if (htx_is_empty(htx) && (htx->flags & HTX_FL_EOM)) |
| 4489 | len = 0; |
| 4490 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4491 | htx_to_buf(htx, &req->buf); |
| 4492 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4493 | /* If we are no other data available, yield waiting for new data. */ |
| 4494 | if (len) { |
| 4495 | if (len > 0) { |
| 4496 | lua_pushinteger(L, len); |
| 4497 | lua_replace(L, 2); |
| 4498 | } |
| 4499 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4500 | 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] | 4501 | } |
| 4502 | |
| 4503 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4504 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4505 | return 1; |
| 4506 | } |
| 4507 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4508 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4509 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 4510 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4511 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4512 | int len = -1; |
| 4513 | |
| 4514 | /* Check arguments. */ |
| 4515 | if (lua_gettop(L) > 2) |
| 4516 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4517 | if (lua_gettop(L) >= 2) { |
| 4518 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4519 | lua_pop(L, 1); |
| 4520 | } |
| 4521 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4522 | lua_pushinteger(L, len); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4523 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4524 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4525 | luaL_buffinit(L, &luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4526 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4527 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4528 | } |
| 4529 | |
| 4530 | /* Append data in the output side of the buffer. This data is immediately |
| 4531 | * sent. The function returns the amount of data written. If the buffer |
| 4532 | * cannot contain the data, the function yields. The function returns -1 |
| 4533 | * if the channel is closed. |
| 4534 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4535 | __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] | 4536 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4537 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4538 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4539 | struct channel *res = si_ic(si); |
| 4540 | struct htx *htx = htx_from_buf(&res->buf); |
| 4541 | const char *data; |
| 4542 | size_t len; |
| 4543 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4544 | int max; |
| 4545 | |
Christopher Faulet | 9060fc0 | 2019-07-03 11:39:30 +0200 | [diff] [blame] | 4546 | max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx)); |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4547 | if (!max) |
| 4548 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4549 | |
| 4550 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4551 | |
| 4552 | /* Get the max amount of data which can write as input in the channel. */ |
| 4553 | if (max > (len - l)) |
| 4554 | max = len - l; |
| 4555 | |
| 4556 | /* Copy data. */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 4557 | max = htx_add_data(htx, ist2(data + l, max)); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4558 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4559 | |
| 4560 | /* update counters. */ |
| 4561 | l += max; |
| 4562 | lua_pop(L, 1); |
| 4563 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4564 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4565 | /* If some data is not send, declares the situation to the |
| 4566 | * applet, and returns a yield. |
| 4567 | */ |
| 4568 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4569 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4570 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4571 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4572 | 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] | 4573 | } |
| 4574 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4575 | htx_to_buf(htx, &res->buf); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4576 | return 1; |
| 4577 | } |
| 4578 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4579 | /* Just a wrapper of "hlua_applet_send_yield". This wrapper permits |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4580 | * yield the LUA process, and resume it without checking the |
| 4581 | * input arguments. |
| 4582 | */ |
| 4583 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 4584 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4585 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4586 | |
| 4587 | /* We want to send some data. Headers must be sent. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4588 | if (!(luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4589 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 4590 | WILL_LJMP(lua_error(L)); |
| 4591 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4592 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4593 | /* This integer is used for followinf the amount of data sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4594 | lua_pushinteger(L, 0); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4595 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4596 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4597 | } |
| 4598 | |
| 4599 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 4600 | { |
| 4601 | const char *name; |
| 4602 | int ret; |
| 4603 | |
| 4604 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4605 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4606 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 4607 | |
| 4608 | /* Push in the stack the "response" entry. */ |
| 4609 | ret = lua_getfield(L, 1, "response"); |
| 4610 | if (ret != LUA_TTABLE) { |
| 4611 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 4612 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 4613 | WILL_LJMP(lua_error(L)); |
| 4614 | } |
| 4615 | |
| 4616 | /* check if the header is already registered if it is not |
| 4617 | * the case, register it. |
| 4618 | */ |
| 4619 | ret = lua_getfield(L, -1, name); |
| 4620 | if (ret == LUA_TNIL) { |
| 4621 | |
| 4622 | /* Entry not found. */ |
| 4623 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 4624 | |
| 4625 | /* Insert the new header name in the array in the top of the stack. |
| 4626 | * It left the new array in the top of the stack. |
| 4627 | */ |
| 4628 | lua_newtable(L); |
| 4629 | lua_pushvalue(L, 2); |
| 4630 | lua_pushvalue(L, -2); |
| 4631 | lua_settable(L, -4); |
| 4632 | |
| 4633 | } else if (ret != LUA_TTABLE) { |
| 4634 | |
| 4635 | /* corruption error. */ |
| 4636 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 4637 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 4638 | WILL_LJMP(lua_error(L)); |
| 4639 | } |
| 4640 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 4641 | /* Now the top of thestack is an array of values. We push |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4642 | * the header value as new entry. |
| 4643 | */ |
| 4644 | lua_pushvalue(L, 3); |
| 4645 | ret = lua_rawlen(L, -2); |
| 4646 | lua_rawseti(L, -2, ret + 1); |
| 4647 | lua_pushboolean(L, 1); |
| 4648 | return 1; |
| 4649 | } |
| 4650 | |
| 4651 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 4652 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4653 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4654 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4655 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4656 | |
| 4657 | if (status < 100 || status > 599) { |
| 4658 | lua_pushboolean(L, 0); |
| 4659 | return 1; |
| 4660 | } |
| 4661 | |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4662 | luactx->appctx->ctx.hlua_apphttp.status = status; |
| 4663 | luactx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4664 | lua_pushboolean(L, 1); |
| 4665 | return 1; |
| 4666 | } |
| 4667 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4668 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4669 | __LJMP static int hlua_applet_http_send_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4670 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4671 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4672 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4673 | struct channel *res = si_ic(si); |
| 4674 | struct htx *htx; |
| 4675 | struct htx_sl *sl; |
| 4676 | struct h1m h1m; |
| 4677 | const char *status, *reason; |
| 4678 | const char *name, *value; |
| 4679 | size_t nlen, vlen; |
| 4680 | unsigned int flags; |
| 4681 | |
| 4682 | /* Send the message at once. */ |
| 4683 | htx = htx_from_buf(&res->buf); |
| 4684 | h1m_init_res(&h1m); |
| 4685 | |
| 4686 | /* Use the same http version than the request. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4687 | status = ultoa_r(luactx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size); |
| 4688 | reason = luactx->appctx->ctx.hlua_apphttp.reason; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4689 | if (reason == NULL) |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4690 | reason = http_get_reason(luactx->appctx->ctx.hlua_apphttp.status); |
| 4691 | if (luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4692 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 4693 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 4694 | } |
| 4695 | else { |
| 4696 | flags = HTX_SL_F_IS_RESP; |
| 4697 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 4698 | } |
| 4699 | if (!sl) { |
| 4700 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4701 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4702 | WILL_LJMP(lua_error(L)); |
| 4703 | } |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4704 | sl->info.res.status = luactx->appctx->ctx.hlua_apphttp.status; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4705 | |
| 4706 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 4707 | lua_pushvalue(L, 0); |
| 4708 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 4709 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4710 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4711 | WILL_LJMP(lua_error(L)); |
| 4712 | } |
| 4713 | |
| 4714 | /* Browse the list of headers. */ |
| 4715 | lua_pushnil(L); |
| 4716 | while(lua_next(L, -2) != 0) { |
| 4717 | /* We expect a string as -2. */ |
| 4718 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 4719 | 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] | 4720 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4721 | lua_typename(L, lua_type(L, -2))); |
| 4722 | WILL_LJMP(lua_error(L)); |
| 4723 | } |
| 4724 | name = lua_tolstring(L, -2, &nlen); |
| 4725 | |
| 4726 | /* We expect an array as -1. */ |
| 4727 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 4728 | 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] | 4729 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4730 | name, |
| 4731 | lua_typename(L, lua_type(L, -1))); |
| 4732 | WILL_LJMP(lua_error(L)); |
| 4733 | } |
| 4734 | |
| 4735 | /* Browse the table who is on the top of the stack. */ |
| 4736 | lua_pushnil(L); |
| 4737 | while(lua_next(L, -2) != 0) { |
| 4738 | int id; |
| 4739 | |
| 4740 | /* We expect a number as -2. */ |
| 4741 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 4742 | 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] | 4743 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4744 | name, |
| 4745 | lua_typename(L, lua_type(L, -2))); |
| 4746 | WILL_LJMP(lua_error(L)); |
| 4747 | } |
| 4748 | id = lua_tointeger(L, -2); |
| 4749 | |
| 4750 | /* We expect a string as -2. */ |
| 4751 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 4752 | 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] | 4753 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4754 | name, id, |
| 4755 | lua_typename(L, lua_type(L, -1))); |
| 4756 | WILL_LJMP(lua_error(L)); |
| 4757 | } |
| 4758 | value = lua_tolstring(L, -1, &vlen); |
| 4759 | |
| 4760 | /* Simple Protocol checks. */ |
| 4761 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 4762 | h1_parse_xfer_enc_header(&h1m, ist2(value, vlen)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4763 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 4764 | struct ist v = ist2(value, vlen); |
| 4765 | int ret; |
| 4766 | |
| 4767 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 4768 | if (ret < 0) { |
| 4769 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4770 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4771 | name); |
| 4772 | WILL_LJMP(lua_error(L)); |
| 4773 | } |
| 4774 | else if (ret == 0) |
| 4775 | goto next; /* Skip it */ |
| 4776 | } |
| 4777 | |
| 4778 | /* Add a new header */ |
| 4779 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 4780 | 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] | 4781 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4782 | name); |
| 4783 | WILL_LJMP(lua_error(L)); |
| 4784 | } |
| 4785 | next: |
| 4786 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4787 | lua_pop(L, 1); |
| 4788 | } |
| 4789 | |
| 4790 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4791 | lua_pop(L, 1); |
| 4792 | } |
| 4793 | |
| 4794 | if (h1m.flags & H1_MF_CHNK) |
| 4795 | h1m.flags &= ~H1_MF_CLEN; |
| 4796 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 4797 | h1m.flags |= H1_MF_XFER_LEN; |
| 4798 | |
| 4799 | /* Uset HTX start-line flags */ |
| 4800 | if (h1m.flags & H1_MF_XFER_ENC) |
| 4801 | flags |= HTX_SL_F_XFER_ENC; |
| 4802 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 4803 | flags |= HTX_SL_F_XFER_LEN; |
| 4804 | if (h1m.flags & H1_MF_CHNK) |
| 4805 | flags |= HTX_SL_F_CHNK; |
| 4806 | else if (h1m.flags & H1_MF_CLEN) |
| 4807 | flags |= HTX_SL_F_CLEN; |
| 4808 | if (h1m.body_len == 0) |
| 4809 | flags |= HTX_SL_F_BODYLESS; |
| 4810 | } |
| 4811 | sl->flags |= flags; |
| 4812 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 4813 | /* 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] | 4814 | * and the status code implies the presence of a message body, we must |
| 4815 | * announce a transfer encoding chunked. This is required by haproxy |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4816 | * for the keepalive compliance. If the applet announces a transfer-encoding |
| 4817 | * chunked itself, don't do anything. |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4818 | */ |
| 4819 | 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] | 4820 | luactx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 4821 | luactx->appctx->ctx.hlua_apphttp.status != 204 && |
| 4822 | luactx->appctx->ctx.hlua_apphttp.status != 304) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4823 | /* Add a new header */ |
| 4824 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 4825 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 4826 | 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] | 4827 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4828 | WILL_LJMP(lua_error(L)); |
| 4829 | } |
| 4830 | } |
| 4831 | |
| 4832 | /* Finalize headers. */ |
| 4833 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 4834 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4835 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4836 | WILL_LJMP(lua_error(L)); |
| 4837 | } |
| 4838 | |
| 4839 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 4840 | b_reset(&res->buf); |
| 4841 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 4842 | WILL_LJMP(lua_error(L)); |
| 4843 | } |
| 4844 | |
| 4845 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4846 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4847 | |
| 4848 | /* Headers sent, set the flag. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4849 | luactx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4850 | return 0; |
| 4851 | |
| 4852 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4853 | /* We will build the status line and the headers of the HTTP response. |
| 4854 | * We will try send at once if its not possible, we give back the hand |
| 4855 | * waiting for more room. |
| 4856 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4857 | __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] | 4858 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4859 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4860 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4861 | struct channel *res = si_ic(si); |
| 4862 | |
| 4863 | if (co_data(res)) { |
| 4864 | si_rx_room_blk(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4865 | 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] | 4866 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4867 | return MAY_LJMP(hlua_applet_http_send_response(L)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4868 | } |
| 4869 | |
| 4870 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4871 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4872 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4873 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4874 | } |
| 4875 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4876 | /* |
| 4877 | * |
| 4878 | * |
| 4879 | * Class HTTP |
| 4880 | * |
| 4881 | * |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4882 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4883 | |
| 4884 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4885 | * a class stream, otherwise it throws an error. |
| 4886 | */ |
| 4887 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4888 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4889 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
| 4890 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4891 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4892 | /* This function creates and push in the stack a HTTP object |
| 4893 | * according with a current TXN. |
| 4894 | */ |
| 4895 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 4896 | { |
| 4897 | struct hlua_txn *htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4898 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4899 | /* Check stack size. */ |
| 4900 | if (!lua_checkstack(L, 3)) |
| 4901 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4902 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4903 | /* Create the object: obj[0] = userdata. |
| 4904 | * Note that the base of the Converters object is the |
| 4905 | * same than the TXN object. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4906 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4907 | lua_newtable(L); |
| 4908 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 4909 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4910 | |
| 4911 | htxn->s = txn->s; |
| 4912 | htxn->p = txn->p; |
Christopher Faulet | 256b69a | 2019-05-23 11:14:21 +0200 | [diff] [blame] | 4913 | htxn->dir = txn->dir; |
| 4914 | htxn->flags = txn->flags; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4915 | |
| 4916 | /* Pop a class stream metatable and affect it to the table. */ |
| 4917 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 4918 | lua_setmetatable(L, -2); |
| 4919 | |
| 4920 | return 1; |
| 4921 | } |
| 4922 | |
| 4923 | /* This function creates ans returns an array of HTTP headers. |
| 4924 | * This function does not fails. It is used as wrapper with the |
| 4925 | * 2 following functions. |
| 4926 | */ |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4927 | __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] | 4928 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4929 | struct htx *htx; |
| 4930 | int32_t pos; |
| 4931 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4932 | /* Create the table. */ |
| 4933 | lua_newtable(L); |
| 4934 | |
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 | htx = htxbuf(&msg->chn->buf); |
| 4937 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4938 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4939 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4940 | struct ist n, v; |
| 4941 | int len; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4942 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4943 | if (type == HTX_BLK_HDR) { |
| 4944 | n = htx_get_blk_name(htx,blk); |
| 4945 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4946 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4947 | else if (type == HTX_BLK_EOH) |
| 4948 | break; |
| 4949 | else |
| 4950 | continue; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4951 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4952 | /* Check for existing entry: |
| 4953 | * assume that the table is on the top of the stack, and |
| 4954 | * push the key in the stack, the function lua_gettable() |
| 4955 | * perform the lookup. |
| 4956 | */ |
| 4957 | lua_pushlstring(L, n.ptr, n.len); |
| 4958 | lua_gettable(L, -2); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4959 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4960 | switch (lua_type(L, -1)) { |
| 4961 | case LUA_TNIL: |
| 4962 | /* Table not found, create it. */ |
| 4963 | lua_pop(L, 1); /* remove the nil value. */ |
| 4964 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 4965 | lua_newtable(L); /* create and push empty table. */ |
| 4966 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 4967 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 4968 | 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] | 4969 | break; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4970 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4971 | case LUA_TTABLE: |
| 4972 | /* Entry found: push the value in the table. */ |
| 4973 | len = lua_rawlen(L, -1); |
| 4974 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 4975 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 4976 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 4977 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4978 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4979 | default: |
| 4980 | /* Other cases are errors. */ |
| 4981 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 4982 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4983 | } |
| 4984 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4985 | return 1; |
| 4986 | } |
| 4987 | |
| 4988 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 4989 | { |
| 4990 | struct hlua_txn *htxn; |
| 4991 | |
| 4992 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 4993 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4994 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4995 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4996 | WILL_LJMP(lua_error(L)); |
| 4997 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4998 | return hlua_http_get_headers(L, &htxn->s->txn->req); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4999 | } |
| 5000 | |
| 5001 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 5002 | { |
| 5003 | struct hlua_txn *htxn; |
| 5004 | |
| 5005 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 5006 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5007 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5008 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5009 | WILL_LJMP(lua_error(L)); |
| 5010 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5011 | return hlua_http_get_headers(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5012 | } |
| 5013 | |
| 5014 | /* This function replace full header, or just a value in |
| 5015 | * the request or in the response. It is a wrapper fir the |
| 5016 | * 4 following functions. |
| 5017 | */ |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5018 | __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] | 5019 | { |
| 5020 | size_t name_len; |
| 5021 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5022 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5023 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5024 | struct htx *htx; |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5025 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5026 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5027 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5028 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 5029 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5030 | htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5031 | 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] | 5032 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5033 | return 0; |
| 5034 | } |
| 5035 | |
| 5036 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 5037 | { |
| 5038 | struct hlua_txn *htxn; |
| 5039 | |
| 5040 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5041 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5042 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5043 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5044 | WILL_LJMP(lua_error(L)); |
| 5045 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5046 | 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] | 5047 | } |
| 5048 | |
| 5049 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 5050 | { |
| 5051 | struct hlua_txn *htxn; |
| 5052 | |
| 5053 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 5054 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5055 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5056 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5057 | WILL_LJMP(lua_error(L)); |
| 5058 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5059 | 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] | 5060 | } |
| 5061 | |
| 5062 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 5063 | { |
| 5064 | struct hlua_txn *htxn; |
| 5065 | |
| 5066 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5067 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5068 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5069 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5070 | WILL_LJMP(lua_error(L)); |
| 5071 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5072 | 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] | 5073 | } |
| 5074 | |
| 5075 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 5076 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5077 | struct hlua_txn *htxn; |
| 5078 | |
| 5079 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 5080 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5081 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5082 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5083 | WILL_LJMP(lua_error(L)); |
| 5084 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5085 | 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] | 5086 | } |
| 5087 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5088 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5089 | * It is a wrapper for the 2 following functions. |
| 5090 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5091 | __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] | 5092 | { |
| 5093 | size_t len; |
| 5094 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5095 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5096 | struct http_hdr_ctx ctx; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5097 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5098 | ctx.blk = NULL; |
| 5099 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 5100 | http_remove_header(htx, &ctx); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5101 | return 0; |
| 5102 | } |
| 5103 | |
| 5104 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 5105 | { |
| 5106 | struct hlua_txn *htxn; |
| 5107 | |
| 5108 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 5109 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5110 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5111 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5112 | WILL_LJMP(lua_error(L)); |
| 5113 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5114 | return hlua_http_del_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5115 | } |
| 5116 | |
| 5117 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 5118 | { |
| 5119 | struct hlua_txn *htxn; |
| 5120 | |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5121 | MAY_LJMP(check_args(L, 2, "res_del_hdr")); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5122 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5123 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5124 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5125 | WILL_LJMP(lua_error(L)); |
| 5126 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5127 | return hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5128 | } |
| 5129 | |
| 5130 | /* This function adds an header. It is a wrapper used by |
| 5131 | * the 2 following functions. |
| 5132 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5133 | __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] | 5134 | { |
| 5135 | size_t name_len; |
| 5136 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5137 | size_t value_len; |
| 5138 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5139 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5140 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5141 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 5142 | ist2(value, value_len))); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5143 | return 0; |
| 5144 | } |
| 5145 | |
| 5146 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 5147 | { |
| 5148 | struct hlua_txn *htxn; |
| 5149 | |
| 5150 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 5151 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5152 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5153 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5154 | WILL_LJMP(lua_error(L)); |
| 5155 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5156 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5157 | } |
| 5158 | |
| 5159 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 5160 | { |
| 5161 | struct hlua_txn *htxn; |
| 5162 | |
| 5163 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 5164 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5165 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5166 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5167 | WILL_LJMP(lua_error(L)); |
| 5168 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5169 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5170 | } |
| 5171 | |
| 5172 | static int hlua_http_req_set_hdr(lua_State *L) |
| 5173 | { |
| 5174 | struct hlua_txn *htxn; |
| 5175 | |
| 5176 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 5177 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5178 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5179 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5180 | WILL_LJMP(lua_error(L)); |
| 5181 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5182 | hlua_http_del_hdr(L, &htxn->s->txn->req); |
| 5183 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5184 | } |
| 5185 | |
| 5186 | static int hlua_http_res_set_hdr(lua_State *L) |
| 5187 | { |
| 5188 | struct hlua_txn *htxn; |
| 5189 | |
| 5190 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 5191 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5192 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5193 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5194 | WILL_LJMP(lua_error(L)); |
| 5195 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5196 | hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
| 5197 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5198 | } |
| 5199 | |
| 5200 | /* This function set the method. */ |
| 5201 | static int hlua_http_req_set_meth(lua_State *L) |
| 5202 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5203 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5204 | size_t name_len; |
| 5205 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5206 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5207 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5208 | WILL_LJMP(lua_error(L)); |
| 5209 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5210 | 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] | 5211 | return 1; |
| 5212 | } |
| 5213 | |
| 5214 | /* This function set the method. */ |
| 5215 | static int hlua_http_req_set_path(lua_State *L) |
| 5216 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5217 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5218 | size_t name_len; |
| 5219 | 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] | 5220 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5221 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5222 | WILL_LJMP(lua_error(L)); |
| 5223 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5224 | 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] | 5225 | return 1; |
| 5226 | } |
| 5227 | |
| 5228 | /* This function set the query-string. */ |
| 5229 | static int hlua_http_req_set_query(lua_State *L) |
| 5230 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5231 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5232 | size_t name_len; |
| 5233 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5234 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5235 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5236 | WILL_LJMP(lua_error(L)); |
| 5237 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5238 | /* Check length. */ |
| 5239 | if (name_len > trash.size - 1) { |
| 5240 | lua_pushboolean(L, 0); |
| 5241 | return 1; |
| 5242 | } |
| 5243 | |
| 5244 | /* Add the mark question as prefix. */ |
| 5245 | chunk_reset(&trash); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5246 | trash.area[trash.data++] = '?'; |
| 5247 | memcpy(trash.area + trash.data, name, name_len); |
| 5248 | trash.data += name_len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5249 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5250 | lua_pushboolean(L, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5251 | 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] | 5252 | return 1; |
| 5253 | } |
| 5254 | |
| 5255 | /* This function set the uri. */ |
| 5256 | static int hlua_http_req_set_uri(lua_State *L) |
| 5257 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5258 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5259 | size_t name_len; |
| 5260 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5261 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5262 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5263 | WILL_LJMP(lua_error(L)); |
| 5264 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5265 | 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] | 5266 | return 1; |
| 5267 | } |
| 5268 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5269 | /* This function set the response code & optionally reason. */ |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5270 | static int hlua_http_res_set_status(lua_State *L) |
| 5271 | { |
| 5272 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5273 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 5274 | const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 5275 | const struct ist reason = ist2(str, (str ? strlen(str) : 0)); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5276 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5277 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5278 | WILL_LJMP(lua_error(L)); |
| 5279 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5280 | http_res_set_status(code, reason, htxn->s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5281 | return 0; |
| 5282 | } |
| 5283 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5284 | /* |
| 5285 | * |
| 5286 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5287 | * Class TXN |
| 5288 | * |
| 5289 | * |
| 5290 | */ |
| 5291 | |
| 5292 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5293 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5294 | */ |
| 5295 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 5296 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5297 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5298 | } |
| 5299 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5300 | __LJMP static int hlua_set_var(lua_State *L) |
| 5301 | { |
| 5302 | struct hlua_txn *htxn; |
| 5303 | const char *name; |
| 5304 | size_t len; |
| 5305 | struct sample smp; |
| 5306 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 5307 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 5308 | 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] | 5309 | |
| 5310 | /* It is useles to retrieve the stream, but this function |
| 5311 | * runs only in a stream context. |
| 5312 | */ |
| 5313 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5314 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5315 | |
| 5316 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 5317 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5318 | hlua_lua2smp(L, 3, &smp); |
| 5319 | |
| 5320 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5321 | 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] | 5322 | |
| 5323 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 5324 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 5325 | else |
| 5326 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 5327 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 5328 | return 1; |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5329 | } |
| 5330 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5331 | __LJMP static int hlua_unset_var(lua_State *L) |
| 5332 | { |
| 5333 | struct hlua_txn *htxn; |
| 5334 | const char *name; |
| 5335 | size_t len; |
| 5336 | struct sample smp; |
| 5337 | |
| 5338 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 5339 | |
| 5340 | /* It is useles to retrieve the stream, but this function |
| 5341 | * runs only in a stream context. |
| 5342 | */ |
| 5343 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5344 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5345 | |
| 5346 | /* Unset the variable. */ |
| 5347 | 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] | 5348 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 5349 | return 1; |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5350 | } |
| 5351 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5352 | __LJMP static int hlua_get_var(lua_State *L) |
| 5353 | { |
| 5354 | struct hlua_txn *htxn; |
| 5355 | const char *name; |
| 5356 | size_t len; |
| 5357 | struct sample smp; |
| 5358 | |
| 5359 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 5360 | |
| 5361 | /* It is useles to retrieve the stream, but this function |
| 5362 | * runs only in a stream context. |
| 5363 | */ |
| 5364 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5365 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5366 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5367 | 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] | 5368 | if (!vars_get_by_name(name, len, &smp)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5369 | lua_pushnil(L); |
| 5370 | return 1; |
| 5371 | } |
| 5372 | |
| 5373 | return hlua_smp2lua(L, &smp); |
| 5374 | } |
| 5375 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5376 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5377 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5378 | struct hlua *hlua; |
| 5379 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5380 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 5381 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5382 | /* It is useles to retrieve the stream, but this function |
| 5383 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5384 | */ |
| 5385 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5386 | |
| 5387 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5388 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5389 | if (!hlua) |
| 5390 | return 0; |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5391 | |
| 5392 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5393 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5394 | |
| 5395 | /* Get and store new value. */ |
| 5396 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5397 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5398 | |
| 5399 | return 0; |
| 5400 | } |
| 5401 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5402 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5403 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5404 | struct hlua *hlua; |
| 5405 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5406 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 5407 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5408 | /* It is useles to retrieve the stream, but this function |
| 5409 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5410 | */ |
| 5411 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5412 | |
| 5413 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5414 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5415 | if (!hlua) { |
| 5416 | lua_pushnil(L); |
| 5417 | return 1; |
| 5418 | } |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5419 | |
| 5420 | /* Push configuration index in the stack. */ |
| 5421 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5422 | |
| 5423 | return 1; |
| 5424 | } |
| 5425 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5426 | /* Create stack entry containing a class TXN. This function |
| 5427 | * return 0 if the stack does not contains free slots, |
| 5428 | * otherwise it returns 1. |
| 5429 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5430 | 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] | 5431 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5432 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5433 | |
| 5434 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5435 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5436 | return 0; |
| 5437 | |
| 5438 | /* NOTE: The allocation never fails. The failure |
| 5439 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5440 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5441 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5442 | /* Create the object: obj[0] = userdata. */ |
| 5443 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5444 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5445 | lua_rawseti(L, -2, 0); |
| 5446 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5447 | htxn->s = s; |
| 5448 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 5449 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5450 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5451 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5452 | /* Create the "f" field that contains a list of fetches. */ |
| 5453 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5454 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5455 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5456 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5457 | |
| 5458 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 5459 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5460 | 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] | 5461 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5462 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5463 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5464 | /* Create the "c" field that contains a list of converters. */ |
| 5465 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5466 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5467 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5468 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5469 | |
| 5470 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 5471 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 5472 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5473 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5474 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5475 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5476 | /* Create the "req" field that contains the request channel object. */ |
| 5477 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5478 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5479 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5480 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5481 | |
| 5482 | /* Create the "res" field that contains the response channel object. */ |
| 5483 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5484 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5485 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5486 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5487 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5488 | /* Creates the HTTP object is the current proxy allows http. */ |
| 5489 | lua_pushstring(L, "http"); |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 5490 | if (IS_HTX_STRM(s)) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5491 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5492 | return 0; |
| 5493 | } |
| 5494 | else |
| 5495 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5496 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5497 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5498 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5499 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 5500 | lua_setmetatable(L, -2); |
| 5501 | |
| 5502 | return 1; |
| 5503 | } |
| 5504 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5505 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 5506 | { |
| 5507 | const char *msg; |
| 5508 | struct hlua_txn *htxn; |
| 5509 | |
| 5510 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 5511 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5512 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5513 | |
| 5514 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 5515 | return 0; |
| 5516 | } |
| 5517 | |
| 5518 | __LJMP static int hlua_txn_log(lua_State *L) |
| 5519 | { |
| 5520 | int level; |
| 5521 | const char *msg; |
| 5522 | struct hlua_txn *htxn; |
| 5523 | |
| 5524 | MAY_LJMP(check_args(L, 3, "log")); |
| 5525 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5526 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5527 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5528 | |
| 5529 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5530 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5531 | |
| 5532 | hlua_sendlog(htxn->s->be, level, msg); |
| 5533 | return 0; |
| 5534 | } |
| 5535 | |
| 5536 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 5537 | { |
| 5538 | const char *msg; |
| 5539 | struct hlua_txn *htxn; |
| 5540 | |
| 5541 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 5542 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5543 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5544 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 5545 | return 0; |
| 5546 | } |
| 5547 | |
| 5548 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 5549 | { |
| 5550 | const char *msg; |
| 5551 | struct hlua_txn *htxn; |
| 5552 | |
| 5553 | MAY_LJMP(check_args(L, 2, "Info")); |
| 5554 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5555 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5556 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 5557 | return 0; |
| 5558 | } |
| 5559 | |
| 5560 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 5561 | { |
| 5562 | const char *msg; |
| 5563 | struct hlua_txn *htxn; |
| 5564 | |
| 5565 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 5566 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5567 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5568 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 5569 | return 0; |
| 5570 | } |
| 5571 | |
| 5572 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 5573 | { |
| 5574 | const char *msg; |
| 5575 | struct hlua_txn *htxn; |
| 5576 | |
| 5577 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 5578 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5579 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5580 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 5581 | return 0; |
| 5582 | } |
| 5583 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5584 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 5585 | { |
| 5586 | struct hlua_txn *htxn; |
| 5587 | int ll; |
| 5588 | |
| 5589 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 5590 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5591 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5592 | |
| 5593 | if (ll < 0 || ll > 7) |
| 5594 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 5595 | |
| 5596 | htxn->s->logs.level = ll; |
| 5597 | return 0; |
| 5598 | } |
| 5599 | |
| 5600 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 5601 | { |
| 5602 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5603 | int tos; |
| 5604 | |
| 5605 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 5606 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5607 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5608 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 5609 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5610 | return 0; |
| 5611 | } |
| 5612 | |
| 5613 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 5614 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5615 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5616 | int mark; |
| 5617 | |
| 5618 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 5619 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5620 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5621 | |
Lukas Tribus | 579e3e3 | 2019-08-11 18:03:45 +0200 | [diff] [blame] | 5622 | conn_set_mark(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5623 | return 0; |
| 5624 | } |
| 5625 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 5626 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 5627 | { |
| 5628 | struct hlua_txn *htxn; |
| 5629 | |
| 5630 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 5631 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5632 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5633 | return 0; |
| 5634 | } |
| 5635 | |
| 5636 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 5637 | { |
| 5638 | struct hlua_txn *htxn; |
| 5639 | |
| 5640 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 5641 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5642 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5643 | return 0; |
| 5644 | } |
| 5645 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5646 | /* 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] | 5647 | * 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] | 5648 | * message and terminate the transaction. It returns 1 on success and 0 on |
| 5649 | * error. The Reply must be on top of the stack. |
| 5650 | */ |
| 5651 | __LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s) |
| 5652 | { |
| 5653 | struct htx *htx; |
| 5654 | struct htx_sl *sl; |
| 5655 | struct h1m h1m; |
| 5656 | const char *status, *reason, *body; |
| 5657 | size_t status_len, reason_len, body_len; |
| 5658 | int ret, code, flags; |
| 5659 | |
| 5660 | code = 200; |
| 5661 | status = "200"; |
| 5662 | status_len = 3; |
| 5663 | ret = lua_getfield(L, -1, "status"); |
| 5664 | if (ret == LUA_TNUMBER) { |
| 5665 | code = lua_tointeger(L, -1); |
| 5666 | status = lua_tolstring(L, -1, &status_len); |
| 5667 | } |
| 5668 | lua_pop(L, 1); |
| 5669 | |
| 5670 | reason = http_get_reason(code); |
| 5671 | reason_len = strlen(reason); |
| 5672 | ret = lua_getfield(L, -1, "reason"); |
| 5673 | if (ret == LUA_TSTRING) |
| 5674 | reason = lua_tolstring(L, -1, &reason_len); |
| 5675 | lua_pop(L, 1); |
| 5676 | |
| 5677 | body = NULL; |
| 5678 | body_len = 0; |
| 5679 | ret = lua_getfield(L, -1, "body"); |
| 5680 | if (ret == LUA_TSTRING) |
| 5681 | body = lua_tolstring(L, -1, &body_len); |
| 5682 | lua_pop(L, 1); |
| 5683 | |
| 5684 | /* Prepare the response before inserting the headers */ |
| 5685 | h1m_init_res(&h1m); |
| 5686 | htx = htx_from_buf(&s->res.buf); |
| 5687 | channel_htx_truncate(&s->res, htx); |
| 5688 | if (s->txn->req.flags & HTTP_MSGF_VER_11) { |
| 5689 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 5690 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), |
| 5691 | ist2(status, status_len), ist2(reason, reason_len)); |
| 5692 | } |
| 5693 | else { |
| 5694 | flags = HTX_SL_F_IS_RESP; |
| 5695 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), |
| 5696 | ist2(status, status_len), ist2(reason, reason_len)); |
| 5697 | } |
| 5698 | if (!sl) |
| 5699 | goto fail; |
| 5700 | sl->info.res.status = code; |
| 5701 | |
| 5702 | /* Push in the stack the "headers" entry. */ |
| 5703 | ret = lua_getfield(L, -1, "headers"); |
| 5704 | if (ret != LUA_TTABLE) |
| 5705 | goto skip_headers; |
| 5706 | |
| 5707 | lua_pushnil(L); |
| 5708 | while (lua_next(L, -2) != 0) { |
| 5709 | struct ist name, value; |
| 5710 | const char *n, *v; |
| 5711 | size_t nlen, vlen; |
| 5712 | |
| 5713 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 5714 | /* Skip element if the key is not a string or if the value is not a table */ |
| 5715 | goto next_hdr; |
| 5716 | } |
| 5717 | |
| 5718 | n = lua_tolstring(L, -2, &nlen); |
| 5719 | name = ist2(n, nlen); |
| 5720 | if (isteqi(name, ist("content-length"))) { |
| 5721 | /* Always skip content-length header. It will be added |
| 5722 | * later with the correct len |
| 5723 | */ |
| 5724 | goto next_hdr; |
| 5725 | } |
| 5726 | |
| 5727 | /* Loop on header's values */ |
| 5728 | lua_pushnil(L); |
| 5729 | while (lua_next(L, -2)) { |
| 5730 | if (!lua_isstring(L, -1)) { |
| 5731 | /* Skip the value if it is not a string */ |
| 5732 | goto next_value; |
| 5733 | } |
| 5734 | |
| 5735 | v = lua_tolstring(L, -1, &vlen); |
| 5736 | value = ist2(v, vlen); |
| 5737 | |
| 5738 | if (isteqi(name, ist("transfer-encoding"))) |
| 5739 | h1_parse_xfer_enc_header(&h1m, value); |
| 5740 | if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen))) |
| 5741 | goto fail; |
| 5742 | |
| 5743 | next_value: |
| 5744 | lua_pop(L, 1); |
| 5745 | } |
| 5746 | |
| 5747 | next_hdr: |
| 5748 | lua_pop(L, 1); |
| 5749 | } |
| 5750 | skip_headers: |
| 5751 | lua_pop(L, 1); |
| 5752 | |
| 5753 | /* Update h1m flags: CLEN is set if CHNK is not present */ |
| 5754 | if (!(h1m.flags & H1_MF_CHNK)) { |
| 5755 | const char *clen = ultoa(body_len); |
| 5756 | |
| 5757 | h1m.flags |= H1_MF_CLEN; |
| 5758 | if (!htx_add_header(htx, ist("content-length"), ist(clen))) |
| 5759 | goto fail; |
| 5760 | } |
| 5761 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 5762 | h1m.flags |= H1_MF_XFER_LEN; |
| 5763 | |
| 5764 | /* Update HTX start-line flags */ |
| 5765 | if (h1m.flags & H1_MF_XFER_ENC) |
| 5766 | flags |= HTX_SL_F_XFER_ENC; |
| 5767 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 5768 | flags |= HTX_SL_F_XFER_LEN; |
| 5769 | if (h1m.flags & H1_MF_CHNK) |
| 5770 | flags |= HTX_SL_F_CHNK; |
| 5771 | else if (h1m.flags & H1_MF_CLEN) |
| 5772 | flags |= HTX_SL_F_CLEN; |
| 5773 | if (h1m.body_len == 0) |
| 5774 | flags |= HTX_SL_F_BODYLESS; |
| 5775 | } |
| 5776 | sl->flags |= flags; |
| 5777 | |
| 5778 | |
| 5779 | if (!htx_add_endof(htx, HTX_BLK_EOH) || |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5780 | (body_len && !htx_add_data_atonce(htx, ist2(body, body_len)))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5781 | goto fail; |
| 5782 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5783 | htx->flags |= HTX_FL_EOM; |
| 5784 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5785 | /* Now, forward the response and terminate the transaction */ |
| 5786 | s->txn->status = code; |
| 5787 | htx_to_buf(htx, &s->res.buf); |
| 5788 | if (!http_forward_proxy_resp(s, 1)) |
| 5789 | goto fail; |
| 5790 | |
| 5791 | return 1; |
| 5792 | |
| 5793 | fail: |
| 5794 | channel_htx_truncate(&s->res, htx); |
| 5795 | return 0; |
| 5796 | } |
| 5797 | |
| 5798 | /* Terminate a transaction if called from a lua action. For TCP streams, |
| 5799 | * processing is just aborted. Nothing is returned to the client and all |
| 5800 | * arguments are ignored. For HTTP streams, if a reply is passed as argument, it |
| 5801 | * is forwarded to the client before terminating the transaction. On success, |
| 5802 | * the function exits with ACT_RET_DONE code. If an error occurred, it exits |
| 5803 | * with ACT_RET_ERR code. If this function is not called from a lua action, it |
| 5804 | * just exits without any processing. |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5805 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5806 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5807 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5808 | struct hlua_txn *htxn; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5809 | struct stream *s; |
| 5810 | int finst; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5811 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5812 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5813 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5814 | /* If the flags NOTERM is set, we cannot terminate the session, so we |
| 5815 | * just end the execution of the current lua code. */ |
| 5816 | if (htxn->flags & HLUA_TXN_NOTERM) |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5817 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5818 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5819 | s = htxn->s; |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5820 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5821 | struct channel *req = &s->req; |
| 5822 | struct channel *res = &s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5823 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5824 | channel_auto_read(req); |
| 5825 | channel_abort(req); |
| 5826 | channel_auto_close(req); |
| 5827 | channel_erase(req); |
| 5828 | |
| 5829 | res->wex = tick_add_ifset(now_ms, res->wto); |
| 5830 | channel_auto_read(res); |
| 5831 | channel_auto_close(res); |
| 5832 | channel_shutr_now(res); |
| 5833 | |
| 5834 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D); |
| 5835 | goto done; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5836 | } |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 5837 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5838 | if (lua_gettop(L) == 1 || !lua_istable(L, 2)) { |
| 5839 | /* No reply or invalid reply */ |
| 5840 | s->txn->status = 0; |
| 5841 | http_reply_and_close(s, 0, NULL); |
| 5842 | } |
| 5843 | else { |
| 5844 | /* Remove extra args to have the reply on top of the stack */ |
| 5845 | if (lua_gettop(L) > 2) |
| 5846 | lua_pop(L, lua_gettop(L) - 2); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5847 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5848 | if (!hlua_txn_forward_reply(L, s)) { |
| 5849 | if (!(s->flags & SF_ERR_MASK)) |
| 5850 | s->flags |= SF_ERR_PRXCOND; |
| 5851 | lua_pushinteger(L, ACT_RET_ERR); |
| 5852 | WILL_LJMP(hlua_done(L)); |
| 5853 | return 0; /* Never reached */ |
| 5854 | } |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 5855 | } |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5856 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5857 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H); |
| 5858 | if (htxn->dir == SMP_OPT_DIR_REQ) { |
| 5859 | /* let's log the request time */ |
| 5860 | s->logs.tv_request = now; |
| 5861 | 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] | 5862 | _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5863 | } |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5864 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5865 | done: |
| 5866 | if (!(s->flags & SF_ERR_MASK)) |
| 5867 | s->flags |= SF_ERR_LOCAL; |
| 5868 | if (!(s->flags & SF_FINST_MASK)) |
| 5869 | s->flags |= finst; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5870 | |
Christopher Faulet | 4ad7310 | 2020-03-05 11:07:31 +0100 | [diff] [blame] | 5871 | lua_pushinteger(L, ACT_RET_ABRT); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5872 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5873 | return 0; |
| 5874 | } |
| 5875 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5876 | /* |
| 5877 | * |
| 5878 | * |
| 5879 | * Class REPLY |
| 5880 | * |
| 5881 | * |
| 5882 | */ |
| 5883 | |
| 5884 | /* Pushes the TXN reply onto the top of the stack. If the stask does not have a |
| 5885 | * free slots, the function fails and returns 0; |
| 5886 | */ |
| 5887 | static int hlua_txn_reply_new(lua_State *L) |
| 5888 | { |
| 5889 | struct hlua_txn *htxn; |
| 5890 | const char *reason, *body = NULL; |
| 5891 | int ret, status; |
| 5892 | |
| 5893 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5894 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5895 | hlua_pusherror(L, "txn object is not an HTTP transaction."); |
| 5896 | WILL_LJMP(lua_error(L)); |
| 5897 | } |
| 5898 | |
| 5899 | /* Default value */ |
| 5900 | status = 200; |
| 5901 | reason = http_get_reason(status); |
| 5902 | |
| 5903 | if (lua_istable(L, 2)) { |
| 5904 | /* load status and reason from the table argument at index 2 */ |
| 5905 | ret = lua_getfield(L, 2, "status"); |
| 5906 | if (ret == LUA_TNIL) |
| 5907 | goto reason; |
| 5908 | else if (ret != LUA_TNUMBER) { |
| 5909 | /* invalid status: ignore the reason */ |
| 5910 | goto body; |
| 5911 | } |
| 5912 | status = lua_tointeger(L, -1); |
| 5913 | |
| 5914 | reason: |
| 5915 | lua_pop(L, 1); /* restore the stack: remove status */ |
| 5916 | ret = lua_getfield(L, 2, "reason"); |
| 5917 | if (ret == LUA_TSTRING) |
| 5918 | reason = lua_tostring(L, -1); |
| 5919 | |
| 5920 | body: |
| 5921 | lua_pop(L, 1); /* restore the stack: remove invalid status or reason */ |
| 5922 | ret = lua_getfield(L, 2, "body"); |
| 5923 | if (ret == LUA_TSTRING) |
| 5924 | body = lua_tostring(L, -1); |
| 5925 | lua_pop(L, 1); /* restore the stack: remove body */ |
| 5926 | } |
| 5927 | |
| 5928 | /* Create the Reply table */ |
| 5929 | lua_newtable(L); |
| 5930 | |
| 5931 | /* Add status element */ |
| 5932 | lua_pushstring(L, "status"); |
| 5933 | lua_pushinteger(L, status); |
| 5934 | lua_settable(L, -3); |
| 5935 | |
| 5936 | /* Add reason element */ |
| 5937 | reason = http_get_reason(status); |
| 5938 | lua_pushstring(L, "reason"); |
| 5939 | lua_pushstring(L, reason); |
| 5940 | lua_settable(L, -3); |
| 5941 | |
| 5942 | /* Add body element, nil if undefined */ |
| 5943 | lua_pushstring(L, "body"); |
| 5944 | if (body) |
| 5945 | lua_pushstring(L, body); |
| 5946 | else |
| 5947 | lua_pushnil(L); |
| 5948 | lua_settable(L, -3); |
| 5949 | |
| 5950 | /* Add headers element */ |
| 5951 | lua_pushstring(L, "headers"); |
| 5952 | lua_newtable(L); |
| 5953 | |
| 5954 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 5955 | if (lua_istable(L, 2)) { |
| 5956 | /* load headers from the table argument at index 2. If it is a table, copy it. */ |
| 5957 | ret = lua_getfield(L, 2, "headers"); |
| 5958 | if (ret == LUA_TTABLE) { |
| 5959 | /* stack: [ ... <headers:table>, <table> ] */ |
| 5960 | lua_pushnil(L); |
| 5961 | while (lua_next(L, -2) != 0) { |
| 5962 | /* stack: [ ... <headers:table>, <table>, k, v] */ |
| 5963 | if (!lua_isstring(L, -1) && !lua_istable(L, -1)) { |
| 5964 | /* invalid value type, skip it */ |
| 5965 | lua_pop(L, 1); |
| 5966 | continue; |
| 5967 | } |
| 5968 | |
| 5969 | |
| 5970 | /* Duplicate the key and swap it with the value. */ |
| 5971 | lua_pushvalue(L, -2); |
| 5972 | lua_insert(L, -2); |
| 5973 | /* stack: [ ... <headers:table>, <table>, k, k, v ] */ |
| 5974 | |
| 5975 | lua_newtable(L); |
| 5976 | lua_insert(L, -2); |
| 5977 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */ |
| 5978 | |
| 5979 | if (lua_isstring(L, -1)) { |
| 5980 | /* push the value in the inner table */ |
| 5981 | lua_rawseti(L, -2, 1); |
| 5982 | } |
| 5983 | else { /* table */ |
| 5984 | lua_pushnil(L); |
| 5985 | while (lua_next(L, -2) != 0) { |
| 5986 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */ |
| 5987 | if (!lua_isstring(L, -1)) { |
| 5988 | /* invalid value type, skip it*/ |
| 5989 | lua_pop(L, 1); |
| 5990 | continue; |
| 5991 | } |
| 5992 | /* push the value in the inner table */ |
| 5993 | lua_rawseti(L, -4, lua_rawlen(L, -4) + 1); |
| 5994 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */ |
| 5995 | } |
| 5996 | lua_pop(L, 1); |
| 5997 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */ |
| 5998 | } |
| 5999 | |
| 6000 | /* push (k,v) on the stack in the headers table: |
| 6001 | * stack: [ ... <headers:table>, <table>, k, k, v ] |
| 6002 | */ |
| 6003 | lua_settable(L, -5); |
| 6004 | /* stack: [ ... <headers:table>, <table>, k ] */ |
| 6005 | } |
| 6006 | } |
| 6007 | lua_pop(L, 1); |
| 6008 | } |
| 6009 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 6010 | lua_settable(L, -3); |
| 6011 | /* stack: [ txn, <Arg:table>, <Reply:table> ] */ |
| 6012 | |
| 6013 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 6014 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref); |
| 6015 | lua_setmetatable(L, -2); |
| 6016 | return 1; |
| 6017 | } |
| 6018 | |
| 6019 | /* Set the reply status code, and optionally the reason. If no reason is |
| 6020 | * provided, the default one corresponding to the status code is used. |
| 6021 | */ |
| 6022 | __LJMP static int hlua_txn_reply_set_status(lua_State *L) |
| 6023 | { |
| 6024 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6025 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 6026 | |
| 6027 | /* First argument (self) must be a table */ |
| 6028 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6029 | |
| 6030 | if (status < 100 || status > 599) { |
| 6031 | lua_pushboolean(L, 0); |
| 6032 | return 1; |
| 6033 | } |
| 6034 | if (!reason) |
| 6035 | reason = http_get_reason(status); |
| 6036 | |
| 6037 | lua_pushinteger(L, status); |
| 6038 | lua_setfield(L, 1, "status"); |
| 6039 | |
| 6040 | lua_pushstring(L, reason); |
| 6041 | lua_setfield(L, 1, "reason"); |
| 6042 | |
| 6043 | lua_pushboolean(L, 1); |
| 6044 | return 1; |
| 6045 | } |
| 6046 | |
| 6047 | /* Add a header into the reply object. Each header name is associated to an |
| 6048 | * array of values in the "headers" table. If the header name is not found, a |
| 6049 | * new entry is created. |
| 6050 | */ |
| 6051 | __LJMP static int hlua_txn_reply_add_header(lua_State *L) |
| 6052 | { |
| 6053 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6054 | const char *value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 6055 | int ret; |
| 6056 | |
| 6057 | /* First argument (self) must be a table */ |
| 6058 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6059 | |
| 6060 | /* Push in the stack the "headers" entry. */ |
| 6061 | ret = lua_getfield(L, 1, "headers"); |
| 6062 | if (ret != LUA_TTABLE) { |
| 6063 | hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret)); |
| 6064 | WILL_LJMP(lua_error(L)); |
| 6065 | } |
| 6066 | |
| 6067 | /* check if the header is already registered. If not, register it. */ |
| 6068 | ret = lua_getfield(L, -1, name); |
| 6069 | if (ret == LUA_TNIL) { |
| 6070 | /* Entry not found. */ |
| 6071 | lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */ |
| 6072 | |
| 6073 | /* Insert the new header name in the array in the top of the stack. |
| 6074 | * It left the new array in the top of the stack. |
| 6075 | */ |
| 6076 | lua_newtable(L); |
| 6077 | lua_pushstring(L, name); |
| 6078 | lua_pushvalue(L, -2); |
| 6079 | lua_settable(L, -4); |
| 6080 | } |
| 6081 | else if (ret != LUA_TTABLE) { |
| 6082 | hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret)); |
| 6083 | WILL_LJMP(lua_error(L)); |
| 6084 | } |
| 6085 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 6086 | /* Now the top of thestack is an array of values. We push |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6087 | * the header value as new entry. |
| 6088 | */ |
| 6089 | lua_pushstring(L, value); |
| 6090 | ret = lua_rawlen(L, -2); |
| 6091 | lua_rawseti(L, -2, ret + 1); |
| 6092 | |
| 6093 | lua_pushboolean(L, 1); |
| 6094 | return 1; |
| 6095 | } |
| 6096 | |
| 6097 | /* Remove all occurrences of a given header name. */ |
| 6098 | __LJMP static int hlua_txn_reply_del_header(lua_State *L) |
| 6099 | { |
| 6100 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6101 | int ret; |
| 6102 | |
| 6103 | /* First argument (self) must be a table */ |
| 6104 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6105 | |
| 6106 | /* Push in the stack the "headers" entry. */ |
| 6107 | ret = lua_getfield(L, 1, "headers"); |
| 6108 | if (ret != LUA_TTABLE) { |
| 6109 | hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret)); |
| 6110 | WILL_LJMP(lua_error(L)); |
| 6111 | } |
| 6112 | |
| 6113 | lua_pushstring(L, name); |
| 6114 | lua_pushnil(L); |
| 6115 | lua_settable(L, -3); |
| 6116 | |
| 6117 | lua_pushboolean(L, 1); |
| 6118 | return 1; |
| 6119 | } |
| 6120 | |
| 6121 | /* Set the reply's body. Overwrite any existing entry. */ |
| 6122 | __LJMP static int hlua_txn_reply_set_body(lua_State *L) |
| 6123 | { |
| 6124 | const char *payload = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6125 | |
| 6126 | /* First argument (self) must be a table */ |
| 6127 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6128 | |
| 6129 | lua_pushstring(L, payload); |
| 6130 | lua_setfield(L, 1, "body"); |
| 6131 | |
| 6132 | lua_pushboolean(L, 1); |
| 6133 | return 1; |
| 6134 | } |
| 6135 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 6136 | __LJMP static int hlua_log(lua_State *L) |
| 6137 | { |
| 6138 | int level; |
| 6139 | const char *msg; |
| 6140 | |
| 6141 | MAY_LJMP(check_args(L, 2, "log")); |
| 6142 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 6143 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6144 | |
| 6145 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 6146 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 6147 | |
| 6148 | hlua_sendlog(NULL, level, msg); |
| 6149 | return 0; |
| 6150 | } |
| 6151 | |
| 6152 | __LJMP static int hlua_log_debug(lua_State *L) |
| 6153 | { |
| 6154 | const char *msg; |
| 6155 | |
| 6156 | MAY_LJMP(check_args(L, 1, "debug")); |
| 6157 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6158 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 6159 | return 0; |
| 6160 | } |
| 6161 | |
| 6162 | __LJMP static int hlua_log_info(lua_State *L) |
| 6163 | { |
| 6164 | const char *msg; |
| 6165 | |
| 6166 | MAY_LJMP(check_args(L, 1, "info")); |
| 6167 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6168 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 6169 | return 0; |
| 6170 | } |
| 6171 | |
| 6172 | __LJMP static int hlua_log_warning(lua_State *L) |
| 6173 | { |
| 6174 | const char *msg; |
| 6175 | |
| 6176 | MAY_LJMP(check_args(L, 1, "warning")); |
| 6177 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6178 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 6179 | return 0; |
| 6180 | } |
| 6181 | |
| 6182 | __LJMP static int hlua_log_alert(lua_State *L) |
| 6183 | { |
| 6184 | const char *msg; |
| 6185 | |
| 6186 | MAY_LJMP(check_args(L, 1, "alert")); |
| 6187 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6188 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6189 | return 0; |
| 6190 | } |
| 6191 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6192 | __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] | 6193 | { |
| 6194 | int wakeup_ms = lua_tointeger(L, -1); |
| 6195 | if (now_ms < wakeup_ms) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6196 | 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] | 6197 | return 0; |
| 6198 | } |
| 6199 | |
| 6200 | __LJMP static int hlua_sleep(lua_State *L) |
| 6201 | { |
| 6202 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6203 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6204 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6205 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6206 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6207 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6208 | wakeup_ms = tick_add(now_ms, delay); |
| 6209 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6210 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6211 | 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] | 6212 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6213 | } |
| 6214 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6215 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6216 | { |
| 6217 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6218 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6219 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6220 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6221 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6222 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6223 | wakeup_ms = tick_add(now_ms, delay); |
| 6224 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6225 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6226 | 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] | 6227 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6228 | } |
| 6229 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6230 | /* This functionis an LUA binding. it permits to give back |
| 6231 | * the hand at the HAProxy scheduler. It is used when the |
| 6232 | * LUA processing consumes a lot of time. |
| 6233 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6234 | __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] | 6235 | { |
| 6236 | return 0; |
| 6237 | } |
| 6238 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6239 | __LJMP static int hlua_yield(lua_State *L) |
| 6240 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6241 | 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] | 6242 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6243 | } |
| 6244 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6245 | /* This function change the nice of the currently executed |
| 6246 | * task. It is used set low or high priority at the current |
| 6247 | * task. |
| 6248 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 6249 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6250 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6251 | struct hlua *hlua; |
| 6252 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6253 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6254 | MAY_LJMP(check_args(L, 1, "set_nice")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6255 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6256 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 6257 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 6258 | hlua = hlua_gethlua(L); |
| 6259 | |
| 6260 | /* If the task is not set, I'm in a start mode. */ |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6261 | if (!hlua || !hlua->task) |
| 6262 | return 0; |
| 6263 | |
| 6264 | if (nice < -1024) |
| 6265 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6266 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6267 | nice = 1024; |
| 6268 | |
| 6269 | hlua->task->nice = nice; |
| 6270 | return 0; |
| 6271 | } |
| 6272 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6273 | /* 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] | 6274 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 6275 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 6276 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6277 | * |
| 6278 | * Task wrapper are longjmp safe because the only one Lua code |
| 6279 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6280 | */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 6281 | 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] | 6282 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6283 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6284 | enum hlua_exec status; |
| 6285 | |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 6286 | if (task->thread_mask == MAX_THREADS_MASK) |
| 6287 | task_set_affinity(task, tid_bit); |
| 6288 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6289 | /* If it is the first call to the task, we must initialize the |
| 6290 | * execution timeouts. |
| 6291 | */ |
| 6292 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6293 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6294 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6295 | /* Execute the Lua code. */ |
| 6296 | status = hlua_ctx_resume(hlua, 1); |
| 6297 | |
| 6298 | switch (status) { |
| 6299 | /* finished or yield */ |
| 6300 | case HLUA_E_OK: |
| 6301 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6302 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 6303 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6304 | break; |
| 6305 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6306 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 6307 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 6308 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6309 | break; |
| 6310 | |
| 6311 | /* finished with error. */ |
| 6312 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6313 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6314 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6315 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6316 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6317 | break; |
| 6318 | |
| 6319 | case HLUA_E_ERR: |
| 6320 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6321 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6322 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6323 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6324 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6325 | break; |
| 6326 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6327 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6328 | } |
| 6329 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6330 | /* This function is an LUA binding that register LUA function to be |
| 6331 | * executed after the HAProxy configuration parsing and before the |
| 6332 | * HAProxy scheduler starts. This function expect only one LUA |
| 6333 | * argument that is a function. This function returns nothing, but |
| 6334 | * throws if an error is encountered. |
| 6335 | */ |
| 6336 | __LJMP static int hlua_register_init(lua_State *L) |
| 6337 | { |
| 6338 | struct hlua_init_function *init; |
| 6339 | int ref; |
| 6340 | |
| 6341 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 6342 | |
| 6343 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6344 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6345 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6346 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6347 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6348 | |
| 6349 | init->function_ref = ref; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 6350 | LIST_APPEND(&hlua_init_functions[hlua_state_id], &init->l); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6351 | return 0; |
| 6352 | } |
| 6353 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6354 | /* This functio is an LUA binding. It permits to register a task |
| 6355 | * executed in parallel of the main HAroxy activity. The task is |
| 6356 | * created and it is set in the HAProxy scheduler. It can be called |
| 6357 | * from the "init" section, "post init" or during the runtime. |
| 6358 | * |
| 6359 | * Lua prototype: |
| 6360 | * |
| 6361 | * <none> core.register_task(<function>) |
| 6362 | */ |
| 6363 | static int hlua_register_task(lua_State *L) |
| 6364 | { |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6365 | struct hlua *hlua = NULL; |
| 6366 | struct task *task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6367 | int ref; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 6368 | int state_id; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6369 | |
| 6370 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 6371 | |
| 6372 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6373 | |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 6374 | /* Get the reference state. If the reference is NULL, L is the master |
| 6375 | * state, otherwise hlua->T is. |
| 6376 | */ |
| 6377 | hlua = hlua_gethlua(L); |
| 6378 | if (hlua) |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 6379 | /* we are in runtime processing */ |
| 6380 | state_id = hlua->state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 6381 | else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 6382 | /* we are in initialization mode */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6383 | state_id = hlua_state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 6384 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6385 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6386 | if (!hlua) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6387 | goto alloc_error; |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6388 | HLUA_INIT(hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6389 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 6390 | /* We are in the common lua state, execute the task anywhere, |
| 6391 | * otherwise, inherit the current thread identifier |
| 6392 | */ |
| 6393 | if (state_id == 0) |
| 6394 | task = task_new(MAX_THREADS_MASK); |
| 6395 | else |
| 6396 | task = task_new(tid_bit); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 6397 | if (!task) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6398 | goto alloc_error; |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 6399 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6400 | task->context = hlua; |
| 6401 | task->process = hlua_process_task; |
| 6402 | |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 6403 | if (!hlua_ctx_init(hlua, state_id, task, 1)) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6404 | goto alloc_error; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6405 | |
| 6406 | /* Restore the function in the stack. */ |
| 6407 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 6408 | hlua->nargs = 0; |
| 6409 | |
| 6410 | /* Schedule task. */ |
| 6411 | task_schedule(task, now_ms); |
| 6412 | |
| 6413 | return 0; |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 6414 | |
| 6415 | alloc_error: |
| 6416 | task_destroy(task); |
| 6417 | hlua_ctx_destroy(hlua); |
| 6418 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 6419 | return 0; /* Never reached */ |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6420 | } |
| 6421 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6422 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 6423 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 6424 | * resume converters. |
| 6425 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6426 | 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] | 6427 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6428 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6429 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6430 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6431 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6432 | if (!stream) |
| 6433 | return 0; |
| 6434 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6435 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6436 | * Lua context can be not initialized. This behavior |
| 6437 | * permits to save performances because a systematic |
| 6438 | * Lua initialization cause 5% performances loss. |
| 6439 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6440 | if (!stream->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6441 | struct hlua *hlua; |
| 6442 | |
| 6443 | hlua = pool_alloc(pool_head_hlua); |
| 6444 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6445 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6446 | return 0; |
| 6447 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6448 | HLUA_INIT(hlua); |
| 6449 | stream->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6450 | 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] | 6451 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6452 | return 0; |
| 6453 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6454 | } |
| 6455 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6456 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6457 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6458 | |
| 6459 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6460 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6461 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6462 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6463 | else |
| 6464 | error = "critical error"; |
| 6465 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6466 | return 0; |
| 6467 | } |
| 6468 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6469 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6470 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6471 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6472 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6473 | return 0; |
| 6474 | } |
| 6475 | |
| 6476 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6477 | 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] | 6478 | |
| 6479 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6480 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6481 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6482 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6483 | return 0; |
| 6484 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6485 | hlua_smp2lua(stream->hlua->T, smp); |
| 6486 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6487 | |
| 6488 | /* push keywords in the stack. */ |
| 6489 | if (arg_p) { |
| 6490 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6491 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6492 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6493 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6494 | return 0; |
| 6495 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6496 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6497 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6498 | } |
| 6499 | } |
| 6500 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6501 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6502 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6503 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6504 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6505 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6506 | } |
| 6507 | |
| 6508 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6509 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6510 | /* finished. */ |
| 6511 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6512 | /* If the stack is empty, the function fails. */ |
| 6513 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6514 | return 0; |
| 6515 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6516 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6517 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6518 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6519 | return 1; |
| 6520 | |
| 6521 | /* yield. */ |
| 6522 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6523 | 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] | 6524 | return 0; |
| 6525 | |
| 6526 | /* finished with error. */ |
| 6527 | case HLUA_E_ERRMSG: |
| 6528 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6529 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6530 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6531 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6532 | return 0; |
| 6533 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6534 | case HLUA_E_ETMOUT: |
| 6535 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 6536 | return 0; |
| 6537 | |
| 6538 | case HLUA_E_NOMEM: |
| 6539 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 6540 | return 0; |
| 6541 | |
| 6542 | case HLUA_E_YIELD: |
| 6543 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 6544 | return 0; |
| 6545 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6546 | case HLUA_E_ERR: |
| 6547 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6548 | 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] | 6549 | /* fall through */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6550 | |
| 6551 | default: |
| 6552 | return 0; |
| 6553 | } |
| 6554 | } |
| 6555 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6556 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 6557 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6558 | * resume sample-fetches. This function will be called by the sample |
| 6559 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6560 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6561 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 6562 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6563 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6564 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6565 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6566 | const char *error; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6567 | unsigned int hflags = HLUA_TXN_NOTERM; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6568 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6569 | if (!stream) |
| 6570 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 6571 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6572 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6573 | * Lua context can be not initialized. This behavior |
| 6574 | * permits to save performances because a systematic |
| 6575 | * Lua initialization cause 5% performances loss. |
| 6576 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6577 | if (!stream->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6578 | struct hlua *hlua; |
| 6579 | |
| 6580 | hlua = pool_alloc(pool_head_hlua); |
| 6581 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6582 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6583 | return 0; |
| 6584 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6585 | hlua->T = NULL; |
| 6586 | stream->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6587 | 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] | 6588 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6589 | return 0; |
| 6590 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6591 | } |
| 6592 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6593 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6594 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6595 | |
| 6596 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6597 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6598 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6599 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6600 | else |
| 6601 | error = "critical error"; |
| 6602 | 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] | 6603 | return 0; |
| 6604 | } |
| 6605 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6606 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6607 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6608 | 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] | 6609 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6610 | return 0; |
| 6611 | } |
| 6612 | |
| 6613 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6614 | 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] | 6615 | |
| 6616 | /* push arguments in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6617 | 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] | 6618 | 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] | 6619 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6620 | return 0; |
| 6621 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6622 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6623 | |
| 6624 | /* push keywords in the stack. */ |
| 6625 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 6626 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6627 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6628 | 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] | 6629 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6630 | return 0; |
| 6631 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6632 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6633 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6634 | } |
| 6635 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6636 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6637 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6638 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6639 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6640 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6641 | } |
| 6642 | |
| 6643 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6644 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6645 | /* finished. */ |
| 6646 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6647 | /* If the stack is empty, the function fails. */ |
| 6648 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6649 | return 0; |
| 6650 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6651 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6652 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6653 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6654 | |
| 6655 | /* Set the end of execution flag. */ |
| 6656 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 6657 | return 1; |
| 6658 | |
| 6659 | /* yield. */ |
| 6660 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6661 | 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] | 6662 | return 0; |
| 6663 | |
| 6664 | /* finished with error. */ |
| 6665 | case HLUA_E_ERRMSG: |
| 6666 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6667 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6668 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6669 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6670 | return 0; |
| 6671 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6672 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6673 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 6674 | return 0; |
| 6675 | |
| 6676 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6677 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 6678 | return 0; |
| 6679 | |
| 6680 | case HLUA_E_YIELD: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6681 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 6682 | return 0; |
| 6683 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6684 | case HLUA_E_ERR: |
| 6685 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6686 | 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] | 6687 | /* fall through */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6688 | |
| 6689 | default: |
| 6690 | return 0; |
| 6691 | } |
| 6692 | } |
| 6693 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6694 | /* This function is an LUA binding used for registering |
| 6695 | * "sample-conv" functions. It expects a converter name used |
| 6696 | * in the haproxy configuration file, and an LUA function. |
| 6697 | */ |
| 6698 | __LJMP static int hlua_register_converters(lua_State *L) |
| 6699 | { |
| 6700 | struct sample_conv_kw_list *sck; |
| 6701 | const char *name; |
| 6702 | int ref; |
| 6703 | int len; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6704 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6705 | struct sample_conv *sc; |
| 6706 | struct buffer *trash; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6707 | |
| 6708 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 6709 | |
| 6710 | /* First argument : converter name. */ |
| 6711 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6712 | |
| 6713 | /* Second argument : lua function. */ |
| 6714 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6715 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6716 | /* Check if the converter is already registered */ |
| 6717 | trash = get_trash_chunk(); |
| 6718 | chunk_printf(trash, "lua.%s", name); |
| 6719 | sc = find_sample_conv(trash->area, trash->data); |
| 6720 | if (sc != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 6721 | fcn = sc->private; |
| 6722 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 6723 | ha_warning("Trying to register converter 'lua.%s' more than once. " |
| 6724 | "This will become a hard error in version 2.5.\n", name); |
| 6725 | } |
| 6726 | fcn->function_ref[hlua_state_id] = ref; |
| 6727 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6728 | } |
| 6729 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6730 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6731 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6732 | if (!sck) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6733 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 6734 | fcn = new_hlua_function(); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6735 | if (!fcn) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6736 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6737 | |
| 6738 | /* Fill fcn. */ |
| 6739 | fcn->name = strdup(name); |
| 6740 | if (!fcn->name) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6741 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6742 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6743 | |
| 6744 | /* List head */ |
| 6745 | sck->list.n = sck->list.p = NULL; |
| 6746 | |
| 6747 | /* converter keyword. */ |
| 6748 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6749 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6750 | if (!sck->kw[0].kw) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6751 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6752 | |
| 6753 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 6754 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6755 | 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] | 6756 | sck->kw[0].val_args = NULL; |
| 6757 | sck->kw[0].in_type = SMP_T_STR; |
| 6758 | sck->kw[0].out_type = SMP_T_STR; |
| 6759 | sck->kw[0].private = fcn; |
| 6760 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6761 | /* Register this new converter */ |
| 6762 | sample_register_convs(sck); |
| 6763 | |
| 6764 | return 0; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 6765 | |
| 6766 | alloc_error: |
| 6767 | release_hlua_function(fcn); |
| 6768 | ha_free(&sck); |
| 6769 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 6770 | return 0; /* Never reached */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6771 | } |
| 6772 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6773 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6774 | * "sample-fetch" functions. It expects a converter name used |
| 6775 | * in the haproxy configuration file, and an LUA function. |
| 6776 | */ |
| 6777 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 6778 | { |
| 6779 | const char *name; |
| 6780 | int ref; |
| 6781 | int len; |
| 6782 | struct sample_fetch_kw_list *sfk; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 6783 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6784 | struct sample_fetch *sf; |
| 6785 | struct buffer *trash; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6786 | |
| 6787 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 6788 | |
| 6789 | /* First argument : sample-fetch name. */ |
| 6790 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6791 | |
| 6792 | /* Second argument : lua function. */ |
| 6793 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6794 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6795 | /* Check if the sample-fetch is already registered */ |
| 6796 | trash = get_trash_chunk(); |
| 6797 | chunk_printf(trash, "lua.%s", name); |
| 6798 | sf = find_sample_fetch(trash->area, trash->data); |
| 6799 | if (sf != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 6800 | fcn = sf->private; |
| 6801 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 6802 | ha_warning("Trying to register sample-fetch 'lua.%s' more than once. " |
| 6803 | "This will become a hard error in version 2.5.\n", name); |
| 6804 | } |
| 6805 | fcn->function_ref[hlua_state_id] = ref; |
| 6806 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6807 | } |
| 6808 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6809 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6810 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6811 | if (!sfk) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 6812 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 6813 | fcn = new_hlua_function(); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6814 | if (!fcn) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 6815 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6816 | |
| 6817 | /* Fill fcn. */ |
| 6818 | fcn->name = strdup(name); |
| 6819 | if (!fcn->name) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 6820 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6821 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6822 | |
| 6823 | /* List head */ |
| 6824 | sfk->list.n = sfk->list.p = NULL; |
| 6825 | |
| 6826 | /* sample-fetch keyword. */ |
| 6827 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6828 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6829 | if (!sfk->kw[0].kw) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 6830 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6831 | |
| 6832 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 6833 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6834 | 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] | 6835 | sfk->kw[0].val_args = NULL; |
| 6836 | sfk->kw[0].out_type = SMP_T_STR; |
| 6837 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 6838 | sfk->kw[0].val = 0; |
| 6839 | sfk->kw[0].private = fcn; |
| 6840 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6841 | /* Register this new fetch. */ |
| 6842 | sample_register_fetches(sfk); |
| 6843 | |
| 6844 | return 0; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 6845 | |
| 6846 | alloc_error: |
| 6847 | release_hlua_function(fcn); |
| 6848 | ha_free(&sfk); |
| 6849 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 6850 | return 0; /* Never reached */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6851 | } |
| 6852 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 6853 | /* This function is a lua binding to set the wake_time. |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6854 | */ |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 6855 | __LJMP static int hlua_set_wake_time(lua_State *L) |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6856 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 6857 | struct hlua *hlua; |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6858 | unsigned int delay; |
| 6859 | unsigned int wakeup_ms; |
| 6860 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 6861 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 6862 | hlua = hlua_gethlua(L); |
| 6863 | if (!hlua) { |
| 6864 | return 0; |
| 6865 | } |
| 6866 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6867 | MAY_LJMP(check_args(L, 1, "wake_time")); |
| 6868 | |
| 6869 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 6870 | wakeup_ms = tick_add(now_ms, delay); |
| 6871 | hlua->wake_time = wakeup_ms; |
| 6872 | return 0; |
| 6873 | } |
| 6874 | |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6875 | /* This function is a wrapper to execute each LUA function declared as an action |
| 6876 | * wrapper during the initialisation period. This function may return any |
| 6877 | * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is |
| 6878 | * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the |
| 6879 | * return value is the first element on the stack. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6880 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6881 | 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] | 6882 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6883 | { |
| 6884 | char **arg; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6885 | unsigned int hflags = 0; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6886 | int dir, act_ret = ACT_RET_CONT; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6887 | const char *error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6888 | |
| 6889 | switch (rule->from) { |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 6890 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 6891 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 6892 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 6893 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6894 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6895 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6896 | goto end; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6897 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6898 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6899 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6900 | * Lua context can be not initialized. This behavior |
| 6901 | * permits to save performances because a systematic |
| 6902 | * Lua initialization cause 5% performances loss. |
| 6903 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6904 | if (!s->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6905 | struct hlua *hlua; |
| 6906 | |
| 6907 | hlua = pool_alloc(pool_head_hlua); |
| 6908 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6909 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6910 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6911 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6912 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6913 | HLUA_INIT(hlua); |
| 6914 | s->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6915 | 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] | 6916 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6917 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6918 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6919 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6920 | } |
| 6921 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6922 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6923 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6924 | |
| 6925 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6926 | if (!SET_SAFE_LJMP(s->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6927 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 6928 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6929 | else |
| 6930 | error = "critical error"; |
| 6931 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6932 | rule->arg.hlua_rule->fcn->name, error); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6933 | goto end; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6934 | } |
| 6935 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6936 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6937 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6938 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6939 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6940 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6941 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6942 | } |
| 6943 | |
| 6944 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6945 | 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] | 6946 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6947 | /* Create and and push object stream in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6948 | if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6949 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6950 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6951 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6952 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6953 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6954 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6955 | |
| 6956 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6957 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6958 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6959 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6960 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6961 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6962 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6963 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6964 | lua_pushstring(s->hlua->T, *arg); |
| 6965 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6966 | } |
| 6967 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6968 | /* Now the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6969 | RESET_SAFE_LJMP(s->hlua); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6970 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6971 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6972 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6973 | } |
| 6974 | |
| 6975 | /* Execute the function. */ |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 6976 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6977 | /* finished. */ |
| 6978 | case HLUA_E_OK: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6979 | /* Catch the return value */ |
| 6980 | if (lua_gettop(s->hlua->T) > 0) |
| 6981 | act_ret = lua_tointeger(s->hlua->T, -1); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6982 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6983 | /* Set timeout in the required channel. */ |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 6984 | if (act_ret == ACT_RET_YIELD) { |
| 6985 | if (flags & ACT_OPT_FINAL) |
| 6986 | goto err_yield; |
| 6987 | |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 6988 | if (dir == SMP_OPT_DIR_REQ) |
| 6989 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 6990 | s->hlua->wake_time); |
| 6991 | else |
| 6992 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 6993 | s->hlua->wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6994 | } |
| 6995 | goto end; |
| 6996 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6997 | /* yield. */ |
| 6998 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6999 | /* Set timeout in the required channel. */ |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 7000 | if (dir == SMP_OPT_DIR_REQ) |
| 7001 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 7002 | s->hlua->wake_time); |
| 7003 | else |
| 7004 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 7005 | s->hlua->wake_time); |
| 7006 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7007 | /* Some actions can be wake up when a "write" event |
| 7008 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7009 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7010 | */ |
Christopher Faulet | 51fa358 | 2019-07-26 14:54:52 +0200 | [diff] [blame] | 7011 | if (HLUA_IS_WAKERESWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7012 | s->res.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7013 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7014 | s->req.flags |= CF_WAKE_WRITE; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7015 | act_ret = ACT_RET_YIELD; |
| 7016 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7017 | |
| 7018 | /* finished with error. */ |
| 7019 | case HLUA_E_ERRMSG: |
| 7020 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7021 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7022 | rule->arg.hlua_rule->fcn->name, lua_tostring(s->hlua->T, -1)); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7023 | lua_pop(s->hlua->T, 1); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7024 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7025 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7026 | case HLUA_E_ETMOUT: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7027 | 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] | 7028 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7029 | |
| 7030 | case HLUA_E_NOMEM: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7031 | 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] | 7032 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7033 | |
| 7034 | case HLUA_E_YIELD: |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 7035 | err_yield: |
| 7036 | act_ret = ACT_RET_CONT; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7037 | 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] | 7038 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7039 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7040 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7041 | case HLUA_E_ERR: |
| 7042 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7043 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7044 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7045 | |
| 7046 | default: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7047 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7048 | } |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7049 | |
| 7050 | end: |
Christopher Faulet | 2361fd9 | 2020-07-30 10:40:58 +0200 | [diff] [blame] | 7051 | if (act_ret != ACT_RET_YIELD && s->hlua) |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 7052 | s->hlua->wake_time = TICK_ETERNITY; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7053 | return act_ret; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7054 | } |
| 7055 | |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 7056 | 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] | 7057 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 7058 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7059 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7060 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 7061 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 7062 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7063 | } |
| 7064 | |
| 7065 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 7066 | { |
| 7067 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7068 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7069 | struct task *task; |
| 7070 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7071 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7072 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7073 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7074 | if (!hlua) { |
| 7075 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7076 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7077 | return 0; |
| 7078 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7079 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7080 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7081 | ctx->ctx.hlua_apptcp.flags = 0; |
| 7082 | |
| 7083 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7084 | task = task_new(tid_bit); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7085 | if (!task) { |
| 7086 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7087 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7088 | return 0; |
| 7089 | } |
| 7090 | task->nice = 0; |
| 7091 | task->context = ctx; |
| 7092 | task->process = hlua_applet_wakeup; |
| 7093 | ctx->ctx.hlua_apptcp.task = task; |
| 7094 | |
| 7095 | /* In the execution wrappers linked with a stream, the |
| 7096 | * Lua context can be not initialized. This behavior |
| 7097 | * permits to save performances because a systematic |
| 7098 | * Lua initialization cause 5% performances loss. |
| 7099 | */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7100 | 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] | 7101 | 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] | 7102 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7103 | return 0; |
| 7104 | } |
| 7105 | |
| 7106 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 7107 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7108 | |
| 7109 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7110 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7111 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7112 | error = lua_tostring(hlua->T, -1); |
| 7113 | else |
| 7114 | error = "critical error"; |
| 7115 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7116 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7117 | return 0; |
| 7118 | } |
| 7119 | |
| 7120 | /* Check stack available size. */ |
| 7121 | if (!lua_checkstack(hlua->T, 1)) { |
| 7122 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7123 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7124 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7125 | return 0; |
| 7126 | } |
| 7127 | |
| 7128 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7129 | 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] | 7130 | |
| 7131 | /* Create and and push object stream in the stack. */ |
| 7132 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 7133 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7134 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7135 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7136 | return 0; |
| 7137 | } |
| 7138 | hlua->nargs = 1; |
| 7139 | |
| 7140 | /* push keywords in the stack. */ |
| 7141 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 7142 | if (!lua_checkstack(hlua->T, 1)) { |
| 7143 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7144 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7145 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7146 | return 0; |
| 7147 | } |
| 7148 | lua_pushstring(hlua->T, *arg); |
| 7149 | hlua->nargs++; |
| 7150 | } |
| 7151 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7152 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7153 | |
| 7154 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7155 | si_cant_get(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 7156 | si_rx_endp_more(si); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7157 | |
| 7158 | return 1; |
| 7159 | } |
| 7160 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7161 | void hlua_applet_tcp_fct(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7162 | { |
| 7163 | struct stream_interface *si = ctx->owner; |
| 7164 | struct stream *strm = si_strm(si); |
| 7165 | struct channel *res = si_ic(si); |
| 7166 | struct act_rule *rule = ctx->rule; |
| 7167 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7168 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7169 | |
| 7170 | /* The applet execution is already done. */ |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 7171 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) { |
| 7172 | /* eat the whole request */ |
| 7173 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7174 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 7175 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7176 | |
| 7177 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7178 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7179 | return; |
| 7180 | |
| 7181 | /* Execute the function. */ |
| 7182 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7183 | /* finished. */ |
| 7184 | case HLUA_E_OK: |
| 7185 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 7186 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7187 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 7188 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7189 | res->flags |= CF_READ_NULL; |
| 7190 | si_shutr(si); |
| 7191 | return; |
| 7192 | |
| 7193 | /* yield. */ |
| 7194 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 7195 | if (hlua->wake_time != TICK_ETERNITY) |
| 7196 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7197 | return; |
| 7198 | |
| 7199 | /* finished with error. */ |
| 7200 | case HLUA_E_ERRMSG: |
| 7201 | /* Display log. */ |
| 7202 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7203 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7204 | lua_pop(hlua->T, 1); |
| 7205 | goto error; |
| 7206 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7207 | case HLUA_E_ETMOUT: |
| 7208 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7209 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7210 | goto error; |
| 7211 | |
| 7212 | case HLUA_E_NOMEM: |
| 7213 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7214 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7215 | goto error; |
| 7216 | |
| 7217 | case HLUA_E_YIELD: /* unexpected */ |
| 7218 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7219 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7220 | goto error; |
| 7221 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7222 | case HLUA_E_ERR: |
| 7223 | /* Display log. */ |
| 7224 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7225 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7226 | goto error; |
| 7227 | |
| 7228 | default: |
| 7229 | goto error; |
| 7230 | } |
| 7231 | |
| 7232 | error: |
| 7233 | |
| 7234 | /* For all other cases, just close the stream. */ |
| 7235 | si_shutw(si); |
| 7236 | si_shutr(si); |
| 7237 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 7238 | } |
| 7239 | |
| 7240 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 7241 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7242 | task_destroy(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7243 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7244 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7245 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7246 | } |
| 7247 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7248 | /* The function returns 1 if the initialisation is complete, 0 if |
| 7249 | * an errors occurs and -1 if more data are required for initializing |
| 7250 | * the applet. |
| 7251 | */ |
| 7252 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 7253 | { |
| 7254 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7255 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7256 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7257 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7258 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7259 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7260 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7261 | txn = strm->txn; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7262 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7263 | if (!hlua) { |
| 7264 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7265 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7266 | return 0; |
| 7267 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7268 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7269 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7270 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 7271 | ctx->ctx.hlua_apphttp.flags = 0; |
| 7272 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 7273 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 7274 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 7275 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7276 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7277 | task = task_new(tid_bit); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7278 | if (!task) { |
| 7279 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7280 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7281 | return 0; |
| 7282 | } |
| 7283 | task->nice = 0; |
| 7284 | task->context = ctx; |
| 7285 | task->process = hlua_applet_wakeup; |
| 7286 | ctx->ctx.hlua_apphttp.task = task; |
| 7287 | |
| 7288 | /* In the execution wrappers linked with a stream, the |
| 7289 | * Lua context can be not initialized. This behavior |
| 7290 | * permits to save performances because a systematic |
| 7291 | * Lua initialization cause 5% performances loss. |
| 7292 | */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7293 | 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] | 7294 | 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] | 7295 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7296 | return 0; |
| 7297 | } |
| 7298 | |
| 7299 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 7300 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7301 | |
| 7302 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7303 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7304 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7305 | error = lua_tostring(hlua->T, -1); |
| 7306 | else |
| 7307 | error = "critical error"; |
| 7308 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7309 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7310 | return 0; |
| 7311 | } |
| 7312 | |
| 7313 | /* Check stack available size. */ |
| 7314 | if (!lua_checkstack(hlua->T, 1)) { |
| 7315 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7316 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7317 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7318 | return 0; |
| 7319 | } |
| 7320 | |
| 7321 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7322 | 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] | 7323 | |
| 7324 | /* Create and and push object stream in the stack. */ |
| 7325 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 7326 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7327 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7328 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7329 | return 0; |
| 7330 | } |
| 7331 | hlua->nargs = 1; |
| 7332 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7333 | /* push keywords in the stack. */ |
| 7334 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 7335 | if (!lua_checkstack(hlua->T, 1)) { |
| 7336 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7337 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7338 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7339 | return 0; |
| 7340 | } |
| 7341 | lua_pushstring(hlua->T, *arg); |
| 7342 | hlua->nargs++; |
| 7343 | } |
| 7344 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7345 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7346 | |
| 7347 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7348 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7349 | |
| 7350 | return 1; |
| 7351 | } |
| 7352 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7353 | void hlua_applet_http_fct(struct appctx *ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7354 | { |
| 7355 | struct stream_interface *si = ctx->owner; |
| 7356 | struct stream *strm = si_strm(si); |
| 7357 | struct channel *req = si_oc(si); |
| 7358 | struct channel *res = si_ic(si); |
| 7359 | struct act_rule *rule = ctx->rule; |
| 7360 | struct proxy *px = strm->be; |
| 7361 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
| 7362 | struct htx *req_htx, *res_htx; |
| 7363 | |
| 7364 | res_htx = htx_from_buf(&res->buf); |
| 7365 | |
| 7366 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7367 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7368 | goto out; |
| 7369 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7370 | /* Check if the input buffer is available. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7371 | if (!b_size(&res->buf)) { |
| 7372 | si_rx_room_blk(si); |
| 7373 | goto out; |
| 7374 | } |
| 7375 | /* check that the output is not closed */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7376 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7377 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7378 | |
| 7379 | /* Set the currently running flag. */ |
| 7380 | if (!HLUA_IS_RUNNING(hlua) && |
| 7381 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
Christopher Faulet | bd878d2 | 2021-04-28 10:50:21 +0200 | [diff] [blame] | 7382 | if (!co_data(req)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7383 | si_cant_get(si); |
| 7384 | goto out; |
| 7385 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7386 | } |
| 7387 | |
| 7388 | /* Executes The applet if it is not done. */ |
| 7389 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7390 | |
| 7391 | /* Execute the function. */ |
| 7392 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7393 | /* finished. */ |
| 7394 | case HLUA_E_OK: |
| 7395 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7396 | break; |
| 7397 | |
| 7398 | /* yield. */ |
| 7399 | case HLUA_E_AGAIN: |
| 7400 | if (hlua->wake_time != TICK_ETERNITY) |
| 7401 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7402 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7403 | |
| 7404 | /* finished with error. */ |
| 7405 | case HLUA_E_ERRMSG: |
| 7406 | /* Display log. */ |
| 7407 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7408 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7409 | lua_pop(hlua->T, 1); |
| 7410 | goto error; |
| 7411 | |
| 7412 | case HLUA_E_ETMOUT: |
| 7413 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7414 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7415 | goto error; |
| 7416 | |
| 7417 | case HLUA_E_NOMEM: |
| 7418 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7419 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7420 | goto error; |
| 7421 | |
| 7422 | case HLUA_E_YIELD: /* unexpected */ |
| 7423 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7424 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7425 | goto error; |
| 7426 | |
| 7427 | case HLUA_E_ERR: |
| 7428 | /* Display log. */ |
| 7429 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7430 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7431 | goto error; |
| 7432 | |
| 7433 | default: |
| 7434 | goto error; |
| 7435 | } |
| 7436 | } |
| 7437 | |
| 7438 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7439 | if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT) |
| 7440 | goto done; |
| 7441 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7442 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) |
| 7443 | goto error; |
| 7444 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 7445 | /* no more data are expected. Don't add TLR because mux-h1 will take care of it */ |
| 7446 | res_htx->flags |= HTX_FL_EOM; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7447 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 7448 | ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7449 | } |
| 7450 | |
| 7451 | done: |
| 7452 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7453 | if (!(res->flags & CF_SHUTR)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7454 | res->flags |= CF_READ_NULL; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7455 | si_shutr(si); |
| 7456 | } |
| 7457 | |
| 7458 | /* eat the whole request */ |
| 7459 | if (co_data(req)) { |
| 7460 | req_htx = htx_from_buf(&req->buf); |
| 7461 | co_htx_skip(req, req_htx, co_data(req)); |
| 7462 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7463 | } |
| 7464 | } |
| 7465 | |
| 7466 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7467 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7468 | return; |
| 7469 | |
| 7470 | error: |
| 7471 | |
| 7472 | /* If we are in HTTP mode, and we are not send any |
| 7473 | * data, return a 500 server error in best effort: |
| 7474 | * if there is no room available in the buffer, |
| 7475 | * just close the connection. |
| 7476 | */ |
| 7477 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 7478 | struct buffer *err = &http_err_chunks[HTTP_ERR_500]; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7479 | |
| 7480 | channel_erase(res); |
| 7481 | res->buf.data = b_data(err); |
| 7482 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 7483 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 7484 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7485 | } |
| 7486 | if (!(strm->flags & SF_ERR_MASK)) |
| 7487 | strm->flags |= SF_ERR_RESOURCE; |
| 7488 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7489 | goto done; |
| 7490 | } |
| 7491 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7492 | static void hlua_applet_http_release(struct appctx *ctx) |
| 7493 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7494 | task_destroy(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7495 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7496 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7497 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7498 | } |
| 7499 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7500 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7501 | * success case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7502 | * |
| 7503 | * This function can fail with an abort() due to an Lua critical error. |
| 7504 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7505 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7506 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7507 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 7508 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7509 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7510 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7511 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7512 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7513 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7514 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7515 | if (!rule->arg.hlua_rule) { |
| 7516 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7517 | goto error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7518 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7519 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7520 | /* Memory for arguments. */ |
Tim Duesterhus | e52b6e5 | 2020-09-12 20:26:43 +0200 | [diff] [blame] | 7521 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, |
| 7522 | sizeof(*rule->arg.hlua_rule->args)); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7523 | if (!rule->arg.hlua_rule->args) { |
| 7524 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7525 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7526 | } |
| 7527 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7528 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7529 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7530 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7531 | /* Expect some arguments */ |
| 7532 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7533 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7534 | memprintf(err, "expect %d arguments", fcn->nargs); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7535 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7536 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7537 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7538 | if (!rule->arg.hlua_rule->args[i]) { |
| 7539 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7540 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7541 | } |
| 7542 | (*cur_arg)++; |
| 7543 | } |
| 7544 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7545 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 7546 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7547 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7548 | return ACT_RET_PRS_OK; |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 7549 | |
| 7550 | error: |
| 7551 | if (rule->arg.hlua_rule) { |
| 7552 | if (rule->arg.hlua_rule->args) { |
| 7553 | for (i = 0; i < fcn->nargs; i++) |
| 7554 | ha_free(&rule->arg.hlua_rule->args[i]); |
| 7555 | ha_free(&rule->arg.hlua_rule->args); |
| 7556 | } |
| 7557 | ha_free(&rule->arg.hlua_rule); |
| 7558 | } |
| 7559 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7560 | } |
| 7561 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7562 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 7563 | struct act_rule *rule, char **err) |
| 7564 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7565 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7566 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7567 | /* HTTP applets are forbidden in tcp-request rules. |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7568 | * HTTP applet request requires everything initialized by |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7569 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7570 | * The applet will be immediately initialized, but its before |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7571 | * the call of this analyzer. |
| 7572 | */ |
| 7573 | if (rule->from != ACT_F_HTTP_REQ) { |
| 7574 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 7575 | return ACT_RET_PRS_ERR; |
| 7576 | } |
| 7577 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7578 | /* Memory for the rule. */ |
| 7579 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7580 | if (!rule->arg.hlua_rule) { |
| 7581 | memprintf(err, "out of memory error"); |
| 7582 | return ACT_RET_PRS_ERR; |
| 7583 | } |
| 7584 | |
| 7585 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7586 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7587 | |
| 7588 | /* TODO: later accept arguments. */ |
| 7589 | rule->arg.hlua_rule->args = NULL; |
| 7590 | |
| 7591 | /* Add applet pointer in the rule. */ |
| 7592 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7593 | rule->applet.name = fcn->name; |
| 7594 | rule->applet.init = hlua_applet_http_init; |
| 7595 | rule->applet.fct = hlua_applet_http_fct; |
| 7596 | rule->applet.release = hlua_applet_http_release; |
| 7597 | rule->applet.timeout = hlua_timeout_applet; |
| 7598 | |
| 7599 | return ACT_RET_PRS_OK; |
| 7600 | } |
| 7601 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7602 | /* This function is an LUA binding used for registering |
| 7603 | * "sample-conv" functions. It expects a converter name used |
| 7604 | * in the haproxy configuration file, and an LUA function. |
| 7605 | */ |
| 7606 | __LJMP static int hlua_register_action(lua_State *L) |
| 7607 | { |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7608 | struct action_kw_list *akl = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7609 | const char *name; |
| 7610 | int ref; |
| 7611 | int len; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7612 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7613 | int nargs; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7614 | struct buffer *trash; |
| 7615 | struct action_kw *akw; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7616 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7617 | /* Initialise the number of expected arguments at 0. */ |
| 7618 | nargs = 0; |
| 7619 | |
| 7620 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 7621 | 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] | 7622 | |
| 7623 | /* First argument : converter name. */ |
| 7624 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7625 | |
| 7626 | /* Second argument : environment. */ |
| 7627 | if (lua_type(L, 2) != LUA_TTABLE) |
| 7628 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7629 | |
| 7630 | /* Third argument : lua function. */ |
| 7631 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7632 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7633 | /* 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] | 7634 | if (lua_gettop(L) >= 4) |
| 7635 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 7636 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7637 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7638 | lua_pushnil(L); |
| 7639 | while (lua_next(L, 2) != 0) { |
| 7640 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7641 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7642 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7643 | /* Check if action exists */ |
| 7644 | trash = get_trash_chunk(); |
| 7645 | chunk_printf(trash, "lua.%s", name); |
| 7646 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) { |
| 7647 | akw = tcp_req_cont_action(trash->area); |
| 7648 | } else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) { |
| 7649 | akw = tcp_res_cont_action(trash->area); |
| 7650 | } else if (strcmp(lua_tostring(L, -1), "http-req") == 0) { |
| 7651 | akw = action_http_req_custom(trash->area); |
| 7652 | } else if (strcmp(lua_tostring(L, -1), "http-res") == 0) { |
| 7653 | akw = action_http_res_custom(trash->area); |
| 7654 | } else { |
| 7655 | akw = NULL; |
| 7656 | } |
| 7657 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 7658 | fcn = akw->private; |
| 7659 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 7660 | ha_warning("Trying to register action 'lua.%s' more than once. " |
| 7661 | "This will become a hard error in version 2.5.\n", name); |
| 7662 | } |
| 7663 | fcn->function_ref[hlua_state_id] = ref; |
| 7664 | |
| 7665 | /* pop the environment string. */ |
| 7666 | lua_pop(L, 1); |
| 7667 | continue; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7668 | } |
| 7669 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7670 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 7671 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7672 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7673 | if (!akl) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7674 | goto alloc_error;; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 7675 | fcn = new_hlua_function(); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7676 | if (!fcn) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7677 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7678 | |
| 7679 | /* Fill fcn. */ |
| 7680 | fcn->name = strdup(name); |
| 7681 | if (!fcn->name) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7682 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7683 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7684 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 7685 | /* Set the expected number of arguments. */ |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7686 | fcn->nargs = nargs; |
| 7687 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7688 | /* List head */ |
| 7689 | akl->list.n = akl->list.p = NULL; |
| 7690 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7691 | /* action keyword. */ |
| 7692 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7693 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7694 | if (!akl->kw[0].kw) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7695 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7696 | |
| 7697 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7698 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 7699 | akl->kw[0].flags = 0; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7700 | akl->kw[0].private = fcn; |
| 7701 | akl->kw[0].parse = action_register_lua; |
| 7702 | |
| 7703 | /* select the action registering point. */ |
| 7704 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 7705 | tcp_req_cont_keywords_register(akl); |
| 7706 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 7707 | tcp_res_cont_keywords_register(akl); |
| 7708 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 7709 | http_req_keywords_register(akl); |
| 7710 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 7711 | http_res_keywords_register(akl); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7712 | else { |
| 7713 | release_hlua_function(fcn); |
| 7714 | if (akl) |
| 7715 | ha_free((char **)&(akl->kw[0].kw)); |
| 7716 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7717 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7718 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 7719 | "are expected.", lua_tostring(L, -1))); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7720 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7721 | |
| 7722 | /* pop the environment string. */ |
| 7723 | lua_pop(L, 1); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7724 | |
| 7725 | /* reset for next loop */ |
| 7726 | akl = NULL; |
| 7727 | fcn = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7728 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7729 | return ACT_RET_PRS_OK; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 7730 | |
| 7731 | alloc_error: |
| 7732 | release_hlua_function(fcn); |
| 7733 | ha_free(&akl); |
| 7734 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 7735 | return 0; /* Never reached */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7736 | } |
| 7737 | |
| 7738 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 7739 | struct act_rule *rule, char **err) |
| 7740 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7741 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7742 | |
Christopher Faulet | 280f85b | 2019-07-15 15:02:04 +0200 | [diff] [blame] | 7743 | if (px->mode == PR_MODE_HTTP) { |
| 7744 | memprintf(err, "Lua TCP services cannot be used on HTTP proxies"); |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 7745 | return ACT_RET_PRS_ERR; |
| 7746 | } |
| 7747 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7748 | /* Memory for the rule. */ |
| 7749 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7750 | if (!rule->arg.hlua_rule) { |
| 7751 | memprintf(err, "out of memory error"); |
| 7752 | return ACT_RET_PRS_ERR; |
| 7753 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7754 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7755 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7756 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7757 | |
| 7758 | /* TODO: later accept arguments. */ |
| 7759 | rule->arg.hlua_rule->args = NULL; |
| 7760 | |
| 7761 | /* Add applet pointer in the rule. */ |
| 7762 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7763 | rule->applet.name = fcn->name; |
| 7764 | rule->applet.init = hlua_applet_tcp_init; |
| 7765 | rule->applet.fct = hlua_applet_tcp_fct; |
| 7766 | rule->applet.release = hlua_applet_tcp_release; |
| 7767 | rule->applet.timeout = hlua_timeout_applet; |
| 7768 | |
| 7769 | return 0; |
| 7770 | } |
| 7771 | |
| 7772 | /* This function is an LUA binding used for registering |
| 7773 | * "sample-conv" functions. It expects a converter name used |
| 7774 | * in the haproxy configuration file, and an LUA function. |
| 7775 | */ |
| 7776 | __LJMP static int hlua_register_service(lua_State *L) |
| 7777 | { |
| 7778 | struct action_kw_list *akl; |
| 7779 | const char *name; |
| 7780 | const char *env; |
| 7781 | int ref; |
| 7782 | int len; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 7783 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7784 | struct buffer *trash; |
| 7785 | struct action_kw *akw; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7786 | |
| 7787 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 7788 | |
| 7789 | /* First argument : converter name. */ |
| 7790 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7791 | |
| 7792 | /* Second argument : environment. */ |
| 7793 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7794 | |
| 7795 | /* Third argument : lua function. */ |
| 7796 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7797 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7798 | /* Check for service already registered */ |
| 7799 | trash = get_trash_chunk(); |
| 7800 | chunk_printf(trash, "lua.%s", name); |
| 7801 | akw = service_find(trash->area); |
| 7802 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 7803 | fcn = akw->private; |
| 7804 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 7805 | ha_warning("Trying to register service 'lua.%s' more than once. " |
| 7806 | "This will become a hard error in version 2.5.\n", name); |
| 7807 | } |
| 7808 | fcn->function_ref[hlua_state_id] = ref; |
| 7809 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7810 | } |
| 7811 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7812 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7813 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 7814 | if (!akl) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 7815 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 7816 | fcn = new_hlua_function(); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7817 | if (!fcn) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 7818 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7819 | |
| 7820 | /* Fill fcn. */ |
| 7821 | len = strlen("<lua.>") + strlen(name) + 1; |
| 7822 | fcn->name = calloc(1, len); |
| 7823 | if (!fcn->name) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 7824 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7825 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7826 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7827 | |
| 7828 | /* List head */ |
| 7829 | akl->list.n = akl->list.p = NULL; |
| 7830 | |
| 7831 | /* converter keyword. */ |
| 7832 | len = strlen("lua.") + strlen(name) + 1; |
| 7833 | akl->kw[0].kw = calloc(1, len); |
| 7834 | if (!akl->kw[0].kw) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 7835 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7836 | |
| 7837 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7838 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 7839 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7840 | if (strcmp(env, "tcp") == 0) |
| 7841 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7842 | else if (strcmp(env, "http") == 0) |
| 7843 | akl->kw[0].parse = action_register_service_http; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 7844 | else { |
| 7845 | release_hlua_function(fcn); |
| 7846 | if (akl) |
| 7847 | ha_free((char **)&(akl->kw[0].kw)); |
| 7848 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7849 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 7850 | "'tcp' or 'http' are expected.", env)); |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 7851 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7852 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 7853 | akl->kw[0].flags = 0; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7854 | akl->kw[0].private = fcn; |
| 7855 | |
| 7856 | /* End of array. */ |
| 7857 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 7858 | |
| 7859 | /* Register this new converter */ |
| 7860 | service_keywords_register(akl); |
| 7861 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7862 | return 0; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 7863 | |
| 7864 | alloc_error: |
| 7865 | release_hlua_function(fcn); |
| 7866 | ha_free(&akl); |
| 7867 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 7868 | return 0; /* Never reached */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7869 | } |
| 7870 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7871 | /* This function initialises Lua cli handler. It copies the |
| 7872 | * arguments in the Lua stack and create channel IO objects. |
| 7873 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 7874 | 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] | 7875 | { |
| 7876 | struct hlua *hlua; |
| 7877 | struct hlua_function *fcn; |
| 7878 | int i; |
| 7879 | const char *error; |
| 7880 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7881 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7882 | appctx->ctx.hlua_cli.fcn = private; |
| 7883 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7884 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7885 | if (!hlua) { |
| 7886 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7887 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7888 | } |
| 7889 | HLUA_INIT(hlua); |
| 7890 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7891 | |
| 7892 | /* Create task used by signal to wakeup applets. |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 7893 | * 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] | 7894 | * applet_http. It is absolutely compatible. |
| 7895 | */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7896 | appctx->ctx.hlua_cli.task = task_new(tid_bit); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7897 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 7898 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7899 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7900 | } |
| 7901 | appctx->ctx.hlua_cli.task->nice = 0; |
| 7902 | appctx->ctx.hlua_cli.task->context = appctx; |
| 7903 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 7904 | |
| 7905 | /* Initialises the Lua context */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7906 | 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] | 7907 | 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] | 7908 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7909 | } |
| 7910 | |
| 7911 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7912 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7913 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7914 | error = lua_tostring(hlua->T, -1); |
| 7915 | else |
| 7916 | error = "critical error"; |
| 7917 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 7918 | goto error; |
| 7919 | } |
| 7920 | |
| 7921 | /* Check stack available size. */ |
| 7922 | if (!lua_checkstack(hlua->T, 2)) { |
| 7923 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7924 | goto error; |
| 7925 | } |
| 7926 | |
| 7927 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7928 | 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] | 7929 | |
| 7930 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 7931 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7932 | */ |
| 7933 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 7934 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7935 | goto error; |
| 7936 | } |
| 7937 | hlua->nargs = 1; |
| 7938 | |
| 7939 | /* push keywords in the stack. */ |
| 7940 | for (i = 0; *args[i]; i++) { |
| 7941 | /* Check stack available size. */ |
| 7942 | if (!lua_checkstack(hlua->T, 1)) { |
| 7943 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7944 | goto error; |
| 7945 | } |
| 7946 | lua_pushstring(hlua->T, args[i]); |
| 7947 | hlua->nargs++; |
| 7948 | } |
| 7949 | |
| 7950 | /* We must initialize the execution timeouts. */ |
| 7951 | hlua->max_time = hlua_timeout_session; |
| 7952 | |
| 7953 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7954 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7955 | |
| 7956 | /* It's ok */ |
| 7957 | return 0; |
| 7958 | |
| 7959 | /* It's not ok. */ |
| 7960 | error: |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7961 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7962 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7963 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7964 | return 1; |
| 7965 | } |
| 7966 | |
| 7967 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 7968 | { |
| 7969 | struct hlua *hlua; |
| 7970 | struct stream_interface *si; |
| 7971 | struct hlua_function *fcn; |
| 7972 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7973 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7974 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 7975 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7976 | |
| 7977 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7978 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7979 | return 1; |
| 7980 | |
| 7981 | /* Execute the function. */ |
| 7982 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7983 | |
| 7984 | /* finished. */ |
| 7985 | case HLUA_E_OK: |
| 7986 | return 1; |
| 7987 | |
| 7988 | /* yield. */ |
| 7989 | case HLUA_E_AGAIN: |
| 7990 | /* We want write. */ |
| 7991 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 7992 | si_rx_room_blk(si); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7993 | /* Set the timeout. */ |
| 7994 | if (hlua->wake_time != TICK_ETERNITY) |
| 7995 | task_schedule(hlua->task, hlua->wake_time); |
| 7996 | return 0; |
| 7997 | |
| 7998 | /* finished with error. */ |
| 7999 | case HLUA_E_ERRMSG: |
| 8000 | /* Display log. */ |
| 8001 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 8002 | fcn->name, lua_tostring(hlua->T, -1)); |
| 8003 | lua_pop(hlua->T, 1); |
| 8004 | return 1; |
| 8005 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8006 | case HLUA_E_ETMOUT: |
| 8007 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 8008 | fcn->name); |
| 8009 | return 1; |
| 8010 | |
| 8011 | case HLUA_E_NOMEM: |
| 8012 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 8013 | fcn->name); |
| 8014 | return 1; |
| 8015 | |
| 8016 | case HLUA_E_YIELD: /* unexpected */ |
| 8017 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 8018 | fcn->name); |
| 8019 | return 1; |
| 8020 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8021 | case HLUA_E_ERR: |
| 8022 | /* Display log. */ |
| 8023 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 8024 | fcn->name); |
| 8025 | return 1; |
| 8026 | |
| 8027 | default: |
| 8028 | return 1; |
| 8029 | } |
| 8030 | |
| 8031 | return 1; |
| 8032 | } |
| 8033 | |
| 8034 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 8035 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8036 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8037 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8038 | } |
| 8039 | |
| 8040 | /* This function is an LUA binding used for registering |
| 8041 | * new keywords in the cli. It expects a list of keywords |
| 8042 | * which are the "path". It is limited to 5 keywords. A |
| 8043 | * description of the command, a function to be executed |
| 8044 | * for the parsing and a function for io handlers. |
| 8045 | */ |
| 8046 | __LJMP static int hlua_register_cli(lua_State *L) |
| 8047 | { |
| 8048 | struct cli_kw_list *cli_kws; |
| 8049 | const char *message; |
| 8050 | int ref_io; |
| 8051 | int len; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8052 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8053 | int index; |
| 8054 | int i; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8055 | struct buffer *trash; |
| 8056 | const char *kw[5]; |
| 8057 | struct cli_kw *cli_kw; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8058 | const char *errmsg; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8059 | |
| 8060 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 8061 | |
| 8062 | /* First argument : an array of maximum 5 keywords. */ |
| 8063 | if (!lua_istable(L, 1)) |
| 8064 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 8065 | |
| 8066 | /* Second argument : string with contextual message. */ |
| 8067 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8068 | |
| 8069 | /* Third and fourth argument : lua function. */ |
| 8070 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 8071 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8072 | /* Check for CLI service already registered */ |
| 8073 | trash = get_trash_chunk(); |
| 8074 | index = 0; |
| 8075 | lua_pushnil(L); |
| 8076 | memset(kw, 0, sizeof(kw)); |
| 8077 | while (lua_next(L, 1) != 0) { |
| 8078 | if (index >= CLI_PREFIX_KW_NB) |
| 8079 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 8080 | if (lua_type(L, -1) != LUA_TSTRING) |
| 8081 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 8082 | kw[index] = lua_tostring(L, -1); |
| 8083 | if (index == 0) |
| 8084 | chunk_printf(trash, "%s", kw[index]); |
| 8085 | else |
| 8086 | chunk_appendf(trash, " %s", kw[index]); |
| 8087 | index++; |
| 8088 | lua_pop(L, 1); |
| 8089 | } |
| 8090 | cli_kw = cli_find_kw_exact((char **)kw); |
| 8091 | if (cli_kw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8092 | fcn = cli_kw->private; |
| 8093 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 8094 | ha_warning("Trying to register CLI keyword 'lua.%s' more than once. " |
| 8095 | "This will become a hard error in version 2.5.\n", trash->area); |
| 8096 | } |
| 8097 | fcn->function_ref[hlua_state_id] = ref_io; |
| 8098 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8099 | } |
| 8100 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8101 | /* Allocate and fill the sample fetch keyword struct. */ |
| 8102 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8103 | if (!cli_kws) { |
| 8104 | errmsg = "Lua out of memory error."; |
| 8105 | goto error; |
| 8106 | } |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 8107 | fcn = new_hlua_function(); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8108 | if (!fcn) { |
| 8109 | errmsg = "Lua out of memory error."; |
| 8110 | goto error; |
| 8111 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8112 | |
| 8113 | /* Fill path. */ |
| 8114 | index = 0; |
| 8115 | lua_pushnil(L); |
| 8116 | while(lua_next(L, 1) != 0) { |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8117 | if (index >= 5) { |
| 8118 | errmsg = "1st argument must be a table with a maximum of 5 entries"; |
| 8119 | goto error; |
| 8120 | } |
| 8121 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 8122 | errmsg = "1st argument must be a table filled with strings"; |
| 8123 | goto error; |
| 8124 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8125 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8126 | if (!cli_kws->kw[0].str_kw[index]) { |
| 8127 | errmsg = "Lua out of memory error."; |
| 8128 | goto error; |
| 8129 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8130 | index++; |
| 8131 | lua_pop(L, 1); |
| 8132 | } |
| 8133 | |
| 8134 | /* Copy help message. */ |
| 8135 | cli_kws->kw[0].usage = strdup(message); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8136 | if (!cli_kws->kw[0].usage) { |
| 8137 | errmsg = "Lua out of memory error."; |
| 8138 | goto error; |
| 8139 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8140 | |
| 8141 | /* Fill fcn io handler. */ |
| 8142 | len = strlen("<lua.cli>") + 1; |
| 8143 | for (i = 0; i < index; i++) |
| 8144 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 8145 | fcn->name = calloc(1, len); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8146 | if (!fcn->name) { |
| 8147 | errmsg = "Lua out of memory error."; |
| 8148 | goto error; |
| 8149 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8150 | strncat((char *)fcn->name, "<lua.cli", len); |
| 8151 | for (i = 0; i < index; i++) { |
| 8152 | strncat((char *)fcn->name, ".", len); |
| 8153 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 8154 | } |
| 8155 | strncat((char *)fcn->name, ">", len); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8156 | fcn->function_ref[hlua_state_id] = ref_io; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8157 | |
| 8158 | /* Fill last entries. */ |
| 8159 | cli_kws->kw[0].private = fcn; |
| 8160 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 8161 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 8162 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 8163 | |
| 8164 | /* Register this new converter */ |
| 8165 | cli_register_kw(cli_kws); |
| 8166 | |
| 8167 | return 0; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 8168 | |
| 8169 | error: |
| 8170 | release_hlua_function(fcn); |
| 8171 | if (cli_kws) { |
| 8172 | for (i = 0; i < index; i++) |
| 8173 | ha_free((char **)&(cli_kws->kw[0].str_kw[i])); |
| 8174 | ha_free((char **)&(cli_kws->kw[0].usage)); |
| 8175 | } |
| 8176 | ha_free(&cli_kws); |
| 8177 | WILL_LJMP(luaL_error(L, errmsg)); |
| 8178 | return 0; /* Never reached */ |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8179 | } |
| 8180 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8181 | 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] | 8182 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8183 | char **err, unsigned int *timeout) |
| 8184 | { |
| 8185 | const char *error; |
| 8186 | |
| 8187 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 8188 | if (error == PARSE_TIME_OVER) { |
| 8189 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 8190 | args[1], args[0]); |
| 8191 | return -1; |
| 8192 | } |
| 8193 | else if (error == PARSE_TIME_UNDER) { |
| 8194 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 8195 | args[1], args[0]); |
| 8196 | return -1; |
| 8197 | } |
| 8198 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8199 | memprintf(err, "%s: invalid timeout", args[0]); |
| 8200 | return -1; |
| 8201 | } |
| 8202 | return 0; |
| 8203 | } |
| 8204 | |
| 8205 | 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] | 8206 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8207 | char **err) |
| 8208 | { |
| 8209 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8210 | file, line, err, &hlua_timeout_session); |
| 8211 | } |
| 8212 | |
| 8213 | 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] | 8214 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8215 | char **err) |
| 8216 | { |
| 8217 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8218 | file, line, err, &hlua_timeout_task); |
| 8219 | } |
| 8220 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8221 | 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] | 8222 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8223 | char **err) |
| 8224 | { |
| 8225 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8226 | file, line, err, &hlua_timeout_applet); |
| 8227 | } |
| 8228 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8229 | 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] | 8230 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8231 | char **err) |
| 8232 | { |
| 8233 | char *error; |
| 8234 | |
| 8235 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 8236 | if (*error != '\0') { |
| 8237 | memprintf(err, "%s: invalid number", args[0]); |
| 8238 | return -1; |
| 8239 | } |
| 8240 | return 0; |
| 8241 | } |
| 8242 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8243 | 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] | 8244 | const struct proxy *defpx, const char *file, int line, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8245 | char **err) |
| 8246 | { |
| 8247 | char *error; |
| 8248 | |
| 8249 | if (*(args[1]) == 0) { |
| 8250 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 8251 | return -1; |
| 8252 | } |
| 8253 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 8254 | if (*error != '\0') { |
| 8255 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 8256 | return -1; |
| 8257 | } |
| 8258 | return 0; |
| 8259 | } |
| 8260 | |
| 8261 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8262 | /* This function is called by the main configuration key "lua-load". It loads and |
| 8263 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 8264 | * the main lua entry point. |
| 8265 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8266 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 8267 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8268 | * |
| 8269 | * In some error case, LUA set an error message in top of the stack. This function |
| 8270 | * 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] | 8271 | * |
| 8272 | * This function can fail with an abort() due to an Lua critical error. |
| 8273 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 8274 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8275 | */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8276 | static int hlua_load_state(char *filename, lua_State *L, char **err) |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8277 | { |
| 8278 | int error; |
| 8279 | |
| 8280 | /* Just load and compile the file. */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8281 | error = luaL_loadfile(L, filename); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8282 | if (error) { |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8283 | memprintf(err, "error in Lua file '%s': %s", filename, lua_tostring(L, -1)); |
| 8284 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8285 | return -1; |
| 8286 | } |
| 8287 | |
| 8288 | /* If no syntax error where detected, execute the code. */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8289 | error = lua_pcall(L, 0, LUA_MULTRET, 0); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8290 | switch (error) { |
| 8291 | case LUA_OK: |
| 8292 | break; |
| 8293 | case LUA_ERRRUN: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8294 | memprintf(err, "Lua runtime error: %s\n", lua_tostring(L, -1)); |
| 8295 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8296 | return -1; |
| 8297 | case LUA_ERRMEM: |
Thierry Fournier | de6145f | 2020-11-29 00:55:53 +0100 | [diff] [blame] | 8298 | memprintf(err, "Lua out of memory error\n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8299 | return -1; |
| 8300 | case LUA_ERRERR: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8301 | memprintf(err, "Lua message handler error: %s\n", lua_tostring(L, -1)); |
| 8302 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8303 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 8304 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8305 | case LUA_ERRGCMM: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8306 | memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(L, -1)); |
| 8307 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8308 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 8309 | #endif |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8310 | default: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8311 | memprintf(err, "Lua unknown error: %s\n", lua_tostring(L, -1)); |
| 8312 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8313 | return -1; |
| 8314 | } |
| 8315 | |
| 8316 | return 0; |
| 8317 | } |
| 8318 | |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8319 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 8320 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8321 | char **err) |
| 8322 | { |
| 8323 | if (*(args[1]) == 0) { |
| 8324 | memprintf(err, "'%s' expects a file name as parameter.\n", args[0]); |
| 8325 | return -1; |
| 8326 | } |
| 8327 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8328 | /* loading for global state */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8329 | hlua_state_id = 0; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8330 | ha_set_tid(0); |
Thierry Fournier | afc63e2 | 2020-11-28 17:06:51 +0100 | [diff] [blame] | 8331 | return hlua_load_state(args[1], hlua_states[0], err); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8332 | } |
| 8333 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8334 | 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] | 8335 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8336 | char **err) |
| 8337 | { |
| 8338 | int len; |
| 8339 | |
| 8340 | if (*(args[1]) == 0) { |
| 8341 | memprintf(err, "'%s' expects a file as parameter.\n", args[0]); |
| 8342 | return -1; |
| 8343 | } |
| 8344 | |
| 8345 | if (per_thread_load == NULL) { |
| 8346 | /* allocate the first entry large enough to store the final NULL */ |
| 8347 | per_thread_load = calloc(1, sizeof(*per_thread_load)); |
| 8348 | if (per_thread_load == NULL) { |
| 8349 | memprintf(err, "out of memory error"); |
| 8350 | return -1; |
| 8351 | } |
| 8352 | } |
| 8353 | |
| 8354 | /* count used entries */ |
| 8355 | for (len = 0; per_thread_load[len] != NULL; len++) |
| 8356 | ; |
| 8357 | |
| 8358 | per_thread_load = realloc(per_thread_load, (len + 2) * sizeof(*per_thread_load)); |
| 8359 | if (per_thread_load == NULL) { |
| 8360 | memprintf(err, "out of memory error"); |
| 8361 | return -1; |
| 8362 | } |
| 8363 | |
| 8364 | per_thread_load[len] = strdup(args[1]); |
| 8365 | per_thread_load[len + 1] = NULL; |
| 8366 | |
| 8367 | if (per_thread_load[len] == NULL) { |
| 8368 | memprintf(err, "out of memory error"); |
| 8369 | return -1; |
| 8370 | } |
| 8371 | |
| 8372 | /* loading for thread 1 only */ |
| 8373 | hlua_state_id = 1; |
| 8374 | ha_set_tid(0); |
| 8375 | return hlua_load_state(args[1], hlua_states[1], err); |
| 8376 | } |
| 8377 | |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 8378 | /* Prepend the given <path> followed by a semicolon to the `package.<type>` variable |
| 8379 | * in the given <ctx>. |
| 8380 | */ |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 8381 | static int hlua_prepend_path(lua_State *L, char *type, char *path) |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 8382 | { |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 8383 | lua_getglobal(L, "package"); /* push package variable */ |
| 8384 | lua_pushstring(L, path); /* push given path */ |
| 8385 | lua_pushstring(L, ";"); /* push semicolon */ |
| 8386 | lua_getfield(L, -3, type); /* push old path */ |
| 8387 | lua_concat(L, 3); /* concatenate to new path */ |
| 8388 | lua_setfield(L, -2, type); /* store new path */ |
| 8389 | lua_pop(L, 1); /* pop package variable */ |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 8390 | |
| 8391 | return 0; |
| 8392 | } |
| 8393 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8394 | 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] | 8395 | const struct proxy *defpx, const char *file, int line, |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8396 | char **err) |
| 8397 | { |
| 8398 | char *path; |
| 8399 | char *type = "path"; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8400 | struct prepend_path *p = NULL; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8401 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8402 | if (too_many_args(2, args, err, NULL)) { |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8403 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8404 | } |
| 8405 | |
| 8406 | if (!(*args[1])) { |
| 8407 | memprintf(err, "'%s' expects to receive a <path> as argument", args[0]); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8408 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8409 | } |
| 8410 | path = args[1]; |
| 8411 | |
| 8412 | if (*args[2]) { |
| 8413 | if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) { |
| 8414 | 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] | 8415 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8416 | } |
| 8417 | type = args[2]; |
| 8418 | } |
| 8419 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8420 | p = calloc(1, sizeof(*p)); |
| 8421 | if (p == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 8422 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8423 | goto err; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8424 | } |
| 8425 | p->path = strdup(path); |
| 8426 | if (p->path == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 8427 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8428 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8429 | } |
| 8430 | p->type = strdup(type); |
| 8431 | if (p->type == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 8432 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8433 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8434 | } |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 8435 | LIST_APPEND(&prepend_path_list, &p->l); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8436 | |
| 8437 | hlua_prepend_path(hlua_states[0], type, path); |
| 8438 | hlua_prepend_path(hlua_states[1], type, path); |
| 8439 | return 0; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8440 | |
| 8441 | err2: |
| 8442 | free(p->type); |
| 8443 | free(p->path); |
| 8444 | err: |
| 8445 | free(p); |
| 8446 | return -1; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8447 | } |
| 8448 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8449 | /* configuration keywords declaration */ |
| 8450 | static struct cfg_kw_list cfg_kws = {{ },{ |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8451 | { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8452 | { CFG_GLOBAL, "lua-load", hlua_load }, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8453 | { CFG_GLOBAL, "lua-load-per-thread", hlua_load_per_thread }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8454 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 8455 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 8456 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8457 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8458 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8459 | { 0, NULL, NULL }, |
| 8460 | }}; |
| 8461 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 8462 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 8463 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 8464 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8465 | /* This function can fail with an abort() due to an Lua critical error. |
| 8466 | * We are in the initialisation process of HAProxy, this abort() is |
| 8467 | * tolerated. |
| 8468 | */ |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8469 | int hlua_post_init_state(lua_State *L) |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8470 | { |
| 8471 | struct hlua_init_function *init; |
| 8472 | const char *msg; |
| 8473 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8474 | const char *error; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8475 | const char *kind; |
| 8476 | const char *trace; |
| 8477 | int return_status = 1; |
| 8478 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 8479 | int nres; |
| 8480 | #endif |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8481 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8482 | /* disable memory limit checks if limit is not set */ |
| 8483 | if (!hlua_global_allocator.limit) |
| 8484 | hlua_global_allocator.limit = ~hlua_global_allocator.limit; |
| 8485 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 8486 | /* Call post initialisation function in safe environment. */ |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8487 | if (setjmp(safe_ljmp_env) != 0) { |
| 8488 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8489 | if (lua_type(L, -1) == LUA_TSTRING) |
| 8490 | error = lua_tostring(L, -1); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8491 | else |
| 8492 | error = "critical error"; |
| 8493 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 8494 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8495 | } else { |
| 8496 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8497 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 8498 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8499 | hlua_fcn_post_init(L); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8500 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8501 | list_for_each_entry(init, &hlua_init_functions[hlua_state_id], l) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8502 | lua_rawgeti(L, LUA_REGISTRYINDEX, init->function_ref); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8503 | |
| 8504 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8505 | ret = lua_resume(L, L, 0, &nres); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8506 | #else |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8507 | ret = lua_resume(L, L, 0); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8508 | #endif |
| 8509 | kind = NULL; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8510 | switch (ret) { |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8511 | |
| 8512 | case LUA_OK: |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8513 | lua_pop(L, -1); |
Thierry Fournier | 13d08b7 | 2020-11-28 11:02:58 +0100 | [diff] [blame] | 8514 | break; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8515 | |
| 8516 | case LUA_ERRERR: |
| 8517 | kind = "message handler error"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8518 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8519 | case LUA_ERRRUN: |
| 8520 | if (!kind) |
| 8521 | kind = "runtime error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8522 | msg = lua_tostring(L, -1); |
| 8523 | lua_settop(L, 0); /* Empty the stack. */ |
| 8524 | lua_pop(L, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 8525 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8526 | if (msg) |
| 8527 | ha_alert("Lua init: %s: '%s' from %s\n", kind, msg, trace); |
| 8528 | else |
| 8529 | ha_alert("Lua init: unknown %s from %s\n", kind, trace); |
| 8530 | return_status = 0; |
| 8531 | break; |
| 8532 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8533 | default: |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8534 | /* Unknown error */ |
| 8535 | kind = "Unknown error"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8536 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8537 | case LUA_YIELD: |
| 8538 | /* yield is not configured at this step, this state doesn't happen */ |
| 8539 | if (!kind) |
| 8540 | kind = "yield not allowed"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8541 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8542 | case LUA_ERRMEM: |
| 8543 | if (!kind) |
| 8544 | kind = "out of memory error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8545 | lua_settop(L, 0); |
| 8546 | lua_pop(L, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 8547 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8548 | ha_alert("Lua init: %s: %s\n", kind, trace); |
| 8549 | return_status = 0; |
| 8550 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8551 | } |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8552 | if (!return_status) |
| 8553 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8554 | } |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8555 | |
| 8556 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8557 | return return_status; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8558 | } |
| 8559 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8560 | int hlua_post_init() |
| 8561 | { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8562 | int ret; |
| 8563 | int i; |
| 8564 | int errors; |
| 8565 | char *err = NULL; |
| 8566 | struct hlua_function *fcn; |
| 8567 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8568 | #if USE_OPENSSL |
| 8569 | /* Initialize SSL server. */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 8570 | if (socket_ssl->xprt->prepare_srv) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8571 | int saved_used_backed = global.ssl_used_backend; |
| 8572 | // don't affect maxconn automatic computation |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 8573 | socket_ssl->xprt->prepare_srv(socket_ssl); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8574 | global.ssl_used_backend = saved_used_backed; |
| 8575 | } |
| 8576 | #endif |
| 8577 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8578 | /* Perform post init of common thread */ |
| 8579 | hlua_state_id = 0; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8580 | ha_set_tid(0); |
| 8581 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 8582 | if (ret == 0) |
| 8583 | return 0; |
| 8584 | |
| 8585 | /* init remaining lua states and load files */ |
| 8586 | for (hlua_state_id = 2; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 8587 | |
| 8588 | /* set thread context */ |
| 8589 | ha_set_tid(hlua_state_id - 1); |
| 8590 | |
| 8591 | /* Init lua state */ |
| 8592 | hlua_states[hlua_state_id] = hlua_init_state(hlua_state_id); |
| 8593 | |
| 8594 | /* Load lua files */ |
| 8595 | for (i = 0; per_thread_load && per_thread_load[i]; i++) { |
| 8596 | ret = hlua_load_state(per_thread_load[i], hlua_states[hlua_state_id], &err); |
| 8597 | if (ret != 0) { |
| 8598 | ha_alert("Lua init: %s\n", err); |
| 8599 | return 0; |
| 8600 | } |
| 8601 | } |
| 8602 | } |
| 8603 | |
| 8604 | /* Reset thread context */ |
| 8605 | ha_set_tid(0); |
| 8606 | |
| 8607 | /* Execute post init for all states */ |
| 8608 | for (hlua_state_id = 1; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 8609 | |
| 8610 | /* set thread context */ |
| 8611 | ha_set_tid(hlua_state_id - 1); |
| 8612 | |
| 8613 | /* run post init */ |
| 8614 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 8615 | if (ret == 0) |
| 8616 | return 0; |
| 8617 | } |
| 8618 | |
| 8619 | /* Reset thread context */ |
| 8620 | ha_set_tid(0); |
| 8621 | |
| 8622 | /* control functions registering. Each function must have: |
| 8623 | * - only the function_ref[0] set positive and all other to -1 |
| 8624 | * - only the function_ref[0] set to -1 and all other positive |
| 8625 | * This ensure a same reference is not used both in shared |
| 8626 | * lua state and thread dedicated lua state. Note: is the case |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 8627 | * reach, the shared state is priority, but the bug will be |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8628 | * complicated to found for the end user. |
| 8629 | */ |
| 8630 | errors = 0; |
| 8631 | list_for_each_entry(fcn, &referenced_functions, l) { |
| 8632 | ret = 0; |
| 8633 | for (i = 1; i < global.nbthread + 1; i++) { |
| 8634 | if (fcn->function_ref[i] == -1) |
| 8635 | ret--; |
| 8636 | else |
| 8637 | ret++; |
| 8638 | } |
| 8639 | if (abs(ret) != global.nbthread) { |
| 8640 | ha_alert("Lua function '%s' is not referenced in all thread. " |
| 8641 | "Expect function in all thread or in none thread.\n", fcn->name); |
| 8642 | errors++; |
| 8643 | continue; |
| 8644 | } |
| 8645 | |
| 8646 | if ((fcn->function_ref[0] == -1) == (ret < 0)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 8647 | ha_alert("Lua function '%s' is referenced both ins shared Lua context (through lua-load) " |
| 8648 | "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] | 8649 | "exclusive.\n", fcn->name); |
| 8650 | errors++; |
| 8651 | } |
| 8652 | } |
| 8653 | |
| 8654 | if (errors > 0) |
| 8655 | return 0; |
| 8656 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 8657 | /* 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] | 8658 | * -1 in order to have probably a segfault if someone use it |
| 8659 | */ |
| 8660 | hlua_state_id = -1; |
| 8661 | |
| 8662 | return 1; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8663 | } |
| 8664 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8665 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 8666 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 8667 | * 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] | 8668 | * allocation. <nsize> is the requested new size. A new allocation is |
| 8669 | * indicated by <ptr> being NULL. A free is indicated by <nsize> being |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8670 | * zero. This one verifies that the limits are respected but is optimized |
| 8671 | * 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] | 8672 | */ |
| 8673 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 8674 | { |
| 8675 | struct hlua_mem_allocator *zone = ud; |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8676 | size_t limit, old, new; |
| 8677 | |
Tim Duesterhus | 2258652 | 2021-01-08 10:35:33 +0100 | [diff] [blame] | 8678 | if (unlikely(!ptr && !nsize)) |
| 8679 | return NULL; |
| 8680 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8681 | /* a limit of ~0 means unlimited and boot complete, so there's no need |
| 8682 | * for accounting anymore. |
| 8683 | */ |
Christopher Faulet | cc2c4f8 | 2021-03-24 14:52:24 +0100 | [diff] [blame] | 8684 | if (likely(~zone->limit == 0)) |
| 8685 | return realloc(ptr, nsize); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8686 | |
Willy Tarreau | d36c7fa | 2020-12-02 12:26:29 +0100 | [diff] [blame] | 8687 | if (!ptr) |
| 8688 | osize = 0; |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8689 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8690 | /* enforce strict limits across all threads */ |
| 8691 | limit = zone->limit; |
| 8692 | old = _HA_ATOMIC_LOAD(&zone->allocated); |
| 8693 | do { |
| 8694 | new = old + nsize - osize; |
| 8695 | if (unlikely(nsize && limit && new > limit)) |
| 8696 | return NULL; |
| 8697 | } while (!_HA_ATOMIC_CAS(&zone->allocated, &old, new)); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8698 | |
| 8699 | ptr = realloc(ptr, nsize); |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8700 | |
| 8701 | if (unlikely(!ptr && nsize)) // failed |
| 8702 | _HA_ATOMIC_SUB(&zone->allocated, nsize - osize); |
| 8703 | |
| 8704 | __ha_barrier_atomic_store(); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8705 | return ptr; |
| 8706 | } |
| 8707 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 8708 | /* 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] | 8709 | * We are in the initialisation process of HAProxy, this abort() is |
| 8710 | * tolerated. |
| 8711 | */ |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 8712 | lua_State *hlua_init_state(int thread_num) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8713 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8714 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8715 | int idx; |
| 8716 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8717 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8718 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8719 | const char *error_msg; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8720 | void **context; |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8721 | lua_State *L; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8722 | struct prepend_path *pp; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8723 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8724 | /* Init main lua stack. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8725 | L = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8726 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8727 | /* Initialise Lua context to NULL */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8728 | context = lua_getextraspace(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8729 | *context = NULL; |
| 8730 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8731 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8732 | * the Lua function can fail with an abort. We are in the initialisation |
| 8733 | * process of HAProxy, this abort() is tolerated. |
| 8734 | */ |
| 8735 | |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8736 | /* Call post initialisation function in safe environment. */ |
| 8737 | if (setjmp(safe_ljmp_env) != 0) { |
| 8738 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8739 | if (lua_type(L, -1) == LUA_TSTRING) |
| 8740 | error_msg = lua_tostring(L, -1); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 8741 | else |
| 8742 | error_msg = "critical error"; |
| 8743 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
| 8744 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8745 | } else { |
| 8746 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 8747 | } |
| 8748 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8749 | /* Initialise lua. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8750 | luaL_openlibs(L); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 8751 | #define HLUA_PREPEND_PATH_TOSTRING1(x) #x |
| 8752 | #define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x) |
| 8753 | #ifdef HLUA_PREPEND_PATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8754 | hlua_prepend_path(L, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 8755 | #endif |
| 8756 | #ifdef HLUA_PREPEND_CPATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8757 | hlua_prepend_path(L, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 8758 | #endif |
| 8759 | #undef HLUA_PREPEND_PATH_TOSTRING |
| 8760 | #undef HLUA_PREPEND_PATH_TOSTRING1 |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8761 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8762 | /* Apply configured prepend path */ |
| 8763 | list_for_each_entry(pp, &prepend_path_list, l) |
| 8764 | hlua_prepend_path(L, pp->type, pp->path); |
| 8765 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8766 | /* |
| 8767 | * |
| 8768 | * Create "core" object. |
| 8769 | * |
| 8770 | */ |
| 8771 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 8772 | /* This table entry is the object "core" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8773 | lua_newtable(L); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8774 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 8775 | /* set the thread id */ |
| 8776 | hlua_class_const_int(L, "thread", thread_num); |
| 8777 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8778 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8779 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8780 | hlua_class_const_int(L, log_levels[i], i); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8781 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8782 | /* Register special functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8783 | hlua_class_function(L, "register_init", hlua_register_init); |
| 8784 | hlua_class_function(L, "register_task", hlua_register_task); |
| 8785 | hlua_class_function(L, "register_fetches", hlua_register_fetches); |
| 8786 | hlua_class_function(L, "register_converters", hlua_register_converters); |
| 8787 | hlua_class_function(L, "register_action", hlua_register_action); |
| 8788 | hlua_class_function(L, "register_service", hlua_register_service); |
| 8789 | hlua_class_function(L, "register_cli", hlua_register_cli); |
| 8790 | hlua_class_function(L, "yield", hlua_yield); |
| 8791 | hlua_class_function(L, "set_nice", hlua_set_nice); |
| 8792 | hlua_class_function(L, "sleep", hlua_sleep); |
| 8793 | hlua_class_function(L, "msleep", hlua_msleep); |
| 8794 | hlua_class_function(L, "add_acl", hlua_add_acl); |
| 8795 | hlua_class_function(L, "del_acl", hlua_del_acl); |
| 8796 | hlua_class_function(L, "set_map", hlua_set_map); |
| 8797 | hlua_class_function(L, "del_map", hlua_del_map); |
| 8798 | hlua_class_function(L, "tcp", hlua_socket_new); |
| 8799 | hlua_class_function(L, "log", hlua_log); |
| 8800 | hlua_class_function(L, "Debug", hlua_log_debug); |
| 8801 | hlua_class_function(L, "Info", hlua_log_info); |
| 8802 | hlua_class_function(L, "Warning", hlua_log_warning); |
| 8803 | hlua_class_function(L, "Alert", hlua_log_alert); |
| 8804 | hlua_class_function(L, "done", hlua_done); |
| 8805 | hlua_fcn_reg_core_fcn(L); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8806 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8807 | lua_setglobal(L, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8808 | |
| 8809 | /* |
| 8810 | * |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8811 | * Create "act" object. |
| 8812 | * |
| 8813 | */ |
| 8814 | |
| 8815 | /* This table entry is the object "act" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8816 | lua_newtable(L); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8817 | |
| 8818 | /* push action return constants */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8819 | hlua_class_const_int(L, "CONTINUE", ACT_RET_CONT); |
| 8820 | hlua_class_const_int(L, "STOP", ACT_RET_STOP); |
| 8821 | hlua_class_const_int(L, "YIELD", ACT_RET_YIELD); |
| 8822 | hlua_class_const_int(L, "ERROR", ACT_RET_ERR); |
| 8823 | hlua_class_const_int(L, "DONE", ACT_RET_DONE); |
| 8824 | hlua_class_const_int(L, "DENY", ACT_RET_DENY); |
| 8825 | hlua_class_const_int(L, "ABORT", ACT_RET_ABRT); |
| 8826 | hlua_class_const_int(L, "INVALID", ACT_RET_INV); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8827 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8828 | hlua_class_function(L, "wake_time", hlua_set_wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8829 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8830 | lua_setglobal(L, "act"); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8831 | |
| 8832 | /* |
| 8833 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8834 | * Register class Map |
| 8835 | * |
| 8836 | */ |
| 8837 | |
| 8838 | /* This table entry is the object "Map" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8839 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8840 | |
| 8841 | /* register pattern types. */ |
| 8842 | for (i=0; i<PAT_MATCH_NUM; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8843 | hlua_class_const_int(L, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8844 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8845 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8846 | hlua_class_const_int(L, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8847 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8848 | |
| 8849 | /* register constructor. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8850 | hlua_class_function(L, "new", hlua_map_new); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8851 | |
| 8852 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8853 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8854 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8855 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8856 | lua_pushstring(L, "__index"); |
| 8857 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8858 | |
| 8859 | /* Register . */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8860 | hlua_class_function(L, "lookup", hlua_map_lookup); |
| 8861 | hlua_class_function(L, "slookup", hlua_map_slookup); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8862 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8863 | lua_rawset(L, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8864 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8865 | /* Register previous table in the registry with reference and named entry. |
| 8866 | * The function hlua_register_metatable() pops the stack, so we |
| 8867 | * previously create a copy of the table. |
| 8868 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8869 | lua_pushvalue(L, -1); /* Copy the -1 entry and push it on the stack. */ |
| 8870 | class_map_ref = hlua_register_metatable(L, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8871 | |
| 8872 | /* Assign the metatable to the mai Map object. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8873 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8874 | |
| 8875 | /* Set a name to the table. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8876 | lua_setglobal(L, "Map"); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8877 | |
| 8878 | /* |
| 8879 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8880 | * Register class Channel |
| 8881 | * |
| 8882 | */ |
| 8883 | |
| 8884 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8885 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8886 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8887 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8888 | lua_pushstring(L, "__index"); |
| 8889 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8890 | |
| 8891 | /* Register . */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8892 | hlua_class_function(L, "get", hlua_channel_get); |
| 8893 | hlua_class_function(L, "dup", hlua_channel_dup); |
| 8894 | hlua_class_function(L, "getline", hlua_channel_getline); |
| 8895 | hlua_class_function(L, "set", hlua_channel_set); |
| 8896 | hlua_class_function(L, "append", hlua_channel_append); |
| 8897 | hlua_class_function(L, "send", hlua_channel_send); |
| 8898 | hlua_class_function(L, "forward", hlua_channel_forward); |
| 8899 | hlua_class_function(L, "get_in_len", hlua_channel_get_in_len); |
| 8900 | hlua_class_function(L, "get_out_len", hlua_channel_get_out_len); |
| 8901 | hlua_class_function(L, "is_full", hlua_channel_is_full); |
| 8902 | hlua_class_function(L, "is_resp", hlua_channel_is_resp); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8903 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8904 | lua_rawset(L, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8905 | |
| 8906 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8907 | class_channel_ref = hlua_register_metatable(L, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8908 | |
| 8909 | /* |
| 8910 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8911 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8912 | * |
| 8913 | */ |
| 8914 | |
| 8915 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8916 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8917 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8918 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8919 | lua_pushstring(L, "__index"); |
| 8920 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8921 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8922 | /* Browse existing fetches and create the associated |
| 8923 | * object method. |
| 8924 | */ |
| 8925 | sf = NULL; |
| 8926 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8927 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 8928 | * by an underscore. |
| 8929 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8930 | strncpy(trash.area, sf->kw, trash.size); |
| 8931 | trash.area[trash.size - 1] = '\0'; |
| 8932 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8933 | if (*p == '.' || *p == '-' || *p == '+') |
| 8934 | *p = '_'; |
| 8935 | |
| 8936 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8937 | lua_pushstring(L, trash.area); |
| 8938 | lua_pushlightuserdata(L, sf); |
| 8939 | lua_pushcclosure(L, hlua_run_sample_fetch, 1); |
| 8940 | lua_rawset(L, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8941 | } |
| 8942 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8943 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8944 | |
| 8945 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8946 | class_fetches_ref = hlua_register_metatable(L, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8947 | |
| 8948 | /* |
| 8949 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8950 | * Register class Converters |
| 8951 | * |
| 8952 | */ |
| 8953 | |
| 8954 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8955 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8956 | |
| 8957 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8958 | lua_pushstring(L, "__index"); |
| 8959 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8960 | |
| 8961 | /* Browse existing converters and create the associated |
| 8962 | * object method. |
| 8963 | */ |
| 8964 | sc = NULL; |
| 8965 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8966 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 8967 | * by an underscore. |
| 8968 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8969 | strncpy(trash.area, sc->kw, trash.size); |
| 8970 | trash.area[trash.size - 1] = '\0'; |
| 8971 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8972 | if (*p == '.' || *p == '-' || *p == '+') |
| 8973 | *p = '_'; |
| 8974 | |
| 8975 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8976 | lua_pushstring(L, trash.area); |
| 8977 | lua_pushlightuserdata(L, sc); |
| 8978 | lua_pushcclosure(L, hlua_run_sample_conv, 1); |
| 8979 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8980 | } |
| 8981 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8982 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8983 | |
| 8984 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8985 | class_converters_ref = hlua_register_metatable(L, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8986 | |
| 8987 | /* |
| 8988 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8989 | * Register class HTTP |
| 8990 | * |
| 8991 | */ |
| 8992 | |
| 8993 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8994 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8995 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8996 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8997 | lua_pushstring(L, "__index"); |
| 8998 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8999 | |
| 9000 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9001 | hlua_class_function(L, "req_get_headers",hlua_http_req_get_headers); |
| 9002 | hlua_class_function(L, "req_del_header", hlua_http_req_del_hdr); |
| 9003 | hlua_class_function(L, "req_rep_header", hlua_http_req_rep_hdr); |
| 9004 | hlua_class_function(L, "req_rep_value", hlua_http_req_rep_val); |
| 9005 | hlua_class_function(L, "req_add_header", hlua_http_req_add_hdr); |
| 9006 | hlua_class_function(L, "req_set_header", hlua_http_req_set_hdr); |
| 9007 | hlua_class_function(L, "req_set_method", hlua_http_req_set_meth); |
| 9008 | hlua_class_function(L, "req_set_path", hlua_http_req_set_path); |
| 9009 | hlua_class_function(L, "req_set_query", hlua_http_req_set_query); |
| 9010 | hlua_class_function(L, "req_set_uri", hlua_http_req_set_uri); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9011 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9012 | hlua_class_function(L, "res_get_headers",hlua_http_res_get_headers); |
| 9013 | hlua_class_function(L, "res_del_header", hlua_http_res_del_hdr); |
| 9014 | hlua_class_function(L, "res_rep_header", hlua_http_res_rep_hdr); |
| 9015 | hlua_class_function(L, "res_rep_value", hlua_http_res_rep_val); |
| 9016 | hlua_class_function(L, "res_add_header", hlua_http_res_add_hdr); |
| 9017 | hlua_class_function(L, "res_set_header", hlua_http_res_set_hdr); |
| 9018 | hlua_class_function(L, "res_set_status", hlua_http_res_set_status); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9019 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9020 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9021 | |
| 9022 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9023 | class_http_ref = hlua_register_metatable(L, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9024 | |
| 9025 | /* |
| 9026 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9027 | * Register class AppletTCP |
| 9028 | * |
| 9029 | */ |
| 9030 | |
| 9031 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9032 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9033 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9034 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9035 | lua_pushstring(L, "__index"); |
| 9036 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9037 | |
| 9038 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9039 | hlua_class_function(L, "getline", hlua_applet_tcp_getline); |
| 9040 | hlua_class_function(L, "receive", hlua_applet_tcp_recv); |
| 9041 | hlua_class_function(L, "send", hlua_applet_tcp_send); |
| 9042 | hlua_class_function(L, "set_priv", hlua_applet_tcp_set_priv); |
| 9043 | hlua_class_function(L, "get_priv", hlua_applet_tcp_get_priv); |
| 9044 | hlua_class_function(L, "set_var", hlua_applet_tcp_set_var); |
| 9045 | hlua_class_function(L, "unset_var", hlua_applet_tcp_unset_var); |
| 9046 | hlua_class_function(L, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9047 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9048 | lua_settable(L, -3); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9049 | |
| 9050 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9051 | class_applet_tcp_ref = hlua_register_metatable(L, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9052 | |
| 9053 | /* |
| 9054 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9055 | * Register class AppletHTTP |
| 9056 | * |
| 9057 | */ |
| 9058 | |
| 9059 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9060 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9061 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9062 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9063 | lua_pushstring(L, "__index"); |
| 9064 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9065 | |
| 9066 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9067 | hlua_class_function(L, "set_priv", hlua_applet_http_set_priv); |
| 9068 | hlua_class_function(L, "get_priv", hlua_applet_http_get_priv); |
| 9069 | hlua_class_function(L, "set_var", hlua_applet_http_set_var); |
| 9070 | hlua_class_function(L, "unset_var", hlua_applet_http_unset_var); |
| 9071 | hlua_class_function(L, "get_var", hlua_applet_http_get_var); |
| 9072 | hlua_class_function(L, "getline", hlua_applet_http_getline); |
| 9073 | hlua_class_function(L, "receive", hlua_applet_http_recv); |
| 9074 | hlua_class_function(L, "send", hlua_applet_http_send); |
| 9075 | hlua_class_function(L, "add_header", hlua_applet_http_addheader); |
| 9076 | hlua_class_function(L, "set_status", hlua_applet_http_status); |
| 9077 | hlua_class_function(L, "start_response", hlua_applet_http_start_response); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9078 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9079 | lua_settable(L, -3); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9080 | |
| 9081 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9082 | class_applet_http_ref = hlua_register_metatable(L, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9083 | |
| 9084 | /* |
| 9085 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9086 | * Register class TXN |
| 9087 | * |
| 9088 | */ |
| 9089 | |
| 9090 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9091 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9092 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9093 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9094 | lua_pushstring(L, "__index"); |
| 9095 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9096 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 9097 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9098 | hlua_class_function(L, "set_priv", hlua_set_priv); |
| 9099 | hlua_class_function(L, "get_priv", hlua_get_priv); |
| 9100 | hlua_class_function(L, "set_var", hlua_set_var); |
| 9101 | hlua_class_function(L, "unset_var", hlua_unset_var); |
| 9102 | hlua_class_function(L, "get_var", hlua_get_var); |
| 9103 | hlua_class_function(L, "done", hlua_txn_done); |
| 9104 | hlua_class_function(L, "reply", hlua_txn_reply_new); |
| 9105 | hlua_class_function(L, "set_loglevel", hlua_txn_set_loglevel); |
| 9106 | hlua_class_function(L, "set_tos", hlua_txn_set_tos); |
| 9107 | hlua_class_function(L, "set_mark", hlua_txn_set_mark); |
| 9108 | hlua_class_function(L, "set_priority_class", hlua_txn_set_priority_class); |
| 9109 | hlua_class_function(L, "set_priority_offset", hlua_txn_set_priority_offset); |
| 9110 | hlua_class_function(L, "deflog", hlua_txn_deflog); |
| 9111 | hlua_class_function(L, "log", hlua_txn_log); |
| 9112 | hlua_class_function(L, "Debug", hlua_txn_log_debug); |
| 9113 | hlua_class_function(L, "Info", hlua_txn_log_info); |
| 9114 | hlua_class_function(L, "Warning", hlua_txn_log_warning); |
| 9115 | hlua_class_function(L, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 9116 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9117 | lua_rawset(L, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 9118 | |
| 9119 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9120 | class_txn_ref = hlua_register_metatable(L, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9121 | |
| 9122 | /* |
| 9123 | * |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 9124 | * Register class reply |
| 9125 | * |
| 9126 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9127 | lua_newtable(L); |
| 9128 | lua_pushstring(L, "__index"); |
| 9129 | lua_newtable(L); |
| 9130 | hlua_class_function(L, "set_status", hlua_txn_reply_set_status); |
| 9131 | hlua_class_function(L, "add_header", hlua_txn_reply_add_header); |
| 9132 | hlua_class_function(L, "del_header", hlua_txn_reply_del_header); |
| 9133 | hlua_class_function(L, "set_body", hlua_txn_reply_set_body); |
| 9134 | lua_settable(L, -3); /* Sets the __index entry. */ |
| 9135 | class_txn_reply_ref = luaL_ref(L, LUA_REGISTRYINDEX); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 9136 | |
| 9137 | |
| 9138 | /* |
| 9139 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9140 | * Register class Socket |
| 9141 | * |
| 9142 | */ |
| 9143 | |
| 9144 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9145 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9146 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9147 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9148 | lua_pushstring(L, "__index"); |
| 9149 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9150 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 9151 | #ifdef USE_OPENSSL |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9152 | hlua_class_function(L, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 9153 | #endif |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9154 | hlua_class_function(L, "connect", hlua_socket_connect); |
| 9155 | hlua_class_function(L, "send", hlua_socket_send); |
| 9156 | hlua_class_function(L, "receive", hlua_socket_receive); |
| 9157 | hlua_class_function(L, "close", hlua_socket_close); |
| 9158 | hlua_class_function(L, "getpeername", hlua_socket_getpeername); |
| 9159 | hlua_class_function(L, "getsockname", hlua_socket_getsockname); |
| 9160 | hlua_class_function(L, "setoption", hlua_socket_setoption); |
| 9161 | hlua_class_function(L, "settimeout", hlua_socket_settimeout); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9162 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9163 | 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] | 9164 | |
| 9165 | /* Register the garbage collector entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9166 | lua_pushstring(L, "__gc"); |
| 9167 | lua_pushcclosure(L, hlua_socket_gc, 0); |
| 9168 | 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] | 9169 | |
| 9170 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9171 | class_socket_ref = hlua_register_metatable(L, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9172 | |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 9173 | lua_atpanic(L, hlua_panic_safe); |
| 9174 | |
| 9175 | return L; |
| 9176 | } |
| 9177 | |
| 9178 | void hlua_init(void) { |
| 9179 | int i; |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 9180 | char *errmsg; |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 9181 | #ifdef USE_OPENSSL |
| 9182 | struct srv_kw *kw; |
| 9183 | int tmp_error; |
| 9184 | char *error; |
| 9185 | char *args[] = { /* SSL client configuration. */ |
| 9186 | "ssl", |
| 9187 | "verify", |
| 9188 | "none", |
| 9189 | NULL |
| 9190 | }; |
| 9191 | #endif |
| 9192 | |
| 9193 | /* Init post init function list head */ |
| 9194 | for (i = 0; i < MAX_THREADS + 1; i++) |
| 9195 | LIST_INIT(&hlua_init_functions[i]); |
| 9196 | |
| 9197 | /* Init state for common/shared lua parts */ |
| 9198 | hlua_state_id = 0; |
| 9199 | ha_set_tid(0); |
| 9200 | hlua_states[0] = hlua_init_state(0); |
| 9201 | |
| 9202 | /* Init state 1 for thread 0. We have at least one thread. */ |
| 9203 | hlua_state_id = 1; |
| 9204 | ha_set_tid(0); |
| 9205 | hlua_states[1] = hlua_init_state(1); |
| 9206 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9207 | /* Proxy and server configuration initialisation. */ |
William Lallemand | 6bb77b9 | 2021-07-28 15:48:16 +0200 | [diff] [blame] | 9208 | socket_proxy = alloc_new_proxy("LUA-SOCKET", PR_CAP_FE|PR_CAP_BE|PR_CAP_INT, &errmsg); |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 9209 | if (!socket_proxy) { |
| 9210 | fprintf(stderr, "Lua init: %s\n", errmsg); |
| 9211 | exit(1); |
| 9212 | } |
| 9213 | proxy_preset_defaults(socket_proxy); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9214 | |
| 9215 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9216 | socket_tcp = new_server(socket_proxy); |
| 9217 | if (!socket_tcp) { |
| 9218 | fprintf(stderr, "Lua init: failed to allocate tcp server socket\n"); |
| 9219 | exit(1); |
| 9220 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9221 | |
| 9222 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9223 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9224 | socket_ssl = new_server(socket_proxy); |
| 9225 | if (!socket_ssl) { |
| 9226 | fprintf(stderr, "Lua init: failed to allocate ssl server socket\n"); |
| 9227 | exit(1); |
| 9228 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9229 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9230 | socket_ssl->use_ssl = 1; |
| 9231 | socket_ssl->xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9232 | |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 9233 | for (i = 0; args[i] != NULL; i++) { |
| 9234 | 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] | 9235 | /* |
| 9236 | * |
| 9237 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9238 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9239 | * features like client certificates and ssl_verify. |
| 9240 | * |
| 9241 | */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9242 | tmp_error = kw->parse(args, &i, socket_proxy, socket_ssl, &error); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9243 | if (tmp_error != 0) { |
| 9244 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 9245 | abort(); /* This must be never arrives because the command line |
| 9246 | not editable by the user. */ |
| 9247 | } |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 9248 | i += kw->skip; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9249 | } |
| 9250 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9251 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 9252 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 9253 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 9254 | |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 9255 | static void hlua_deinit() |
| 9256 | { |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 9257 | int thr; |
| 9258 | |
| 9259 | for (thr = 0; thr < MAX_THREADS+1; thr++) { |
| 9260 | if (hlua_states[thr]) |
| 9261 | lua_close(hlua_states[thr]); |
| 9262 | } |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 9263 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9264 | free_server(socket_tcp); |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 9265 | |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 9266 | #ifdef USE_OPENSSL |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9267 | free_server(socket_ssl); |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 9268 | #endif |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 9269 | |
| 9270 | free_proxy(socket_proxy); |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 9271 | } |
| 9272 | |
| 9273 | REGISTER_POST_DEINIT(hlua_deinit); |
| 9274 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9275 | static void hlua_register_build_options(void) |
| 9276 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 9277 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9278 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 9279 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 9280 | hap_register_build_opts(ptr, 1); |
| 9281 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9282 | |
| 9283 | INITCALL0(STG_REGISTER, hlua_register_build_options); |