Thierry Fournier | e726b14 | 2016-02-11 17:57:57 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Lua unsafe core engine |
| 3 | * |
| 4 | * Copyright 2015-2016 Thierry Fournier <tfournier@arpalert.org> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
Bertrand Jacquin | f4c12d4 | 2021-01-21 21:14:07 +0000 | [diff] [blame] | 13 | #define _GNU_SOURCE |
| 14 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 15 | #include <ctype.h> |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 16 | #include <setjmp.h> |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 17 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 18 | #include <lauxlib.h> |
| 19 | #include <lua.h> |
| 20 | #include <lualib.h> |
| 21 | |
Thierry FOURNIER | 463119c | 2015-03-10 00:35:36 +0100 | [diff] [blame] | 22 | #if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 503 |
| 23 | #error "Requires Lua 5.3 or later." |
Cyril Bonté | dc0306e | 2015-03-02 00:08:40 +0100 | [diff] [blame] | 24 | #endif |
| 25 | |
Willy Tarreau | 8d2b777 | 2020-05-27 10:58:19 +0200 | [diff] [blame] | 26 | #include <import/ebpttree.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 27 | |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 28 | #include <haproxy/api.h> |
| 29 | #include <haproxy/applet.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 30 | #include <haproxy/arg.h> |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 31 | #include <haproxy/auth.h> |
Willy Tarreau | 6be7849 | 2020-06-05 00:00:29 +0200 | [diff] [blame] | 32 | #include <haproxy/cfgparse.h> |
Willy Tarreau | f1d32c4 | 2020-06-04 21:07:02 +0200 | [diff] [blame] | 33 | #include <haproxy/channel.h> |
Willy Tarreau | 83487a8 | 2020-06-04 20:19:54 +0200 | [diff] [blame] | 34 | #include <haproxy/cli.h> |
Willy Tarreau | 7ea393d | 2020-06-04 18:02:10 +0200 | [diff] [blame] | 35 | #include <haproxy/connection.h> |
Willy Tarreau | 5413a87 | 2020-06-02 19:33:08 +0200 | [diff] [blame] | 36 | #include <haproxy/h1.h> |
Willy Tarreau | 8641605 | 2020-06-04 09:20:54 +0200 | [diff] [blame] | 37 | #include <haproxy/hlua.h> |
Willy Tarreau | 8c79400 | 2020-06-04 10:05:25 +0200 | [diff] [blame] | 38 | #include <haproxy/hlua_fcn.h> |
Willy Tarreau | c2b1ff0 | 2020-06-04 21:21:03 +0200 | [diff] [blame] | 39 | #include <haproxy/http_ana.h> |
Willy Tarreau | 126ba3a | 2020-06-04 18:26:43 +0200 | [diff] [blame] | 40 | #include <haproxy/http_fetch.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 41 | #include <haproxy/http_htx.h> |
Willy Tarreau | c761f84 | 2020-06-04 11:40:28 +0200 | [diff] [blame] | 42 | #include <haproxy/http_rules.h> |
Willy Tarreau | 36979d9 | 2020-06-05 17:27:29 +0200 | [diff] [blame] | 43 | #include <haproxy/log.h> |
Willy Tarreau | 2cd5809 | 2020-06-04 15:10:43 +0200 | [diff] [blame] | 44 | #include <haproxy/map.h> |
Willy Tarreau | 8efbdfb | 2020-06-04 11:29:21 +0200 | [diff] [blame] | 45 | #include <haproxy/obj_type.h> |
Willy Tarreau | 225a90a | 2020-06-04 15:06:28 +0200 | [diff] [blame] | 46 | #include <haproxy/pattern.h> |
Willy Tarreau | 469509b | 2020-06-04 15:13:30 +0200 | [diff] [blame] | 47 | #include <haproxy/payload.h> |
Willy Tarreau | a264d96 | 2020-06-04 22:29:18 +0200 | [diff] [blame] | 48 | #include <haproxy/proxy-t.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 49 | #include <haproxy/regex.h> |
Willy Tarreau | e6ce10b | 2020-06-04 15:33:47 +0200 | [diff] [blame] | 50 | #include <haproxy/sample.h> |
Willy Tarreau | 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 | 9d6bb5a | 2020-02-06 15:55:41 +0100 | [diff] [blame] | 127 | static int hlua_panic_ljmp(lua_State *L) { WILL_LJMP(longjmp(safe_ljmp_env, 1)); } |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 128 | |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 129 | /* This is the chained list of struct hlua_function referenced |
| 130 | * for haproxy action, sample-fetches, converters, cli and |
| 131 | * applet bindings. It is used for a post-initialisation control. |
| 132 | */ |
| 133 | static struct list referenced_functions = LIST_HEAD_INIT(referenced_functions); |
| 134 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 135 | /* This variable is used only during initialization to identify the Lua state |
| 136 | * currently being initialized. 0 is the common lua state, 1 to n are the Lua |
| 137 | * states dedicated to each thread (in this case hlua_state_id==tid+1). |
| 138 | */ |
| 139 | static int hlua_state_id; |
| 140 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 141 | /* This is a NULL-terminated list of lua file which are referenced to load per thread */ |
| 142 | static char **per_thread_load = NULL; |
| 143 | |
| 144 | lua_State *hlua_init_state(int thread_id); |
| 145 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 146 | #define SET_SAFE_LJMP_L(__L, __HLUA) \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 147 | ({ \ |
| 148 | int ret; \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 149 | if ((__HLUA)->state_id == 0) \ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 150 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 151 | if (setjmp(safe_ljmp_env) != 0) { \ |
| 152 | lua_atpanic(__L, hlua_panic_safe); \ |
| 153 | ret = 0; \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 154 | if ((__HLUA)->state_id == 0) \ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 155 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 156 | } else { \ |
| 157 | lua_atpanic(__L, hlua_panic_ljmp); \ |
| 158 | ret = 1; \ |
| 159 | } \ |
| 160 | ret; \ |
| 161 | }) |
| 162 | |
| 163 | /* If we are the last function catching Lua errors, we |
| 164 | * must reset the panic function. |
| 165 | */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 166 | #define RESET_SAFE_LJMP_L(__L, __HLUA) \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 167 | do { \ |
| 168 | lua_atpanic(__L, hlua_panic_safe); \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 169 | if ((__HLUA)->state_id == 0) \ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 170 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 171 | } while(0) |
| 172 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 173 | #define SET_SAFE_LJMP(__HLUA) \ |
| 174 | SET_SAFE_LJMP_L((__HLUA)->T, __HLUA) |
| 175 | |
| 176 | #define RESET_SAFE_LJMP(__HLUA) \ |
| 177 | RESET_SAFE_LJMP_L((__HLUA)->T, __HLUA) |
| 178 | |
| 179 | #define SET_SAFE_LJMP_PARENT(__HLUA) \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 180 | SET_SAFE_LJMP_L(hlua_states[(__HLUA)->state_id], __HLUA) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 181 | |
| 182 | #define RESET_SAFE_LJMP_PARENT(__HLUA) \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 183 | RESET_SAFE_LJMP_L(hlua_states[(__HLUA)->state_id], __HLUA) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 184 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 185 | /* Applet status flags */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 186 | #define APPLET_DONE 0x01 /* applet processing is done. */ |
Christopher Faulet | 18c2e8d | 2019-03-01 12:02:08 +0100 | [diff] [blame] | 187 | /* unused: 0x02 */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 188 | #define APPLET_HDR_SENT 0x04 /* Response header sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 189 | /* unused: 0x08, 0x10 */ |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 190 | #define APPLET_HTTP11 0x20 /* Last chunk sent. */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 191 | #define APPLET_RSP_SENT 0x40 /* The response was fully sent */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 192 | |
Thierry Fournier | afc63e2 | 2020-11-28 17:06:51 +0100 | [diff] [blame] | 193 | /* The main Lua execution context. The 0 index is the |
| 194 | * common state shared by all threads. |
| 195 | */ |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 196 | static lua_State *hlua_states[MAX_THREADS + 1]; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 197 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 198 | /* This is the memory pool containing struct lua for applets |
| 199 | * (including cli). |
| 200 | */ |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 201 | DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua)); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 202 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 203 | /* Used for Socket connection. */ |
| 204 | static struct proxy socket_proxy; |
| 205 | static struct server socket_tcp; |
| 206 | #ifdef USE_OPENSSL |
| 207 | static struct server socket_ssl; |
| 208 | #endif |
| 209 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 210 | /* List head of the function called at the initialisation time. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 211 | struct list hlua_init_functions[MAX_THREADS + 1]; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 212 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 213 | /* The following variables contains the reference of the different |
| 214 | * Lua classes. These references are useful for identify metadata |
| 215 | * associated with an object. |
| 216 | */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 217 | static int class_txn_ref; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 218 | static int class_socket_ref; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 219 | static int class_channel_ref; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 220 | static int class_fetches_ref; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 221 | static int class_converters_ref; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 222 | static int class_http_ref; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 223 | static int class_map_ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 224 | static int class_applet_tcp_ref; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 225 | static int class_applet_http_ref; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 226 | static int class_txn_reply_ref; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 227 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 228 | /* Global Lua execution timeout. By default Lua, execution linked |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 229 | * with stream (actions, sample-fetches and converters) have a |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 230 | * short timeout. Lua linked with tasks doesn't have a timeout |
| 231 | * because a task may remain alive during all the haproxy execution. |
| 232 | */ |
| 233 | static unsigned int hlua_timeout_session = 4000; /* session timeout. */ |
| 234 | static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 235 | static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 236 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 237 | /* Interrupts the Lua processing each "hlua_nb_instruction" instructions. |
| 238 | * it is used for preventing infinite loops. |
| 239 | * |
| 240 | * I test the scheer with an infinite loop containing one incrementation |
| 241 | * and one test. I run this loop between 10 seconds, I raise a ceil of |
| 242 | * 710M loops from one interrupt each 9000 instructions, so I fix the value |
| 243 | * to one interrupt each 10 000 instructions. |
| 244 | * |
| 245 | * configured | Number of |
| 246 | * instructions | loops executed |
| 247 | * between two | in milions |
| 248 | * forced yields | |
| 249 | * ---------------+--------------- |
| 250 | * 10 | 160 |
| 251 | * 500 | 670 |
| 252 | * 1000 | 680 |
| 253 | * 5000 | 700 |
| 254 | * 7000 | 700 |
| 255 | * 8000 | 700 |
| 256 | * 9000 | 710 <- ceil |
| 257 | * 10000 | 710 |
| 258 | * 100000 | 710 |
| 259 | * 1000000 | 710 |
| 260 | * |
| 261 | */ |
| 262 | static unsigned int hlua_nb_instruction = 10000; |
| 263 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 264 | /* Descriptor for the memory allocation state. The limit is pre-initialised to |
| 265 | * 0 until it is replaced by "tune.lua.maxmem" during the config parsing, or it |
| 266 | * is replaced with ~0 during post_init after everything was loaded. This way |
| 267 | * it is guaranteed that if limit is ~0 the boot is complete and that if it's |
| 268 | * zero it's not yet limited and proper accounting is required. |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 269 | */ |
| 270 | struct hlua_mem_allocator { |
| 271 | size_t allocated; |
| 272 | size_t limit; |
| 273 | }; |
| 274 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 275 | static struct hlua_mem_allocator hlua_global_allocator THREAD_ALIGNED(64); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 276 | |
Christopher Faulet | a61789a | 2021-03-19 15:16:28 +0100 | [diff] [blame] | 277 | /* > 0 if lua is in a non-rentrant part, thus with a non-dumpable stack */ |
| 278 | THREAD_LOCAL unsigned int hlua_not_dumpable = 0; |
| 279 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 280 | /* These functions converts types between HAProxy internal args or |
| 281 | * sample and LUA types. Another function permits to check if the |
| 282 | * LUA stack contains arguments according with an required ARG_T |
| 283 | * format. |
| 284 | */ |
| 285 | static int hlua_arg2lua(lua_State *L, const struct arg *arg); |
| 286 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 287 | __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] | 288 | uint64_t mask, struct proxy *p); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 289 | static int hlua_smp2lua(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 290 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 291 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp); |
| 292 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 293 | __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] | 294 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 295 | struct prepend_path { |
| 296 | struct list l; |
| 297 | char *type; |
| 298 | char *path; |
| 299 | }; |
| 300 | |
| 301 | static struct list prepend_path_list = LIST_HEAD_INIT(prepend_path_list); |
| 302 | |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 303 | #define SEND_ERR(__be, __fmt, __args...) \ |
| 304 | do { \ |
| 305 | send_log(__be, LOG_ERR, __fmt, ## __args); \ |
| 306 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 307 | ha_alert(__fmt, ## __args); \ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 308 | } while (0) |
| 309 | |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 310 | static inline struct hlua_function *new_hlua_function() |
| 311 | { |
| 312 | struct hlua_function *fcn; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 313 | int i; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 314 | |
| 315 | fcn = calloc(1, sizeof(*fcn)); |
| 316 | if (!fcn) |
| 317 | return NULL; |
| 318 | LIST_ADDQ(&referenced_functions, &fcn->l); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 319 | for (i = 0; i < MAX_THREADS + 1; i++) |
| 320 | fcn->function_ref[i] = -1; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 321 | return fcn; |
| 322 | } |
| 323 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 324 | /* If the common state is set, the stack id is 0, otherwise it is the tid + 1 */ |
| 325 | static inline int fcn_ref_to_stack_id(struct hlua_function *fcn) |
| 326 | { |
| 327 | if (fcn->function_ref[0] == -1) |
| 328 | return tid + 1; |
| 329 | return 0; |
| 330 | } |
| 331 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 332 | /* Used to check an Lua function type in the stack. It creates and |
| 333 | * returns a reference of the function. This function throws an |
| 334 | * error if the rgument is not a "function". |
| 335 | */ |
| 336 | __LJMP unsigned int hlua_checkfunction(lua_State *L, int argno) |
| 337 | { |
| 338 | if (!lua_isfunction(L, argno)) { |
Thierry FOURNIER | fd1e955 | 2018-02-23 18:41:18 +0100 | [diff] [blame] | 339 | 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] | 340 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 341 | } |
| 342 | lua_pushvalue(L, argno); |
| 343 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 344 | } |
| 345 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 346 | /* Return the string that is of the top of the stack. */ |
| 347 | const char *hlua_get_top_error_string(lua_State *L) |
| 348 | { |
| 349 | if (lua_gettop(L) < 1) |
| 350 | return "unknown error"; |
| 351 | if (lua_type(L, -1) != LUA_TSTRING) |
| 352 | return "unknown error"; |
| 353 | return lua_tostring(L, -1); |
| 354 | } |
| 355 | |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 356 | __LJMP static const char *hlua_traceback(lua_State *L) |
| 357 | { |
| 358 | lua_Debug ar; |
| 359 | int level = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 360 | struct buffer *msg = get_trash_chunk(); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 361 | int filled = 0; |
| 362 | |
| 363 | while (lua_getstack(L, level++, &ar)) { |
| 364 | |
| 365 | /* Add separator */ |
| 366 | if (filled) |
| 367 | chunk_appendf(msg, ", "); |
| 368 | filled = 1; |
| 369 | |
| 370 | /* Fill fields: |
| 371 | * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what; |
| 372 | * 'l': fills in the field currentline; |
| 373 | * 'n': fills in the field name and namewhat; |
| 374 | * 't': fills in the field istailcall; |
| 375 | */ |
| 376 | lua_getinfo(L, "Slnt", &ar); |
| 377 | |
| 378 | /* Append code localisation */ |
| 379 | if (ar.currentline > 0) |
| 380 | chunk_appendf(msg, "%s:%d ", ar.short_src, ar.currentline); |
| 381 | else |
| 382 | chunk_appendf(msg, "%s ", ar.short_src); |
| 383 | |
| 384 | /* |
| 385 | * Get function name |
| 386 | * |
| 387 | * if namewhat is no empty, name is defined. |
| 388 | * what contains "Lua" for Lua function, "C" for C function, |
| 389 | * or "main" for main code. |
| 390 | */ |
| 391 | if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */ |
| 392 | chunk_appendf(msg, "%s '%s'", ar.namewhat, ar.name); /* use it */ |
| 393 | |
| 394 | else if (*ar.what == 'm') /* "main", the code is not executed in a function */ |
| 395 | chunk_appendf(msg, "main chunk"); |
| 396 | |
| 397 | else if (*ar.what != 'C') /* for Lua functions, use <file:line> */ |
| 398 | chunk_appendf(msg, "C function line %d", ar.linedefined); |
| 399 | |
| 400 | else /* nothing left... */ |
| 401 | chunk_appendf(msg, "?"); |
| 402 | |
| 403 | |
| 404 | /* Display tailed call */ |
| 405 | if (ar.istailcall) |
| 406 | chunk_appendf(msg, " ..."); |
| 407 | } |
| 408 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 409 | return msg->area; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 413 | /* This function check the number of arguments available in the |
| 414 | * stack. If the number of arguments available is not the same |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 415 | * then <nb> an error is thrown. |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 416 | */ |
| 417 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 418 | { |
| 419 | if (lua_gettop(L) == nb) |
| 420 | return; |
| 421 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 422 | } |
| 423 | |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 424 | /* This function pushes an error string prefixed by the file name |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 425 | * and the line number where the error is encountered. |
| 426 | */ |
| 427 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 428 | { |
| 429 | va_list argp; |
| 430 | va_start(argp, fmt); |
| 431 | luaL_where(L, 1); |
| 432 | lua_pushvfstring(L, fmt, argp); |
| 433 | va_end(argp); |
| 434 | lua_concat(L, 2); |
| 435 | return 1; |
| 436 | } |
| 437 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 438 | /* This functions is used with sample fetch and converters. It |
| 439 | * converts the HAProxy configuration argument in a lua stack |
| 440 | * values. |
| 441 | * |
| 442 | * It takes an array of "arg", and each entry of the array is |
| 443 | * converted and pushed in the LUA stack. |
| 444 | */ |
| 445 | static int hlua_arg2lua(lua_State *L, const struct arg *arg) |
| 446 | { |
| 447 | switch (arg->type) { |
| 448 | case ARGT_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 449 | case ARGT_TIME: |
| 450 | case ARGT_SIZE: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 451 | lua_pushinteger(L, arg->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 452 | break; |
| 453 | |
| 454 | case ARGT_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 455 | lua_pushlstring(L, arg->data.str.area, arg->data.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 456 | break; |
| 457 | |
| 458 | case ARGT_IPV4: |
| 459 | case ARGT_IPV6: |
| 460 | case ARGT_MSK4: |
| 461 | case ARGT_MSK6: |
| 462 | case ARGT_FE: |
| 463 | case ARGT_BE: |
| 464 | case ARGT_TAB: |
| 465 | case ARGT_SRV: |
| 466 | case ARGT_USR: |
| 467 | case ARGT_MAP: |
| 468 | default: |
| 469 | lua_pushnil(L); |
| 470 | break; |
| 471 | } |
| 472 | return 1; |
| 473 | } |
| 474 | |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 475 | /* 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] | 476 | * 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] | 477 | * with sample fetch wrappers. The input arguments are given to the |
| 478 | * lua wrapper and converted as arg list by the function. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 479 | */ |
| 480 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg) |
| 481 | { |
| 482 | switch (lua_type(L, ud)) { |
| 483 | |
| 484 | case LUA_TNUMBER: |
| 485 | case LUA_TBOOLEAN: |
| 486 | arg->type = ARGT_SINT; |
| 487 | arg->data.sint = lua_tointeger(L, ud); |
| 488 | break; |
| 489 | |
| 490 | case LUA_TSTRING: |
| 491 | arg->type = ARGT_STR; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 492 | 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] | 493 | /* 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] | 494 | 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] | 495 | arg->data.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 496 | break; |
| 497 | |
| 498 | case LUA_TUSERDATA: |
| 499 | case LUA_TNIL: |
| 500 | case LUA_TTABLE: |
| 501 | case LUA_TFUNCTION: |
| 502 | case LUA_TTHREAD: |
| 503 | case LUA_TLIGHTUSERDATA: |
| 504 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 505 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 506 | break; |
| 507 | } |
| 508 | return 1; |
| 509 | } |
| 510 | |
| 511 | /* the following functions are used to convert a struct sample |
| 512 | * in Lua type. This useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 513 | * fetches or converters. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 514 | */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 515 | static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 516 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 517 | switch (smp->data.type) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 518 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 519 | case SMP_T_BOOL: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 520 | lua_pushinteger(L, smp->data.u.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 521 | break; |
| 522 | |
| 523 | case SMP_T_BIN: |
| 524 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 525 | 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] | 526 | break; |
| 527 | |
| 528 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 529 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 530 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 531 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 532 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 533 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 534 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 535 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 536 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 537 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 538 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 539 | 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] | 540 | break; |
| 541 | default: |
| 542 | lua_pushnil(L); |
| 543 | break; |
| 544 | } |
| 545 | break; |
| 546 | |
| 547 | case SMP_T_IPV4: |
| 548 | case SMP_T_IPV6: |
| 549 | 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] | 550 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 551 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 552 | 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] | 553 | else |
| 554 | lua_pushnil(L); |
| 555 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 556 | default: |
| 557 | lua_pushnil(L); |
| 558 | break; |
| 559 | } |
| 560 | return 1; |
| 561 | } |
| 562 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 563 | /* the following functions are used to convert a struct sample |
| 564 | * in Lua strings. This is useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 565 | * fetches or converters. |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 566 | */ |
| 567 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
| 568 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 569 | switch (smp->data.type) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 570 | |
| 571 | case SMP_T_BIN: |
| 572 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 573 | 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] | 574 | break; |
| 575 | |
| 576 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 577 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 578 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 579 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 580 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 581 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 582 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 583 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 584 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 585 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 586 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 587 | 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] | 588 | break; |
| 589 | default: |
| 590 | lua_pushstring(L, ""); |
| 591 | break; |
| 592 | } |
| 593 | break; |
| 594 | |
| 595 | case SMP_T_SINT: |
| 596 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 597 | case SMP_T_IPV4: |
| 598 | case SMP_T_IPV6: |
| 599 | 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] | 600 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 601 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 602 | 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] | 603 | else |
| 604 | lua_pushstring(L, ""); |
| 605 | break; |
| 606 | default: |
| 607 | lua_pushstring(L, ""); |
| 608 | break; |
| 609 | } |
| 610 | return 1; |
| 611 | } |
| 612 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 613 | /* the following functions are used to convert an Lua type in a |
| 614 | * struct sample. This is useful to provide data from a converter |
| 615 | * to the LUA code. |
| 616 | */ |
| 617 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 618 | { |
| 619 | switch (lua_type(L, ud)) { |
| 620 | |
| 621 | case LUA_TNUMBER: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 622 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 623 | smp->data.u.sint = lua_tointeger(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 624 | break; |
| 625 | |
| 626 | |
| 627 | case LUA_TBOOLEAN: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 628 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 629 | smp->data.u.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 630 | break; |
| 631 | |
| 632 | case LUA_TSTRING: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 633 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 634 | smp->flags |= SMP_F_CONST; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 635 | 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] | 636 | /* 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] | 637 | 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] | 638 | smp->data.u.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 639 | break; |
| 640 | |
| 641 | case LUA_TUSERDATA: |
| 642 | case LUA_TNIL: |
| 643 | case LUA_TTABLE: |
| 644 | case LUA_TFUNCTION: |
| 645 | case LUA_TTHREAD: |
| 646 | case LUA_TLIGHTUSERDATA: |
Thierry FOURNIER | 93405e1 | 2015-08-26 14:19:03 +0200 | [diff] [blame] | 647 | case LUA_TNONE: |
| 648 | default: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 649 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 650 | smp->data.u.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 651 | break; |
| 652 | } |
| 653 | return 1; |
| 654 | } |
| 655 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 656 | /* This function check the "argp" built by another conversion function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 657 | * 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] | 658 | * 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] | 659 | * |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 660 | * This function assumes that the argp argument contains ARGM_NBARGS + 1 |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 661 | * entries. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 662 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 663 | __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] | 664 | uint64_t mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 665 | { |
| 666 | int min_arg; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 667 | int i, idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 668 | struct proxy *px; |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 669 | struct userlist *ul; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 670 | struct my_regex *reg; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 671 | const char *msg = NULL; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 672 | char *sname, *pname, *err = NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 673 | |
| 674 | idx = 0; |
| 675 | min_arg = ARGM(mask); |
| 676 | mask >>= ARGM_BITS; |
| 677 | |
| 678 | while (1) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 679 | struct buffer tmp = BUF_NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 680 | |
| 681 | /* Check oversize. */ |
| 682 | if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 683 | msg = "Malformed argument mask"; |
| 684 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | /* Check for mandatory arguments. */ |
| 688 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 689 | if (idx < min_arg) { |
| 690 | |
| 691 | /* If miss other argument than the first one, we return an error. */ |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 692 | if (idx > 0) { |
| 693 | msg = "Mandatory argument expected"; |
| 694 | goto error; |
| 695 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 696 | |
| 697 | /* If first argument have a certain type, some default values |
| 698 | * may be used. See the function smp_resolve_args(). |
| 699 | */ |
| 700 | switch (mask & ARGT_MASK) { |
| 701 | |
| 702 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 703 | if (!(p->cap & PR_CAP_FE)) { |
| 704 | msg = "Mandatory argument expected"; |
| 705 | goto error; |
| 706 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 707 | argp[idx].data.prx = p; |
| 708 | argp[idx].type = ARGT_FE; |
| 709 | argp[idx+1].type = ARGT_STOP; |
| 710 | break; |
| 711 | |
| 712 | case ARGT_BE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 713 | if (!(p->cap & PR_CAP_BE)) { |
| 714 | msg = "Mandatory argument expected"; |
| 715 | goto error; |
| 716 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 717 | argp[idx].data.prx = p; |
| 718 | argp[idx].type = ARGT_BE; |
| 719 | argp[idx+1].type = ARGT_STOP; |
| 720 | break; |
| 721 | |
| 722 | case ARGT_TAB: |
| 723 | argp[idx].data.prx = p; |
| 724 | argp[idx].type = ARGT_TAB; |
| 725 | argp[idx+1].type = ARGT_STOP; |
| 726 | break; |
| 727 | |
| 728 | default: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 729 | msg = "Mandatory argument expected"; |
| 730 | goto error; |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 731 | break; |
| 732 | } |
| 733 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 734 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 735 | } |
| 736 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 737 | /* Check for exceed the number of required argument. */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 738 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 739 | argp[idx].type != ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 740 | msg = "Last argument expected"; |
| 741 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 745 | argp[idx].type == ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 746 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 747 | } |
| 748 | |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 749 | /* Convert some argument types. All string in argp[] are for not |
| 750 | * duplicated yet. |
| 751 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 752 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 753 | case ARGT_SINT: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 754 | if (argp[idx].type != ARGT_SINT) { |
| 755 | msg = "integer expected"; |
| 756 | goto error; |
| 757 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 758 | argp[idx].type = ARGT_SINT; |
| 759 | break; |
| 760 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 761 | case ARGT_TIME: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 762 | if (argp[idx].type != ARGT_SINT) { |
| 763 | msg = "integer expected"; |
| 764 | goto error; |
| 765 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 766 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 767 | break; |
| 768 | |
| 769 | case ARGT_SIZE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 770 | if (argp[idx].type != ARGT_SINT) { |
| 771 | msg = "integer expected"; |
| 772 | goto error; |
| 773 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 774 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 775 | break; |
| 776 | |
| 777 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 778 | if (argp[idx].type != ARGT_STR) { |
| 779 | msg = "string expected"; |
| 780 | goto error; |
| 781 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 782 | 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] | 783 | if (!argp[idx].data.prx) { |
| 784 | msg = "frontend doesn't exist"; |
| 785 | goto error; |
| 786 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 787 | argp[idx].type = ARGT_FE; |
| 788 | break; |
| 789 | |
| 790 | case ARGT_BE: |
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_be_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 = "backend doesn't exist"; |
| 798 | goto error; |
| 799 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 800 | argp[idx].type = ARGT_BE; |
| 801 | break; |
| 802 | |
| 803 | case ARGT_TAB: |
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.t = stktable_find_by_name(argp[idx].data.str.area); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 809 | if (!argp[idx].data.t) { |
| 810 | msg = "table doesn't exist"; |
| 811 | goto error; |
| 812 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 813 | argp[idx].type = ARGT_TAB; |
| 814 | break; |
| 815 | |
| 816 | case ARGT_SRV: |
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 | sname = strrchr(argp[idx].data.str.area, '/'); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 822 | if (sname) { |
| 823 | *sname++ = '\0'; |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 824 | pname = argp[idx].data.str.area; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 825 | px = proxy_be_by_name(pname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 826 | if (!px) { |
| 827 | msg = "backend doesn't exist"; |
| 828 | goto error; |
| 829 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 830 | } |
| 831 | else { |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 832 | sname = argp[idx].data.str.area; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 833 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 834 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 835 | argp[idx].data.srv = findserver(px, sname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 836 | if (!argp[idx].data.srv) { |
| 837 | msg = "server doesn't exist"; |
| 838 | goto error; |
| 839 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 840 | argp[idx].type = ARGT_SRV; |
| 841 | break; |
| 842 | |
| 843 | case ARGT_IPV4: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 844 | if (argp[idx].type != ARGT_STR) { |
| 845 | msg = "string expected"; |
| 846 | goto error; |
| 847 | } |
| 848 | if (inet_pton(AF_INET, argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 849 | msg = "invalid IPv4 address"; |
| 850 | goto error; |
| 851 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 852 | argp[idx].type = ARGT_IPV4; |
| 853 | break; |
| 854 | |
| 855 | case ARGT_MSK4: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 856 | if (argp[idx].type == ARGT_SINT) |
| 857 | len2mask4(argp[idx].data.sint, &argp[idx].data.ipv4); |
| 858 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 859 | if (!str2mask(argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 860 | msg = "invalid IPv4 mask"; |
| 861 | goto error; |
| 862 | } |
| 863 | } |
| 864 | else { |
| 865 | msg = "integer or string expected"; |
| 866 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 867 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 868 | argp[idx].type = ARGT_MSK4; |
| 869 | break; |
| 870 | |
| 871 | case ARGT_IPV6: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 872 | if (argp[idx].type != ARGT_STR) { |
| 873 | msg = "string expected"; |
| 874 | goto error; |
| 875 | } |
| 876 | if (inet_pton(AF_INET6, argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 877 | msg = "invalid IPv6 address"; |
| 878 | goto error; |
| 879 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 880 | argp[idx].type = ARGT_IPV6; |
| 881 | break; |
| 882 | |
| 883 | case ARGT_MSK6: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 884 | if (argp[idx].type == ARGT_SINT) |
| 885 | len2mask6(argp[idx].data.sint, &argp[idx].data.ipv6); |
| 886 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 887 | if (!str2mask6(argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 888 | msg = "invalid IPv6 mask"; |
| 889 | goto error; |
| 890 | } |
| 891 | } |
| 892 | else { |
| 893 | msg = "integer or string expected"; |
| 894 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 895 | } |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 896 | argp[idx].type = ARGT_MSK6; |
| 897 | break; |
| 898 | |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 899 | case ARGT_REG: |
| 900 | if (argp[idx].type != ARGT_STR) { |
| 901 | msg = "string expected"; |
| 902 | goto error; |
| 903 | } |
| 904 | reg = regex_comp(argp[idx].data.str.area, !(argp[idx].type_flags & ARGF_REG_ICASE), 1, &err); |
| 905 | if (!reg) { |
| 906 | msg = lua_pushfstring(L, "error compiling regex '%s' : '%s'", |
| 907 | argp[idx].data.str.area, err); |
| 908 | free(err); |
| 909 | goto error; |
| 910 | } |
| 911 | argp[idx].type = ARGT_REG; |
| 912 | argp[idx].data.reg = reg; |
| 913 | break; |
| 914 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 915 | case ARGT_USR: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 916 | if (argp[idx].type != ARGT_STR) { |
| 917 | msg = "string expected"; |
| 918 | goto error; |
| 919 | } |
| 920 | if (p->uri_auth && p->uri_auth->userlist && |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 921 | strcmp(p->uri_auth->userlist->name, argp[idx].data.str.area) == 0) |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 922 | ul = p->uri_auth->userlist; |
| 923 | else |
| 924 | ul = auth_find_userlist(argp[idx].data.str.area); |
| 925 | |
| 926 | if (!ul) { |
| 927 | msg = lua_pushfstring(L, "unable to find userlist '%s'", argp[idx].data.str.area); |
| 928 | goto error; |
| 929 | } |
| 930 | argp[idx].type = ARGT_USR; |
| 931 | argp[idx].data.usr = ul; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 932 | break; |
| 933 | |
| 934 | case ARGT_STR: |
| 935 | if (!chunk_dup(&tmp, &argp[idx].data.str)) { |
| 936 | msg = "unable to duplicate string arg"; |
| 937 | goto error; |
| 938 | } |
| 939 | argp[idx].data.str = tmp; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 940 | break; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 941 | |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 942 | case ARGT_MAP: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 943 | msg = "type not yet supported"; |
| 944 | goto error; |
| 945 | break; |
| 946 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 947 | } |
| 948 | |
| 949 | /* Check for type of argument. */ |
| 950 | if ((mask & ARGT_MASK) != argp[idx].type) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 951 | msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 952 | arg_type_names[(mask & ARGT_MASK)], |
| 953 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 954 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 955 | } |
| 956 | |
| 957 | /* Next argument. */ |
| 958 | mask >>= ARGT_BITS; |
| 959 | idx++; |
| 960 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 961 | return 0; |
| 962 | |
| 963 | error: |
| 964 | for (i = 0; i < idx; i++) { |
| 965 | if (argp[i].type == ARGT_STR) |
| 966 | chunk_destroy(&argp[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 967 | else if (argp[i].type == ARGT_REG) |
| 968 | regex_free(argp[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 969 | } |
| 970 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 971 | return 0; /* Never reached */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 972 | } |
| 973 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 974 | /* |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 975 | * The following functions are used to make correspondence between the the |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 976 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 977 | * |
| 978 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 979 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 980 | */ |
| 981 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 982 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 983 | struct hlua **hlua = lua_getextraspace(L); |
| 984 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 985 | } |
| 986 | static inline void hlua_sethlua(struct hlua *hlua) |
| 987 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 988 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 989 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 990 | } |
| 991 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 992 | /* This function is used to send logs. It try to send on screen (stderr) |
| 993 | * and on the default syslog server. |
| 994 | */ |
| 995 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 996 | { |
| 997 | struct tm tm; |
| 998 | char *p; |
| 999 | |
| 1000 | /* Cleanup the log message. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1001 | p = trash.area; |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1002 | for (; *msg != '\0'; msg++, p++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1003 | if (p >= trash.area + trash.size - 1) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 1004 | /* Break the message if exceed the buffer size. */ |
| 1005 | *(p-4) = ' '; |
| 1006 | *(p-3) = '.'; |
| 1007 | *(p-2) = '.'; |
| 1008 | *(p-1) = '.'; |
| 1009 | break; |
| 1010 | } |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 1011 | if (isprint((unsigned char)*msg)) |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1012 | *p = *msg; |
| 1013 | else |
| 1014 | *p = '.'; |
| 1015 | } |
| 1016 | *p = '\0'; |
| 1017 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1018 | send_log(px, level, "%s\n", trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1019 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Christopher Faulet | f98d821 | 2020-10-02 18:13:52 +0200 | [diff] [blame] | 1020 | if (level == LOG_DEBUG && !(global.mode & MODE_DEBUG)) |
| 1021 | return; |
| 1022 | |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 1023 | get_localtime(date.tv_sec, &tm); |
| 1024 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1025 | 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] | 1026 | (int)getpid(), trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1027 | fflush(stderr); |
| 1028 | } |
| 1029 | } |
| 1030 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1031 | /* This function just ensure that the yield will be always |
| 1032 | * returned with a timeout and permit to set some flags |
| 1033 | */ |
| 1034 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1035 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1036 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1037 | struct hlua *hlua; |
| 1038 | |
| 1039 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1040 | hlua = hlua_gethlua(L); |
| 1041 | if (!hlua) { |
| 1042 | return; |
| 1043 | } |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1044 | |
| 1045 | /* Set the wake timeout. If timeout is required, we set |
| 1046 | * the expiration time. |
| 1047 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1048 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1049 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 1050 | hlua->flags |= flags; |
| 1051 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1052 | /* Process the yield. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1053 | MAY_LJMP(lua_yieldk(L, nresults, ctx, k)); |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1054 | } |
| 1055 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1056 | /* This function initialises the Lua environment stored in the stream. |
| 1057 | * 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] | 1058 | * 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] | 1059 | * |
| 1060 | * This function is particular. it initialises a new Lua thread. If the |
| 1061 | * initialisation fails (example: out of memory error), the lua function |
| 1062 | * throws an error (longjmp). |
| 1063 | * |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1064 | * 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] | 1065 | * environment, so we must not initialise it. While the support of |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1066 | * threads appear, the safe environment set a lock to ensure only one |
| 1067 | * Lua execution at a time. If we initialize safe environment in another |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1068 | * safe environment, we have a dead lock. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1069 | * |
| 1070 | * set "already_safe" true if the context is initialized form safe |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1071 | * Lua function. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1072 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1073 | * This function manipulates two Lua stacks: the main and the thread. Only |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 1074 | * the main stack can fail. The thread is not manipulated. This function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1075 | * MUST NOT manipulate the created thread stack state, because it is not |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1076 | * protected against errors thrown by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1077 | */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1078 | 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] | 1079 | { |
| 1080 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1081 | lua->flags = 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1082 | lua->gc_count = 0; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1083 | lua->wake_time = TICK_ETERNITY; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1084 | lua->state_id = state_id; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1085 | LIST_INIT(&lua->com); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1086 | if (!already_safe) { |
| 1087 | if (!SET_SAFE_LJMP_PARENT(lua)) { |
| 1088 | lua->Tref = LUA_REFNIL; |
| 1089 | return 0; |
| 1090 | } |
| 1091 | } |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1092 | lua->T = lua_newthread(hlua_states[state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1093 | if (!lua->T) { |
| 1094 | lua->Tref = LUA_REFNIL; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1095 | if (!already_safe) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1096 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1097 | return 0; |
| 1098 | } |
| 1099 | hlua_sethlua(lua); |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1100 | lua->Tref = luaL_ref(hlua_states[state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1101 | lua->task = task; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1102 | if (!already_safe) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1103 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1104 | return 1; |
| 1105 | } |
| 1106 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1107 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1108 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 1109 | * is not freed. |
| 1110 | */ |
| 1111 | void hlua_ctx_destroy(struct hlua *lua) |
| 1112 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1113 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 1114 | return; |
| 1115 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1116 | if (!lua->T) |
| 1117 | goto end; |
| 1118 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1119 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1120 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1121 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1122 | if (!SET_SAFE_LJMP(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1123 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1124 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1125 | RESET_SAFE_LJMP(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1126 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1127 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1128 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1129 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1130 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1131 | /* Forces a garbage collecting process. If the Lua program is finished |
| 1132 | * without error, we run the GC on the thread pointer. Its freed all |
| 1133 | * the unused memory. |
| 1134 | * If the thread is finnish with an error or is currently yielded, |
| 1135 | * it seems that the GC applied on the thread doesn't clean anything, |
| 1136 | * so e run the GC on the main thread. |
| 1137 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 1138 | * the garbage collection. |
| 1139 | */ |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1140 | if (lua->gc_count) { |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1141 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1142 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1143 | lua_gc(hlua_states[lua->state_id], LUA_GCCOLLECT, 0); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1144 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1145 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1146 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 1147 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1148 | |
| 1149 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1150 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | /* This function is used to restore the Lua context when a coroutine |
| 1154 | * fails. This function copy the common memory between old coroutine |
| 1155 | * and the new coroutine. The old coroutine is destroyed, and its |
| 1156 | * replaced by the new coroutine. |
| 1157 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 1158 | * as string error message and it is copied in the new stack. |
| 1159 | */ |
| 1160 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 1161 | { |
| 1162 | lua_State *T; |
| 1163 | int new_ref; |
| 1164 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1165 | /* New Lua coroutine. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1166 | T = lua_newthread(hlua_states[lua->state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1167 | if (!T) |
| 1168 | return 0; |
| 1169 | |
| 1170 | /* Copy last error message. */ |
| 1171 | if (keep_msg) |
| 1172 | lua_xmove(lua->T, T, 1); |
| 1173 | |
| 1174 | /* Copy data between the coroutines. */ |
| 1175 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1176 | lua_xmove(lua->T, T, 1); |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 1177 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */ |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1178 | |
| 1179 | /* Destroy old data. */ |
| 1180 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1181 | |
| 1182 | /* The thread is garbage collected by Lua. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1183 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1184 | |
| 1185 | /* Fill the struct with the new coroutine values. */ |
| 1186 | lua->Mref = new_ref; |
| 1187 | lua->T = T; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1188 | lua->Tref = luaL_ref(hlua_states[lua->state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1189 | |
| 1190 | /* Set context. */ |
| 1191 | hlua_sethlua(lua); |
| 1192 | |
| 1193 | return 1; |
| 1194 | } |
| 1195 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1196 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1197 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1198 | struct hlua *hlua; |
| 1199 | |
| 1200 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1201 | hlua = hlua_gethlua(L); |
| 1202 | if (!hlua) |
| 1203 | return; |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1204 | |
| 1205 | /* Lua cannot yield when its returning from a function, |
| 1206 | * so, we can fix the interrupt hook to 1 instruction, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1207 | * expecting that the function is finished. |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1208 | */ |
| 1209 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1210 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1211 | return; |
| 1212 | } |
| 1213 | |
| 1214 | /* restore the interrupt condition. */ |
| 1215 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1216 | |
| 1217 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1218 | * If the state is not yieldable, trying yield causes an error. |
| 1219 | */ |
| 1220 | if (lua_isyieldable(L)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1221 | MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1222 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 1223 | /* If we cannot yield, update the clock and check the timeout. */ |
| 1224 | tv_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1225 | hlua->run_time += now_ms - hlua->start_time; |
| 1226 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1227 | lua_pushfstring(L, "execution timeout"); |
| 1228 | WILL_LJMP(lua_error(L)); |
| 1229 | } |
| 1230 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1231 | /* Update the start time. */ |
| 1232 | hlua->start_time = now_ms; |
| 1233 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1234 | /* Try to interrupt the process at the end of the current |
| 1235 | * unyieldable function. |
| 1236 | */ |
| 1237 | 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] | 1238 | } |
| 1239 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1240 | /* This function start or resumes the Lua stack execution. If the flag |
| 1241 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1242 | * The function return an error. |
| 1243 | * |
| 1244 | * The function can returns 4 values: |
| 1245 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1246 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1247 | * task wakeup. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1248 | * - 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] | 1249 | * the top of the stack. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1250 | * - HLUA_E_ERR : An error has occurred without error message. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1251 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1252 | * 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] | 1253 | * LUA code. |
| 1254 | */ |
| 1255 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1256 | { |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1257 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 1258 | int nres; |
| 1259 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1260 | int ret; |
| 1261 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1262 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1263 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1264 | /* Initialise run time counter. */ |
| 1265 | if (!HLUA_IS_RUNNING(lua)) |
| 1266 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1267 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1268 | /* Lock the whole Lua execution. This lock must be before the |
| 1269 | * label "resume_execution". |
| 1270 | */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1271 | if (lua->state_id == 0) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1272 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1273 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1274 | resume_execution: |
| 1275 | |
| 1276 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1277 | * instructions. it is used for preventing infinite loops. |
| 1278 | */ |
| 1279 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1280 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1281 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1282 | HLUA_SET_RUN(lua); |
| 1283 | HLUA_CLR_CTRLYIELD(lua); |
| 1284 | HLUA_CLR_WAKERESWR(lua); |
| 1285 | HLUA_CLR_WAKEREQWR(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1286 | |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1287 | /* Update the start time and reset wake_time. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1288 | lua->start_time = now_ms; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1289 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1290 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1291 | /* Call the function. */ |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1292 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1293 | 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] | 1294 | #else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1295 | ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs); |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1296 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1297 | switch (ret) { |
| 1298 | |
| 1299 | case LUA_OK: |
| 1300 | ret = HLUA_E_OK; |
| 1301 | break; |
| 1302 | |
| 1303 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1304 | /* Check if the execution timeout is expired. It it is the case, we |
| 1305 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1306 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1307 | tv_update_date(0, 1); |
| 1308 | lua->run_time += now_ms - lua->start_time; |
| 1309 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1310 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1311 | ret = HLUA_E_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1312 | break; |
| 1313 | } |
| 1314 | /* Process the forced yield. if the general yield is not allowed or |
| 1315 | * if no task were associated this the current Lua execution |
| 1316 | * coroutine, we resume the execution. Else we want to return in the |
| 1317 | * scheduler and we want to be waked up again, to continue the |
| 1318 | * current Lua execution. So we schedule our own task. |
| 1319 | */ |
| 1320 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1321 | if (!yield_allowed || !lua->task) |
| 1322 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1323 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1324 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1325 | if (!yield_allowed) { |
| 1326 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1327 | ret = HLUA_E_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1328 | break; |
| 1329 | } |
| 1330 | ret = HLUA_E_AGAIN; |
| 1331 | break; |
| 1332 | |
| 1333 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1334 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1335 | /* Special exit case. The traditional exit is returned as an error |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1336 | * because the errors ares the only one mean to return immediately |
| 1337 | * from and lua execution. |
| 1338 | */ |
| 1339 | if (lua->flags & HLUA_EXIT) { |
| 1340 | ret = HLUA_E_OK; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 1341 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1342 | break; |
| 1343 | } |
| 1344 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1345 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1346 | if (!lua_checkstack(lua->T, 1)) { |
| 1347 | ret = HLUA_E_ERR; |
| 1348 | break; |
| 1349 | } |
| 1350 | msg = lua_tostring(lua->T, -1); |
| 1351 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1352 | lua_pop(lua->T, 1); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1353 | trace = hlua_traceback(lua->T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1354 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1355 | 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] | 1356 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1357 | 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] | 1358 | ret = HLUA_E_ERRMSG; |
| 1359 | break; |
| 1360 | |
| 1361 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1362 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1363 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1364 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1365 | break; |
| 1366 | |
| 1367 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1368 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1369 | if (!lua_checkstack(lua->T, 1)) { |
| 1370 | ret = HLUA_E_ERR; |
| 1371 | break; |
| 1372 | } |
| 1373 | msg = lua_tostring(lua->T, -1); |
| 1374 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1375 | lua_pop(lua->T, 1); |
| 1376 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1377 | 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] | 1378 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1379 | 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] | 1380 | ret = HLUA_E_ERRMSG; |
| 1381 | break; |
| 1382 | |
| 1383 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1384 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1385 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1386 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1387 | break; |
| 1388 | } |
| 1389 | |
| 1390 | switch (ret) { |
| 1391 | case HLUA_E_AGAIN: |
| 1392 | break; |
| 1393 | |
| 1394 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1395 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1396 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1397 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1398 | break; |
| 1399 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1400 | case HLUA_E_ETMOUT: |
| 1401 | case HLUA_E_NOMEM: |
| 1402 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1403 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1404 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1405 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1406 | hlua_ctx_renew(lua, 0); |
| 1407 | break; |
| 1408 | |
| 1409 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1410 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1411 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1412 | break; |
| 1413 | } |
| 1414 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1415 | /* This is the main exit point, remove the Lua lock. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1416 | if (lua->state_id == 0) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1417 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1418 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1419 | return ret; |
| 1420 | } |
| 1421 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1422 | /* This function exit the current code. */ |
| 1423 | __LJMP static int hlua_done(lua_State *L) |
| 1424 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1425 | struct hlua *hlua; |
| 1426 | |
| 1427 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1428 | hlua = hlua_gethlua(L); |
| 1429 | if (!hlua) |
| 1430 | return 0; |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1431 | |
| 1432 | hlua->flags |= HLUA_EXIT; |
| 1433 | WILL_LJMP(lua_error(L)); |
| 1434 | |
| 1435 | return 0; |
| 1436 | } |
| 1437 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1438 | /* This function is an LUA binding. It provides a function |
| 1439 | * for deleting ACL from a referenced ACL file. |
| 1440 | */ |
| 1441 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1442 | { |
| 1443 | const char *name; |
| 1444 | const char *key; |
| 1445 | struct pat_ref *ref; |
| 1446 | |
| 1447 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1448 | |
| 1449 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1450 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1451 | |
| 1452 | ref = pat_ref_lookup(name); |
| 1453 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1454 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1455 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1456 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1457 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1458 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1459 | return 0; |
| 1460 | } |
| 1461 | |
| 1462 | /* This function is an LUA binding. It provides a function |
| 1463 | * for deleting map entry from a referenced map file. |
| 1464 | */ |
| 1465 | static int hlua_del_map(lua_State *L) |
| 1466 | { |
| 1467 | const char *name; |
| 1468 | const char *key; |
| 1469 | struct pat_ref *ref; |
| 1470 | |
| 1471 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1472 | |
| 1473 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1474 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1475 | |
| 1476 | ref = pat_ref_lookup(name); |
| 1477 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1478 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1479 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1480 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1481 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1482 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1483 | return 0; |
| 1484 | } |
| 1485 | |
| 1486 | /* This function is an LUA binding. It provides a function |
| 1487 | * for adding ACL pattern from a referenced ACL file. |
| 1488 | */ |
| 1489 | static int hlua_add_acl(lua_State *L) |
| 1490 | { |
| 1491 | const char *name; |
| 1492 | const char *key; |
| 1493 | struct pat_ref *ref; |
| 1494 | |
| 1495 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1496 | |
| 1497 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1498 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1499 | |
| 1500 | ref = pat_ref_lookup(name); |
| 1501 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1502 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1503 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1504 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1505 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1506 | pat_ref_add(ref, key, NULL, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1507 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1508 | return 0; |
| 1509 | } |
| 1510 | |
| 1511 | /* This function is an LUA binding. It provides a function |
| 1512 | * for setting map pattern and sample from a referenced map |
| 1513 | * file. |
| 1514 | */ |
| 1515 | static int hlua_set_map(lua_State *L) |
| 1516 | { |
| 1517 | const char *name; |
| 1518 | const char *key; |
| 1519 | const char *value; |
| 1520 | struct pat_ref *ref; |
| 1521 | |
| 1522 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1523 | |
| 1524 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1525 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1526 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1527 | |
| 1528 | ref = pat_ref_lookup(name); |
| 1529 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1530 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1531 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1532 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1533 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1534 | pat_ref_set(ref, key, value, NULL); |
| 1535 | else |
| 1536 | pat_ref_add(ref, key, value, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1537 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1538 | return 0; |
| 1539 | } |
| 1540 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1541 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1542 | * 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] | 1543 | * metatable have an original version registered in the global context with |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1544 | * the name of the object (_G[<name>] = <metable> ). |
| 1545 | * |
| 1546 | * A metable is a table that modify the standard behavior of a standard |
| 1547 | * access to the associated data. The entries of this new metatable are |
| 1548 | * defined as is: |
| 1549 | * |
| 1550 | * http://lua-users.org/wiki/MetatableEvents |
| 1551 | * |
| 1552 | * __index |
| 1553 | * |
| 1554 | * we access an absent field in a table, the result is nil. This is |
| 1555 | * true, but it is not the whole truth. Actually, such access triggers |
| 1556 | * the interpreter to look for an __index metamethod: If there is no |
| 1557 | * such method, as usually happens, then the access results in nil; |
| 1558 | * otherwise, the metamethod will provide the result. |
| 1559 | * |
| 1560 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1561 | * the key does not appear in the table, but the metatable has an __index |
| 1562 | * property: |
| 1563 | * |
| 1564 | * - if the value is a function, the function is called, passing in the |
| 1565 | * table and the key; the return value of that function is returned as |
| 1566 | * the result. |
| 1567 | * |
| 1568 | * - if the value is another table, the value of the key in that table is |
| 1569 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1570 | * table's metatable has an __index property, then it continues on up) |
| 1571 | * |
| 1572 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1573 | * |
| 1574 | * http://www.lua.org/pil/13.4.1.html |
| 1575 | * |
| 1576 | * __newindex |
| 1577 | * |
| 1578 | * Like __index, but control property assignment. |
| 1579 | * |
| 1580 | * __mode - Control weak references. A string value with one or both |
| 1581 | * of the characters 'k' and 'v' which specifies that the the |
| 1582 | * keys and/or values in the table are weak references. |
| 1583 | * |
| 1584 | * __call - Treat a table like a function. When a table is followed by |
| 1585 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1586 | * a __call key pointing to a function, that function is invoked |
| 1587 | * (passing any specified arguments) and the return value is |
| 1588 | * returned. |
| 1589 | * |
| 1590 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1591 | * called, if the metatable for myTable has a __metatable |
| 1592 | * key, the value of that key is returned instead of the |
| 1593 | * actual metatable. |
| 1594 | * |
| 1595 | * __tostring - Control string representation. When the builtin |
| 1596 | * "tostring( myTable )" function is called, if the metatable |
| 1597 | * for myTable has a __tostring property set to a function, |
| 1598 | * that function is invoked (passing myTable to it) and the |
| 1599 | * return value is used as the string representation. |
| 1600 | * |
| 1601 | * __len - Control table length. When the table length is requested using |
| 1602 | * the length operator ( '#' ), if the metatable for myTable has |
| 1603 | * a __len key pointing to a function, that function is invoked |
| 1604 | * (passing myTable to it) and the return value used as the value |
| 1605 | * of "#myTable". |
| 1606 | * |
| 1607 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1608 | * collected, if the metatable has a __gc field pointing to a |
| 1609 | * function, that function is first invoked, passing the userdata |
| 1610 | * to it. The __gc metamethod is not called for tables. |
| 1611 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1612 | * |
| 1613 | * Special metamethods for redefining standard operators: |
| 1614 | * http://www.lua.org/pil/13.1.html |
| 1615 | * |
| 1616 | * __add "+" |
| 1617 | * __sub "-" |
| 1618 | * __mul "*" |
| 1619 | * __div "/" |
| 1620 | * __unm "!" |
| 1621 | * __pow "^" |
| 1622 | * __concat ".." |
| 1623 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1624 | * Special methods for redefining standard relations |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1625 | * http://www.lua.org/pil/13.2.html |
| 1626 | * |
| 1627 | * __eq "==" |
| 1628 | * __lt "<" |
| 1629 | * __le "<=" |
| 1630 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1631 | |
| 1632 | /* |
| 1633 | * |
| 1634 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1635 | * Class Map |
| 1636 | * |
| 1637 | * |
| 1638 | */ |
| 1639 | |
| 1640 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1641 | * a class session, otherwise it throws an error. |
| 1642 | */ |
| 1643 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1644 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1645 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1646 | } |
| 1647 | |
| 1648 | /* This function is the map constructor. It don't need |
| 1649 | * the class Map object. It creates and return a new Map |
| 1650 | * object. It must be called only during "body" or "init" |
| 1651 | * context because it process some filesystem accesses. |
| 1652 | */ |
| 1653 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1654 | { |
| 1655 | const char *fn; |
| 1656 | int match = PAT_MATCH_STR; |
| 1657 | struct sample_conv conv; |
| 1658 | const char *file = ""; |
| 1659 | int line = 0; |
| 1660 | lua_Debug ar; |
| 1661 | char *err = NULL; |
| 1662 | struct arg args[2]; |
| 1663 | |
| 1664 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1665 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1666 | |
| 1667 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1668 | |
| 1669 | if (lua_gettop(L) >= 2) { |
| 1670 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1671 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1672 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1673 | } |
| 1674 | |
| 1675 | /* Get Lua filename and line number. */ |
| 1676 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1677 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1678 | if (ar.currentline > 0) { /* is there info? */ |
| 1679 | file = ar.short_src; |
| 1680 | line = ar.currentline; |
| 1681 | } |
| 1682 | } |
| 1683 | |
| 1684 | /* fill fake sample_conv struct. */ |
| 1685 | conv.kw = ""; /* unused. */ |
| 1686 | conv.process = NULL; /* unused. */ |
| 1687 | conv.arg_mask = 0; /* unused. */ |
| 1688 | conv.val_args = NULL; /* unused. */ |
| 1689 | conv.out_type = SMP_T_STR; |
| 1690 | conv.private = (void *)(long)match; |
| 1691 | switch (match) { |
| 1692 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1693 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1694 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1695 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1696 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1697 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1698 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1699 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1700 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1701 | default: |
| 1702 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1703 | } |
| 1704 | |
| 1705 | /* fill fake args. */ |
| 1706 | args[0].type = ARGT_STR; |
Christopher Faulet | 73292e9 | 2020-08-06 08:40:09 +0200 | [diff] [blame] | 1707 | args[0].data.str.area = strdup(fn); |
| 1708 | args[0].data.str.data = strlen(fn); |
| 1709 | args[0].data.str.size = args[0].data.str.data+1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1710 | args[1].type = ARGT_STOP; |
| 1711 | |
| 1712 | /* load the map. */ |
| 1713 | if (!sample_load_map(args, &conv, file, line, &err)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1714 | /* error case: we can't use luaL_error because we must |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1715 | * free the err variable. |
| 1716 | */ |
| 1717 | luaL_where(L, 1); |
| 1718 | lua_pushfstring(L, "'new': %s.", err); |
| 1719 | lua_concat(L, 2); |
| 1720 | free(err); |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 1721 | chunk_destroy(&args[0].data.str); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1722 | WILL_LJMP(lua_error(L)); |
| 1723 | } |
| 1724 | |
| 1725 | /* create the lua object. */ |
| 1726 | lua_newtable(L); |
| 1727 | lua_pushlightuserdata(L, args[0].data.map); |
| 1728 | lua_rawseti(L, -2, 0); |
| 1729 | |
| 1730 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1731 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1732 | lua_setmetatable(L, -2); |
| 1733 | |
| 1734 | |
| 1735 | return 1; |
| 1736 | } |
| 1737 | |
| 1738 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1739 | { |
| 1740 | struct map_descriptor *desc; |
| 1741 | struct pattern *pat; |
| 1742 | struct sample smp; |
| 1743 | |
| 1744 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1745 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1746 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1747 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1748 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1749 | } |
| 1750 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1751 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1752 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1753 | 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] | 1754 | smp.data.u.str.size = smp.data.u.str.data + 1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1755 | } |
| 1756 | |
| 1757 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1758 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1759 | if (str) |
| 1760 | lua_pushstring(L, ""); |
| 1761 | else |
| 1762 | lua_pushnil(L); |
| 1763 | return 1; |
| 1764 | } |
| 1765 | |
| 1766 | /* 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] | 1767 | 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] | 1768 | return 1; |
| 1769 | } |
| 1770 | |
| 1771 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1772 | { |
| 1773 | return _hlua_map_lookup(L, 0); |
| 1774 | } |
| 1775 | |
| 1776 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1777 | { |
| 1778 | return _hlua_map_lookup(L, 1); |
| 1779 | } |
| 1780 | |
| 1781 | /* |
| 1782 | * |
| 1783 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1784 | * Class Socket |
| 1785 | * |
| 1786 | * |
| 1787 | */ |
| 1788 | |
| 1789 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1790 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1791 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1792 | } |
| 1793 | |
| 1794 | /* 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] | 1795 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1796 | * received. |
| 1797 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1798 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1799 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1800 | struct stream_interface *si = appctx->owner; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1801 | |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1802 | if (appctx->ctx.hlua_cosocket.die) { |
| 1803 | si_shutw(si); |
| 1804 | si_shutr(si); |
| 1805 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1806 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1807 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1808 | stream_shutdown(si_strm(si), SF_ERR_KILLED); |
| 1809 | } |
| 1810 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1811 | /* If we can't write, wakeup the pending write signals. */ |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1812 | if (channel_output_closed(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1813 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1814 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1815 | /* If we can't read, wakeup the pending read signals. */ |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1816 | if (channel_input_closed(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1817 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1818 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1819 | /* if the connection is not established, inform the stream that we want |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1820 | * to be notified whenever the connection completes. |
| 1821 | */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1822 | if (si_opposite(si)->state < SI_ST_EST) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 1823 | si_cant_get(si); |
Willy Tarreau | 12c2423 | 2018-12-06 15:29:50 +0100 | [diff] [blame] | 1824 | si_rx_conn_blk(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1825 | si_rx_endp_more(si); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1826 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1827 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1828 | |
| 1829 | /* This function is called after the connect. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1830 | appctx->ctx.hlua_cosocket.connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1831 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1832 | /* 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] | 1833 | if (channel_may_recv(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1834 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1835 | |
| 1836 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1837 | if (!channel_is_empty(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1838 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1839 | |
| 1840 | /* Some data were injected in the buffer, notify the stream |
| 1841 | * interface. |
| 1842 | */ |
| 1843 | if (!channel_is_empty(si_ic(si))) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 1844 | si_update(si); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1845 | |
| 1846 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1847 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1848 | */ |
| 1849 | if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write)) |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1850 | si_rx_endp_more(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1851 | } |
| 1852 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1853 | /* This function is called when the "struct stream" is destroyed. |
| 1854 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1855 | * Wake all the pending signals. |
| 1856 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1857 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1858 | { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1859 | struct xref *peer; |
| 1860 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1861 | /* Remove my link in the original object. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1862 | peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref); |
| 1863 | if (peer) |
| 1864 | xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1865 | |
| 1866 | /* Wake all the task waiting for me. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1867 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1868 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1869 | } |
| 1870 | |
| 1871 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1872 | * uses this object. If the stream does not exists, just quit. |
| 1873 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1874 | * pending signal can rest in the read and write lists. destroy |
| 1875 | * it. |
| 1876 | */ |
| 1877 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1878 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1879 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1880 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1881 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1882 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1883 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 1884 | |
| 1885 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1886 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1887 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1888 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1889 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1890 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1891 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1892 | appctx->ctx.hlua_cosocket.die = 1; |
| 1893 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1894 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1895 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1896 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1897 | return 0; |
| 1898 | } |
| 1899 | |
| 1900 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1901 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1902 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1903 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1904 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1905 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1906 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1907 | struct xref *peer; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1908 | struct hlua *hlua; |
| 1909 | |
| 1910 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1911 | hlua = hlua_gethlua(L); |
| 1912 | if (!hlua) |
| 1913 | return 0; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1914 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1915 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1916 | |
| 1917 | /* Check if we run on the same thread than the xreator thread. |
| 1918 | * We cannot access to the socket if the thread is different. |
| 1919 | */ |
| 1920 | if (socket->tid != tid) |
| 1921 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1922 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1923 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1924 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1925 | return 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1926 | |
| 1927 | hlua->gc_count--; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1928 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1929 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1930 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1931 | appctx->ctx.hlua_cosocket.die = 1; |
| 1932 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1933 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1934 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1935 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1936 | return 0; |
| 1937 | } |
| 1938 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1939 | /* The close function calls close_helper. |
| 1940 | */ |
| 1941 | __LJMP static int hlua_socket_close(lua_State *L) |
| 1942 | { |
| 1943 | MAY_LJMP(check_args(L, 1, "close")); |
| 1944 | return hlua_socket_close_helper(L); |
| 1945 | } |
| 1946 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1947 | /* This Lua function assumes that the stack contain three parameters. |
| 1948 | * 1 - USERDATA containing a struct socket |
| 1949 | * 2 - INTEGER with values of the macro defined below |
| 1950 | * If the integer is -1, we must read at most one line. |
| 1951 | * If the integer is -2, we ust read all the data until the |
| 1952 | * end of the stream. |
| 1953 | * If the integer is positive value, we must read a number of |
| 1954 | * bytes corresponding to this value. |
| 1955 | */ |
| 1956 | #define HLSR_READ_LINE (-1) |
| 1957 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1958 | __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] | 1959 | { |
| 1960 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1961 | int wanted = lua_tointeger(L, 2); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1962 | struct hlua *hlua; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1963 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1964 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1965 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1966 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1967 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1968 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1969 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1970 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1971 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1972 | struct stream_interface *si; |
| 1973 | struct stream *s; |
| 1974 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1975 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1976 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1977 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1978 | hlua = hlua_gethlua(L); |
| 1979 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1980 | /* Check if this lua stack is schedulable. */ |
| 1981 | if (!hlua || !hlua->task) |
| 1982 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 1983 | "'frontend', 'backend' or 'task'")); |
| 1984 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1985 | /* Check if we run on the same thread than the xreator thread. |
| 1986 | * We cannot access to the socket if the thread is different. |
| 1987 | */ |
| 1988 | if (socket->tid != tid) |
| 1989 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1990 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1991 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1992 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1993 | if (!peer) |
| 1994 | goto no_peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1995 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 1996 | si = appctx->owner; |
| 1997 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1998 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1999 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2000 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2001 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2002 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2003 | if (nblk < 0) /* Connection close. */ |
| 2004 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2005 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2006 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 2007 | |
| 2008 | /* remove final \r\n. */ |
| 2009 | if (nblk == 1) { |
| 2010 | if (blk1[len1-1] == '\n') { |
| 2011 | len1--; |
| 2012 | skip_at_end++; |
| 2013 | if (blk1[len1-1] == '\r') { |
| 2014 | len1--; |
| 2015 | skip_at_end++; |
| 2016 | } |
| 2017 | } |
| 2018 | } |
| 2019 | else { |
| 2020 | if (blk2[len2-1] == '\n') { |
| 2021 | len2--; |
| 2022 | skip_at_end++; |
| 2023 | if (blk2[len2-1] == '\r') { |
| 2024 | len2--; |
| 2025 | skip_at_end++; |
| 2026 | } |
| 2027 | } |
| 2028 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2029 | } |
| 2030 | |
| 2031 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2032 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2033 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2034 | if (nblk < 0) /* Connection close. */ |
| 2035 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2036 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2037 | goto connection_empty; |
| 2038 | } |
| 2039 | |
| 2040 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2041 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2042 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2043 | if (nblk < 0) /* Connection close. */ |
| 2044 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2045 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2046 | goto connection_empty; |
| 2047 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2048 | missing_bytes = wanted - socket->b.n; |
| 2049 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2050 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2051 | len1 = missing_bytes; |
| 2052 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 2053 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2054 | } |
| 2055 | |
| 2056 | len = len1; |
| 2057 | |
| 2058 | luaL_addlstring(&socket->b, blk1, len1); |
| 2059 | if (nblk == 2) { |
| 2060 | len += len2; |
| 2061 | luaL_addlstring(&socket->b, blk2, len2); |
| 2062 | } |
| 2063 | |
| 2064 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2065 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2066 | |
| 2067 | /* Don't wait anything. */ |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 2068 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2069 | |
| 2070 | /* If the pattern reclaim to read all the data |
| 2071 | * in the connection, got out. |
| 2072 | */ |
| 2073 | if (wanted == HLSR_READ_ALL) |
| 2074 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2075 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2076 | goto connection_empty; |
| 2077 | |
| 2078 | /* Return result. */ |
| 2079 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2080 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2081 | return 1; |
| 2082 | |
| 2083 | connection_closed: |
| 2084 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2085 | xref_unlock(&socket->xref, peer); |
| 2086 | |
| 2087 | no_peer: |
| 2088 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2089 | /* If the buffer containds data. */ |
| 2090 | if (socket->b.n > 0) { |
| 2091 | luaL_pushresult(&socket->b); |
| 2092 | return 1; |
| 2093 | } |
| 2094 | lua_pushnil(L); |
| 2095 | lua_pushstring(L, "connection closed."); |
| 2096 | return 2; |
| 2097 | |
| 2098 | connection_empty: |
| 2099 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2100 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) { |
| 2101 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2102 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2103 | } |
| 2104 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2105 | 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] | 2106 | return 0; |
| 2107 | } |
| 2108 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2109 | /* This Lua function gets two parameters. The first one can be string |
| 2110 | * or a number. If the string is "*l", the user requires one line. If |
| 2111 | * 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] | 2112 | * If the value is a number, the user require a number of bytes equal |
| 2113 | * to the value. The default value is "*l" (a line). |
| 2114 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2115 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2116 | * integer takes this values: |
| 2117 | * -1 : read a line |
| 2118 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2119 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2120 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2121 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2122 | * concatenated with the read data. |
| 2123 | */ |
| 2124 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 2125 | { |
| 2126 | int wanted = HLSR_READ_LINE; |
| 2127 | const char *pattern; |
| 2128 | int type; |
| 2129 | char *error; |
| 2130 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2131 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2132 | |
| 2133 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 2134 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 2135 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2136 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2137 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2138 | /* Check if we run on the same thread than the xreator thread. |
| 2139 | * We cannot access to the socket if the thread is different. |
| 2140 | */ |
| 2141 | if (socket->tid != tid) |
| 2142 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2143 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2144 | /* check for pattern. */ |
| 2145 | if (lua_gettop(L) >= 2) { |
| 2146 | type = lua_type(L, 2); |
| 2147 | if (type == LUA_TSTRING) { |
| 2148 | pattern = lua_tostring(L, 2); |
| 2149 | if (strcmp(pattern, "*a") == 0) |
| 2150 | wanted = HLSR_READ_ALL; |
| 2151 | else if (strcmp(pattern, "*l") == 0) |
| 2152 | wanted = HLSR_READ_LINE; |
| 2153 | else { |
| 2154 | wanted = strtoll(pattern, &error, 10); |
| 2155 | if (*error != '\0') |
| 2156 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 2157 | } |
| 2158 | } |
| 2159 | else if (type == LUA_TNUMBER) { |
| 2160 | wanted = lua_tointeger(L, 2); |
| 2161 | if (wanted < 0) |
| 2162 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 2163 | } |
| 2164 | } |
| 2165 | |
| 2166 | /* Set pattern. */ |
| 2167 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 2168 | |
| 2169 | /* Check if we would replace the top by itself. */ |
| 2170 | if (lua_gettop(L) != 2) |
| 2171 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2172 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2173 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2174 | luaL_buffinit(L, &socket->b); |
| 2175 | |
| 2176 | /* Check prefix. */ |
| 2177 | if (lua_gettop(L) >= 3) { |
| 2178 | if (lua_type(L, 3) != LUA_TSTRING) |
| 2179 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 2180 | pattern = lua_tolstring(L, 3, &len); |
| 2181 | luaL_addlstring(&socket->b, pattern, len); |
| 2182 | } |
| 2183 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2184 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2185 | } |
| 2186 | |
| 2187 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 2188 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2189 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2190 | 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] | 2191 | { |
| 2192 | struct hlua_socket *socket; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2193 | struct hlua *hlua; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2194 | struct appctx *appctx; |
| 2195 | size_t buf_len; |
| 2196 | const char *buf; |
| 2197 | int len; |
| 2198 | int send_len; |
| 2199 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2200 | struct xref *peer; |
| 2201 | struct stream_interface *si; |
| 2202 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2203 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2204 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2205 | hlua = hlua_gethlua(L); |
| 2206 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2207 | /* Check if this lua stack is schedulable. */ |
| 2208 | if (!hlua || !hlua->task) |
| 2209 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 2210 | "'frontend', 'backend' or 'task'")); |
| 2211 | |
| 2212 | /* Get object */ |
| 2213 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2214 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2215 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2216 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2217 | /* Check if we run on the same thread than the xreator thread. |
| 2218 | * We cannot access to the socket if the thread is different. |
| 2219 | */ |
| 2220 | if (socket->tid != tid) |
| 2221 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2222 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2223 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2224 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2225 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2226 | lua_pushinteger(L, -1); |
| 2227 | return 1; |
| 2228 | } |
| 2229 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2230 | si = appctx->owner; |
| 2231 | s = si_strm(si); |
| 2232 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2233 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2234 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2235 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2236 | lua_pushinteger(L, -1); |
| 2237 | return 1; |
| 2238 | } |
| 2239 | |
| 2240 | /* Update the input buffer data. */ |
| 2241 | buf += sent; |
| 2242 | send_len = buf_len - sent; |
| 2243 | |
| 2244 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2245 | if (sent >= buf_len) { |
| 2246 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2247 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2248 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2249 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2250 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2251 | * the request buffer if its not required. |
| 2252 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2253 | if (s->req.buf.size == 0) { |
Willy Tarreau | 581abd3 | 2018-10-25 10:21:41 +0200 | [diff] [blame] | 2254 | if (!si_alloc_ibuf(si, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2255 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2256 | } |
| 2257 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2258 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2259 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2260 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2261 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2262 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2263 | |
| 2264 | /* send data */ |
| 2265 | if (len < send_len) |
| 2266 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2267 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2268 | |
| 2269 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2270 | * the data" (-2) are not expected because the available length |
| 2271 | * is tested. |
| 2272 | * Other unknown error are also not expected. |
| 2273 | */ |
| 2274 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2275 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2276 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2277 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2278 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2279 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2280 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2281 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2282 | return 1; |
| 2283 | } |
| 2284 | |
| 2285 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2286 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2287 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2288 | s->req.rex = TICK_ETERNITY; |
| 2289 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2290 | |
| 2291 | /* Update length sent. */ |
| 2292 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2293 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2294 | |
| 2295 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2296 | if (sent + len >= buf_len) { |
| 2297 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2298 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2299 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2300 | |
| 2301 | hlua_socket_write_yield_return: |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2302 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2303 | xref_unlock(&socket->xref, peer); |
| 2304 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2305 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2306 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2307 | 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] | 2308 | return 0; |
| 2309 | } |
| 2310 | |
| 2311 | /* This function initiate the send of data. It just check the input |
| 2312 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2313 | * 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] | 2314 | * "hlua_socket_write_yield" that can yield. |
| 2315 | * |
| 2316 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2317 | * the associated object. The second is a string buffer. The third is |
| 2318 | * a facultative integer that represents where is the buffer position |
| 2319 | * of the start of the data that can send. The first byte is the |
| 2320 | * position "1". The default value is "1". The fourth argument is a |
| 2321 | * facultative integer that represents where is the buffer position |
| 2322 | * of the end of the data that can send. The default is the last byte. |
| 2323 | */ |
| 2324 | static int hlua_socket_send(struct lua_State *L) |
| 2325 | { |
| 2326 | int i; |
| 2327 | int j; |
| 2328 | const char *buf; |
| 2329 | size_t buf_len; |
| 2330 | |
| 2331 | /* Check number of arguments. */ |
| 2332 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2333 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2334 | |
| 2335 | /* Get the string. */ |
| 2336 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2337 | |
| 2338 | /* Get and check j. */ |
| 2339 | if (lua_gettop(L) == 4) { |
| 2340 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2341 | if (j < 0) |
| 2342 | j = buf_len + j + 1; |
| 2343 | if (j > buf_len) |
| 2344 | j = buf_len + 1; |
| 2345 | lua_pop(L, 1); |
| 2346 | } |
| 2347 | else |
| 2348 | j = buf_len; |
| 2349 | |
| 2350 | /* Get and check i. */ |
| 2351 | if (lua_gettop(L) == 3) { |
| 2352 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2353 | if (i < 0) |
| 2354 | i = buf_len + i + 1; |
| 2355 | if (i > buf_len) |
| 2356 | i = buf_len + 1; |
| 2357 | lua_pop(L, 1); |
| 2358 | } else |
| 2359 | i = 1; |
| 2360 | |
| 2361 | /* Check bth i and j. */ |
| 2362 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2363 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2364 | return 1; |
| 2365 | } |
| 2366 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2367 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2368 | return 1; |
| 2369 | } |
| 2370 | if (i == 0) |
| 2371 | i = 1; |
| 2372 | if (j == 0) |
| 2373 | j = 1; |
| 2374 | |
| 2375 | /* Pop the string. */ |
| 2376 | lua_pop(L, 1); |
| 2377 | |
| 2378 | /* Update the buffer length. */ |
| 2379 | buf += i - 1; |
| 2380 | buf_len = j - i + 1; |
| 2381 | lua_pushlstring(L, buf, buf_len); |
| 2382 | |
| 2383 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2384 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2385 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2386 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2387 | } |
| 2388 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2389 | #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] | 2390 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 2391 | { |
| 2392 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2393 | int ret; |
| 2394 | int len; |
| 2395 | char *p; |
| 2396 | |
| 2397 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2398 | if (ret <= 0) { |
| 2399 | lua_pushnil(L); |
| 2400 | return 1; |
| 2401 | } |
| 2402 | |
| 2403 | if (ret == AF_UNIX) { |
| 2404 | lua_pushstring(L, buffer+1); |
| 2405 | return 1; |
| 2406 | } |
| 2407 | else if (ret == AF_INET6) { |
| 2408 | buffer[0] = '['; |
| 2409 | len = strlen(buffer); |
| 2410 | buffer[len] = ']'; |
| 2411 | len++; |
| 2412 | buffer[len] = ':'; |
| 2413 | len++; |
| 2414 | p = buffer; |
| 2415 | } |
| 2416 | else if (ret == AF_INET) { |
| 2417 | p = buffer + 1; |
| 2418 | len = strlen(p); |
| 2419 | p[len] = ':'; |
| 2420 | len++; |
| 2421 | } |
| 2422 | else { |
| 2423 | lua_pushnil(L); |
| 2424 | return 1; |
| 2425 | } |
| 2426 | |
| 2427 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2428 | lua_pushnil(L); |
| 2429 | return 1; |
| 2430 | } |
| 2431 | |
| 2432 | lua_pushstring(L, p); |
| 2433 | return 1; |
| 2434 | } |
| 2435 | |
| 2436 | /* Returns information about the peer of the connection. */ |
| 2437 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2438 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2439 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2440 | struct xref *peer; |
| 2441 | struct appctx *appctx; |
| 2442 | struct stream_interface *si; |
| 2443 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2444 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2445 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2446 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2447 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2448 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2449 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2450 | /* Check if we run on the same thread than the xreator thread. |
| 2451 | * We cannot access to the socket if the thread is different. |
| 2452 | */ |
| 2453 | if (socket->tid != tid) |
| 2454 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2455 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2456 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2457 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2458 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2459 | lua_pushnil(L); |
| 2460 | return 1; |
| 2461 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2462 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2463 | si = appctx->owner; |
| 2464 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2465 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2466 | if (!s->target_addr) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2467 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2468 | lua_pushnil(L); |
| 2469 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2470 | } |
| 2471 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2472 | ret = MAY_LJMP(hlua_socket_info(L, s->target_addr)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2473 | xref_unlock(&socket->xref, peer); |
| 2474 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2475 | } |
| 2476 | |
| 2477 | /* Returns information about my connection side. */ |
| 2478 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2479 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2480 | struct hlua_socket *socket; |
| 2481 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2482 | struct appctx *appctx; |
| 2483 | struct xref *peer; |
| 2484 | struct stream_interface *si; |
| 2485 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2486 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2487 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2488 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2489 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2490 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2491 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2492 | /* Check if we run on the same thread than the xreator thread. |
| 2493 | * We cannot access to the socket if the thread is different. |
| 2494 | */ |
| 2495 | if (socket->tid != tid) |
| 2496 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2497 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2498 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2499 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2500 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2501 | lua_pushnil(L); |
| 2502 | return 1; |
| 2503 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2504 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2505 | si = appctx->owner; |
| 2506 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2507 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2508 | conn = cs_conn(objt_cs(s->si[1].end)); |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2509 | if (!conn || !conn_get_src(conn)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2510 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2511 | lua_pushnil(L); |
| 2512 | return 1; |
| 2513 | } |
| 2514 | |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2515 | ret = hlua_socket_info(L, conn->src); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2516 | xref_unlock(&socket->xref, peer); |
| 2517 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2518 | } |
| 2519 | |
| 2520 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2521 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2522 | .obj_type = OBJ_TYPE_APPLET, |
| 2523 | .name = "<LUA_TCP>", |
| 2524 | .fct = hlua_socket_handler, |
| 2525 | .release = hlua_socket_release, |
| 2526 | }; |
| 2527 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2528 | __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] | 2529 | { |
| 2530 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2531 | struct hlua *hlua; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2532 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2533 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2534 | struct stream_interface *si; |
| 2535 | struct stream *s; |
| 2536 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2537 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2538 | hlua = hlua_gethlua(L); |
| 2539 | if (!hlua) |
| 2540 | return 0; |
| 2541 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2542 | /* Check if we run on the same thread than the xreator thread. |
| 2543 | * We cannot access to the socket if the thread is different. |
| 2544 | */ |
| 2545 | if (socket->tid != tid) |
| 2546 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2547 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2548 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2549 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2550 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2551 | lua_pushnil(L); |
| 2552 | lua_pushstring(L, "Can't connect"); |
| 2553 | return 2; |
| 2554 | } |
| 2555 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2556 | si = appctx->owner; |
| 2557 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2558 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2559 | /* Check if we run on the same thread than the xreator thread. |
| 2560 | * We cannot access to the socket if the thread is different. |
| 2561 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2562 | if (socket->tid != tid) { |
| 2563 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2564 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2565 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2566 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2567 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2568 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2569 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2570 | lua_pushnil(L); |
| 2571 | lua_pushstring(L, "Can't connect"); |
| 2572 | return 2; |
| 2573 | } |
| 2574 | |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2575 | appctx = __objt_appctx(s->si[0].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2576 | |
| 2577 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2578 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2579 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2580 | lua_pushinteger(L, 1); |
| 2581 | return 1; |
| 2582 | } |
| 2583 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2584 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2585 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2586 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2587 | } |
| 2588 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2589 | 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] | 2590 | return 0; |
| 2591 | } |
| 2592 | |
| 2593 | /* This function fail or initite the connection. */ |
| 2594 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2595 | { |
| 2596 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2597 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2598 | const char *ip; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2599 | struct hlua *hlua; |
| 2600 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2601 | int low, high; |
| 2602 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2603 | struct xref *peer; |
| 2604 | struct stream_interface *si; |
| 2605 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2606 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2607 | if (lua_gettop(L) < 2) |
| 2608 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2609 | |
| 2610 | /* Get args. */ |
| 2611 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2612 | |
| 2613 | /* Check if we run on the same thread than the xreator thread. |
| 2614 | * We cannot access to the socket if the thread is different. |
| 2615 | */ |
| 2616 | if (socket->tid != tid) |
| 2617 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2618 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2619 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2620 | if (lua_gettop(L) >= 3) { |
| 2621 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2622 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2623 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2624 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2625 | * that are not enclosed within square brackets. |
| 2626 | */ |
| 2627 | if (port > 0) { |
| 2628 | luaL_buffinit(L, &b); |
| 2629 | luaL_addstring(&b, ip); |
| 2630 | luaL_addchar(&b, ':'); |
| 2631 | luaL_pushresult(&b); |
| 2632 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2633 | } |
| 2634 | } |
| 2635 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2636 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2637 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2638 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2639 | lua_pushnil(L); |
| 2640 | return 1; |
| 2641 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2642 | |
| 2643 | /* Parse ip address. */ |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 2644 | 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] | 2645 | if (!addr) { |
| 2646 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2647 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2648 | } |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2649 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2650 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2651 | if (low == 0) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2652 | if (addr->ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2653 | if (port == -1) { |
| 2654 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2655 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2656 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2657 | ((struct sockaddr_in *)addr)->sin_port = htons(port); |
| 2658 | } else if (addr->ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2659 | if (port == -1) { |
| 2660 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2661 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2662 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2663 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(port); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2664 | } |
| 2665 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2666 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2667 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2668 | si = appctx->owner; |
| 2669 | s = si_strm(si); |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2670 | |
Willy Tarreau | 9b7587a | 2020-10-15 07:32:10 +0200 | [diff] [blame] | 2671 | if (!sockaddr_alloc(&s->target_addr, addr, sizeof(*addr))) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2672 | xref_unlock(&socket->xref, peer); |
| 2673 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
| 2674 | } |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2675 | s->flags |= SF_ADDR_SET; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2676 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2677 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2678 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2679 | if (!hlua) |
| 2680 | return 0; |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2681 | |
| 2682 | /* inform the stream that we want to be notified whenever the |
| 2683 | * connection completes. |
| 2684 | */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 2685 | si_cant_get(&s->si[0]); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2686 | si_rx_endp_more(&s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2687 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2688 | |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2689 | hlua->gc_count++; |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2690 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2691 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2692 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2693 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2694 | } |
| 2695 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2696 | |
| 2697 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2698 | /* Return yield waiting for connection. */ |
| 2699 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2700 | 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] | 2701 | |
| 2702 | return 0; |
| 2703 | } |
| 2704 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2705 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2706 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2707 | { |
| 2708 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2709 | struct xref *peer; |
| 2710 | struct appctx *appctx; |
| 2711 | struct stream_interface *si; |
| 2712 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2713 | |
| 2714 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2715 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2716 | |
| 2717 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2718 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2719 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2720 | lua_pushnil(L); |
| 2721 | return 1; |
| 2722 | } |
| 2723 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2724 | si = appctx->owner; |
| 2725 | s = si_strm(si); |
| 2726 | |
| 2727 | s->target = &socket_ssl.obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2728 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2729 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2730 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2731 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2732 | |
| 2733 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2734 | { |
| 2735 | return 0; |
| 2736 | } |
| 2737 | |
| 2738 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2739 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2740 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2741 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2742 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2743 | struct xref *peer; |
| 2744 | struct appctx *appctx; |
| 2745 | struct stream_interface *si; |
| 2746 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2747 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2748 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2749 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2750 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2751 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2752 | /* convert the timeout to millis */ |
| 2753 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2754 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2755 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2756 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2757 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2758 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2759 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2760 | 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] | 2761 | |
| 2762 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2763 | if (tmout == 0) |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2764 | tmout++; /* very small timeouts are adjusted to a minimum of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2765 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2766 | /* Check if we run on the same thread than the xreator thread. |
| 2767 | * We cannot access to the socket if the thread is different. |
| 2768 | */ |
| 2769 | if (socket->tid != tid) |
| 2770 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2771 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2772 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2773 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2774 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2775 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2776 | WILL_LJMP(lua_error(L)); |
| 2777 | return 0; |
| 2778 | } |
| 2779 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2780 | si = appctx->owner; |
| 2781 | s = si_strm(si); |
| 2782 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2783 | s->sess->fe->timeout.connect = tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2784 | s->req.rto = tmout; |
| 2785 | s->req.wto = tmout; |
| 2786 | s->res.rto = tmout; |
| 2787 | s->res.wto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2788 | s->req.rex = tick_add_ifset(now_ms, tmout); |
| 2789 | s->req.wex = tick_add_ifset(now_ms, tmout); |
| 2790 | s->res.rex = tick_add_ifset(now_ms, tmout); |
| 2791 | s->res.wex = tick_add_ifset(now_ms, tmout); |
| 2792 | |
| 2793 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 2794 | task_queue(s->task); |
| 2795 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2796 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2797 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 2798 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 2799 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2800 | } |
| 2801 | |
| 2802 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2803 | { |
| 2804 | struct hlua_socket *socket; |
| 2805 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2806 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2807 | struct stream *strm; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2808 | |
| 2809 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2810 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2811 | hlua_pusherror(L, "socket: full stack"); |
| 2812 | goto out_fail_conf; |
| 2813 | } |
| 2814 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2815 | /* Create the object: obj[0] = userdata. */ |
| 2816 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2817 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2818 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2819 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2820 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2821 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2822 | /* Check if the various memory pools are initialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2823 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2824 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2825 | goto out_fail_conf; |
| 2826 | } |
| 2827 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2828 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2829 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2830 | lua_setmetatable(L, -2); |
| 2831 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2832 | /* Create the applet context */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 2833 | appctx = appctx_new(&update_applet, tid_bit); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2834 | if (!appctx) { |
| 2835 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2836 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2837 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2838 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2839 | appctx->ctx.hlua_cosocket.connected = 0; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2840 | appctx->ctx.hlua_cosocket.die = 0; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2841 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 2842 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2843 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2844 | /* Now create a session, task and stream for this applet */ |
| 2845 | sess = session_new(&socket_proxy, NULL, &appctx->obj_type); |
| 2846 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2847 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2848 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2849 | } |
| 2850 | |
Christopher Faulet | 26256f8 | 2020-09-14 11:40:13 +0200 | [diff] [blame] | 2851 | strm = stream_new(sess, &appctx->obj_type, &BUF_NULL); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2852 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2853 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2854 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2855 | } |
| 2856 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2857 | /* Initialise cross reference between stream and Lua socket object. */ |
| 2858 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2859 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2860 | /* Configure "right" stream interface. this "si" is used to connect |
| 2861 | * and retrieve data from the server. The connection is initialized |
| 2862 | * with the "struct server". |
| 2863 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2864 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2865 | |
| 2866 | /* Force destination server. */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2867 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2868 | strm->target = &socket_tcp.obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2869 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2870 | return 1; |
| 2871 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2872 | out_fail_stream: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 2873 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2874 | out_fail_sess: |
| 2875 | appctx_free(appctx); |
| 2876 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2877 | WILL_LJMP(lua_error(L)); |
| 2878 | return 0; |
| 2879 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2880 | |
| 2881 | /* |
| 2882 | * |
| 2883 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2884 | * Class Channel |
| 2885 | * |
| 2886 | * |
| 2887 | */ |
| 2888 | |
| 2889 | /* Returns the struct hlua_channel join to the class channel in the |
| 2890 | * stack entry "ud" or throws an argument error. |
| 2891 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2892 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2893 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2894 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2895 | } |
| 2896 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2897 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 2898 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2899 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 2900 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2901 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2902 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2903 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2904 | return 0; |
| 2905 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2906 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2907 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2908 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2909 | |
| 2910 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 2911 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 2912 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2913 | return 1; |
| 2914 | } |
| 2915 | |
| 2916 | /* Duplicate all the data present in the input channel and put it |
| 2917 | * in a string LUA variables. Returns -1 and push a nil value in |
| 2918 | * the stack if the channel is closed and all the data are consumed, |
| 2919 | * returns 0 if no data are available, otherwise it returns the length |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 2920 | * of the built string. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2921 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2922 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2923 | { |
| 2924 | char *blk1; |
| 2925 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2926 | size_t len1; |
| 2927 | size_t len2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2928 | int ret; |
| 2929 | luaL_Buffer b; |
| 2930 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2931 | ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2932 | if (unlikely(ret == 0)) |
| 2933 | return 0; |
| 2934 | |
| 2935 | if (unlikely(ret < 0)) { |
| 2936 | lua_pushnil(L); |
| 2937 | return -1; |
| 2938 | } |
| 2939 | |
| 2940 | luaL_buffinit(L, &b); |
| 2941 | luaL_addlstring(&b, blk1, len1); |
| 2942 | if (unlikely(ret == 2)) |
| 2943 | luaL_addlstring(&b, blk2, len2); |
| 2944 | luaL_pushresult(&b); |
| 2945 | |
| 2946 | if (unlikely(ret == 2)) |
| 2947 | return len1 + len2; |
| 2948 | return len1; |
| 2949 | } |
| 2950 | |
| 2951 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2952 | * a yield. This function keep the data in the buffer. |
| 2953 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2954 | __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] | 2955 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2956 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2957 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2958 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2959 | |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2960 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) { |
| 2961 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2962 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2963 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2964 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2965 | if (_hlua_channel_dup(chn, L) == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2966 | 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] | 2967 | return 1; |
| 2968 | } |
| 2969 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2970 | /* Check arguments for the function "hlua_channel_dup_yield". */ |
| 2971 | __LJMP static int hlua_channel_dup(lua_State *L) |
| 2972 | { |
| 2973 | MAY_LJMP(check_args(L, 1, "dup")); |
| 2974 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2975 | return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0)); |
| 2976 | } |
| 2977 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2978 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2979 | * a yield. This function consumes the data in the buffer. It returns |
| 2980 | * a string containing the data or a nil pointer if no data are available |
| 2981 | * and the channel is closed. |
| 2982 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2983 | __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] | 2984 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2985 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2986 | int ret; |
| 2987 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2988 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2989 | |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2990 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) { |
| 2991 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2992 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2993 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2994 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2995 | ret = _hlua_channel_dup(chn, L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2996 | if (unlikely(ret == 0)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2997 | 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] | 2998 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2999 | if (unlikely(ret == -1)) |
| 3000 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3001 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3002 | b_sub(&chn->buf, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3003 | return 1; |
| 3004 | } |
| 3005 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3006 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3007 | __LJMP static int hlua_channel_get(lua_State *L) |
| 3008 | { |
| 3009 | MAY_LJMP(check_args(L, 1, "get")); |
| 3010 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3011 | return MAY_LJMP(hlua_channel_get_yield(L, 0, 0)); |
| 3012 | } |
| 3013 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3014 | /* This functions consumes and returns one line. If the channel is closed, |
| 3015 | * 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] | 3016 | * 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] | 3017 | * value. |
| 3018 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3019 | __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] | 3020 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3021 | char *blk1; |
| 3022 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3023 | size_t len1; |
| 3024 | size_t len2; |
| 3025 | size_t len; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3026 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3027 | int ret; |
| 3028 | luaL_Buffer b; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3029 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3030 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3031 | |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3032 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) { |
| 3033 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3034 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3035 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3036 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3037 | ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3038 | if (ret == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3039 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3040 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3041 | if (ret == -1) { |
| 3042 | lua_pushnil(L); |
| 3043 | return 1; |
| 3044 | } |
| 3045 | |
| 3046 | luaL_buffinit(L, &b); |
| 3047 | luaL_addlstring(&b, blk1, len1); |
| 3048 | len = len1; |
| 3049 | if (unlikely(ret == 2)) { |
| 3050 | luaL_addlstring(&b, blk2, len2); |
| 3051 | len += len2; |
| 3052 | } |
| 3053 | luaL_pushresult(&b); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3054 | 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] | 3055 | return 1; |
| 3056 | } |
| 3057 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3058 | /* Check arguments for the function "hlua_channel_getline_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3059 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 3060 | { |
| 3061 | MAY_LJMP(check_args(L, 1, "getline")); |
| 3062 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3063 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 3064 | } |
| 3065 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3066 | /* This function takes a string as input, and append it at the |
| 3067 | * input side of channel. If the data is too big, but a space |
| 3068 | * is probably available after sending some data, the function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3069 | * yields. If the data is bigger than the buffer, or if the |
| 3070 | * channel is closed, it returns -1. Otherwise, it returns the |
| 3071 | * amount of data written. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3072 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3073 | __LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3074 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3075 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3076 | size_t len; |
| 3077 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3078 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3079 | int ret; |
| 3080 | int max; |
| 3081 | |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3082 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) { |
| 3083 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3084 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3085 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3086 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3087 | /* Check if the buffer is available because HAProxy doesn't allocate |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 3088 | * the request buffer if its not required. |
| 3089 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3090 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 3091 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3092 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0)); |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 3093 | } |
| 3094 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3095 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3096 | if (max > len - l) |
| 3097 | max = len - l; |
| 3098 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3099 | ret = ci_putblk(chn, str + l, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3100 | if (ret == -2 || ret == -3) { |
| 3101 | lua_pushinteger(L, -1); |
| 3102 | return 1; |
| 3103 | } |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 3104 | if (ret == -1) { |
| 3105 | chn->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3106 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0)); |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 3107 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3108 | l += ret; |
| 3109 | lua_pop(L, 1); |
| 3110 | lua_pushinteger(L, l); |
| 3111 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3112 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3113 | if (max == 0 && co_data(chn) == 0) { |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3114 | /* There are no space available, and the output buffer is empty. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3115 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3116 | * we return the amount of copied data. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3117 | */ |
| 3118 | return 1; |
| 3119 | } |
| 3120 | if (l < len) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3121 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3122 | return 1; |
| 3123 | } |
| 3124 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3125 | /* Just a wrapper of "hlua_channel_append_yield". It returns the length |
| 3126 | * of the written string, or -1 if the channel is closed or if the |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3127 | * buffer size is too little for the data. |
| 3128 | */ |
| 3129 | __LJMP static int hlua_channel_append(lua_State *L) |
| 3130 | { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3131 | size_t len; |
| 3132 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3133 | MAY_LJMP(check_args(L, 2, "append")); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3134 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3135 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3136 | MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3137 | lua_pushinteger(L, 0); |
| 3138 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3139 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3140 | } |
| 3141 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3142 | /* Just a wrapper of "hlua_channel_append_yield". This wrapper starts |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3143 | * his process by cleaning the buffer. The result is a replacement |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3144 | * of the current data. It returns the length of the written string, |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3145 | * or -1 if the channel is closed or if the buffer size is too |
| 3146 | * little for the data. |
| 3147 | */ |
| 3148 | __LJMP static int hlua_channel_set(lua_State *L) |
| 3149 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3150 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3151 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3152 | MAY_LJMP(check_args(L, 2, "set")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3153 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3154 | lua_pushinteger(L, 0); |
| 3155 | |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3156 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) { |
| 3157 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3158 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3159 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3160 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3161 | b_set_data(&chn->buf, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3162 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3163 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3164 | } |
| 3165 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3166 | /* Append data in the output side of the buffer. This data is immediately |
| 3167 | * sent. The function returns the amount of data written. If the buffer |
| 3168 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3169 | * if the channel is closed. |
| 3170 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3171 | __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] | 3172 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3173 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3174 | size_t len; |
| 3175 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3176 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3177 | int max; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3178 | struct hlua *hlua; |
| 3179 | |
| 3180 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3181 | hlua = hlua_gethlua(L); |
| 3182 | if (!hlua) { |
| 3183 | lua_pushnil(L); |
| 3184 | return 1; |
| 3185 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3186 | |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3187 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) { |
| 3188 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3189 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3190 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3191 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3192 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3193 | lua_pushinteger(L, -1); |
| 3194 | return 1; |
| 3195 | } |
| 3196 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3197 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 3198 | * the request buffer if its not required. |
| 3199 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3200 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 3201 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3202 | 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] | 3203 | } |
| 3204 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3205 | /* The written data will be immediately sent, so we can check |
| 3206 | * the available space without taking in account the reserve. |
| 3207 | * The reserve is guaranteed for the processing of incoming |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3208 | * data, because the buffer will be flushed. |
| 3209 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3210 | max = b_room(&chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3211 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3212 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3213 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3214 | * we return the amount of copied data. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3215 | */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3216 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3217 | return 1; |
| 3218 | |
| 3219 | /* Adjust the real required length. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3220 | if (max > len - l) |
| 3221 | max = len - l; |
| 3222 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3223 | /* The buffer available size may be not contiguous. This test |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3224 | * detects a non contiguous buffer and realign it. |
| 3225 | */ |
Willy Tarreau | 3f67999 | 2018-06-15 15:06:42 +0200 | [diff] [blame] | 3226 | if (ci_space_for_replace(chn) < max) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3227 | channel_slow_realign(chn, trash.area); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3228 | |
| 3229 | /* Copy input data in the buffer. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3230 | 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] | 3231 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3232 | /* buffer replace considers that the input part is filled. |
| 3233 | * so, I must forward these new data in the output part. |
| 3234 | */ |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 3235 | c_adv(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3236 | |
| 3237 | l += max; |
| 3238 | lua_pop(L, 1); |
| 3239 | lua_pushinteger(L, l); |
| 3240 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3241 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3242 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3243 | * we return the amount of copied data. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3244 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3245 | max = b_room(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3246 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3247 | return 1; |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3248 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3249 | if (l < len) { |
| 3250 | /* If we are waiting for space in the response buffer, we |
| 3251 | * must set the flag WAKERESWR. This flag required the task |
| 3252 | * wake up if any activity is detected on the response buffer. |
| 3253 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3254 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3255 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3256 | else |
| 3257 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3258 | 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] | 3259 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3260 | |
| 3261 | return 1; |
| 3262 | } |
| 3263 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3264 | /* Just a wrapper of "_hlua_channel_send". This wrapper permits |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3265 | * yield the LUA process, and resume it without checking the |
| 3266 | * input arguments. |
| 3267 | */ |
| 3268 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3269 | { |
| 3270 | MAY_LJMP(check_args(L, 2, "send")); |
| 3271 | lua_pushinteger(L, 0); |
| 3272 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3273 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3274 | } |
| 3275 | |
| 3276 | /* This function forward and amount of butes. The data pass from |
| 3277 | * the input side of the buffer to the output side, and can be |
| 3278 | * forwarded. This function never fails. |
| 3279 | * |
| 3280 | * The Lua function takes an amount of bytes to be forwarded in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3281 | * input. It returns the number of bytes forwarded. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3282 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3283 | __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] | 3284 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3285 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3286 | int len; |
| 3287 | int l; |
| 3288 | int max; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3289 | struct hlua *hlua; |
| 3290 | |
| 3291 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3292 | hlua = hlua_gethlua(L); |
| 3293 | if (!hlua) |
| 3294 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3295 | |
| 3296 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3297 | |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3298 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) { |
| 3299 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3300 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3301 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3302 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3303 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3304 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3305 | |
| 3306 | max = len - l; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3307 | if (max > ci_data(chn)) |
| 3308 | max = ci_data(chn); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3309 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3310 | l += max; |
| 3311 | |
| 3312 | lua_pop(L, 1); |
| 3313 | lua_pushinteger(L, l); |
| 3314 | |
| 3315 | /* Check if it miss bytes to forward. */ |
| 3316 | if (l < len) { |
| 3317 | /* The the input channel or the output channel are closed, we |
| 3318 | * must return the amount of data forwarded. |
| 3319 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3320 | if (channel_input_closed(chn) || channel_output_closed(chn)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3321 | return 1; |
| 3322 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3323 | /* If we are waiting for space data in the response buffer, we |
| 3324 | * must set the flag WAKERESWR. This flag required the task |
| 3325 | * wake up if any activity is detected on the response buffer. |
| 3326 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3327 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3328 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3329 | else |
| 3330 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3331 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3332 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3333 | 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] | 3334 | } |
| 3335 | |
| 3336 | return 1; |
| 3337 | } |
| 3338 | |
| 3339 | /* Just check the input and prepare the stack for the previous |
| 3340 | * function "hlua_channel_forward_yield" |
| 3341 | */ |
| 3342 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3343 | { |
| 3344 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3345 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3346 | MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3347 | |
| 3348 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3349 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3350 | } |
| 3351 | |
| 3352 | /* Just returns the number of bytes available in the input |
| 3353 | * side of the buffer. This function never fails. |
| 3354 | */ |
| 3355 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3356 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3357 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3358 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3359 | MAY_LJMP(check_args(L, 1, "get_in_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3360 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3361 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3362 | struct htx *htx = htxbuf(&chn->buf); |
| 3363 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 3364 | } |
| 3365 | else |
| 3366 | lua_pushinteger(L, ci_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3367 | return 1; |
| 3368 | } |
| 3369 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3370 | /* Returns true if the channel is full. */ |
| 3371 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3372 | { |
| 3373 | struct channel *chn; |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3374 | |
| 3375 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3376 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 0ec740e | 2020-02-26 11:59:19 +0100 | [diff] [blame] | 3377 | /* ignore the reserve, we are not on a producer side (ie in an |
| 3378 | * applet). |
| 3379 | */ |
| 3380 | lua_pushboolean(L, channel_full(chn, 0)); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3381 | return 1; |
| 3382 | } |
| 3383 | |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 3384 | /* Returns true if the channel is the response channel. */ |
| 3385 | __LJMP static int hlua_channel_is_resp(lua_State *L) |
| 3386 | { |
| 3387 | struct channel *chn; |
| 3388 | |
| 3389 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 3390 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3391 | |
| 3392 | lua_pushboolean(L, !!(chn->flags & CF_ISRESP)); |
| 3393 | return 1; |
| 3394 | } |
| 3395 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3396 | /* Just returns the number of bytes available in the output |
| 3397 | * side of the buffer. This function never fails. |
| 3398 | */ |
| 3399 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 3400 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3401 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3402 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3403 | MAY_LJMP(check_args(L, 1, "get_out_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3404 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3405 | lua_pushinteger(L, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3406 | return 1; |
| 3407 | } |
| 3408 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3409 | /* |
| 3410 | * |
| 3411 | * |
| 3412 | * Class Fetches |
| 3413 | * |
| 3414 | * |
| 3415 | */ |
| 3416 | |
| 3417 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3418 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3419 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3420 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3421 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3422 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3423 | } |
| 3424 | |
| 3425 | /* This function creates and push in the stack a fetch object according |
| 3426 | * with a current TXN. |
| 3427 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3428 | 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] | 3429 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3430 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3431 | |
| 3432 | /* Check stack size. */ |
| 3433 | if (!lua_checkstack(L, 3)) |
| 3434 | return 0; |
| 3435 | |
| 3436 | /* Create the object: obj[0] = userdata. |
| 3437 | * Note that the base of the Fetches object is the |
| 3438 | * transaction object. |
| 3439 | */ |
| 3440 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3441 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3442 | lua_rawseti(L, -2, 0); |
| 3443 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3444 | hsmp->s = txn->s; |
| 3445 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3446 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3447 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3448 | |
| 3449 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3450 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 3451 | lua_setmetatable(L, -2); |
| 3452 | |
| 3453 | return 1; |
| 3454 | } |
| 3455 | |
| 3456 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 3457 | * It uses closure argument to store the associated sample-fetch. It |
| 3458 | * returns only one argument or throws an error. An error is thrown |
| 3459 | * only if an error is encountered during the argument parsing. If |
| 3460 | * the "sample-fetch" function fails, nil is returned. |
| 3461 | */ |
| 3462 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 3463 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3464 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3465 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3466 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3467 | int i; |
| 3468 | struct sample smp; |
| 3469 | |
| 3470 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3471 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3472 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3473 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3474 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3475 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 3476 | /* Check execution authorization. */ |
| 3477 | if (f->use & SMP_USE_HTTP_ANY && |
| 3478 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 3479 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 3480 | "is not available in Lua services", f->kw); |
| 3481 | WILL_LJMP(lua_error(L)); |
| 3482 | } |
| 3483 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3484 | /* Get extra arguments. */ |
| 3485 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 3486 | if (i >= ARGM_NBARGS) |
| 3487 | break; |
| 3488 | hlua_lua2arg(L, i + 2, &args[i]); |
| 3489 | } |
| 3490 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3491 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3492 | |
| 3493 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3494 | 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] | 3495 | |
| 3496 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3497 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3498 | lua_pushfstring(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3499 | goto error; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3500 | } |
| 3501 | |
| 3502 | /* Initialise the sample. */ |
| 3503 | memset(&smp, 0, sizeof(smp)); |
| 3504 | |
| 3505 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3506 | 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] | 3507 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3508 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3509 | lua_pushstring(L, ""); |
| 3510 | else |
| 3511 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3512 | goto end; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3513 | } |
| 3514 | |
| 3515 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3516 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3517 | hlua_smp2lua_str(L, &smp); |
| 3518 | else |
| 3519 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3520 | |
| 3521 | end: |
| 3522 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3523 | if (args[i].type == ARGT_STR) |
| 3524 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3525 | else if (args[i].type == ARGT_REG) |
| 3526 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3527 | } |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3528 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3529 | |
| 3530 | error: |
| 3531 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3532 | if (args[i].type == ARGT_STR) |
| 3533 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3534 | else if (args[i].type == ARGT_REG) |
| 3535 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3536 | } |
| 3537 | WILL_LJMP(lua_error(L)); |
| 3538 | return 0; /* Never reached */ |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3539 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3540 | |
| 3541 | /* |
| 3542 | * |
| 3543 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3544 | * Class Converters |
| 3545 | * |
| 3546 | * |
| 3547 | */ |
| 3548 | |
| 3549 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3550 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3551 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3552 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3553 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3554 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3555 | } |
| 3556 | |
| 3557 | /* This function creates and push in the stack a Converters object |
| 3558 | * according with a current TXN. |
| 3559 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3560 | 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] | 3561 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3562 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3563 | |
| 3564 | /* Check stack size. */ |
| 3565 | if (!lua_checkstack(L, 3)) |
| 3566 | return 0; |
| 3567 | |
| 3568 | /* Create the object: obj[0] = userdata. |
| 3569 | * Note that the base of the Converters object is the |
| 3570 | * same than the TXN object. |
| 3571 | */ |
| 3572 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3573 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3574 | lua_rawseti(L, -2, 0); |
| 3575 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3576 | hsmp->s = txn->s; |
| 3577 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3578 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3579 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3580 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3581 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3582 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 3583 | lua_setmetatable(L, -2); |
| 3584 | |
| 3585 | return 1; |
| 3586 | } |
| 3587 | |
| 3588 | /* This function is an LUA binding. It is called with each converter. |
| 3589 | * It uses closure argument to store the associated converter. It |
| 3590 | * returns only one argument or throws an error. An error is thrown |
| 3591 | * only if an error is encountered during the argument parsing. If |
| 3592 | * the converter function function fails, nil is returned. |
| 3593 | */ |
| 3594 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 3595 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3596 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3597 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3598 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3599 | int i; |
| 3600 | struct sample smp; |
| 3601 | |
| 3602 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3603 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3604 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3605 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3606 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3607 | |
| 3608 | /* Get extra arguments. */ |
| 3609 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 3610 | if (i >= ARGM_NBARGS) |
| 3611 | break; |
| 3612 | hlua_lua2arg(L, i + 3, &args[i]); |
| 3613 | } |
| 3614 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3615 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3616 | |
| 3617 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3618 | 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] | 3619 | |
| 3620 | /* Run the special args checker. */ |
| 3621 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 3622 | hlua_pusherror(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3623 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3624 | } |
| 3625 | |
| 3626 | /* Initialise the sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 3627 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3628 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 3629 | hlua_pusherror(L, "error in the input argument"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3630 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3631 | } |
| 3632 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3633 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 3634 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3635 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3636 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3637 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3638 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3639 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3640 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3641 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 3642 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3643 | hlua_pusherror(L, "error during the input argument casting"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3644 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3645 | } |
| 3646 | |
| 3647 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3648 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3649 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3650 | lua_pushstring(L, ""); |
| 3651 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3652 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3653 | goto end; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3654 | } |
| 3655 | |
| 3656 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3657 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3658 | hlua_smp2lua_str(L, &smp); |
| 3659 | else |
| 3660 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3661 | end: |
| 3662 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3663 | if (args[i].type == ARGT_STR) |
| 3664 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3665 | else if (args[i].type == ARGT_REG) |
| 3666 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3667 | } |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3668 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3669 | |
| 3670 | error: |
| 3671 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3672 | if (args[i].type == ARGT_STR) |
| 3673 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3674 | else if (args[i].type == ARGT_REG) |
| 3675 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3676 | } |
| 3677 | WILL_LJMP(lua_error(L)); |
| 3678 | return 0; /* Never reached */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3679 | } |
| 3680 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3681 | /* |
| 3682 | * |
| 3683 | * |
| 3684 | * Class AppletTCP |
| 3685 | * |
| 3686 | * |
| 3687 | */ |
| 3688 | |
| 3689 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 3690 | * a class stream, otherwise it throws an error. |
| 3691 | */ |
| 3692 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 3693 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3694 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3695 | } |
| 3696 | |
| 3697 | /* This function creates and push in the stack an Applet object |
| 3698 | * according with a current TXN. |
| 3699 | */ |
| 3700 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 3701 | { |
| 3702 | struct hlua_appctx *appctx; |
| 3703 | struct stream_interface *si = ctx->owner; |
| 3704 | struct stream *s = si_strm(si); |
| 3705 | struct proxy *p = s->be; |
| 3706 | |
| 3707 | /* Check stack size. */ |
| 3708 | if (!lua_checkstack(L, 3)) |
| 3709 | return 0; |
| 3710 | |
| 3711 | /* Create the object: obj[0] = userdata. |
| 3712 | * Note that the base of the Converters object is the |
| 3713 | * same than the TXN object. |
| 3714 | */ |
| 3715 | lua_newtable(L); |
| 3716 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
| 3717 | lua_rawseti(L, -2, 0); |
| 3718 | appctx->appctx = ctx; |
| 3719 | appctx->htxn.s = s; |
| 3720 | appctx->htxn.p = p; |
| 3721 | |
| 3722 | /* Create the "f" field that contains a list of fetches. */ |
| 3723 | lua_pushstring(L, "f"); |
| 3724 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3725 | return 0; |
| 3726 | lua_settable(L, -3); |
| 3727 | |
| 3728 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3729 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3730 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3731 | return 0; |
| 3732 | lua_settable(L, -3); |
| 3733 | |
| 3734 | /* Create the "c" field that contains a list of converters. */ |
| 3735 | lua_pushstring(L, "c"); |
| 3736 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3737 | return 0; |
| 3738 | lua_settable(L, -3); |
| 3739 | |
| 3740 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3741 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3742 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3743 | return 0; |
| 3744 | lua_settable(L, -3); |
| 3745 | |
| 3746 | /* Pop a class stream metatable and affect it to the table. */ |
| 3747 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 3748 | lua_setmetatable(L, -2); |
| 3749 | |
| 3750 | return 1; |
| 3751 | } |
| 3752 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3753 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 3754 | { |
| 3755 | struct hlua_appctx *appctx; |
| 3756 | struct stream *s; |
| 3757 | const char *name; |
| 3758 | size_t len; |
| 3759 | struct sample smp; |
| 3760 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3761 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 3762 | 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] | 3763 | |
| 3764 | /* It is useles to retrieve the stream, but this function |
| 3765 | * runs only in a stream context. |
| 3766 | */ |
| 3767 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3768 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3769 | s = appctx->htxn.s; |
| 3770 | |
| 3771 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 3772 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3773 | hlua_lua2smp(L, 3, &smp); |
| 3774 | |
| 3775 | /* Store the sample in a variable. */ |
| 3776 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3777 | |
| 3778 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 3779 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 3780 | else |
| 3781 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 3782 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 3783 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3784 | } |
| 3785 | |
| 3786 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 3787 | { |
| 3788 | struct hlua_appctx *appctx; |
| 3789 | struct stream *s; |
| 3790 | const char *name; |
| 3791 | size_t len; |
| 3792 | struct sample smp; |
| 3793 | |
| 3794 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3795 | |
| 3796 | /* It is useles to retrieve the stream, but this function |
| 3797 | * runs only in a stream context. |
| 3798 | */ |
| 3799 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3800 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3801 | s = appctx->htxn.s; |
| 3802 | |
| 3803 | /* Unset the variable. */ |
| 3804 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 3805 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 3806 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3807 | } |
| 3808 | |
| 3809 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 3810 | { |
| 3811 | struct hlua_appctx *appctx; |
| 3812 | struct stream *s; |
| 3813 | const char *name; |
| 3814 | size_t len; |
| 3815 | struct sample smp; |
| 3816 | |
| 3817 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3818 | |
| 3819 | /* It is useles to retrieve the stream, but this function |
| 3820 | * runs only in a stream context. |
| 3821 | */ |
| 3822 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3823 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3824 | s = appctx->htxn.s; |
| 3825 | |
| 3826 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3827 | if (!vars_get_by_name(name, len, &smp)) { |
| 3828 | lua_pushnil(L); |
| 3829 | return 1; |
| 3830 | } |
| 3831 | |
| 3832 | return hlua_smp2lua(L, &smp); |
| 3833 | } |
| 3834 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3835 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 3836 | { |
| 3837 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3838 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3839 | struct hlua *hlua; |
| 3840 | |
| 3841 | /* 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] | 3842 | if (!s->hlua) |
| 3843 | return 0; |
| 3844 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3845 | |
| 3846 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3847 | |
| 3848 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 3849 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3850 | |
| 3851 | /* Get and store new value. */ |
| 3852 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3853 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 3854 | |
| 3855 | return 0; |
| 3856 | } |
| 3857 | |
| 3858 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 3859 | { |
| 3860 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3861 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3862 | struct hlua *hlua; |
| 3863 | |
| 3864 | /* 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] | 3865 | if (!s->hlua) { |
| 3866 | lua_pushnil(L); |
| 3867 | return 1; |
| 3868 | } |
| 3869 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3870 | |
| 3871 | /* Push configuration index in the stack. */ |
| 3872 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 3873 | |
| 3874 | return 1; |
| 3875 | } |
| 3876 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3877 | /* If expected data not yet available, it returns a yield. This function |
| 3878 | * consumes the data in the buffer. It returns a string containing the |
| 3879 | * data. This string can be empty. |
| 3880 | */ |
| 3881 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 3882 | { |
| 3883 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3884 | struct stream_interface *si = appctx->appctx->owner; |
| 3885 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3886 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3887 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3888 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3889 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3890 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3891 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3892 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3893 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3894 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3895 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3896 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3897 | 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] | 3898 | } |
| 3899 | |
| 3900 | /* End of data: commit the total strings and return. */ |
| 3901 | if (ret < 0) { |
| 3902 | luaL_pushresult(&appctx->b); |
| 3903 | return 1; |
| 3904 | } |
| 3905 | |
| 3906 | /* Ensure that the block 2 length is usable. */ |
| 3907 | if (ret == 1) |
| 3908 | len2 = 0; |
| 3909 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 3910 | /* don't check the max length read and don't check. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3911 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3912 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3913 | |
| 3914 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3915 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3916 | luaL_pushresult(&appctx->b); |
| 3917 | return 1; |
| 3918 | } |
| 3919 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3920 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3921 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 3922 | { |
| 3923 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3924 | |
| 3925 | /* Initialise the string catenation. */ |
| 3926 | luaL_buffinit(L, &appctx->b); |
| 3927 | |
| 3928 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 3929 | } |
| 3930 | |
| 3931 | /* If expected data not yet available, it returns a yield. This function |
| 3932 | * consumes the data in the buffer. It returns a string containing the |
| 3933 | * data. This string can be empty. |
| 3934 | */ |
| 3935 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 3936 | { |
| 3937 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3938 | struct stream_interface *si = appctx->appctx->owner; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3939 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3940 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3941 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3942 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3943 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3944 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3945 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3946 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3947 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3948 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3949 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3950 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3951 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3952 | 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] | 3953 | } |
| 3954 | |
| 3955 | /* End of data: commit the total strings and return. */ |
| 3956 | if (ret < 0) { |
| 3957 | luaL_pushresult(&appctx->b); |
| 3958 | return 1; |
| 3959 | } |
| 3960 | |
| 3961 | /* Ensure that the block 2 length is usable. */ |
| 3962 | if (ret == 1) |
| 3963 | len2 = 0; |
| 3964 | |
| 3965 | if (len == -1) { |
| 3966 | |
| 3967 | /* If len == -1, catenate all the data avalaile and |
| 3968 | * yield because we want to get all the data until |
| 3969 | * the end of data stream. |
| 3970 | */ |
| 3971 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3972 | luaL_addlstring(&appctx->b, blk2, len2); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3973 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3974 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3975 | 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] | 3976 | |
| 3977 | } else { |
| 3978 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3979 | /* Copy the first block caping to the length required. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3980 | if (len1 > len) |
| 3981 | len1 = len; |
| 3982 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3983 | len -= len1; |
| 3984 | |
| 3985 | /* Copy the second block. */ |
| 3986 | if (len2 > len) |
| 3987 | len2 = len; |
| 3988 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3989 | len -= len2; |
| 3990 | |
| 3991 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3992 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3993 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3994 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3995 | if (len > 0) { |
| 3996 | lua_pushinteger(L, len); |
| 3997 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3998 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3999 | 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] | 4000 | } |
| 4001 | |
| 4002 | /* return the result. */ |
| 4003 | luaL_pushresult(&appctx->b); |
| 4004 | return 1; |
| 4005 | } |
| 4006 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4007 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4008 | hlua_pusherror(L, "Lua: internal error"); |
| 4009 | WILL_LJMP(lua_error(L)); |
| 4010 | return 0; |
| 4011 | } |
| 4012 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4013 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4014 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 4015 | { |
| 4016 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4017 | int len = -1; |
| 4018 | |
| 4019 | if (lua_gettop(L) > 2) |
| 4020 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4021 | if (lua_gettop(L) >= 2) { |
| 4022 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4023 | lua_pop(L, 1); |
| 4024 | } |
| 4025 | |
| 4026 | /* Confirm or set the required length */ |
| 4027 | lua_pushinteger(L, len); |
| 4028 | |
| 4029 | /* Initialise the string catenation. */ |
| 4030 | luaL_buffinit(L, &appctx->b); |
| 4031 | |
| 4032 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 4033 | } |
| 4034 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4035 | /* Append data in the output side of the buffer. This data is immediately |
| 4036 | * sent. The function returns the amount of data written. If the buffer |
| 4037 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4038 | * if the channel is closed. |
| 4039 | */ |
| 4040 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4041 | { |
| 4042 | size_t len; |
| 4043 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4044 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4045 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4046 | struct stream_interface *si = appctx->appctx->owner; |
| 4047 | struct channel *chn = si_ic(si); |
| 4048 | int max; |
| 4049 | |
| 4050 | /* Get the max amount of data which can write as input in the channel. */ |
| 4051 | max = channel_recv_max(chn); |
| 4052 | if (max > (len - l)) |
| 4053 | max = len - l; |
| 4054 | |
| 4055 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4056 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4057 | |
| 4058 | /* update counters. */ |
| 4059 | l += max; |
| 4060 | lua_pop(L, 1); |
| 4061 | lua_pushinteger(L, l); |
| 4062 | |
| 4063 | /* If some data is not send, declares the situation to the |
| 4064 | * applet, and returns a yield. |
| 4065 | */ |
| 4066 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 4067 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4068 | 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] | 4069 | } |
| 4070 | |
| 4071 | return 1; |
| 4072 | } |
| 4073 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4074 | /* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4075 | * yield the LUA process, and resume it without checking the |
| 4076 | * input arguments. |
| 4077 | */ |
| 4078 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 4079 | { |
| 4080 | MAY_LJMP(check_args(L, 2, "send")); |
| 4081 | lua_pushinteger(L, 0); |
| 4082 | |
| 4083 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 4084 | } |
| 4085 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4086 | /* |
| 4087 | * |
| 4088 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4089 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4090 | * |
| 4091 | * |
| 4092 | */ |
| 4093 | |
| 4094 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4095 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4096 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4097 | __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] | 4098 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4099 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4100 | } |
| 4101 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4102 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4103 | * according with a current TXN. |
| 4104 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4105 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4106 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4107 | struct hlua_appctx *appctx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 4108 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4109 | struct stream_interface *si = ctx->owner; |
| 4110 | struct stream *s = si_strm(si); |
| 4111 | struct proxy *px = s->be; |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4112 | struct htx *htx; |
| 4113 | struct htx_blk *blk; |
| 4114 | struct htx_sl *sl; |
| 4115 | struct ist path; |
| 4116 | unsigned long long len = 0; |
| 4117 | int32_t pos; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4118 | |
| 4119 | /* Check stack size. */ |
| 4120 | if (!lua_checkstack(L, 3)) |
| 4121 | return 0; |
| 4122 | |
| 4123 | /* Create the object: obj[0] = userdata. |
| 4124 | * Note that the base of the Converters object is the |
| 4125 | * same than the TXN object. |
| 4126 | */ |
| 4127 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4128 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4129 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4130 | appctx->appctx = ctx; |
| 4131 | appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4132 | appctx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4133 | appctx->htxn.s = s; |
| 4134 | appctx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4135 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4136 | /* Create the "f" field that contains a list of fetches. */ |
| 4137 | lua_pushstring(L, "f"); |
| 4138 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 4139 | return 0; |
| 4140 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4141 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4142 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4143 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4144 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4145 | return 0; |
| 4146 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4147 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4148 | /* Create the "c" field that contains a list of converters. */ |
| 4149 | lua_pushstring(L, "c"); |
| 4150 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 4151 | return 0; |
| 4152 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4153 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4154 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4155 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4156 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4157 | return 0; |
| 4158 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4159 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4160 | htx = htxbuf(&s->req.buf); |
| 4161 | blk = htx_get_first_blk(htx); |
Christopher Faulet | ea00973 | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 4162 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4163 | sl = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4164 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4165 | /* Stores the request method. */ |
| 4166 | lua_pushstring(L, "method"); |
| 4167 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 4168 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4169 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4170 | /* Stores the http version. */ |
| 4171 | lua_pushstring(L, "version"); |
| 4172 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 4173 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4174 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4175 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 4176 | * the array on the top of the stack. |
| 4177 | */ |
| 4178 | lua_pushstring(L, "headers"); |
| 4179 | htxn.s = s; |
| 4180 | htxn.p = px; |
| 4181 | htxn.dir = SMP_OPT_DIR_REQ; |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4182 | if (!hlua_http_get_headers(L, &htxn.s->txn->req)) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4183 | return 0; |
| 4184 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4185 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4186 | path = http_get_path(htx_sl_req_uri(sl)); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 4187 | if (isttest(path)) { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4188 | char *p, *q, *end; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4189 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4190 | p = path.ptr; |
| 4191 | end = path.ptr + path.len; |
| 4192 | q = p; |
| 4193 | while (q < end && *q != '?') |
| 4194 | q++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4195 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4196 | /* Stores the request path. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4197 | lua_pushstring(L, "path"); |
| 4198 | lua_pushlstring(L, p, q - p); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4199 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4200 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4201 | /* Stores the query string. */ |
| 4202 | lua_pushstring(L, "qs"); |
| 4203 | if (*q == '?') |
| 4204 | q++; |
| 4205 | lua_pushlstring(L, q, end - q); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4206 | lua_settable(L, -3); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4207 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4208 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4209 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4210 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4211 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4212 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4213 | if (type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4214 | break; |
| 4215 | if (type == HTX_BLK_DATA) |
| 4216 | len += htx_get_blksz(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4217 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4218 | if (htx->extra != ULLONG_MAX) |
| 4219 | len += htx->extra; |
| 4220 | |
| 4221 | /* Stores the request path. */ |
| 4222 | lua_pushstring(L, "length"); |
| 4223 | lua_pushinteger(L, len); |
| 4224 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4225 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4226 | /* Create an empty array of HTTP request headers. */ |
| 4227 | lua_pushstring(L, "response"); |
| 4228 | lua_newtable(L); |
| 4229 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4230 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4231 | /* Pop a class stream metatable and affect it to the table. */ |
| 4232 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 4233 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4234 | |
| 4235 | return 1; |
| 4236 | } |
| 4237 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4238 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 4239 | { |
| 4240 | struct hlua_appctx *appctx; |
| 4241 | struct stream *s; |
| 4242 | const char *name; |
| 4243 | size_t len; |
| 4244 | struct sample smp; |
| 4245 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4246 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 4247 | 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] | 4248 | |
| 4249 | /* It is useles to retrieve the stream, but this function |
| 4250 | * runs only in a stream context. |
| 4251 | */ |
| 4252 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4253 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4254 | s = appctx->htxn.s; |
| 4255 | |
| 4256 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4257 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4258 | hlua_lua2smp(L, 3, &smp); |
| 4259 | |
| 4260 | /* Store the sample in a variable. */ |
| 4261 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4262 | |
| 4263 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 4264 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 4265 | else |
| 4266 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 4267 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4268 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4269 | } |
| 4270 | |
| 4271 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 4272 | { |
| 4273 | struct hlua_appctx *appctx; |
| 4274 | struct stream *s; |
| 4275 | const char *name; |
| 4276 | size_t len; |
| 4277 | struct sample smp; |
| 4278 | |
| 4279 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4280 | |
| 4281 | /* It is useles to retrieve the stream, but this function |
| 4282 | * runs only in a stream context. |
| 4283 | */ |
| 4284 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4285 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4286 | s = appctx->htxn.s; |
| 4287 | |
| 4288 | /* Unset the variable. */ |
| 4289 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4290 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4291 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4292 | } |
| 4293 | |
| 4294 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4295 | { |
| 4296 | struct hlua_appctx *appctx; |
| 4297 | struct stream *s; |
| 4298 | const char *name; |
| 4299 | size_t len; |
| 4300 | struct sample smp; |
| 4301 | |
| 4302 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4303 | |
| 4304 | /* It is useles to retrieve the stream, but this function |
| 4305 | * runs only in a stream context. |
| 4306 | */ |
| 4307 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4308 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4309 | s = appctx->htxn.s; |
| 4310 | |
| 4311 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4312 | if (!vars_get_by_name(name, len, &smp)) { |
| 4313 | lua_pushnil(L); |
| 4314 | return 1; |
| 4315 | } |
| 4316 | |
| 4317 | return hlua_smp2lua(L, &smp); |
| 4318 | } |
| 4319 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4320 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 4321 | { |
| 4322 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4323 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4324 | struct hlua *hlua; |
| 4325 | |
| 4326 | /* 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] | 4327 | if (!s->hlua) |
| 4328 | return 0; |
| 4329 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4330 | |
| 4331 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4332 | |
| 4333 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4334 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4335 | |
| 4336 | /* Get and store new value. */ |
| 4337 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4338 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4339 | |
| 4340 | return 0; |
| 4341 | } |
| 4342 | |
| 4343 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4344 | { |
| 4345 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4346 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4347 | struct hlua *hlua; |
| 4348 | |
| 4349 | /* 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] | 4350 | if (!s->hlua) { |
| 4351 | lua_pushnil(L); |
| 4352 | return 1; |
| 4353 | } |
| 4354 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4355 | |
| 4356 | /* Push configuration index in the stack. */ |
| 4357 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4358 | |
| 4359 | return 1; |
| 4360 | } |
| 4361 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4362 | /* If expected data not yet available, it returns a yield. This function |
| 4363 | * consumes the data in the buffer. It returns a string containing the |
| 4364 | * data. This string can be empty. |
| 4365 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4366 | __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] | 4367 | { |
| 4368 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4369 | struct stream_interface *si = appctx->appctx->owner; |
| 4370 | struct channel *req = si_oc(si); |
| 4371 | struct htx *htx; |
| 4372 | struct htx_blk *blk; |
| 4373 | size_t count; |
| 4374 | int stop = 0; |
| 4375 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4376 | htx = htx_from_buf(&req->buf); |
| 4377 | count = co_data(req); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4378 | blk = htx_get_first_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 4379 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4380 | while (count && !stop && blk) { |
| 4381 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4382 | uint32_t sz = htx_get_blksz(blk); |
| 4383 | struct ist v; |
| 4384 | uint32_t vlen; |
| 4385 | char *nl; |
| 4386 | |
| 4387 | vlen = sz; |
| 4388 | if (vlen > count) { |
| 4389 | if (type != HTX_BLK_DATA) |
| 4390 | break; |
| 4391 | vlen = count; |
| 4392 | } |
| 4393 | |
| 4394 | switch (type) { |
| 4395 | case HTX_BLK_UNUSED: |
| 4396 | break; |
| 4397 | |
| 4398 | case HTX_BLK_DATA: |
| 4399 | v = htx_get_blk_value(htx, blk); |
| 4400 | v.len = vlen; |
| 4401 | nl = istchr(v, '\n'); |
| 4402 | if (nl != NULL) { |
| 4403 | stop = 1; |
| 4404 | vlen = nl - v.ptr + 1; |
| 4405 | } |
| 4406 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4407 | break; |
| 4408 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4409 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4410 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4411 | stop = 1; |
| 4412 | break; |
| 4413 | |
| 4414 | default: |
| 4415 | break; |
| 4416 | } |
| 4417 | |
| 4418 | co_set_data(req, co_data(req) - vlen); |
| 4419 | count -= vlen; |
| 4420 | if (sz == vlen) |
| 4421 | blk = htx_remove_blk(htx, blk); |
| 4422 | else { |
| 4423 | htx_cut_data_blk(htx, blk, vlen); |
| 4424 | break; |
| 4425 | } |
| 4426 | } |
| 4427 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4428 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4429 | if (!stop) { |
| 4430 | si_cant_get(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4431 | 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] | 4432 | } |
| 4433 | |
| 4434 | /* return the result. */ |
| 4435 | luaL_pushresult(&appctx->b); |
| 4436 | return 1; |
| 4437 | } |
| 4438 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4439 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4440 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4441 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4442 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4443 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4444 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4445 | /* Initialise the string catenation. */ |
| 4446 | luaL_buffinit(L, &appctx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4447 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4448 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4449 | } |
| 4450 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4451 | /* If expected data not yet available, it returns a yield. This function |
| 4452 | * consumes the data in the buffer. It returns a string containing the |
| 4453 | * data. This string can be empty. |
| 4454 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4455 | __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] | 4456 | { |
| 4457 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4458 | struct stream_interface *si = appctx->appctx->owner; |
| 4459 | struct channel *req = si_oc(si); |
| 4460 | struct htx *htx; |
| 4461 | struct htx_blk *blk; |
| 4462 | size_t count; |
| 4463 | int len; |
| 4464 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4465 | htx = htx_from_buf(&req->buf); |
| 4466 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4467 | count = co_data(req); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 4468 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4469 | while (count && len && blk) { |
| 4470 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4471 | uint32_t sz = htx_get_blksz(blk); |
| 4472 | struct ist v; |
| 4473 | uint32_t vlen; |
| 4474 | |
| 4475 | vlen = sz; |
| 4476 | if (len > 0 && vlen > len) |
| 4477 | vlen = len; |
| 4478 | if (vlen > count) { |
| 4479 | if (type != HTX_BLK_DATA) |
| 4480 | break; |
| 4481 | vlen = count; |
| 4482 | } |
| 4483 | |
| 4484 | switch (type) { |
| 4485 | case HTX_BLK_UNUSED: |
| 4486 | break; |
| 4487 | |
| 4488 | case HTX_BLK_DATA: |
| 4489 | v = htx_get_blk_value(htx, blk); |
| 4490 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4491 | break; |
| 4492 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4493 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4494 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4495 | len = 0; |
| 4496 | break; |
| 4497 | |
| 4498 | default: |
| 4499 | break; |
| 4500 | } |
| 4501 | |
| 4502 | co_set_data(req, co_data(req) - vlen); |
| 4503 | count -= vlen; |
| 4504 | if (len > 0) |
| 4505 | len -= vlen; |
| 4506 | if (sz == vlen) |
| 4507 | blk = htx_remove_blk(htx, blk); |
| 4508 | else { |
| 4509 | htx_cut_data_blk(htx, blk, vlen); |
| 4510 | break; |
| 4511 | } |
| 4512 | } |
| 4513 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4514 | htx_to_buf(htx, &req->buf); |
| 4515 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4516 | /* If we are no other data available, yield waiting for new data. */ |
| 4517 | if (len) { |
| 4518 | if (len > 0) { |
| 4519 | lua_pushinteger(L, len); |
| 4520 | lua_replace(L, 2); |
| 4521 | } |
| 4522 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4523 | 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] | 4524 | } |
| 4525 | |
| 4526 | /* return the result. */ |
| 4527 | luaL_pushresult(&appctx->b); |
| 4528 | return 1; |
| 4529 | } |
| 4530 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4531 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4532 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 4533 | { |
| 4534 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4535 | int len = -1; |
| 4536 | |
| 4537 | /* Check arguments. */ |
| 4538 | if (lua_gettop(L) > 2) |
| 4539 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4540 | if (lua_gettop(L) >= 2) { |
| 4541 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4542 | lua_pop(L, 1); |
| 4543 | } |
| 4544 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4545 | lua_pushinteger(L, len); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4546 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4547 | /* Initialise the string catenation. */ |
| 4548 | luaL_buffinit(L, &appctx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4549 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4550 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4551 | } |
| 4552 | |
| 4553 | /* Append data in the output side of the buffer. This data is immediately |
| 4554 | * sent. The function returns the amount of data written. If the buffer |
| 4555 | * cannot contain the data, the function yields. The function returns -1 |
| 4556 | * if the channel is closed. |
| 4557 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4558 | __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] | 4559 | { |
| 4560 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4561 | struct stream_interface *si = appctx->appctx->owner; |
| 4562 | struct channel *res = si_ic(si); |
| 4563 | struct htx *htx = htx_from_buf(&res->buf); |
| 4564 | const char *data; |
| 4565 | size_t len; |
| 4566 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4567 | int max; |
| 4568 | |
Christopher Faulet | 9060fc0 | 2019-07-03 11:39:30 +0200 | [diff] [blame] | 4569 | max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx)); |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4570 | if (!max) |
| 4571 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4572 | |
| 4573 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4574 | |
| 4575 | /* Get the max amount of data which can write as input in the channel. */ |
| 4576 | if (max > (len - l)) |
| 4577 | max = len - l; |
| 4578 | |
| 4579 | /* Copy data. */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 4580 | max = htx_add_data(htx, ist2(data + l, max)); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4581 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4582 | |
| 4583 | /* update counters. */ |
| 4584 | l += max; |
| 4585 | lua_pop(L, 1); |
| 4586 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4587 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4588 | /* If some data is not send, declares the situation to the |
| 4589 | * applet, and returns a yield. |
| 4590 | */ |
| 4591 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4592 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4593 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4594 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4595 | 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] | 4596 | } |
| 4597 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4598 | htx_to_buf(htx, &res->buf); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4599 | return 1; |
| 4600 | } |
| 4601 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4602 | /* Just a wrapper of "hlua_applet_send_yield". This wrapper permits |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4603 | * yield the LUA process, and resume it without checking the |
| 4604 | * input arguments. |
| 4605 | */ |
| 4606 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 4607 | { |
| 4608 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4609 | |
| 4610 | /* We want to send some data. Headers must be sent. */ |
| 4611 | if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 4612 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 4613 | WILL_LJMP(lua_error(L)); |
| 4614 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4615 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4616 | /* This integer is used for followinf the amount of data sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4617 | lua_pushinteger(L, 0); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4618 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4619 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4620 | } |
| 4621 | |
| 4622 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 4623 | { |
| 4624 | const char *name; |
| 4625 | int ret; |
| 4626 | |
| 4627 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4628 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4629 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 4630 | |
| 4631 | /* Push in the stack the "response" entry. */ |
| 4632 | ret = lua_getfield(L, 1, "response"); |
| 4633 | if (ret != LUA_TTABLE) { |
| 4634 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 4635 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 4636 | WILL_LJMP(lua_error(L)); |
| 4637 | } |
| 4638 | |
| 4639 | /* check if the header is already registered if it is not |
| 4640 | * the case, register it. |
| 4641 | */ |
| 4642 | ret = lua_getfield(L, -1, name); |
| 4643 | if (ret == LUA_TNIL) { |
| 4644 | |
| 4645 | /* Entry not found. */ |
| 4646 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 4647 | |
| 4648 | /* Insert the new header name in the array in the top of the stack. |
| 4649 | * It left the new array in the top of the stack. |
| 4650 | */ |
| 4651 | lua_newtable(L); |
| 4652 | lua_pushvalue(L, 2); |
| 4653 | lua_pushvalue(L, -2); |
| 4654 | lua_settable(L, -4); |
| 4655 | |
| 4656 | } else if (ret != LUA_TTABLE) { |
| 4657 | |
| 4658 | /* corruption error. */ |
| 4659 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 4660 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 4661 | WILL_LJMP(lua_error(L)); |
| 4662 | } |
| 4663 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 4664 | /* Now the top of thestack is an array of values. We push |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4665 | * the header value as new entry. |
| 4666 | */ |
| 4667 | lua_pushvalue(L, 3); |
| 4668 | ret = lua_rawlen(L, -2); |
| 4669 | lua_rawseti(L, -2, ret + 1); |
| 4670 | lua_pushboolean(L, 1); |
| 4671 | return 1; |
| 4672 | } |
| 4673 | |
| 4674 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 4675 | { |
| 4676 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4677 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4678 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4679 | |
| 4680 | if (status < 100 || status > 599) { |
| 4681 | lua_pushboolean(L, 0); |
| 4682 | return 1; |
| 4683 | } |
| 4684 | |
| 4685 | appctx->appctx->ctx.hlua_apphttp.status = status; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4686 | appctx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4687 | lua_pushboolean(L, 1); |
| 4688 | return 1; |
| 4689 | } |
| 4690 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4691 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4692 | __LJMP static int hlua_applet_http_send_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4693 | { |
| 4694 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4695 | struct stream_interface *si = appctx->appctx->owner; |
| 4696 | struct channel *res = si_ic(si); |
| 4697 | struct htx *htx; |
| 4698 | struct htx_sl *sl; |
| 4699 | struct h1m h1m; |
| 4700 | const char *status, *reason; |
| 4701 | const char *name, *value; |
| 4702 | size_t nlen, vlen; |
| 4703 | unsigned int flags; |
| 4704 | |
| 4705 | /* Send the message at once. */ |
| 4706 | htx = htx_from_buf(&res->buf); |
| 4707 | h1m_init_res(&h1m); |
| 4708 | |
| 4709 | /* Use the same http version than the request. */ |
| 4710 | status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size); |
| 4711 | reason = appctx->appctx->ctx.hlua_apphttp.reason; |
| 4712 | if (reason == NULL) |
| 4713 | reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status); |
| 4714 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) { |
| 4715 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 4716 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 4717 | } |
| 4718 | else { |
| 4719 | flags = HTX_SL_F_IS_RESP; |
| 4720 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 4721 | } |
| 4722 | if (!sl) { |
| 4723 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 4724 | appctx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4725 | WILL_LJMP(lua_error(L)); |
| 4726 | } |
| 4727 | sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status; |
| 4728 | |
| 4729 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 4730 | lua_pushvalue(L, 0); |
| 4731 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 4732 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 4733 | appctx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4734 | WILL_LJMP(lua_error(L)); |
| 4735 | } |
| 4736 | |
| 4737 | /* Browse the list of headers. */ |
| 4738 | lua_pushnil(L); |
| 4739 | while(lua_next(L, -2) != 0) { |
| 4740 | /* We expect a string as -2. */ |
| 4741 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 4742 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 4743 | appctx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4744 | lua_typename(L, lua_type(L, -2))); |
| 4745 | WILL_LJMP(lua_error(L)); |
| 4746 | } |
| 4747 | name = lua_tolstring(L, -2, &nlen); |
| 4748 | |
| 4749 | /* We expect an array as -1. */ |
| 4750 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 4751 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 4752 | appctx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4753 | name, |
| 4754 | lua_typename(L, lua_type(L, -1))); |
| 4755 | WILL_LJMP(lua_error(L)); |
| 4756 | } |
| 4757 | |
| 4758 | /* Browse the table who is on the top of the stack. */ |
| 4759 | lua_pushnil(L); |
| 4760 | while(lua_next(L, -2) != 0) { |
| 4761 | int id; |
| 4762 | |
| 4763 | /* We expect a number as -2. */ |
| 4764 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 4765 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 4766 | appctx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4767 | name, |
| 4768 | lua_typename(L, lua_type(L, -2))); |
| 4769 | WILL_LJMP(lua_error(L)); |
| 4770 | } |
| 4771 | id = lua_tointeger(L, -2); |
| 4772 | |
| 4773 | /* We expect a string as -2. */ |
| 4774 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 4775 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 4776 | appctx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4777 | name, id, |
| 4778 | lua_typename(L, lua_type(L, -1))); |
| 4779 | WILL_LJMP(lua_error(L)); |
| 4780 | } |
| 4781 | value = lua_tolstring(L, -1, &vlen); |
| 4782 | |
| 4783 | /* Simple Protocol checks. */ |
| 4784 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 4785 | h1_parse_xfer_enc_header(&h1m, ist2(value, vlen)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4786 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 4787 | struct ist v = ist2(value, vlen); |
| 4788 | int ret; |
| 4789 | |
| 4790 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 4791 | if (ret < 0) { |
| 4792 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 4793 | appctx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4794 | name); |
| 4795 | WILL_LJMP(lua_error(L)); |
| 4796 | } |
| 4797 | else if (ret == 0) |
| 4798 | goto next; /* Skip it */ |
| 4799 | } |
| 4800 | |
| 4801 | /* Add a new header */ |
| 4802 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 4803 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 4804 | appctx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4805 | name); |
| 4806 | WILL_LJMP(lua_error(L)); |
| 4807 | } |
| 4808 | next: |
| 4809 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4810 | lua_pop(L, 1); |
| 4811 | } |
| 4812 | |
| 4813 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4814 | lua_pop(L, 1); |
| 4815 | } |
| 4816 | |
| 4817 | if (h1m.flags & H1_MF_CHNK) |
| 4818 | h1m.flags &= ~H1_MF_CLEN; |
| 4819 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 4820 | h1m.flags |= H1_MF_XFER_LEN; |
| 4821 | |
| 4822 | /* Uset HTX start-line flags */ |
| 4823 | if (h1m.flags & H1_MF_XFER_ENC) |
| 4824 | flags |= HTX_SL_F_XFER_ENC; |
| 4825 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 4826 | flags |= HTX_SL_F_XFER_LEN; |
| 4827 | if (h1m.flags & H1_MF_CHNK) |
| 4828 | flags |= HTX_SL_F_CHNK; |
| 4829 | else if (h1m.flags & H1_MF_CLEN) |
| 4830 | flags |= HTX_SL_F_CLEN; |
| 4831 | if (h1m.body_len == 0) |
| 4832 | flags |= HTX_SL_F_BODYLESS; |
| 4833 | } |
| 4834 | sl->flags |= flags; |
| 4835 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 4836 | /* 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] | 4837 | * and the status code implies the presence of a message body, we must |
| 4838 | * announce a transfer encoding chunked. This is required by haproxy |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4839 | * for the keepalive compliance. If the applet announces a transfer-encoding |
| 4840 | * chunked itself, don't do anything. |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4841 | */ |
| 4842 | if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 && |
| 4843 | appctx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 4844 | appctx->appctx->ctx.hlua_apphttp.status != 204 && |
| 4845 | appctx->appctx->ctx.hlua_apphttp.status != 304) { |
| 4846 | /* Add a new header */ |
| 4847 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 4848 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 4849 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 4850 | appctx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4851 | WILL_LJMP(lua_error(L)); |
| 4852 | } |
| 4853 | } |
| 4854 | |
| 4855 | /* Finalize headers. */ |
| 4856 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 4857 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 4858 | appctx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4859 | WILL_LJMP(lua_error(L)); |
| 4860 | } |
| 4861 | |
| 4862 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 4863 | b_reset(&res->buf); |
| 4864 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 4865 | WILL_LJMP(lua_error(L)); |
| 4866 | } |
| 4867 | |
| 4868 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4869 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4870 | |
| 4871 | /* Headers sent, set the flag. */ |
| 4872 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
| 4873 | return 0; |
| 4874 | |
| 4875 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4876 | /* We will build the status line and the headers of the HTTP response. |
| 4877 | * We will try send at once if its not possible, we give back the hand |
| 4878 | * waiting for more room. |
| 4879 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4880 | __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] | 4881 | { |
| 4882 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4883 | struct stream_interface *si = appctx->appctx->owner; |
| 4884 | struct channel *res = si_ic(si); |
| 4885 | |
| 4886 | if (co_data(res)) { |
| 4887 | si_rx_room_blk(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4888 | 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] | 4889 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4890 | return MAY_LJMP(hlua_applet_http_send_response(L)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4891 | } |
| 4892 | |
| 4893 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4894 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4895 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4896 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4897 | } |
| 4898 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4899 | /* |
| 4900 | * |
| 4901 | * |
| 4902 | * Class HTTP |
| 4903 | * |
| 4904 | * |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4905 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4906 | |
| 4907 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4908 | * a class stream, otherwise it throws an error. |
| 4909 | */ |
| 4910 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4911 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4912 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
| 4913 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4914 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4915 | /* This function creates and push in the stack a HTTP object |
| 4916 | * according with a current TXN. |
| 4917 | */ |
| 4918 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 4919 | { |
| 4920 | struct hlua_txn *htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4921 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4922 | /* Check stack size. */ |
| 4923 | if (!lua_checkstack(L, 3)) |
| 4924 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4925 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4926 | /* Create the object: obj[0] = userdata. |
| 4927 | * Note that the base of the Converters object is the |
| 4928 | * same than the TXN object. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4929 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4930 | lua_newtable(L); |
| 4931 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 4932 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4933 | |
| 4934 | htxn->s = txn->s; |
| 4935 | htxn->p = txn->p; |
Christopher Faulet | 256b69a | 2019-05-23 11:14:21 +0200 | [diff] [blame] | 4936 | htxn->dir = txn->dir; |
| 4937 | htxn->flags = txn->flags; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4938 | |
| 4939 | /* Pop a class stream metatable and affect it to the table. */ |
| 4940 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 4941 | lua_setmetatable(L, -2); |
| 4942 | |
| 4943 | return 1; |
| 4944 | } |
| 4945 | |
| 4946 | /* This function creates ans returns an array of HTTP headers. |
| 4947 | * This function does not fails. It is used as wrapper with the |
| 4948 | * 2 following functions. |
| 4949 | */ |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4950 | __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] | 4951 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4952 | struct htx *htx; |
| 4953 | int32_t pos; |
| 4954 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4955 | /* Create the table. */ |
| 4956 | lua_newtable(L); |
| 4957 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4958 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4959 | htx = htxbuf(&msg->chn->buf); |
| 4960 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4961 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4962 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4963 | struct ist n, v; |
| 4964 | int len; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4965 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4966 | if (type == HTX_BLK_HDR) { |
| 4967 | n = htx_get_blk_name(htx,blk); |
| 4968 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4969 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4970 | else if (type == HTX_BLK_EOH) |
| 4971 | break; |
| 4972 | else |
| 4973 | continue; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4974 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4975 | /* Check for existing entry: |
| 4976 | * assume that the table is on the top of the stack, and |
| 4977 | * push the key in the stack, the function lua_gettable() |
| 4978 | * perform the lookup. |
| 4979 | */ |
| 4980 | lua_pushlstring(L, n.ptr, n.len); |
| 4981 | lua_gettable(L, -2); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4982 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4983 | switch (lua_type(L, -1)) { |
| 4984 | case LUA_TNIL: |
| 4985 | /* Table not found, create it. */ |
| 4986 | lua_pop(L, 1); /* remove the nil value. */ |
| 4987 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 4988 | lua_newtable(L); /* create and push empty table. */ |
| 4989 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 4990 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 4991 | 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] | 4992 | break; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4993 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4994 | case LUA_TTABLE: |
| 4995 | /* Entry found: push the value in the table. */ |
| 4996 | len = lua_rawlen(L, -1); |
| 4997 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 4998 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 4999 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 5000 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5001 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5002 | default: |
| 5003 | /* Other cases are errors. */ |
| 5004 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 5005 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5006 | } |
| 5007 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5008 | return 1; |
| 5009 | } |
| 5010 | |
| 5011 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 5012 | { |
| 5013 | struct hlua_txn *htxn; |
| 5014 | |
| 5015 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 5016 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5017 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5018 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5019 | WILL_LJMP(lua_error(L)); |
| 5020 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5021 | return hlua_http_get_headers(L, &htxn->s->txn->req); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5022 | } |
| 5023 | |
| 5024 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 5025 | { |
| 5026 | struct hlua_txn *htxn; |
| 5027 | |
| 5028 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 5029 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5030 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5031 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5032 | WILL_LJMP(lua_error(L)); |
| 5033 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5034 | return hlua_http_get_headers(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5035 | } |
| 5036 | |
| 5037 | /* This function replace full header, or just a value in |
| 5038 | * the request or in the response. It is a wrapper fir the |
| 5039 | * 4 following functions. |
| 5040 | */ |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5041 | __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] | 5042 | { |
| 5043 | size_t name_len; |
| 5044 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5045 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5046 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5047 | struct htx *htx; |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5048 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5049 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5050 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5051 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 5052 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5053 | htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5054 | 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] | 5055 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5056 | return 0; |
| 5057 | } |
| 5058 | |
| 5059 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 5060 | { |
| 5061 | struct hlua_txn *htxn; |
| 5062 | |
| 5063 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5064 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5065 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5066 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5067 | WILL_LJMP(lua_error(L)); |
| 5068 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5069 | 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] | 5070 | } |
| 5071 | |
| 5072 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 5073 | { |
| 5074 | struct hlua_txn *htxn; |
| 5075 | |
| 5076 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 5077 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5078 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5079 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5080 | WILL_LJMP(lua_error(L)); |
| 5081 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5082 | 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] | 5083 | } |
| 5084 | |
| 5085 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 5086 | { |
| 5087 | struct hlua_txn *htxn; |
| 5088 | |
| 5089 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5090 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5091 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5092 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5093 | WILL_LJMP(lua_error(L)); |
| 5094 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5095 | 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] | 5096 | } |
| 5097 | |
| 5098 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 5099 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5100 | struct hlua_txn *htxn; |
| 5101 | |
| 5102 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 5103 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5104 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5105 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5106 | WILL_LJMP(lua_error(L)); |
| 5107 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5108 | 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] | 5109 | } |
| 5110 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5111 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5112 | * It is a wrapper for the 2 following functions. |
| 5113 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5114 | __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] | 5115 | { |
| 5116 | size_t len; |
| 5117 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5118 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5119 | struct http_hdr_ctx ctx; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5120 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5121 | ctx.blk = NULL; |
| 5122 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 5123 | http_remove_header(htx, &ctx); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5124 | return 0; |
| 5125 | } |
| 5126 | |
| 5127 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 5128 | { |
| 5129 | struct hlua_txn *htxn; |
| 5130 | |
| 5131 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 5132 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5133 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5134 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5135 | WILL_LJMP(lua_error(L)); |
| 5136 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5137 | return hlua_http_del_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5138 | } |
| 5139 | |
| 5140 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 5141 | { |
| 5142 | struct hlua_txn *htxn; |
| 5143 | |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5144 | MAY_LJMP(check_args(L, 2, "res_del_hdr")); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5145 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5146 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5147 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5148 | WILL_LJMP(lua_error(L)); |
| 5149 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5150 | return hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5151 | } |
| 5152 | |
| 5153 | /* This function adds an header. It is a wrapper used by |
| 5154 | * the 2 following functions. |
| 5155 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5156 | __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] | 5157 | { |
| 5158 | size_t name_len; |
| 5159 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5160 | size_t value_len; |
| 5161 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5162 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5163 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5164 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 5165 | ist2(value, value_len))); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5166 | return 0; |
| 5167 | } |
| 5168 | |
| 5169 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 5170 | { |
| 5171 | struct hlua_txn *htxn; |
| 5172 | |
| 5173 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 5174 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5175 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5176 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5177 | WILL_LJMP(lua_error(L)); |
| 5178 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5179 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5180 | } |
| 5181 | |
| 5182 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 5183 | { |
| 5184 | struct hlua_txn *htxn; |
| 5185 | |
| 5186 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 5187 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5188 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5189 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5190 | WILL_LJMP(lua_error(L)); |
| 5191 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5192 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5193 | } |
| 5194 | |
| 5195 | static int hlua_http_req_set_hdr(lua_State *L) |
| 5196 | { |
| 5197 | struct hlua_txn *htxn; |
| 5198 | |
| 5199 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 5200 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5201 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5202 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5203 | WILL_LJMP(lua_error(L)); |
| 5204 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5205 | hlua_http_del_hdr(L, &htxn->s->txn->req); |
| 5206 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5207 | } |
| 5208 | |
| 5209 | static int hlua_http_res_set_hdr(lua_State *L) |
| 5210 | { |
| 5211 | struct hlua_txn *htxn; |
| 5212 | |
| 5213 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 5214 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5215 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5216 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5217 | WILL_LJMP(lua_error(L)); |
| 5218 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5219 | hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
| 5220 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5221 | } |
| 5222 | |
| 5223 | /* This function set the method. */ |
| 5224 | static int hlua_http_req_set_meth(lua_State *L) |
| 5225 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5226 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5227 | size_t name_len; |
| 5228 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5229 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5230 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5231 | WILL_LJMP(lua_error(L)); |
| 5232 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5233 | 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] | 5234 | return 1; |
| 5235 | } |
| 5236 | |
| 5237 | /* This function set the method. */ |
| 5238 | static int hlua_http_req_set_path(lua_State *L) |
| 5239 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5240 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5241 | size_t name_len; |
| 5242 | 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] | 5243 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5244 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5245 | WILL_LJMP(lua_error(L)); |
| 5246 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5247 | 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] | 5248 | return 1; |
| 5249 | } |
| 5250 | |
| 5251 | /* This function set the query-string. */ |
| 5252 | static int hlua_http_req_set_query(lua_State *L) |
| 5253 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5254 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5255 | size_t name_len; |
| 5256 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5257 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5258 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5259 | WILL_LJMP(lua_error(L)); |
| 5260 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5261 | /* Check length. */ |
| 5262 | if (name_len > trash.size - 1) { |
| 5263 | lua_pushboolean(L, 0); |
| 5264 | return 1; |
| 5265 | } |
| 5266 | |
| 5267 | /* Add the mark question as prefix. */ |
| 5268 | chunk_reset(&trash); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5269 | trash.area[trash.data++] = '?'; |
| 5270 | memcpy(trash.area + trash.data, name, name_len); |
| 5271 | trash.data += name_len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5272 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5273 | lua_pushboolean(L, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5274 | 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] | 5275 | return 1; |
| 5276 | } |
| 5277 | |
| 5278 | /* This function set the uri. */ |
| 5279 | static int hlua_http_req_set_uri(lua_State *L) |
| 5280 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5281 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5282 | size_t name_len; |
| 5283 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5284 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5285 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5286 | WILL_LJMP(lua_error(L)); |
| 5287 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5288 | 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] | 5289 | return 1; |
| 5290 | } |
| 5291 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5292 | /* This function set the response code & optionally reason. */ |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5293 | static int hlua_http_res_set_status(lua_State *L) |
| 5294 | { |
| 5295 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5296 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 5297 | const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 5298 | const struct ist reason = ist2(str, (str ? strlen(str) : 0)); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5299 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5300 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5301 | WILL_LJMP(lua_error(L)); |
| 5302 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5303 | http_res_set_status(code, reason, htxn->s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5304 | return 0; |
| 5305 | } |
| 5306 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5307 | /* |
| 5308 | * |
| 5309 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5310 | * Class TXN |
| 5311 | * |
| 5312 | * |
| 5313 | */ |
| 5314 | |
| 5315 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5316 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5317 | */ |
| 5318 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 5319 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5320 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5321 | } |
| 5322 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5323 | __LJMP static int hlua_set_var(lua_State *L) |
| 5324 | { |
| 5325 | struct hlua_txn *htxn; |
| 5326 | const char *name; |
| 5327 | size_t len; |
| 5328 | struct sample smp; |
| 5329 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 5330 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 5331 | 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] | 5332 | |
| 5333 | /* It is useles to retrieve the stream, but this function |
| 5334 | * runs only in a stream context. |
| 5335 | */ |
| 5336 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5337 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5338 | |
| 5339 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 5340 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5341 | hlua_lua2smp(L, 3, &smp); |
| 5342 | |
| 5343 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5344 | 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] | 5345 | |
| 5346 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 5347 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 5348 | else |
| 5349 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 5350 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 5351 | return 1; |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5352 | } |
| 5353 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5354 | __LJMP static int hlua_unset_var(lua_State *L) |
| 5355 | { |
| 5356 | struct hlua_txn *htxn; |
| 5357 | const char *name; |
| 5358 | size_t len; |
| 5359 | struct sample smp; |
| 5360 | |
| 5361 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 5362 | |
| 5363 | /* It is useles to retrieve the stream, but this function |
| 5364 | * runs only in a stream context. |
| 5365 | */ |
| 5366 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5367 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5368 | |
| 5369 | /* Unset the variable. */ |
| 5370 | 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] | 5371 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 5372 | return 1; |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5373 | } |
| 5374 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5375 | __LJMP static int hlua_get_var(lua_State *L) |
| 5376 | { |
| 5377 | struct hlua_txn *htxn; |
| 5378 | const char *name; |
| 5379 | size_t len; |
| 5380 | struct sample smp; |
| 5381 | |
| 5382 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 5383 | |
| 5384 | /* It is useles to retrieve the stream, but this function |
| 5385 | * runs only in a stream context. |
| 5386 | */ |
| 5387 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5388 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5389 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5390 | 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] | 5391 | if (!vars_get_by_name(name, len, &smp)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5392 | lua_pushnil(L); |
| 5393 | return 1; |
| 5394 | } |
| 5395 | |
| 5396 | return hlua_smp2lua(L, &smp); |
| 5397 | } |
| 5398 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5399 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5400 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5401 | struct hlua *hlua; |
| 5402 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5403 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 5404 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5405 | /* It is useles to retrieve the stream, but this function |
| 5406 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5407 | */ |
| 5408 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5409 | |
| 5410 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5411 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5412 | if (!hlua) |
| 5413 | return 0; |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5414 | |
| 5415 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5416 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5417 | |
| 5418 | /* Get and store new value. */ |
| 5419 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5420 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5421 | |
| 5422 | return 0; |
| 5423 | } |
| 5424 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5425 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5426 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5427 | struct hlua *hlua; |
| 5428 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5429 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 5430 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5431 | /* It is useles to retrieve the stream, but this function |
| 5432 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5433 | */ |
| 5434 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5435 | |
| 5436 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5437 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5438 | if (!hlua) { |
| 5439 | lua_pushnil(L); |
| 5440 | return 1; |
| 5441 | } |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5442 | |
| 5443 | /* Push configuration index in the stack. */ |
| 5444 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5445 | |
| 5446 | return 1; |
| 5447 | } |
| 5448 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5449 | /* Create stack entry containing a class TXN. This function |
| 5450 | * return 0 if the stack does not contains free slots, |
| 5451 | * otherwise it returns 1. |
| 5452 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5453 | 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] | 5454 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5455 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5456 | |
| 5457 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5458 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5459 | return 0; |
| 5460 | |
| 5461 | /* NOTE: The allocation never fails. The failure |
| 5462 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5463 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5464 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5465 | /* Create the object: obj[0] = userdata. */ |
| 5466 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5467 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5468 | lua_rawseti(L, -2, 0); |
| 5469 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5470 | htxn->s = s; |
| 5471 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 5472 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5473 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5474 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5475 | /* Create the "f" field that contains a list of fetches. */ |
| 5476 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5477 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5478 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5479 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5480 | |
| 5481 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 5482 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5483 | 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] | 5484 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5485 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5486 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5487 | /* Create the "c" field that contains a list of converters. */ |
| 5488 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5489 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5490 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5491 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5492 | |
| 5493 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 5494 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 5495 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5496 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5497 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5498 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5499 | /* Create the "req" field that contains the request channel object. */ |
| 5500 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5501 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5502 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5503 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5504 | |
| 5505 | /* Create the "res" field that contains the response channel object. */ |
| 5506 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5507 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5508 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5509 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5510 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5511 | /* Creates the HTTP object is the current proxy allows http. */ |
| 5512 | lua_pushstring(L, "http"); |
| 5513 | if (p->mode == PR_MODE_HTTP) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5514 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5515 | return 0; |
| 5516 | } |
| 5517 | else |
| 5518 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5519 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5520 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5521 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5522 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 5523 | lua_setmetatable(L, -2); |
| 5524 | |
| 5525 | return 1; |
| 5526 | } |
| 5527 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5528 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 5529 | { |
| 5530 | const char *msg; |
| 5531 | struct hlua_txn *htxn; |
| 5532 | |
| 5533 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 5534 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5535 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5536 | |
| 5537 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 5538 | return 0; |
| 5539 | } |
| 5540 | |
| 5541 | __LJMP static int hlua_txn_log(lua_State *L) |
| 5542 | { |
| 5543 | int level; |
| 5544 | const char *msg; |
| 5545 | struct hlua_txn *htxn; |
| 5546 | |
| 5547 | MAY_LJMP(check_args(L, 3, "log")); |
| 5548 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5549 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5550 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5551 | |
| 5552 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5553 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5554 | |
| 5555 | hlua_sendlog(htxn->s->be, level, msg); |
| 5556 | return 0; |
| 5557 | } |
| 5558 | |
| 5559 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 5560 | { |
| 5561 | const char *msg; |
| 5562 | struct hlua_txn *htxn; |
| 5563 | |
| 5564 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 5565 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5566 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5567 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 5568 | return 0; |
| 5569 | } |
| 5570 | |
| 5571 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 5572 | { |
| 5573 | const char *msg; |
| 5574 | struct hlua_txn *htxn; |
| 5575 | |
| 5576 | MAY_LJMP(check_args(L, 2, "Info")); |
| 5577 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5578 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5579 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 5580 | return 0; |
| 5581 | } |
| 5582 | |
| 5583 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 5584 | { |
| 5585 | const char *msg; |
| 5586 | struct hlua_txn *htxn; |
| 5587 | |
| 5588 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 5589 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5590 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5591 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 5592 | return 0; |
| 5593 | } |
| 5594 | |
| 5595 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 5596 | { |
| 5597 | const char *msg; |
| 5598 | struct hlua_txn *htxn; |
| 5599 | |
| 5600 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 5601 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5602 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5603 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 5604 | return 0; |
| 5605 | } |
| 5606 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5607 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 5608 | { |
| 5609 | struct hlua_txn *htxn; |
| 5610 | int ll; |
| 5611 | |
| 5612 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 5613 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5614 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5615 | |
| 5616 | if (ll < 0 || ll > 7) |
| 5617 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 5618 | |
| 5619 | htxn->s->logs.level = ll; |
| 5620 | return 0; |
| 5621 | } |
| 5622 | |
| 5623 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 5624 | { |
| 5625 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5626 | int tos; |
| 5627 | |
| 5628 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 5629 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5630 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5631 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 5632 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5633 | return 0; |
| 5634 | } |
| 5635 | |
| 5636 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 5637 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5638 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5639 | int mark; |
| 5640 | |
| 5641 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 5642 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5643 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5644 | |
Lukas Tribus | 579e3e3 | 2019-08-11 18:03:45 +0200 | [diff] [blame] | 5645 | conn_set_mark(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5646 | return 0; |
| 5647 | } |
| 5648 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 5649 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 5650 | { |
| 5651 | struct hlua_txn *htxn; |
| 5652 | |
| 5653 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 5654 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5655 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5656 | return 0; |
| 5657 | } |
| 5658 | |
| 5659 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 5660 | { |
| 5661 | struct hlua_txn *htxn; |
| 5662 | |
| 5663 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 5664 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5665 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5666 | return 0; |
| 5667 | } |
| 5668 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5669 | /* 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] | 5670 | * 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] | 5671 | * message and terminate the transaction. It returns 1 on success and 0 on |
| 5672 | * error. The Reply must be on top of the stack. |
| 5673 | */ |
| 5674 | __LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s) |
| 5675 | { |
| 5676 | struct htx *htx; |
| 5677 | struct htx_sl *sl; |
| 5678 | struct h1m h1m; |
| 5679 | const char *status, *reason, *body; |
| 5680 | size_t status_len, reason_len, body_len; |
| 5681 | int ret, code, flags; |
| 5682 | |
| 5683 | code = 200; |
| 5684 | status = "200"; |
| 5685 | status_len = 3; |
| 5686 | ret = lua_getfield(L, -1, "status"); |
| 5687 | if (ret == LUA_TNUMBER) { |
| 5688 | code = lua_tointeger(L, -1); |
| 5689 | status = lua_tolstring(L, -1, &status_len); |
| 5690 | } |
| 5691 | lua_pop(L, 1); |
| 5692 | |
| 5693 | reason = http_get_reason(code); |
| 5694 | reason_len = strlen(reason); |
| 5695 | ret = lua_getfield(L, -1, "reason"); |
| 5696 | if (ret == LUA_TSTRING) |
| 5697 | reason = lua_tolstring(L, -1, &reason_len); |
| 5698 | lua_pop(L, 1); |
| 5699 | |
| 5700 | body = NULL; |
| 5701 | body_len = 0; |
| 5702 | ret = lua_getfield(L, -1, "body"); |
| 5703 | if (ret == LUA_TSTRING) |
| 5704 | body = lua_tolstring(L, -1, &body_len); |
| 5705 | lua_pop(L, 1); |
| 5706 | |
| 5707 | /* Prepare the response before inserting the headers */ |
| 5708 | h1m_init_res(&h1m); |
| 5709 | htx = htx_from_buf(&s->res.buf); |
| 5710 | channel_htx_truncate(&s->res, htx); |
| 5711 | if (s->txn->req.flags & HTTP_MSGF_VER_11) { |
| 5712 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 5713 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), |
| 5714 | ist2(status, status_len), ist2(reason, reason_len)); |
| 5715 | } |
| 5716 | else { |
| 5717 | flags = HTX_SL_F_IS_RESP; |
| 5718 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), |
| 5719 | ist2(status, status_len), ist2(reason, reason_len)); |
| 5720 | } |
| 5721 | if (!sl) |
| 5722 | goto fail; |
| 5723 | sl->info.res.status = code; |
| 5724 | |
| 5725 | /* Push in the stack the "headers" entry. */ |
| 5726 | ret = lua_getfield(L, -1, "headers"); |
| 5727 | if (ret != LUA_TTABLE) |
| 5728 | goto skip_headers; |
| 5729 | |
| 5730 | lua_pushnil(L); |
| 5731 | while (lua_next(L, -2) != 0) { |
| 5732 | struct ist name, value; |
| 5733 | const char *n, *v; |
| 5734 | size_t nlen, vlen; |
| 5735 | |
| 5736 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 5737 | /* Skip element if the key is not a string or if the value is not a table */ |
| 5738 | goto next_hdr; |
| 5739 | } |
| 5740 | |
| 5741 | n = lua_tolstring(L, -2, &nlen); |
| 5742 | name = ist2(n, nlen); |
| 5743 | if (isteqi(name, ist("content-length"))) { |
| 5744 | /* Always skip content-length header. It will be added |
| 5745 | * later with the correct len |
| 5746 | */ |
| 5747 | goto next_hdr; |
| 5748 | } |
| 5749 | |
| 5750 | /* Loop on header's values */ |
| 5751 | lua_pushnil(L); |
| 5752 | while (lua_next(L, -2)) { |
| 5753 | if (!lua_isstring(L, -1)) { |
| 5754 | /* Skip the value if it is not a string */ |
| 5755 | goto next_value; |
| 5756 | } |
| 5757 | |
| 5758 | v = lua_tolstring(L, -1, &vlen); |
| 5759 | value = ist2(v, vlen); |
| 5760 | |
| 5761 | if (isteqi(name, ist("transfer-encoding"))) |
| 5762 | h1_parse_xfer_enc_header(&h1m, value); |
| 5763 | if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen))) |
| 5764 | goto fail; |
| 5765 | |
| 5766 | next_value: |
| 5767 | lua_pop(L, 1); |
| 5768 | } |
| 5769 | |
| 5770 | next_hdr: |
| 5771 | lua_pop(L, 1); |
| 5772 | } |
| 5773 | skip_headers: |
| 5774 | lua_pop(L, 1); |
| 5775 | |
| 5776 | /* Update h1m flags: CLEN is set if CHNK is not present */ |
| 5777 | if (!(h1m.flags & H1_MF_CHNK)) { |
| 5778 | const char *clen = ultoa(body_len); |
| 5779 | |
| 5780 | h1m.flags |= H1_MF_CLEN; |
| 5781 | if (!htx_add_header(htx, ist("content-length"), ist(clen))) |
| 5782 | goto fail; |
| 5783 | } |
| 5784 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 5785 | h1m.flags |= H1_MF_XFER_LEN; |
| 5786 | |
| 5787 | /* Update HTX start-line flags */ |
| 5788 | if (h1m.flags & H1_MF_XFER_ENC) |
| 5789 | flags |= HTX_SL_F_XFER_ENC; |
| 5790 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 5791 | flags |= HTX_SL_F_XFER_LEN; |
| 5792 | if (h1m.flags & H1_MF_CHNK) |
| 5793 | flags |= HTX_SL_F_CHNK; |
| 5794 | else if (h1m.flags & H1_MF_CLEN) |
| 5795 | flags |= HTX_SL_F_CLEN; |
| 5796 | if (h1m.body_len == 0) |
| 5797 | flags |= HTX_SL_F_BODYLESS; |
| 5798 | } |
| 5799 | sl->flags |= flags; |
| 5800 | |
| 5801 | |
| 5802 | if (!htx_add_endof(htx, HTX_BLK_EOH) || |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5803 | (body_len && !htx_add_data_atonce(htx, ist2(body, body_len)))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5804 | goto fail; |
| 5805 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5806 | htx->flags |= HTX_FL_EOM; |
| 5807 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5808 | /* Now, forward the response and terminate the transaction */ |
| 5809 | s->txn->status = code; |
| 5810 | htx_to_buf(htx, &s->res.buf); |
| 5811 | if (!http_forward_proxy_resp(s, 1)) |
| 5812 | goto fail; |
| 5813 | |
| 5814 | return 1; |
| 5815 | |
| 5816 | fail: |
| 5817 | channel_htx_truncate(&s->res, htx); |
| 5818 | return 0; |
| 5819 | } |
| 5820 | |
| 5821 | /* Terminate a transaction if called from a lua action. For TCP streams, |
| 5822 | * processing is just aborted. Nothing is returned to the client and all |
| 5823 | * arguments are ignored. For HTTP streams, if a reply is passed as argument, it |
| 5824 | * is forwarded to the client before terminating the transaction. On success, |
| 5825 | * the function exits with ACT_RET_DONE code. If an error occurred, it exits |
| 5826 | * with ACT_RET_ERR code. If this function is not called from a lua action, it |
| 5827 | * just exits without any processing. |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5828 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5829 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5830 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5831 | struct hlua_txn *htxn; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5832 | struct stream *s; |
| 5833 | int finst; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5834 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5835 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5836 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5837 | /* If the flags NOTERM is set, we cannot terminate the session, so we |
| 5838 | * just end the execution of the current lua code. */ |
| 5839 | if (htxn->flags & HLUA_TXN_NOTERM) |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5840 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5841 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5842 | s = htxn->s; |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5843 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5844 | struct channel *req = &s->req; |
| 5845 | struct channel *res = &s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5846 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5847 | channel_auto_read(req); |
| 5848 | channel_abort(req); |
| 5849 | channel_auto_close(req); |
| 5850 | channel_erase(req); |
| 5851 | |
| 5852 | res->wex = tick_add_ifset(now_ms, res->wto); |
| 5853 | channel_auto_read(res); |
| 5854 | channel_auto_close(res); |
| 5855 | channel_shutr_now(res); |
| 5856 | |
| 5857 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D); |
| 5858 | goto done; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5859 | } |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 5860 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5861 | if (lua_gettop(L) == 1 || !lua_istable(L, 2)) { |
| 5862 | /* No reply or invalid reply */ |
| 5863 | s->txn->status = 0; |
| 5864 | http_reply_and_close(s, 0, NULL); |
| 5865 | } |
| 5866 | else { |
| 5867 | /* Remove extra args to have the reply on top of the stack */ |
| 5868 | if (lua_gettop(L) > 2) |
| 5869 | lua_pop(L, lua_gettop(L) - 2); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5870 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5871 | if (!hlua_txn_forward_reply(L, s)) { |
| 5872 | if (!(s->flags & SF_ERR_MASK)) |
| 5873 | s->flags |= SF_ERR_PRXCOND; |
| 5874 | lua_pushinteger(L, ACT_RET_ERR); |
| 5875 | WILL_LJMP(hlua_done(L)); |
| 5876 | return 0; /* Never reached */ |
| 5877 | } |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 5878 | } |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5879 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5880 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H); |
| 5881 | if (htxn->dir == SMP_OPT_DIR_REQ) { |
| 5882 | /* let's log the request time */ |
| 5883 | s->logs.tv_request = now; |
| 5884 | if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */ |
| 5885 | _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1); |
| 5886 | } |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5887 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5888 | done: |
| 5889 | if (!(s->flags & SF_ERR_MASK)) |
| 5890 | s->flags |= SF_ERR_LOCAL; |
| 5891 | if (!(s->flags & SF_FINST_MASK)) |
| 5892 | s->flags |= finst; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5893 | |
Christopher Faulet | 4ad7310 | 2020-03-05 11:07:31 +0100 | [diff] [blame] | 5894 | lua_pushinteger(L, ACT_RET_ABRT); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5895 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5896 | return 0; |
| 5897 | } |
| 5898 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5899 | /* |
| 5900 | * |
| 5901 | * |
| 5902 | * Class REPLY |
| 5903 | * |
| 5904 | * |
| 5905 | */ |
| 5906 | |
| 5907 | /* Pushes the TXN reply onto the top of the stack. If the stask does not have a |
| 5908 | * free slots, the function fails and returns 0; |
| 5909 | */ |
| 5910 | static int hlua_txn_reply_new(lua_State *L) |
| 5911 | { |
| 5912 | struct hlua_txn *htxn; |
| 5913 | const char *reason, *body = NULL; |
| 5914 | int ret, status; |
| 5915 | |
| 5916 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5917 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5918 | hlua_pusherror(L, "txn object is not an HTTP transaction."); |
| 5919 | WILL_LJMP(lua_error(L)); |
| 5920 | } |
| 5921 | |
| 5922 | /* Default value */ |
| 5923 | status = 200; |
| 5924 | reason = http_get_reason(status); |
| 5925 | |
| 5926 | if (lua_istable(L, 2)) { |
| 5927 | /* load status and reason from the table argument at index 2 */ |
| 5928 | ret = lua_getfield(L, 2, "status"); |
| 5929 | if (ret == LUA_TNIL) |
| 5930 | goto reason; |
| 5931 | else if (ret != LUA_TNUMBER) { |
| 5932 | /* invalid status: ignore the reason */ |
| 5933 | goto body; |
| 5934 | } |
| 5935 | status = lua_tointeger(L, -1); |
| 5936 | |
| 5937 | reason: |
| 5938 | lua_pop(L, 1); /* restore the stack: remove status */ |
| 5939 | ret = lua_getfield(L, 2, "reason"); |
| 5940 | if (ret == LUA_TSTRING) |
| 5941 | reason = lua_tostring(L, -1); |
| 5942 | |
| 5943 | body: |
| 5944 | lua_pop(L, 1); /* restore the stack: remove invalid status or reason */ |
| 5945 | ret = lua_getfield(L, 2, "body"); |
| 5946 | if (ret == LUA_TSTRING) |
| 5947 | body = lua_tostring(L, -1); |
| 5948 | lua_pop(L, 1); /* restore the stack: remove body */ |
| 5949 | } |
| 5950 | |
| 5951 | /* Create the Reply table */ |
| 5952 | lua_newtable(L); |
| 5953 | |
| 5954 | /* Add status element */ |
| 5955 | lua_pushstring(L, "status"); |
| 5956 | lua_pushinteger(L, status); |
| 5957 | lua_settable(L, -3); |
| 5958 | |
| 5959 | /* Add reason element */ |
| 5960 | reason = http_get_reason(status); |
| 5961 | lua_pushstring(L, "reason"); |
| 5962 | lua_pushstring(L, reason); |
| 5963 | lua_settable(L, -3); |
| 5964 | |
| 5965 | /* Add body element, nil if undefined */ |
| 5966 | lua_pushstring(L, "body"); |
| 5967 | if (body) |
| 5968 | lua_pushstring(L, body); |
| 5969 | else |
| 5970 | lua_pushnil(L); |
| 5971 | lua_settable(L, -3); |
| 5972 | |
| 5973 | /* Add headers element */ |
| 5974 | lua_pushstring(L, "headers"); |
| 5975 | lua_newtable(L); |
| 5976 | |
| 5977 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 5978 | if (lua_istable(L, 2)) { |
| 5979 | /* load headers from the table argument at index 2. If it is a table, copy it. */ |
| 5980 | ret = lua_getfield(L, 2, "headers"); |
| 5981 | if (ret == LUA_TTABLE) { |
| 5982 | /* stack: [ ... <headers:table>, <table> ] */ |
| 5983 | lua_pushnil(L); |
| 5984 | while (lua_next(L, -2) != 0) { |
| 5985 | /* stack: [ ... <headers:table>, <table>, k, v] */ |
| 5986 | if (!lua_isstring(L, -1) && !lua_istable(L, -1)) { |
| 5987 | /* invalid value type, skip it */ |
| 5988 | lua_pop(L, 1); |
| 5989 | continue; |
| 5990 | } |
| 5991 | |
| 5992 | |
| 5993 | /* Duplicate the key and swap it with the value. */ |
| 5994 | lua_pushvalue(L, -2); |
| 5995 | lua_insert(L, -2); |
| 5996 | /* stack: [ ... <headers:table>, <table>, k, k, v ] */ |
| 5997 | |
| 5998 | lua_newtable(L); |
| 5999 | lua_insert(L, -2); |
| 6000 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */ |
| 6001 | |
| 6002 | if (lua_isstring(L, -1)) { |
| 6003 | /* push the value in the inner table */ |
| 6004 | lua_rawseti(L, -2, 1); |
| 6005 | } |
| 6006 | else { /* table */ |
| 6007 | lua_pushnil(L); |
| 6008 | while (lua_next(L, -2) != 0) { |
| 6009 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */ |
| 6010 | if (!lua_isstring(L, -1)) { |
| 6011 | /* invalid value type, skip it*/ |
| 6012 | lua_pop(L, 1); |
| 6013 | continue; |
| 6014 | } |
| 6015 | /* push the value in the inner table */ |
| 6016 | lua_rawseti(L, -4, lua_rawlen(L, -4) + 1); |
| 6017 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */ |
| 6018 | } |
| 6019 | lua_pop(L, 1); |
| 6020 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */ |
| 6021 | } |
| 6022 | |
| 6023 | /* push (k,v) on the stack in the headers table: |
| 6024 | * stack: [ ... <headers:table>, <table>, k, k, v ] |
| 6025 | */ |
| 6026 | lua_settable(L, -5); |
| 6027 | /* stack: [ ... <headers:table>, <table>, k ] */ |
| 6028 | } |
| 6029 | } |
| 6030 | lua_pop(L, 1); |
| 6031 | } |
| 6032 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 6033 | lua_settable(L, -3); |
| 6034 | /* stack: [ txn, <Arg:table>, <Reply:table> ] */ |
| 6035 | |
| 6036 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 6037 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref); |
| 6038 | lua_setmetatable(L, -2); |
| 6039 | return 1; |
| 6040 | } |
| 6041 | |
| 6042 | /* Set the reply status code, and optionally the reason. If no reason is |
| 6043 | * provided, the default one corresponding to the status code is used. |
| 6044 | */ |
| 6045 | __LJMP static int hlua_txn_reply_set_status(lua_State *L) |
| 6046 | { |
| 6047 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6048 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 6049 | |
| 6050 | /* First argument (self) must be a table */ |
| 6051 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6052 | |
| 6053 | if (status < 100 || status > 599) { |
| 6054 | lua_pushboolean(L, 0); |
| 6055 | return 1; |
| 6056 | } |
| 6057 | if (!reason) |
| 6058 | reason = http_get_reason(status); |
| 6059 | |
| 6060 | lua_pushinteger(L, status); |
| 6061 | lua_setfield(L, 1, "status"); |
| 6062 | |
| 6063 | lua_pushstring(L, reason); |
| 6064 | lua_setfield(L, 1, "reason"); |
| 6065 | |
| 6066 | lua_pushboolean(L, 1); |
| 6067 | return 1; |
| 6068 | } |
| 6069 | |
| 6070 | /* Add a header into the reply object. Each header name is associated to an |
| 6071 | * array of values in the "headers" table. If the header name is not found, a |
| 6072 | * new entry is created. |
| 6073 | */ |
| 6074 | __LJMP static int hlua_txn_reply_add_header(lua_State *L) |
| 6075 | { |
| 6076 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6077 | const char *value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 6078 | int ret; |
| 6079 | |
| 6080 | /* First argument (self) must be a table */ |
| 6081 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6082 | |
| 6083 | /* Push in the stack the "headers" entry. */ |
| 6084 | ret = lua_getfield(L, 1, "headers"); |
| 6085 | if (ret != LUA_TTABLE) { |
| 6086 | hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret)); |
| 6087 | WILL_LJMP(lua_error(L)); |
| 6088 | } |
| 6089 | |
| 6090 | /* check if the header is already registered. If not, register it. */ |
| 6091 | ret = lua_getfield(L, -1, name); |
| 6092 | if (ret == LUA_TNIL) { |
| 6093 | /* Entry not found. */ |
| 6094 | lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */ |
| 6095 | |
| 6096 | /* Insert the new header name in the array in the top of the stack. |
| 6097 | * It left the new array in the top of the stack. |
| 6098 | */ |
| 6099 | lua_newtable(L); |
| 6100 | lua_pushstring(L, name); |
| 6101 | lua_pushvalue(L, -2); |
| 6102 | lua_settable(L, -4); |
| 6103 | } |
| 6104 | else if (ret != LUA_TTABLE) { |
| 6105 | hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret)); |
| 6106 | WILL_LJMP(lua_error(L)); |
| 6107 | } |
| 6108 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 6109 | /* Now the top of thestack is an array of values. We push |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6110 | * the header value as new entry. |
| 6111 | */ |
| 6112 | lua_pushstring(L, value); |
| 6113 | ret = lua_rawlen(L, -2); |
| 6114 | lua_rawseti(L, -2, ret + 1); |
| 6115 | |
| 6116 | lua_pushboolean(L, 1); |
| 6117 | return 1; |
| 6118 | } |
| 6119 | |
| 6120 | /* Remove all occurrences of a given header name. */ |
| 6121 | __LJMP static int hlua_txn_reply_del_header(lua_State *L) |
| 6122 | { |
| 6123 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6124 | int ret; |
| 6125 | |
| 6126 | /* First argument (self) must be a table */ |
| 6127 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6128 | |
| 6129 | /* Push in the stack the "headers" entry. */ |
| 6130 | ret = lua_getfield(L, 1, "headers"); |
| 6131 | if (ret != LUA_TTABLE) { |
| 6132 | hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret)); |
| 6133 | WILL_LJMP(lua_error(L)); |
| 6134 | } |
| 6135 | |
| 6136 | lua_pushstring(L, name); |
| 6137 | lua_pushnil(L); |
| 6138 | lua_settable(L, -3); |
| 6139 | |
| 6140 | lua_pushboolean(L, 1); |
| 6141 | return 1; |
| 6142 | } |
| 6143 | |
| 6144 | /* Set the reply's body. Overwrite any existing entry. */ |
| 6145 | __LJMP static int hlua_txn_reply_set_body(lua_State *L) |
| 6146 | { |
| 6147 | const char *payload = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6148 | |
| 6149 | /* First argument (self) must be a table */ |
| 6150 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6151 | |
| 6152 | lua_pushstring(L, payload); |
| 6153 | lua_setfield(L, 1, "body"); |
| 6154 | |
| 6155 | lua_pushboolean(L, 1); |
| 6156 | return 1; |
| 6157 | } |
| 6158 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 6159 | __LJMP static int hlua_log(lua_State *L) |
| 6160 | { |
| 6161 | int level; |
| 6162 | const char *msg; |
| 6163 | |
| 6164 | MAY_LJMP(check_args(L, 2, "log")); |
| 6165 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 6166 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6167 | |
| 6168 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 6169 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 6170 | |
| 6171 | hlua_sendlog(NULL, level, msg); |
| 6172 | return 0; |
| 6173 | } |
| 6174 | |
| 6175 | __LJMP static int hlua_log_debug(lua_State *L) |
| 6176 | { |
| 6177 | const char *msg; |
| 6178 | |
| 6179 | MAY_LJMP(check_args(L, 1, "debug")); |
| 6180 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6181 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 6182 | return 0; |
| 6183 | } |
| 6184 | |
| 6185 | __LJMP static int hlua_log_info(lua_State *L) |
| 6186 | { |
| 6187 | const char *msg; |
| 6188 | |
| 6189 | MAY_LJMP(check_args(L, 1, "info")); |
| 6190 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6191 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 6192 | return 0; |
| 6193 | } |
| 6194 | |
| 6195 | __LJMP static int hlua_log_warning(lua_State *L) |
| 6196 | { |
| 6197 | const char *msg; |
| 6198 | |
| 6199 | MAY_LJMP(check_args(L, 1, "warning")); |
| 6200 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6201 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 6202 | return 0; |
| 6203 | } |
| 6204 | |
| 6205 | __LJMP static int hlua_log_alert(lua_State *L) |
| 6206 | { |
| 6207 | const char *msg; |
| 6208 | |
| 6209 | MAY_LJMP(check_args(L, 1, "alert")); |
| 6210 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6211 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6212 | return 0; |
| 6213 | } |
| 6214 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6215 | __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] | 6216 | { |
| 6217 | int wakeup_ms = lua_tointeger(L, -1); |
| 6218 | if (now_ms < wakeup_ms) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6219 | 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] | 6220 | return 0; |
| 6221 | } |
| 6222 | |
| 6223 | __LJMP static int hlua_sleep(lua_State *L) |
| 6224 | { |
| 6225 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6226 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6227 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6228 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6229 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6230 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6231 | wakeup_ms = tick_add(now_ms, delay); |
| 6232 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6233 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6234 | 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] | 6235 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6236 | } |
| 6237 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6238 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6239 | { |
| 6240 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6241 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6242 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6243 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6244 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6245 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6246 | wakeup_ms = tick_add(now_ms, delay); |
| 6247 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6248 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6249 | 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] | 6250 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6251 | } |
| 6252 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6253 | /* This functionis an LUA binding. it permits to give back |
| 6254 | * the hand at the HAProxy scheduler. It is used when the |
| 6255 | * LUA processing consumes a lot of time. |
| 6256 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6257 | __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] | 6258 | { |
| 6259 | return 0; |
| 6260 | } |
| 6261 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6262 | __LJMP static int hlua_yield(lua_State *L) |
| 6263 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6264 | 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] | 6265 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6266 | } |
| 6267 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6268 | /* This function change the nice of the currently executed |
| 6269 | * task. It is used set low or high priority at the current |
| 6270 | * task. |
| 6271 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 6272 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6273 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6274 | struct hlua *hlua; |
| 6275 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6276 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6277 | MAY_LJMP(check_args(L, 1, "set_nice")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6278 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6279 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 6280 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 6281 | hlua = hlua_gethlua(L); |
| 6282 | |
| 6283 | /* If the task is not set, I'm in a start mode. */ |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6284 | if (!hlua || !hlua->task) |
| 6285 | return 0; |
| 6286 | |
| 6287 | if (nice < -1024) |
| 6288 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6289 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6290 | nice = 1024; |
| 6291 | |
| 6292 | hlua->task->nice = nice; |
| 6293 | return 0; |
| 6294 | } |
| 6295 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6296 | /* 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] | 6297 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 6298 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 6299 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6300 | * |
| 6301 | * Task wrapper are longjmp safe because the only one Lua code |
| 6302 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6303 | */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 6304 | 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] | 6305 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6306 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6307 | enum hlua_exec status; |
| 6308 | |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 6309 | if (task->thread_mask == MAX_THREADS_MASK) |
| 6310 | task_set_affinity(task, tid_bit); |
| 6311 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6312 | /* If it is the first call to the task, we must initialize the |
| 6313 | * execution timeouts. |
| 6314 | */ |
| 6315 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6316 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6317 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6318 | /* Execute the Lua code. */ |
| 6319 | status = hlua_ctx_resume(hlua, 1); |
| 6320 | |
| 6321 | switch (status) { |
| 6322 | /* finished or yield */ |
| 6323 | case HLUA_E_OK: |
| 6324 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6325 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 6326 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6327 | break; |
| 6328 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6329 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 6330 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 6331 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6332 | break; |
| 6333 | |
| 6334 | /* finished with error. */ |
| 6335 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6336 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6337 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6338 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6339 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6340 | break; |
| 6341 | |
| 6342 | case HLUA_E_ERR: |
| 6343 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6344 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6345 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6346 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6347 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6348 | break; |
| 6349 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6350 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6351 | } |
| 6352 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6353 | /* This function is an LUA binding that register LUA function to be |
| 6354 | * executed after the HAProxy configuration parsing and before the |
| 6355 | * HAProxy scheduler starts. This function expect only one LUA |
| 6356 | * argument that is a function. This function returns nothing, but |
| 6357 | * throws if an error is encountered. |
| 6358 | */ |
| 6359 | __LJMP static int hlua_register_init(lua_State *L) |
| 6360 | { |
| 6361 | struct hlua_init_function *init; |
| 6362 | int ref; |
| 6363 | |
| 6364 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 6365 | |
| 6366 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6367 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6368 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6369 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6370 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6371 | |
| 6372 | init->function_ref = ref; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6373 | LIST_ADDQ(&hlua_init_functions[hlua_state_id], &init->l); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6374 | return 0; |
| 6375 | } |
| 6376 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6377 | /* This functio is an LUA binding. It permits to register a task |
| 6378 | * executed in parallel of the main HAroxy activity. The task is |
| 6379 | * created and it is set in the HAProxy scheduler. It can be called |
| 6380 | * from the "init" section, "post init" or during the runtime. |
| 6381 | * |
| 6382 | * Lua prototype: |
| 6383 | * |
| 6384 | * <none> core.register_task(<function>) |
| 6385 | */ |
| 6386 | static int hlua_register_task(lua_State *L) |
| 6387 | { |
| 6388 | struct hlua *hlua; |
| 6389 | struct task *task; |
| 6390 | int ref; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 6391 | int state_id; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6392 | |
| 6393 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 6394 | |
| 6395 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6396 | |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 6397 | /* Get the reference state. If the reference is NULL, L is the master |
| 6398 | * state, otherwise hlua->T is. |
| 6399 | */ |
| 6400 | hlua = hlua_gethlua(L); |
| 6401 | if (hlua) |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 6402 | /* we are in runtime processing */ |
| 6403 | state_id = hlua->state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 6404 | else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 6405 | /* we are in initialization mode */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6406 | state_id = hlua_state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 6407 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6408 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6409 | if (!hlua) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6410 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6411 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 6412 | /* We are in the common lua state, execute the task anywhere, |
| 6413 | * otherwise, inherit the current thread identifier |
| 6414 | */ |
| 6415 | if (state_id == 0) |
| 6416 | task = task_new(MAX_THREADS_MASK); |
| 6417 | else |
| 6418 | task = task_new(tid_bit); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 6419 | if (!task) |
| 6420 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 6421 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6422 | task->context = hlua; |
| 6423 | task->process = hlua_process_task; |
| 6424 | |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 6425 | if (!hlua_ctx_init(hlua, state_id, task, 1)) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6426 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6427 | |
| 6428 | /* Restore the function in the stack. */ |
| 6429 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 6430 | hlua->nargs = 0; |
| 6431 | |
| 6432 | /* Schedule task. */ |
| 6433 | task_schedule(task, now_ms); |
| 6434 | |
| 6435 | return 0; |
| 6436 | } |
| 6437 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6438 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 6439 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 6440 | * resume converters. |
| 6441 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6442 | static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private) |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6443 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6444 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6445 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6446 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6447 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6448 | if (!stream) |
| 6449 | return 0; |
| 6450 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6451 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6452 | * Lua context can be not initialized. This behavior |
| 6453 | * permits to save performances because a systematic |
| 6454 | * Lua initialization cause 5% performances loss. |
| 6455 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6456 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6457 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6458 | if (!stream->hlua) { |
| 6459 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6460 | return 0; |
| 6461 | } |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6462 | 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] | 6463 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6464 | return 0; |
| 6465 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6466 | } |
| 6467 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6468 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6469 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6470 | |
| 6471 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6472 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6473 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6474 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6475 | else |
| 6476 | error = "critical error"; |
| 6477 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6478 | return 0; |
| 6479 | } |
| 6480 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6481 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6482 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6483 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6484 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6485 | return 0; |
| 6486 | } |
| 6487 | |
| 6488 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6489 | 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] | 6490 | |
| 6491 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6492 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6493 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6494 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6495 | return 0; |
| 6496 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6497 | hlua_smp2lua(stream->hlua->T, smp); |
| 6498 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6499 | |
| 6500 | /* push keywords in the stack. */ |
| 6501 | if (arg_p) { |
| 6502 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6503 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6504 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
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 | return 0; |
| 6507 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6508 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6509 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6510 | } |
| 6511 | } |
| 6512 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6513 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6514 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6515 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6516 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6517 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6518 | } |
| 6519 | |
| 6520 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6521 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6522 | /* finished. */ |
| 6523 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6524 | /* If the stack is empty, the function fails. */ |
| 6525 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6526 | return 0; |
| 6527 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6528 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6529 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6530 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6531 | return 1; |
| 6532 | |
| 6533 | /* yield. */ |
| 6534 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6535 | 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] | 6536 | return 0; |
| 6537 | |
| 6538 | /* finished with error. */ |
| 6539 | case HLUA_E_ERRMSG: |
| 6540 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6541 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6542 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6543 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6544 | return 0; |
| 6545 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6546 | case HLUA_E_ETMOUT: |
| 6547 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 6548 | return 0; |
| 6549 | |
| 6550 | case HLUA_E_NOMEM: |
| 6551 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 6552 | return 0; |
| 6553 | |
| 6554 | case HLUA_E_YIELD: |
| 6555 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 6556 | return 0; |
| 6557 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6558 | case HLUA_E_ERR: |
| 6559 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6560 | 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] | 6561 | /* fall through */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6562 | |
| 6563 | default: |
| 6564 | return 0; |
| 6565 | } |
| 6566 | } |
| 6567 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6568 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 6569 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6570 | * resume sample-fetches. This function will be called by the sample |
| 6571 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6572 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6573 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 6574 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6575 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6576 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6577 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6578 | const char *error; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6579 | unsigned int hflags = HLUA_TXN_NOTERM; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6580 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6581 | if (!stream) |
| 6582 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 6583 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6584 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6585 | * Lua context can be not initialized. This behavior |
| 6586 | * permits to save performances because a systematic |
| 6587 | * Lua initialization cause 5% performances loss. |
| 6588 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6589 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6590 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6591 | if (!stream->hlua) { |
| 6592 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6593 | return 0; |
| 6594 | } |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6595 | 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] | 6596 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6597 | return 0; |
| 6598 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6599 | } |
| 6600 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6601 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6602 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6603 | |
| 6604 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6605 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6606 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6607 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6608 | else |
| 6609 | error = "critical error"; |
| 6610 | 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] | 6611 | return 0; |
| 6612 | } |
| 6613 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6614 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6615 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6616 | 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] | 6617 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6618 | return 0; |
| 6619 | } |
| 6620 | |
| 6621 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6622 | 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] | 6623 | |
| 6624 | /* push arguments in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6625 | 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] | 6626 | 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] | 6627 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6628 | return 0; |
| 6629 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6630 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6631 | |
| 6632 | /* push keywords in the stack. */ |
| 6633 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 6634 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6635 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6636 | 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] | 6637 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6638 | return 0; |
| 6639 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6640 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6641 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6642 | } |
| 6643 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6644 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6645 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6646 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6647 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6648 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6649 | } |
| 6650 | |
| 6651 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6652 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6653 | /* finished. */ |
| 6654 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6655 | /* If the stack is empty, the function fails. */ |
| 6656 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6657 | return 0; |
| 6658 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6659 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6660 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6661 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6662 | |
| 6663 | /* Set the end of execution flag. */ |
| 6664 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 6665 | return 1; |
| 6666 | |
| 6667 | /* yield. */ |
| 6668 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6669 | 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] | 6670 | return 0; |
| 6671 | |
| 6672 | /* finished with error. */ |
| 6673 | case HLUA_E_ERRMSG: |
| 6674 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6675 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6676 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6677 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6678 | return 0; |
| 6679 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6680 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6681 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 6682 | return 0; |
| 6683 | |
| 6684 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6685 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 6686 | return 0; |
| 6687 | |
| 6688 | case HLUA_E_YIELD: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6689 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 6690 | return 0; |
| 6691 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6692 | case HLUA_E_ERR: |
| 6693 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6694 | 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] | 6695 | /* fall through */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6696 | |
| 6697 | default: |
| 6698 | return 0; |
| 6699 | } |
| 6700 | } |
| 6701 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6702 | /* This function is an LUA binding used for registering |
| 6703 | * "sample-conv" functions. It expects a converter name used |
| 6704 | * in the haproxy configuration file, and an LUA function. |
| 6705 | */ |
| 6706 | __LJMP static int hlua_register_converters(lua_State *L) |
| 6707 | { |
| 6708 | struct sample_conv_kw_list *sck; |
| 6709 | const char *name; |
| 6710 | int ref; |
| 6711 | int len; |
| 6712 | struct hlua_function *fcn; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6713 | struct sample_conv *sc; |
| 6714 | struct buffer *trash; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6715 | |
| 6716 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 6717 | |
| 6718 | /* First argument : converter name. */ |
| 6719 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6720 | |
| 6721 | /* Second argument : lua function. */ |
| 6722 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6723 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6724 | /* Check if the converter is already registered */ |
| 6725 | trash = get_trash_chunk(); |
| 6726 | chunk_printf(trash, "lua.%s", name); |
| 6727 | sc = find_sample_conv(trash->area, trash->data); |
| 6728 | if (sc != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 6729 | fcn = sc->private; |
| 6730 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 6731 | ha_warning("Trying to register converter 'lua.%s' more than once. " |
| 6732 | "This will become a hard error in version 2.5.\n", name); |
| 6733 | } |
| 6734 | fcn->function_ref[hlua_state_id] = ref; |
| 6735 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6736 | } |
| 6737 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6738 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6739 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6740 | if (!sck) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6741 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 6742 | fcn = new_hlua_function(); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6743 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6744 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6745 | |
| 6746 | /* Fill fcn. */ |
| 6747 | fcn->name = strdup(name); |
| 6748 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6749 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6750 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6751 | |
| 6752 | /* List head */ |
| 6753 | sck->list.n = sck->list.p = NULL; |
| 6754 | |
| 6755 | /* converter keyword. */ |
| 6756 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6757 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6758 | if (!sck->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6759 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6760 | |
| 6761 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 6762 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6763 | 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] | 6764 | sck->kw[0].val_args = NULL; |
| 6765 | sck->kw[0].in_type = SMP_T_STR; |
| 6766 | sck->kw[0].out_type = SMP_T_STR; |
| 6767 | sck->kw[0].private = fcn; |
| 6768 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6769 | /* Register this new converter */ |
| 6770 | sample_register_convs(sck); |
| 6771 | |
| 6772 | return 0; |
| 6773 | } |
| 6774 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6775 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6776 | * "sample-fetch" functions. It expects a converter name used |
| 6777 | * in the haproxy configuration file, and an LUA function. |
| 6778 | */ |
| 6779 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 6780 | { |
| 6781 | const char *name; |
| 6782 | int ref; |
| 6783 | int len; |
| 6784 | struct sample_fetch_kw_list *sfk; |
| 6785 | struct hlua_function *fcn; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6786 | struct sample_fetch *sf; |
| 6787 | struct buffer *trash; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6788 | |
| 6789 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 6790 | |
| 6791 | /* First argument : sample-fetch name. */ |
| 6792 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6793 | |
| 6794 | /* Second argument : lua function. */ |
| 6795 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6796 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6797 | /* Check if the sample-fetch is already registered */ |
| 6798 | trash = get_trash_chunk(); |
| 6799 | chunk_printf(trash, "lua.%s", name); |
| 6800 | sf = find_sample_fetch(trash->area, trash->data); |
| 6801 | if (sf != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 6802 | fcn = sf->private; |
| 6803 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 6804 | ha_warning("Trying to register sample-fetch 'lua.%s' more than once. " |
| 6805 | "This will become a hard error in version 2.5.\n", name); |
| 6806 | } |
| 6807 | fcn->function_ref[hlua_state_id] = ref; |
| 6808 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6809 | } |
| 6810 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6811 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6812 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6813 | if (!sfk) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6814 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 6815 | fcn = new_hlua_function(); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6816 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6817 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6818 | |
| 6819 | /* Fill fcn. */ |
| 6820 | fcn->name = strdup(name); |
| 6821 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6822 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6823 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6824 | |
| 6825 | /* List head */ |
| 6826 | sfk->list.n = sfk->list.p = NULL; |
| 6827 | |
| 6828 | /* sample-fetch keyword. */ |
| 6829 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6830 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6831 | if (!sfk->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6832 | return luaL_error(L, "Lua out of memory error."); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6833 | |
| 6834 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 6835 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6836 | 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] | 6837 | sfk->kw[0].val_args = NULL; |
| 6838 | sfk->kw[0].out_type = SMP_T_STR; |
| 6839 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 6840 | sfk->kw[0].val = 0; |
| 6841 | sfk->kw[0].private = fcn; |
| 6842 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6843 | /* Register this new fetch. */ |
| 6844 | sample_register_fetches(sfk); |
| 6845 | |
| 6846 | return 0; |
| 6847 | } |
| 6848 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 6849 | /* This function is a lua binding to set the wake_time. |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6850 | */ |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 6851 | __LJMP static int hlua_set_wake_time(lua_State *L) |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6852 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 6853 | struct hlua *hlua; |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6854 | unsigned int delay; |
| 6855 | unsigned int wakeup_ms; |
| 6856 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 6857 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 6858 | hlua = hlua_gethlua(L); |
| 6859 | if (!hlua) { |
| 6860 | return 0; |
| 6861 | } |
| 6862 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6863 | MAY_LJMP(check_args(L, 1, "wake_time")); |
| 6864 | |
| 6865 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 6866 | wakeup_ms = tick_add(now_ms, delay); |
| 6867 | hlua->wake_time = wakeup_ms; |
| 6868 | return 0; |
| 6869 | } |
| 6870 | |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6871 | /* This function is a wrapper to execute each LUA function declared as an action |
| 6872 | * wrapper during the initialisation period. This function may return any |
| 6873 | * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is |
| 6874 | * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the |
| 6875 | * return value is the first element on the stack. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6876 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6877 | 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] | 6878 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6879 | { |
| 6880 | char **arg; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6881 | unsigned int hflags = 0; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6882 | int dir, act_ret = ACT_RET_CONT; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6883 | const char *error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6884 | |
| 6885 | switch (rule->from) { |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 6886 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 6887 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 6888 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 6889 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6890 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6891 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6892 | goto end; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6893 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6894 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6895 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6896 | * Lua context can be not initialized. This behavior |
| 6897 | * permits to save performances because a systematic |
| 6898 | * Lua initialization cause 5% performances loss. |
| 6899 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6900 | if (!s->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6901 | s->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6902 | if (!s->hlua) { |
| 6903 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6904 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6905 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6906 | } |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6907 | 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] | 6908 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6909 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6910 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6911 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6912 | } |
| 6913 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6914 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6915 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6916 | |
| 6917 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6918 | if (!SET_SAFE_LJMP(s->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6919 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 6920 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6921 | else |
| 6922 | error = "critical error"; |
| 6923 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6924 | rule->arg.hlua_rule->fcn->name, error); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6925 | goto end; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6926 | } |
| 6927 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6928 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6929 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6930 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6931 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6932 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6933 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6934 | } |
| 6935 | |
| 6936 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6937 | 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] | 6938 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6939 | /* Create and and push object stream in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6940 | if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6941 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6942 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6943 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6944 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6945 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6946 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6947 | |
| 6948 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6949 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6950 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6951 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6952 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6953 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6954 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6955 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6956 | lua_pushstring(s->hlua->T, *arg); |
| 6957 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6958 | } |
| 6959 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6960 | /* Now the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6961 | RESET_SAFE_LJMP(s->hlua); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6962 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6963 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6964 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6965 | } |
| 6966 | |
| 6967 | /* Execute the function. */ |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 6968 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6969 | /* finished. */ |
| 6970 | case HLUA_E_OK: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6971 | /* Catch the return value */ |
| 6972 | if (lua_gettop(s->hlua->T) > 0) |
| 6973 | act_ret = lua_tointeger(s->hlua->T, -1); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6974 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6975 | /* Set timeout in the required channel. */ |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 6976 | if (act_ret == ACT_RET_YIELD) { |
| 6977 | if (flags & ACT_OPT_FINAL) |
| 6978 | goto err_yield; |
| 6979 | |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 6980 | if (dir == SMP_OPT_DIR_REQ) |
| 6981 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 6982 | s->hlua->wake_time); |
| 6983 | else |
| 6984 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 6985 | s->hlua->wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6986 | } |
| 6987 | goto end; |
| 6988 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6989 | /* yield. */ |
| 6990 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6991 | /* Set timeout in the required channel. */ |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 6992 | if (dir == SMP_OPT_DIR_REQ) |
| 6993 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 6994 | s->hlua->wake_time); |
| 6995 | else |
| 6996 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 6997 | s->hlua->wake_time); |
| 6998 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6999 | /* Some actions can be wake up when a "write" event |
| 7000 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7001 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7002 | */ |
Christopher Faulet | 51fa358 | 2019-07-26 14:54:52 +0200 | [diff] [blame] | 7003 | if (HLUA_IS_WAKERESWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7004 | s->res.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7005 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7006 | s->req.flags |= CF_WAKE_WRITE; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7007 | act_ret = ACT_RET_YIELD; |
| 7008 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7009 | |
| 7010 | /* finished with error. */ |
| 7011 | case HLUA_E_ERRMSG: |
| 7012 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7013 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7014 | rule->arg.hlua_rule->fcn->name, lua_tostring(s->hlua->T, -1)); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7015 | lua_pop(s->hlua->T, 1); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7016 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7017 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7018 | case HLUA_E_ETMOUT: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7019 | 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] | 7020 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7021 | |
| 7022 | case HLUA_E_NOMEM: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7023 | 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] | 7024 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7025 | |
| 7026 | case HLUA_E_YIELD: |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 7027 | err_yield: |
| 7028 | act_ret = ACT_RET_CONT; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7029 | 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] | 7030 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7031 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7032 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7033 | case HLUA_E_ERR: |
| 7034 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7035 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7036 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7037 | |
| 7038 | default: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7039 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7040 | } |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7041 | |
| 7042 | end: |
Christopher Faulet | 2361fd9 | 2020-07-30 10:40:58 +0200 | [diff] [blame] | 7043 | if (act_ret != ACT_RET_YIELD && s->hlua) |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 7044 | s->hlua->wake_time = TICK_ETERNITY; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7045 | return act_ret; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7046 | } |
| 7047 | |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 7048 | 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] | 7049 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 7050 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7051 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7052 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 7053 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 7054 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7055 | } |
| 7056 | |
| 7057 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 7058 | { |
| 7059 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7060 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7061 | struct task *task; |
| 7062 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7063 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7064 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7065 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7066 | if (!hlua) { |
| 7067 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7068 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7069 | return 0; |
| 7070 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7071 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7072 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7073 | ctx->ctx.hlua_apptcp.flags = 0; |
| 7074 | |
| 7075 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7076 | task = task_new(tid_bit); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7077 | if (!task) { |
| 7078 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7079 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7080 | return 0; |
| 7081 | } |
| 7082 | task->nice = 0; |
| 7083 | task->context = ctx; |
| 7084 | task->process = hlua_applet_wakeup; |
| 7085 | ctx->ctx.hlua_apptcp.task = task; |
| 7086 | |
| 7087 | /* In the execution wrappers linked with a stream, the |
| 7088 | * Lua context can be not initialized. This behavior |
| 7089 | * permits to save performances because a systematic |
| 7090 | * Lua initialization cause 5% performances loss. |
| 7091 | */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7092 | 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] | 7093 | 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] | 7094 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7095 | return 0; |
| 7096 | } |
| 7097 | |
| 7098 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 7099 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7100 | |
| 7101 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7102 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7103 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7104 | error = lua_tostring(hlua->T, -1); |
| 7105 | else |
| 7106 | error = "critical error"; |
| 7107 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7108 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7109 | return 0; |
| 7110 | } |
| 7111 | |
| 7112 | /* Check stack available size. */ |
| 7113 | if (!lua_checkstack(hlua->T, 1)) { |
| 7114 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7115 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7116 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7117 | return 0; |
| 7118 | } |
| 7119 | |
| 7120 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7121 | 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] | 7122 | |
| 7123 | /* Create and and push object stream in the stack. */ |
| 7124 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 7125 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7126 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7127 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7128 | return 0; |
| 7129 | } |
| 7130 | hlua->nargs = 1; |
| 7131 | |
| 7132 | /* push keywords in the stack. */ |
| 7133 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 7134 | if (!lua_checkstack(hlua->T, 1)) { |
| 7135 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7136 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7137 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7138 | return 0; |
| 7139 | } |
| 7140 | lua_pushstring(hlua->T, *arg); |
| 7141 | hlua->nargs++; |
| 7142 | } |
| 7143 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7144 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7145 | |
| 7146 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7147 | si_cant_get(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 7148 | si_rx_endp_more(si); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7149 | |
| 7150 | return 1; |
| 7151 | } |
| 7152 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7153 | void hlua_applet_tcp_fct(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7154 | { |
| 7155 | struct stream_interface *si = ctx->owner; |
| 7156 | struct stream *strm = si_strm(si); |
| 7157 | struct channel *res = si_ic(si); |
| 7158 | struct act_rule *rule = ctx->rule; |
| 7159 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7160 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7161 | |
| 7162 | /* The applet execution is already done. */ |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 7163 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) { |
| 7164 | /* eat the whole request */ |
| 7165 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7166 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 7167 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7168 | |
| 7169 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7170 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7171 | return; |
| 7172 | |
| 7173 | /* Execute the function. */ |
| 7174 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7175 | /* finished. */ |
| 7176 | case HLUA_E_OK: |
| 7177 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 7178 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7179 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 7180 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7181 | res->flags |= CF_READ_NULL; |
| 7182 | si_shutr(si); |
| 7183 | return; |
| 7184 | |
| 7185 | /* yield. */ |
| 7186 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 7187 | if (hlua->wake_time != TICK_ETERNITY) |
| 7188 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7189 | return; |
| 7190 | |
| 7191 | /* finished with error. */ |
| 7192 | case HLUA_E_ERRMSG: |
| 7193 | /* Display log. */ |
| 7194 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7195 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7196 | lua_pop(hlua->T, 1); |
| 7197 | goto error; |
| 7198 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7199 | case HLUA_E_ETMOUT: |
| 7200 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7201 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7202 | goto error; |
| 7203 | |
| 7204 | case HLUA_E_NOMEM: |
| 7205 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7206 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7207 | goto error; |
| 7208 | |
| 7209 | case HLUA_E_YIELD: /* unexpected */ |
| 7210 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7211 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7212 | goto error; |
| 7213 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7214 | case HLUA_E_ERR: |
| 7215 | /* Display log. */ |
| 7216 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7217 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7218 | goto error; |
| 7219 | |
| 7220 | default: |
| 7221 | goto error; |
| 7222 | } |
| 7223 | |
| 7224 | error: |
| 7225 | |
| 7226 | /* For all other cases, just close the stream. */ |
| 7227 | si_shutw(si); |
| 7228 | si_shutr(si); |
| 7229 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 7230 | } |
| 7231 | |
| 7232 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 7233 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7234 | task_destroy(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7235 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7236 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7237 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7238 | } |
| 7239 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7240 | /* The function returns 1 if the initialisation is complete, 0 if |
| 7241 | * an errors occurs and -1 if more data are required for initializing |
| 7242 | * the applet. |
| 7243 | */ |
| 7244 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 7245 | { |
| 7246 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7247 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7248 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7249 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7250 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7251 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7252 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7253 | txn = strm->txn; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7254 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7255 | if (!hlua) { |
| 7256 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7257 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7258 | return 0; |
| 7259 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7260 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7261 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7262 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 7263 | ctx->ctx.hlua_apphttp.flags = 0; |
| 7264 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 7265 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 7266 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 7267 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7268 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7269 | task = task_new(tid_bit); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7270 | if (!task) { |
| 7271 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7272 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7273 | return 0; |
| 7274 | } |
| 7275 | task->nice = 0; |
| 7276 | task->context = ctx; |
| 7277 | task->process = hlua_applet_wakeup; |
| 7278 | ctx->ctx.hlua_apphttp.task = task; |
| 7279 | |
| 7280 | /* In the execution wrappers linked with a stream, the |
| 7281 | * Lua context can be not initialized. This behavior |
| 7282 | * permits to save performances because a systematic |
| 7283 | * Lua initialization cause 5% performances loss. |
| 7284 | */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7285 | 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] | 7286 | 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] | 7287 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7288 | return 0; |
| 7289 | } |
| 7290 | |
| 7291 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 7292 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7293 | |
| 7294 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7295 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7296 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7297 | error = lua_tostring(hlua->T, -1); |
| 7298 | else |
| 7299 | error = "critical error"; |
| 7300 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7301 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7302 | return 0; |
| 7303 | } |
| 7304 | |
| 7305 | /* Check stack available size. */ |
| 7306 | if (!lua_checkstack(hlua->T, 1)) { |
| 7307 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7308 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7309 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7310 | return 0; |
| 7311 | } |
| 7312 | |
| 7313 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7314 | 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] | 7315 | |
| 7316 | /* Create and and push object stream in the stack. */ |
| 7317 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 7318 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7319 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7320 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7321 | return 0; |
| 7322 | } |
| 7323 | hlua->nargs = 1; |
| 7324 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7325 | /* push keywords in the stack. */ |
| 7326 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 7327 | if (!lua_checkstack(hlua->T, 1)) { |
| 7328 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7329 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7330 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7331 | return 0; |
| 7332 | } |
| 7333 | lua_pushstring(hlua->T, *arg); |
| 7334 | hlua->nargs++; |
| 7335 | } |
| 7336 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7337 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7338 | |
| 7339 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7340 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7341 | |
| 7342 | return 1; |
| 7343 | } |
| 7344 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7345 | void hlua_applet_http_fct(struct appctx *ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7346 | { |
| 7347 | struct stream_interface *si = ctx->owner; |
| 7348 | struct stream *strm = si_strm(si); |
| 7349 | struct channel *req = si_oc(si); |
| 7350 | struct channel *res = si_ic(si); |
| 7351 | struct act_rule *rule = ctx->rule; |
| 7352 | struct proxy *px = strm->be; |
| 7353 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
| 7354 | struct htx *req_htx, *res_htx; |
| 7355 | |
| 7356 | res_htx = htx_from_buf(&res->buf); |
| 7357 | |
| 7358 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7359 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7360 | goto out; |
| 7361 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7362 | /* Check if the input buffer is available. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7363 | if (!b_size(&res->buf)) { |
| 7364 | si_rx_room_blk(si); |
| 7365 | goto out; |
| 7366 | } |
| 7367 | /* check that the output is not closed */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7368 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7369 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7370 | |
| 7371 | /* Set the currently running flag. */ |
| 7372 | if (!HLUA_IS_RUNNING(hlua) && |
| 7373 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7374 | struct htx_blk *blk; |
| 7375 | size_t count = co_data(req); |
| 7376 | |
| 7377 | if (!count) { |
| 7378 | si_cant_get(si); |
| 7379 | goto out; |
| 7380 | } |
| 7381 | |
| 7382 | /* We need to flush the request header. This left the body for |
| 7383 | * the Lua. |
| 7384 | */ |
| 7385 | req_htx = htx_from_buf(&req->buf); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 7386 | blk = htx_get_first_blk(req_htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7387 | while (count && blk) { |
| 7388 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 7389 | uint32_t sz = htx_get_blksz(blk); |
| 7390 | |
| 7391 | if (sz > count) { |
| 7392 | si_cant_get(si); |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 7393 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7394 | goto out; |
| 7395 | } |
| 7396 | |
| 7397 | count -= sz; |
| 7398 | co_set_data(req, co_data(req) - sz); |
| 7399 | blk = htx_remove_blk(req_htx, blk); |
| 7400 | |
| 7401 | if (type == HTX_BLK_EOH) |
| 7402 | break; |
| 7403 | } |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 7404 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7405 | } |
| 7406 | |
| 7407 | /* Executes The applet if it is not done. */ |
| 7408 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7409 | |
| 7410 | /* Execute the function. */ |
| 7411 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7412 | /* finished. */ |
| 7413 | case HLUA_E_OK: |
| 7414 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7415 | break; |
| 7416 | |
| 7417 | /* yield. */ |
| 7418 | case HLUA_E_AGAIN: |
| 7419 | if (hlua->wake_time != TICK_ETERNITY) |
| 7420 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7421 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7422 | |
| 7423 | /* finished with error. */ |
| 7424 | case HLUA_E_ERRMSG: |
| 7425 | /* Display log. */ |
| 7426 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7427 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7428 | lua_pop(hlua->T, 1); |
| 7429 | goto error; |
| 7430 | |
| 7431 | case HLUA_E_ETMOUT: |
| 7432 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7433 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7434 | goto error; |
| 7435 | |
| 7436 | case HLUA_E_NOMEM: |
| 7437 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7438 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7439 | goto error; |
| 7440 | |
| 7441 | case HLUA_E_YIELD: /* unexpected */ |
| 7442 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7443 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7444 | goto error; |
| 7445 | |
| 7446 | case HLUA_E_ERR: |
| 7447 | /* Display log. */ |
| 7448 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7449 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7450 | goto error; |
| 7451 | |
| 7452 | default: |
| 7453 | goto error; |
| 7454 | } |
| 7455 | } |
| 7456 | |
| 7457 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7458 | if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT) |
| 7459 | goto done; |
| 7460 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7461 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) |
| 7462 | goto error; |
| 7463 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 7464 | /* no more data are expected. Don't add TLR because mux-h1 will take care of it */ |
| 7465 | res_htx->flags |= HTX_FL_EOM; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7466 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 7467 | ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7468 | } |
| 7469 | |
| 7470 | done: |
| 7471 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7472 | if (!(res->flags & CF_SHUTR)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7473 | res->flags |= CF_READ_NULL; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7474 | si_shutr(si); |
| 7475 | } |
| 7476 | |
| 7477 | /* eat the whole request */ |
| 7478 | if (co_data(req)) { |
| 7479 | req_htx = htx_from_buf(&req->buf); |
| 7480 | co_htx_skip(req, req_htx, co_data(req)); |
| 7481 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7482 | } |
| 7483 | } |
| 7484 | |
| 7485 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7486 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7487 | return; |
| 7488 | |
| 7489 | error: |
| 7490 | |
| 7491 | /* If we are in HTTP mode, and we are not send any |
| 7492 | * data, return a 500 server error in best effort: |
| 7493 | * if there is no room available in the buffer, |
| 7494 | * just close the connection. |
| 7495 | */ |
| 7496 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 7497 | struct buffer *err = &http_err_chunks[HTTP_ERR_500]; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7498 | |
| 7499 | channel_erase(res); |
| 7500 | res->buf.data = b_data(err); |
| 7501 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 7502 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 7503 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7504 | } |
| 7505 | if (!(strm->flags & SF_ERR_MASK)) |
| 7506 | strm->flags |= SF_ERR_RESOURCE; |
| 7507 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7508 | goto done; |
| 7509 | } |
| 7510 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7511 | static void hlua_applet_http_release(struct appctx *ctx) |
| 7512 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7513 | task_destroy(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7514 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7515 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7516 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7517 | } |
| 7518 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7519 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7520 | * success case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7521 | * |
| 7522 | * This function can fail with an abort() due to an Lua critical error. |
| 7523 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7524 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7525 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7526 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 7527 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7528 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7529 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7530 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7531 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7532 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7533 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7534 | if (!rule->arg.hlua_rule) { |
| 7535 | memprintf(err, "out of memory error"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7536 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7537 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7538 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7539 | /* Memory for arguments. */ |
Tim Duesterhus | e52b6e5 | 2020-09-12 20:26:43 +0200 | [diff] [blame] | 7540 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, |
| 7541 | sizeof(*rule->arg.hlua_rule->args)); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7542 | if (!rule->arg.hlua_rule->args) { |
| 7543 | memprintf(err, "out of memory error"); |
| 7544 | return ACT_RET_PRS_ERR; |
| 7545 | } |
| 7546 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7547 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7548 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7549 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7550 | /* Expect some arguments */ |
| 7551 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7552 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7553 | memprintf(err, "expect %d arguments", fcn->nargs); |
| 7554 | return ACT_RET_PRS_ERR; |
| 7555 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7556 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7557 | if (!rule->arg.hlua_rule->args[i]) { |
| 7558 | memprintf(err, "out of memory error"); |
| 7559 | return ACT_RET_PRS_ERR; |
| 7560 | } |
| 7561 | (*cur_arg)++; |
| 7562 | } |
| 7563 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7564 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 7565 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7566 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7567 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7568 | } |
| 7569 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7570 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 7571 | struct act_rule *rule, char **err) |
| 7572 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7573 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7574 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7575 | /* HTTP applets are forbidden in tcp-request rules. |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7576 | * HTTP applet request requires everything initialized by |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7577 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7578 | * The applet will be immediately initialized, but its before |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7579 | * the call of this analyzer. |
| 7580 | */ |
| 7581 | if (rule->from != ACT_F_HTTP_REQ) { |
| 7582 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 7583 | return ACT_RET_PRS_ERR; |
| 7584 | } |
| 7585 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7586 | /* Memory for the rule. */ |
| 7587 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7588 | if (!rule->arg.hlua_rule) { |
| 7589 | memprintf(err, "out of memory error"); |
| 7590 | return ACT_RET_PRS_ERR; |
| 7591 | } |
| 7592 | |
| 7593 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7594 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7595 | |
| 7596 | /* TODO: later accept arguments. */ |
| 7597 | rule->arg.hlua_rule->args = NULL; |
| 7598 | |
| 7599 | /* Add applet pointer in the rule. */ |
| 7600 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7601 | rule->applet.name = fcn->name; |
| 7602 | rule->applet.init = hlua_applet_http_init; |
| 7603 | rule->applet.fct = hlua_applet_http_fct; |
| 7604 | rule->applet.release = hlua_applet_http_release; |
| 7605 | rule->applet.timeout = hlua_timeout_applet; |
| 7606 | |
| 7607 | return ACT_RET_PRS_OK; |
| 7608 | } |
| 7609 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7610 | /* This function is an LUA binding used for registering |
| 7611 | * "sample-conv" functions. It expects a converter name used |
| 7612 | * in the haproxy configuration file, and an LUA function. |
| 7613 | */ |
| 7614 | __LJMP static int hlua_register_action(lua_State *L) |
| 7615 | { |
| 7616 | struct action_kw_list *akl; |
| 7617 | const char *name; |
| 7618 | int ref; |
| 7619 | int len; |
| 7620 | struct hlua_function *fcn; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7621 | int nargs; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7622 | struct buffer *trash; |
| 7623 | struct action_kw *akw; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7624 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7625 | /* Initialise the number of expected arguments at 0. */ |
| 7626 | nargs = 0; |
| 7627 | |
| 7628 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 7629 | 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] | 7630 | |
| 7631 | /* First argument : converter name. */ |
| 7632 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7633 | |
| 7634 | /* Second argument : environment. */ |
| 7635 | if (lua_type(L, 2) != LUA_TTABLE) |
| 7636 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7637 | |
| 7638 | /* Third argument : lua function. */ |
| 7639 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7640 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7641 | /* 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] | 7642 | if (lua_gettop(L) >= 4) |
| 7643 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 7644 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7645 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7646 | lua_pushnil(L); |
| 7647 | while (lua_next(L, 2) != 0) { |
| 7648 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7649 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7650 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7651 | /* Check if action exists */ |
| 7652 | trash = get_trash_chunk(); |
| 7653 | chunk_printf(trash, "lua.%s", name); |
| 7654 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) { |
| 7655 | akw = tcp_req_cont_action(trash->area); |
| 7656 | } else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) { |
| 7657 | akw = tcp_res_cont_action(trash->area); |
| 7658 | } else if (strcmp(lua_tostring(L, -1), "http-req") == 0) { |
| 7659 | akw = action_http_req_custom(trash->area); |
| 7660 | } else if (strcmp(lua_tostring(L, -1), "http-res") == 0) { |
| 7661 | akw = action_http_res_custom(trash->area); |
| 7662 | } else { |
| 7663 | akw = NULL; |
| 7664 | } |
| 7665 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 7666 | fcn = akw->private; |
| 7667 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 7668 | ha_warning("Trying to register action 'lua.%s' more than once. " |
| 7669 | "This will become a hard error in version 2.5.\n", name); |
| 7670 | } |
| 7671 | fcn->function_ref[hlua_state_id] = ref; |
| 7672 | |
| 7673 | /* pop the environment string. */ |
| 7674 | lua_pop(L, 1); |
| 7675 | continue; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7676 | } |
| 7677 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7678 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 7679 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7680 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7681 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7682 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 7683 | fcn = new_hlua_function(); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7684 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7685 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7686 | |
| 7687 | /* Fill fcn. */ |
| 7688 | fcn->name = strdup(name); |
| 7689 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7690 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7691 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7692 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 7693 | /* Set the expected number of arguments. */ |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7694 | fcn->nargs = nargs; |
| 7695 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7696 | /* List head */ |
| 7697 | akl->list.n = akl->list.p = NULL; |
| 7698 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7699 | /* action keyword. */ |
| 7700 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7701 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7702 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7703 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7704 | |
| 7705 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7706 | |
| 7707 | akl->kw[0].match_pfx = 0; |
| 7708 | akl->kw[0].private = fcn; |
| 7709 | akl->kw[0].parse = action_register_lua; |
| 7710 | |
| 7711 | /* select the action registering point. */ |
| 7712 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 7713 | tcp_req_cont_keywords_register(akl); |
| 7714 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 7715 | tcp_res_cont_keywords_register(akl); |
| 7716 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 7717 | http_req_keywords_register(akl); |
| 7718 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 7719 | http_res_keywords_register(akl); |
| 7720 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7721 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7722 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 7723 | "are expected.", lua_tostring(L, -1))); |
| 7724 | |
| 7725 | /* pop the environment string. */ |
| 7726 | lua_pop(L, 1); |
| 7727 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7728 | return ACT_RET_PRS_OK; |
| 7729 | } |
| 7730 | |
| 7731 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 7732 | struct act_rule *rule, char **err) |
| 7733 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7734 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7735 | |
Christopher Faulet | 280f85b | 2019-07-15 15:02:04 +0200 | [diff] [blame] | 7736 | if (px->mode == PR_MODE_HTTP) { |
| 7737 | memprintf(err, "Lua TCP services cannot be used on HTTP proxies"); |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 7738 | return ACT_RET_PRS_ERR; |
| 7739 | } |
| 7740 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7741 | /* Memory for the rule. */ |
| 7742 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7743 | if (!rule->arg.hlua_rule) { |
| 7744 | memprintf(err, "out of memory error"); |
| 7745 | return ACT_RET_PRS_ERR; |
| 7746 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7747 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7748 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7749 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7750 | |
| 7751 | /* TODO: later accept arguments. */ |
| 7752 | rule->arg.hlua_rule->args = NULL; |
| 7753 | |
| 7754 | /* Add applet pointer in the rule. */ |
| 7755 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7756 | rule->applet.name = fcn->name; |
| 7757 | rule->applet.init = hlua_applet_tcp_init; |
| 7758 | rule->applet.fct = hlua_applet_tcp_fct; |
| 7759 | rule->applet.release = hlua_applet_tcp_release; |
| 7760 | rule->applet.timeout = hlua_timeout_applet; |
| 7761 | |
| 7762 | return 0; |
| 7763 | } |
| 7764 | |
| 7765 | /* This function is an LUA binding used for registering |
| 7766 | * "sample-conv" functions. It expects a converter name used |
| 7767 | * in the haproxy configuration file, and an LUA function. |
| 7768 | */ |
| 7769 | __LJMP static int hlua_register_service(lua_State *L) |
| 7770 | { |
| 7771 | struct action_kw_list *akl; |
| 7772 | const char *name; |
| 7773 | const char *env; |
| 7774 | int ref; |
| 7775 | int len; |
| 7776 | struct hlua_function *fcn; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7777 | struct buffer *trash; |
| 7778 | struct action_kw *akw; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7779 | |
| 7780 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 7781 | |
| 7782 | /* First argument : converter name. */ |
| 7783 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7784 | |
| 7785 | /* Second argument : environment. */ |
| 7786 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7787 | |
| 7788 | /* Third argument : lua function. */ |
| 7789 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7790 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7791 | /* Check for service already registered */ |
| 7792 | trash = get_trash_chunk(); |
| 7793 | chunk_printf(trash, "lua.%s", name); |
| 7794 | akw = service_find(trash->area); |
| 7795 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 7796 | fcn = akw->private; |
| 7797 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 7798 | ha_warning("Trying to register service 'lua.%s' more than once. " |
| 7799 | "This will become a hard error in version 2.5.\n", name); |
| 7800 | } |
| 7801 | fcn->function_ref[hlua_state_id] = ref; |
| 7802 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7803 | } |
| 7804 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7805 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7806 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 7807 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7808 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 7809 | fcn = new_hlua_function(); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7810 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7811 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7812 | |
| 7813 | /* Fill fcn. */ |
| 7814 | len = strlen("<lua.>") + strlen(name) + 1; |
| 7815 | fcn->name = calloc(1, len); |
| 7816 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7817 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7818 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7819 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7820 | |
| 7821 | /* List head */ |
| 7822 | akl->list.n = akl->list.p = NULL; |
| 7823 | |
| 7824 | /* converter keyword. */ |
| 7825 | len = strlen("lua.") + strlen(name) + 1; |
| 7826 | akl->kw[0].kw = calloc(1, len); |
| 7827 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7828 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7829 | |
| 7830 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7831 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 7832 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7833 | if (strcmp(env, "tcp") == 0) |
| 7834 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7835 | else if (strcmp(env, "http") == 0) |
| 7836 | akl->kw[0].parse = action_register_service_http; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7837 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7838 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 7839 | "'tcp' or 'http' are expected.", env)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7840 | |
| 7841 | akl->kw[0].match_pfx = 0; |
| 7842 | akl->kw[0].private = fcn; |
| 7843 | |
| 7844 | /* End of array. */ |
| 7845 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 7846 | |
| 7847 | /* Register this new converter */ |
| 7848 | service_keywords_register(akl); |
| 7849 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7850 | return 0; |
| 7851 | } |
| 7852 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7853 | /* This function initialises Lua cli handler. It copies the |
| 7854 | * arguments in the Lua stack and create channel IO objects. |
| 7855 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 7856 | 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] | 7857 | { |
| 7858 | struct hlua *hlua; |
| 7859 | struct hlua_function *fcn; |
| 7860 | int i; |
| 7861 | const char *error; |
| 7862 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7863 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7864 | appctx->ctx.hlua_cli.fcn = private; |
| 7865 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7866 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7867 | if (!hlua) { |
| 7868 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7869 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7870 | } |
| 7871 | HLUA_INIT(hlua); |
| 7872 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7873 | |
| 7874 | /* Create task used by signal to wakeup applets. |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 7875 | * 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] | 7876 | * applet_http. It is absolutely compatible. |
| 7877 | */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7878 | appctx->ctx.hlua_cli.task = task_new(tid_bit); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7879 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 7880 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7881 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7882 | } |
| 7883 | appctx->ctx.hlua_cli.task->nice = 0; |
| 7884 | appctx->ctx.hlua_cli.task->context = appctx; |
| 7885 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 7886 | |
| 7887 | /* Initialises the Lua context */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7888 | 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] | 7889 | 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] | 7890 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7891 | } |
| 7892 | |
| 7893 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7894 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7895 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7896 | error = lua_tostring(hlua->T, -1); |
| 7897 | else |
| 7898 | error = "critical error"; |
| 7899 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 7900 | goto error; |
| 7901 | } |
| 7902 | |
| 7903 | /* Check stack available size. */ |
| 7904 | if (!lua_checkstack(hlua->T, 2)) { |
| 7905 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7906 | goto error; |
| 7907 | } |
| 7908 | |
| 7909 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7910 | 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] | 7911 | |
| 7912 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 7913 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7914 | */ |
| 7915 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 7916 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7917 | goto error; |
| 7918 | } |
| 7919 | hlua->nargs = 1; |
| 7920 | |
| 7921 | /* push keywords in the stack. */ |
| 7922 | for (i = 0; *args[i]; i++) { |
| 7923 | /* Check stack available size. */ |
| 7924 | if (!lua_checkstack(hlua->T, 1)) { |
| 7925 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7926 | goto error; |
| 7927 | } |
| 7928 | lua_pushstring(hlua->T, args[i]); |
| 7929 | hlua->nargs++; |
| 7930 | } |
| 7931 | |
| 7932 | /* We must initialize the execution timeouts. */ |
| 7933 | hlua->max_time = hlua_timeout_session; |
| 7934 | |
| 7935 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7936 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7937 | |
| 7938 | /* It's ok */ |
| 7939 | return 0; |
| 7940 | |
| 7941 | /* It's not ok. */ |
| 7942 | error: |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7943 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7944 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7945 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7946 | return 1; |
| 7947 | } |
| 7948 | |
| 7949 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 7950 | { |
| 7951 | struct hlua *hlua; |
| 7952 | struct stream_interface *si; |
| 7953 | struct hlua_function *fcn; |
| 7954 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7955 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7956 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 7957 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7958 | |
| 7959 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7960 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7961 | return 1; |
| 7962 | |
| 7963 | /* Execute the function. */ |
| 7964 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7965 | |
| 7966 | /* finished. */ |
| 7967 | case HLUA_E_OK: |
| 7968 | return 1; |
| 7969 | |
| 7970 | /* yield. */ |
| 7971 | case HLUA_E_AGAIN: |
| 7972 | /* We want write. */ |
| 7973 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 7974 | si_rx_room_blk(si); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7975 | /* Set the timeout. */ |
| 7976 | if (hlua->wake_time != TICK_ETERNITY) |
| 7977 | task_schedule(hlua->task, hlua->wake_time); |
| 7978 | return 0; |
| 7979 | |
| 7980 | /* finished with error. */ |
| 7981 | case HLUA_E_ERRMSG: |
| 7982 | /* Display log. */ |
| 7983 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 7984 | fcn->name, lua_tostring(hlua->T, -1)); |
| 7985 | lua_pop(hlua->T, 1); |
| 7986 | return 1; |
| 7987 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7988 | case HLUA_E_ETMOUT: |
| 7989 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 7990 | fcn->name); |
| 7991 | return 1; |
| 7992 | |
| 7993 | case HLUA_E_NOMEM: |
| 7994 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 7995 | fcn->name); |
| 7996 | return 1; |
| 7997 | |
| 7998 | case HLUA_E_YIELD: /* unexpected */ |
| 7999 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 8000 | fcn->name); |
| 8001 | return 1; |
| 8002 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8003 | case HLUA_E_ERR: |
| 8004 | /* Display log. */ |
| 8005 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 8006 | fcn->name); |
| 8007 | return 1; |
| 8008 | |
| 8009 | default: |
| 8010 | return 1; |
| 8011 | } |
| 8012 | |
| 8013 | return 1; |
| 8014 | } |
| 8015 | |
| 8016 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 8017 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8018 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8019 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8020 | } |
| 8021 | |
| 8022 | /* This function is an LUA binding used for registering |
| 8023 | * new keywords in the cli. It expects a list of keywords |
| 8024 | * which are the "path". It is limited to 5 keywords. A |
| 8025 | * description of the command, a function to be executed |
| 8026 | * for the parsing and a function for io handlers. |
| 8027 | */ |
| 8028 | __LJMP static int hlua_register_cli(lua_State *L) |
| 8029 | { |
| 8030 | struct cli_kw_list *cli_kws; |
| 8031 | const char *message; |
| 8032 | int ref_io; |
| 8033 | int len; |
| 8034 | struct hlua_function *fcn; |
| 8035 | int index; |
| 8036 | int i; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8037 | struct buffer *trash; |
| 8038 | const char *kw[5]; |
| 8039 | struct cli_kw *cli_kw; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8040 | |
| 8041 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 8042 | |
| 8043 | /* First argument : an array of maximum 5 keywords. */ |
| 8044 | if (!lua_istable(L, 1)) |
| 8045 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 8046 | |
| 8047 | /* Second argument : string with contextual message. */ |
| 8048 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8049 | |
| 8050 | /* Third and fourth argument : lua function. */ |
| 8051 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 8052 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8053 | /* Check for CLI service already registered */ |
| 8054 | trash = get_trash_chunk(); |
| 8055 | index = 0; |
| 8056 | lua_pushnil(L); |
| 8057 | memset(kw, 0, sizeof(kw)); |
| 8058 | while (lua_next(L, 1) != 0) { |
| 8059 | if (index >= CLI_PREFIX_KW_NB) |
| 8060 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 8061 | if (lua_type(L, -1) != LUA_TSTRING) |
| 8062 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 8063 | kw[index] = lua_tostring(L, -1); |
| 8064 | if (index == 0) |
| 8065 | chunk_printf(trash, "%s", kw[index]); |
| 8066 | else |
| 8067 | chunk_appendf(trash, " %s", kw[index]); |
| 8068 | index++; |
| 8069 | lua_pop(L, 1); |
| 8070 | } |
| 8071 | cli_kw = cli_find_kw_exact((char **)kw); |
| 8072 | if (cli_kw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8073 | fcn = cli_kw->private; |
| 8074 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 8075 | ha_warning("Trying to register CLI keyword 'lua.%s' more than once. " |
| 8076 | "This will become a hard error in version 2.5.\n", trash->area); |
| 8077 | } |
| 8078 | fcn->function_ref[hlua_state_id] = ref_io; |
| 8079 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8080 | } |
| 8081 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8082 | /* Allocate and fill the sample fetch keyword struct. */ |
| 8083 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
| 8084 | if (!cli_kws) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8085 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 8086 | fcn = new_hlua_function(); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8087 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8088 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8089 | |
| 8090 | /* Fill path. */ |
| 8091 | index = 0; |
| 8092 | lua_pushnil(L); |
| 8093 | while(lua_next(L, 1) != 0) { |
| 8094 | if (index >= 5) |
| 8095 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 8096 | if (lua_type(L, -1) != LUA_TSTRING) |
| 8097 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 8098 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
| 8099 | if (!cli_kws->kw[0].str_kw[index]) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8100 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8101 | index++; |
| 8102 | lua_pop(L, 1); |
| 8103 | } |
| 8104 | |
| 8105 | /* Copy help message. */ |
| 8106 | cli_kws->kw[0].usage = strdup(message); |
| 8107 | if (!cli_kws->kw[0].usage) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8108 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8109 | |
| 8110 | /* Fill fcn io handler. */ |
| 8111 | len = strlen("<lua.cli>") + 1; |
| 8112 | for (i = 0; i < index; i++) |
| 8113 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 8114 | fcn->name = calloc(1, len); |
| 8115 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8116 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8117 | strncat((char *)fcn->name, "<lua.cli", len); |
| 8118 | for (i = 0; i < index; i++) { |
| 8119 | strncat((char *)fcn->name, ".", len); |
| 8120 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 8121 | } |
| 8122 | strncat((char *)fcn->name, ">", len); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8123 | fcn->function_ref[hlua_state_id] = ref_io; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8124 | |
| 8125 | /* Fill last entries. */ |
| 8126 | cli_kws->kw[0].private = fcn; |
| 8127 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 8128 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 8129 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 8130 | |
| 8131 | /* Register this new converter */ |
| 8132 | cli_register_kw(cli_kws); |
| 8133 | |
| 8134 | return 0; |
| 8135 | } |
| 8136 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8137 | 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] | 8138 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8139 | char **err, unsigned int *timeout) |
| 8140 | { |
| 8141 | const char *error; |
| 8142 | |
| 8143 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 8144 | if (error == PARSE_TIME_OVER) { |
| 8145 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 8146 | args[1], args[0]); |
| 8147 | return -1; |
| 8148 | } |
| 8149 | else if (error == PARSE_TIME_UNDER) { |
| 8150 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 8151 | args[1], args[0]); |
| 8152 | return -1; |
| 8153 | } |
| 8154 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8155 | memprintf(err, "%s: invalid timeout", args[0]); |
| 8156 | return -1; |
| 8157 | } |
| 8158 | return 0; |
| 8159 | } |
| 8160 | |
| 8161 | 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] | 8162 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8163 | char **err) |
| 8164 | { |
| 8165 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8166 | file, line, err, &hlua_timeout_session); |
| 8167 | } |
| 8168 | |
| 8169 | 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] | 8170 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8171 | char **err) |
| 8172 | { |
| 8173 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8174 | file, line, err, &hlua_timeout_task); |
| 8175 | } |
| 8176 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8177 | 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] | 8178 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8179 | char **err) |
| 8180 | { |
| 8181 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8182 | file, line, err, &hlua_timeout_applet); |
| 8183 | } |
| 8184 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8185 | 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] | 8186 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8187 | char **err) |
| 8188 | { |
| 8189 | char *error; |
| 8190 | |
| 8191 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 8192 | if (*error != '\0') { |
| 8193 | memprintf(err, "%s: invalid number", args[0]); |
| 8194 | return -1; |
| 8195 | } |
| 8196 | return 0; |
| 8197 | } |
| 8198 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8199 | 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] | 8200 | const struct proxy *defpx, const char *file, int line, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8201 | char **err) |
| 8202 | { |
| 8203 | char *error; |
| 8204 | |
| 8205 | if (*(args[1]) == 0) { |
| 8206 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 8207 | return -1; |
| 8208 | } |
| 8209 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 8210 | if (*error != '\0') { |
| 8211 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 8212 | return -1; |
| 8213 | } |
| 8214 | return 0; |
| 8215 | } |
| 8216 | |
| 8217 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8218 | /* This function is called by the main configuration key "lua-load". It loads and |
| 8219 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 8220 | * the main lua entry point. |
| 8221 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8222 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 8223 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8224 | * |
| 8225 | * In some error case, LUA set an error message in top of the stack. This function |
| 8226 | * 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] | 8227 | * |
| 8228 | * This function can fail with an abort() due to an Lua critical error. |
| 8229 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 8230 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8231 | */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8232 | static int hlua_load_state(char *filename, lua_State *L, char **err) |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8233 | { |
| 8234 | int error; |
| 8235 | |
| 8236 | /* Just load and compile the file. */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8237 | error = luaL_loadfile(L, filename); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8238 | if (error) { |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8239 | memprintf(err, "error in Lua file '%s': %s", filename, lua_tostring(L, -1)); |
| 8240 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8241 | return -1; |
| 8242 | } |
| 8243 | |
| 8244 | /* If no syntax error where detected, execute the code. */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8245 | error = lua_pcall(L, 0, LUA_MULTRET, 0); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8246 | switch (error) { |
| 8247 | case LUA_OK: |
| 8248 | break; |
| 8249 | case LUA_ERRRUN: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8250 | memprintf(err, "Lua runtime error: %s\n", lua_tostring(L, -1)); |
| 8251 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8252 | return -1; |
| 8253 | case LUA_ERRMEM: |
Thierry Fournier | de6145f | 2020-11-29 00:55:53 +0100 | [diff] [blame] | 8254 | memprintf(err, "Lua out of memory error\n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8255 | return -1; |
| 8256 | case LUA_ERRERR: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8257 | memprintf(err, "Lua message handler error: %s\n", lua_tostring(L, -1)); |
| 8258 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8259 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 8260 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8261 | case LUA_ERRGCMM: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8262 | memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(L, -1)); |
| 8263 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8264 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 8265 | #endif |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8266 | default: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8267 | memprintf(err, "Lua unknown error: %s\n", lua_tostring(L, -1)); |
| 8268 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8269 | return -1; |
| 8270 | } |
| 8271 | |
| 8272 | return 0; |
| 8273 | } |
| 8274 | |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8275 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 8276 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8277 | char **err) |
| 8278 | { |
| 8279 | if (*(args[1]) == 0) { |
| 8280 | memprintf(err, "'%s' expects a file name as parameter.\n", args[0]); |
| 8281 | return -1; |
| 8282 | } |
| 8283 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8284 | /* loading for global state */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8285 | hlua_state_id = 0; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8286 | ha_set_tid(0); |
Thierry Fournier | afc63e2 | 2020-11-28 17:06:51 +0100 | [diff] [blame] | 8287 | return hlua_load_state(args[1], hlua_states[0], err); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8288 | } |
| 8289 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8290 | 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] | 8291 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8292 | char **err) |
| 8293 | { |
| 8294 | int len; |
| 8295 | |
| 8296 | if (*(args[1]) == 0) { |
| 8297 | memprintf(err, "'%s' expects a file as parameter.\n", args[0]); |
| 8298 | return -1; |
| 8299 | } |
| 8300 | |
| 8301 | if (per_thread_load == NULL) { |
| 8302 | /* allocate the first entry large enough to store the final NULL */ |
| 8303 | per_thread_load = calloc(1, sizeof(*per_thread_load)); |
| 8304 | if (per_thread_load == NULL) { |
| 8305 | memprintf(err, "out of memory error"); |
| 8306 | return -1; |
| 8307 | } |
| 8308 | } |
| 8309 | |
| 8310 | /* count used entries */ |
| 8311 | for (len = 0; per_thread_load[len] != NULL; len++) |
| 8312 | ; |
| 8313 | |
| 8314 | per_thread_load = realloc(per_thread_load, (len + 2) * sizeof(*per_thread_load)); |
| 8315 | if (per_thread_load == NULL) { |
| 8316 | memprintf(err, "out of memory error"); |
| 8317 | return -1; |
| 8318 | } |
| 8319 | |
| 8320 | per_thread_load[len] = strdup(args[1]); |
| 8321 | per_thread_load[len + 1] = NULL; |
| 8322 | |
| 8323 | if (per_thread_load[len] == NULL) { |
| 8324 | memprintf(err, "out of memory error"); |
| 8325 | return -1; |
| 8326 | } |
| 8327 | |
| 8328 | /* loading for thread 1 only */ |
| 8329 | hlua_state_id = 1; |
| 8330 | ha_set_tid(0); |
| 8331 | return hlua_load_state(args[1], hlua_states[1], err); |
| 8332 | } |
| 8333 | |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 8334 | /* Prepend the given <path> followed by a semicolon to the `package.<type>` variable |
| 8335 | * in the given <ctx>. |
| 8336 | */ |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 8337 | static int hlua_prepend_path(lua_State *L, char *type, char *path) |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 8338 | { |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 8339 | lua_getglobal(L, "package"); /* push package variable */ |
| 8340 | lua_pushstring(L, path); /* push given path */ |
| 8341 | lua_pushstring(L, ";"); /* push semicolon */ |
| 8342 | lua_getfield(L, -3, type); /* push old path */ |
| 8343 | lua_concat(L, 3); /* concatenate to new path */ |
| 8344 | lua_setfield(L, -2, type); /* store new path */ |
| 8345 | lua_pop(L, 1); /* pop package variable */ |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 8346 | |
| 8347 | return 0; |
| 8348 | } |
| 8349 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8350 | 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] | 8351 | const struct proxy *defpx, const char *file, int line, |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8352 | char **err) |
| 8353 | { |
| 8354 | char *path; |
| 8355 | char *type = "path"; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8356 | struct prepend_path *p = NULL; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8357 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8358 | if (too_many_args(2, args, err, NULL)) { |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8359 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8360 | } |
| 8361 | |
| 8362 | if (!(*args[1])) { |
| 8363 | memprintf(err, "'%s' expects to receive a <path> as argument", args[0]); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8364 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8365 | } |
| 8366 | path = args[1]; |
| 8367 | |
| 8368 | if (*args[2]) { |
| 8369 | if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) { |
| 8370 | 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] | 8371 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8372 | } |
| 8373 | type = args[2]; |
| 8374 | } |
| 8375 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8376 | p = calloc(1, sizeof(*p)); |
| 8377 | if (p == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 8378 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8379 | goto err; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8380 | } |
| 8381 | p->path = strdup(path); |
| 8382 | if (p->path == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 8383 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8384 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8385 | } |
| 8386 | p->type = strdup(type); |
| 8387 | if (p->type == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 8388 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8389 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8390 | } |
| 8391 | LIST_ADDQ(&prepend_path_list, &p->l); |
| 8392 | |
| 8393 | hlua_prepend_path(hlua_states[0], type, path); |
| 8394 | hlua_prepend_path(hlua_states[1], type, path); |
| 8395 | return 0; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8396 | |
| 8397 | err2: |
| 8398 | free(p->type); |
| 8399 | free(p->path); |
| 8400 | err: |
| 8401 | free(p); |
| 8402 | return -1; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8403 | } |
| 8404 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8405 | /* configuration keywords declaration */ |
| 8406 | static struct cfg_kw_list cfg_kws = {{ },{ |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8407 | { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8408 | { CFG_GLOBAL, "lua-load", hlua_load }, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8409 | { CFG_GLOBAL, "lua-load-per-thread", hlua_load_per_thread }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8410 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 8411 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 8412 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8413 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8414 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8415 | { 0, NULL, NULL }, |
| 8416 | }}; |
| 8417 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 8418 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 8419 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 8420 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8421 | /* This function can fail with an abort() due to an Lua critical error. |
| 8422 | * We are in the initialisation process of HAProxy, this abort() is |
| 8423 | * tolerated. |
| 8424 | */ |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8425 | int hlua_post_init_state(lua_State *L) |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8426 | { |
| 8427 | struct hlua_init_function *init; |
| 8428 | const char *msg; |
| 8429 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8430 | const char *error; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8431 | const char *kind; |
| 8432 | const char *trace; |
| 8433 | int return_status = 1; |
| 8434 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 8435 | int nres; |
| 8436 | #endif |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8437 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8438 | /* disable memory limit checks if limit is not set */ |
| 8439 | if (!hlua_global_allocator.limit) |
| 8440 | hlua_global_allocator.limit = ~hlua_global_allocator.limit; |
| 8441 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 8442 | /* Call post initialisation function in safe environment. */ |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8443 | if (setjmp(safe_ljmp_env) != 0) { |
| 8444 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8445 | if (lua_type(L, -1) == LUA_TSTRING) |
| 8446 | error = lua_tostring(L, -1); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8447 | else |
| 8448 | error = "critical error"; |
| 8449 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 8450 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8451 | } else { |
| 8452 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8453 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 8454 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8455 | hlua_fcn_post_init(L); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8456 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8457 | list_for_each_entry(init, &hlua_init_functions[hlua_state_id], l) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8458 | lua_rawgeti(L, LUA_REGISTRYINDEX, init->function_ref); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8459 | |
| 8460 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8461 | ret = lua_resume(L, L, 0, &nres); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8462 | #else |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8463 | ret = lua_resume(L, L, 0); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8464 | #endif |
| 8465 | kind = NULL; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8466 | switch (ret) { |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8467 | |
| 8468 | case LUA_OK: |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8469 | lua_pop(L, -1); |
Thierry Fournier | 13d08b7 | 2020-11-28 11:02:58 +0100 | [diff] [blame] | 8470 | break; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8471 | |
| 8472 | case LUA_ERRERR: |
| 8473 | kind = "message handler error"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8474 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8475 | case LUA_ERRRUN: |
| 8476 | if (!kind) |
| 8477 | kind = "runtime error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8478 | msg = lua_tostring(L, -1); |
| 8479 | lua_settop(L, 0); /* Empty the stack. */ |
| 8480 | lua_pop(L, 1); |
| 8481 | trace = hlua_traceback(L); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8482 | if (msg) |
| 8483 | ha_alert("Lua init: %s: '%s' from %s\n", kind, msg, trace); |
| 8484 | else |
| 8485 | ha_alert("Lua init: unknown %s from %s\n", kind, trace); |
| 8486 | return_status = 0; |
| 8487 | break; |
| 8488 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8489 | default: |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8490 | /* Unknown error */ |
| 8491 | kind = "Unknown error"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8492 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8493 | case LUA_YIELD: |
| 8494 | /* yield is not configured at this step, this state doesn't happen */ |
| 8495 | if (!kind) |
| 8496 | kind = "yield not allowed"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8497 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8498 | case LUA_ERRMEM: |
| 8499 | if (!kind) |
| 8500 | kind = "out of memory error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8501 | lua_settop(L, 0); |
| 8502 | lua_pop(L, 1); |
| 8503 | trace = hlua_traceback(L); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8504 | ha_alert("Lua init: %s: %s\n", kind, trace); |
| 8505 | return_status = 0; |
| 8506 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8507 | } |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8508 | if (!return_status) |
| 8509 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8510 | } |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8511 | |
| 8512 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8513 | return return_status; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8514 | } |
| 8515 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8516 | int hlua_post_init() |
| 8517 | { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8518 | int ret; |
| 8519 | int i; |
| 8520 | int errors; |
| 8521 | char *err = NULL; |
| 8522 | struct hlua_function *fcn; |
| 8523 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8524 | #if USE_OPENSSL |
| 8525 | /* Initialize SSL server. */ |
| 8526 | if (socket_ssl.xprt->prepare_srv) { |
| 8527 | int saved_used_backed = global.ssl_used_backend; |
| 8528 | // don't affect maxconn automatic computation |
| 8529 | socket_ssl.xprt->prepare_srv(&socket_ssl); |
| 8530 | global.ssl_used_backend = saved_used_backed; |
| 8531 | } |
| 8532 | #endif |
| 8533 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8534 | /* Perform post init of common thread */ |
| 8535 | hlua_state_id = 0; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8536 | ha_set_tid(0); |
| 8537 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 8538 | if (ret == 0) |
| 8539 | return 0; |
| 8540 | |
| 8541 | /* init remaining lua states and load files */ |
| 8542 | for (hlua_state_id = 2; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 8543 | |
| 8544 | /* set thread context */ |
| 8545 | ha_set_tid(hlua_state_id - 1); |
| 8546 | |
| 8547 | /* Init lua state */ |
| 8548 | hlua_states[hlua_state_id] = hlua_init_state(hlua_state_id); |
| 8549 | |
| 8550 | /* Load lua files */ |
| 8551 | for (i = 0; per_thread_load && per_thread_load[i]; i++) { |
| 8552 | ret = hlua_load_state(per_thread_load[i], hlua_states[hlua_state_id], &err); |
| 8553 | if (ret != 0) { |
| 8554 | ha_alert("Lua init: %s\n", err); |
| 8555 | return 0; |
| 8556 | } |
| 8557 | } |
| 8558 | } |
| 8559 | |
| 8560 | /* Reset thread context */ |
| 8561 | ha_set_tid(0); |
| 8562 | |
| 8563 | /* Execute post init for all states */ |
| 8564 | for (hlua_state_id = 1; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 8565 | |
| 8566 | /* set thread context */ |
| 8567 | ha_set_tid(hlua_state_id - 1); |
| 8568 | |
| 8569 | /* run post init */ |
| 8570 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 8571 | if (ret == 0) |
| 8572 | return 0; |
| 8573 | } |
| 8574 | |
| 8575 | /* Reset thread context */ |
| 8576 | ha_set_tid(0); |
| 8577 | |
| 8578 | /* control functions registering. Each function must have: |
| 8579 | * - only the function_ref[0] set positive and all other to -1 |
| 8580 | * - only the function_ref[0] set to -1 and all other positive |
| 8581 | * This ensure a same reference is not used both in shared |
| 8582 | * lua state and thread dedicated lua state. Note: is the case |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 8583 | * reach, the shared state is priority, but the bug will be |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8584 | * complicated to found for the end user. |
| 8585 | */ |
| 8586 | errors = 0; |
| 8587 | list_for_each_entry(fcn, &referenced_functions, l) { |
| 8588 | ret = 0; |
| 8589 | for (i = 1; i < global.nbthread + 1; i++) { |
| 8590 | if (fcn->function_ref[i] == -1) |
| 8591 | ret--; |
| 8592 | else |
| 8593 | ret++; |
| 8594 | } |
| 8595 | if (abs(ret) != global.nbthread) { |
| 8596 | ha_alert("Lua function '%s' is not referenced in all thread. " |
| 8597 | "Expect function in all thread or in none thread.\n", fcn->name); |
| 8598 | errors++; |
| 8599 | continue; |
| 8600 | } |
| 8601 | |
| 8602 | if ((fcn->function_ref[0] == -1) == (ret < 0)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 8603 | ha_alert("Lua function '%s' is referenced both ins shared Lua context (through lua-load) " |
| 8604 | "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] | 8605 | "exclusive.\n", fcn->name); |
| 8606 | errors++; |
| 8607 | } |
| 8608 | } |
| 8609 | |
| 8610 | if (errors > 0) |
| 8611 | return 0; |
| 8612 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 8613 | /* 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] | 8614 | * -1 in order to have probably a segfault if someone use it |
| 8615 | */ |
| 8616 | hlua_state_id = -1; |
| 8617 | |
| 8618 | return 1; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8619 | } |
| 8620 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8621 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 8622 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 8623 | * 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] | 8624 | * allocation. <nsize> is the requested new size. A new allocation is |
| 8625 | * indicated by <ptr> being NULL. A free is indicated by <nsize> being |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8626 | * zero. This one verifies that the limits are respected but is optimized |
| 8627 | * 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] | 8628 | */ |
| 8629 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 8630 | { |
| 8631 | struct hlua_mem_allocator *zone = ud; |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8632 | size_t limit, old, new; |
| 8633 | |
Tim Duesterhus | 2258652 | 2021-01-08 10:35:33 +0100 | [diff] [blame] | 8634 | if (unlikely(!ptr && !nsize)) |
| 8635 | return NULL; |
| 8636 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8637 | /* a limit of ~0 means unlimited and boot complete, so there's no need |
| 8638 | * for accounting anymore. |
| 8639 | */ |
Christopher Faulet | a61789a | 2021-03-19 15:16:28 +0100 | [diff] [blame] | 8640 | if (likely(~zone->limit == 0)) { |
| 8641 | hlua_not_dumpable++; |
| 8642 | ptr = realloc(ptr, nsize); |
| 8643 | hlua_not_dumpable--; |
| 8644 | return ptr; |
| 8645 | } |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8646 | |
Willy Tarreau | d36c7fa | 2020-12-02 12:26:29 +0100 | [diff] [blame] | 8647 | if (!ptr) |
| 8648 | osize = 0; |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8649 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8650 | /* enforce strict limits across all threads */ |
| 8651 | limit = zone->limit; |
| 8652 | old = _HA_ATOMIC_LOAD(&zone->allocated); |
| 8653 | do { |
| 8654 | new = old + nsize - osize; |
| 8655 | if (unlikely(nsize && limit && new > limit)) |
| 8656 | return NULL; |
| 8657 | } while (!_HA_ATOMIC_CAS(&zone->allocated, &old, new)); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8658 | |
Christopher Faulet | a61789a | 2021-03-19 15:16:28 +0100 | [diff] [blame] | 8659 | hlua_not_dumpable++; |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8660 | ptr = realloc(ptr, nsize); |
Christopher Faulet | a61789a | 2021-03-19 15:16:28 +0100 | [diff] [blame] | 8661 | hlua_not_dumpable--; |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8662 | |
| 8663 | if (unlikely(!ptr && nsize)) // failed |
| 8664 | _HA_ATOMIC_SUB(&zone->allocated, nsize - osize); |
| 8665 | |
| 8666 | __ha_barrier_atomic_store(); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8667 | return ptr; |
| 8668 | } |
| 8669 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 8670 | /* 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] | 8671 | * We are in the initialisation process of HAProxy, this abort() is |
| 8672 | * tolerated. |
| 8673 | */ |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 8674 | lua_State *hlua_init_state(int thread_num) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8675 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8676 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8677 | int idx; |
| 8678 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8679 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8680 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8681 | const char *error_msg; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8682 | void **context; |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8683 | lua_State *L; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8684 | struct prepend_path *pp; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8685 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8686 | /* Init main lua stack. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8687 | L = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8688 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8689 | /* Initialise Lua context to NULL */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8690 | context = lua_getextraspace(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8691 | *context = NULL; |
| 8692 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8693 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8694 | * the Lua function can fail with an abort. We are in the initialisation |
| 8695 | * process of HAProxy, this abort() is tolerated. |
| 8696 | */ |
| 8697 | |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8698 | /* Call post initialisation function in safe environment. */ |
| 8699 | if (setjmp(safe_ljmp_env) != 0) { |
| 8700 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8701 | if (lua_type(L, -1) == LUA_TSTRING) |
| 8702 | error_msg = lua_tostring(L, -1); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 8703 | else |
| 8704 | error_msg = "critical error"; |
| 8705 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
| 8706 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8707 | } else { |
| 8708 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 8709 | } |
| 8710 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8711 | /* Initialise lua. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8712 | luaL_openlibs(L); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 8713 | #define HLUA_PREPEND_PATH_TOSTRING1(x) #x |
| 8714 | #define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x) |
| 8715 | #ifdef HLUA_PREPEND_PATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8716 | hlua_prepend_path(L, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 8717 | #endif |
| 8718 | #ifdef HLUA_PREPEND_CPATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8719 | hlua_prepend_path(L, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 8720 | #endif |
| 8721 | #undef HLUA_PREPEND_PATH_TOSTRING |
| 8722 | #undef HLUA_PREPEND_PATH_TOSTRING1 |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8723 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8724 | /* Apply configured prepend path */ |
| 8725 | list_for_each_entry(pp, &prepend_path_list, l) |
| 8726 | hlua_prepend_path(L, pp->type, pp->path); |
| 8727 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8728 | /* |
| 8729 | * |
| 8730 | * Create "core" object. |
| 8731 | * |
| 8732 | */ |
| 8733 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 8734 | /* This table entry is the object "core" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8735 | lua_newtable(L); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8736 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 8737 | /* set the thread id */ |
| 8738 | hlua_class_const_int(L, "thread", thread_num); |
| 8739 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8740 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8741 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8742 | hlua_class_const_int(L, log_levels[i], i); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8743 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8744 | /* Register special functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8745 | hlua_class_function(L, "register_init", hlua_register_init); |
| 8746 | hlua_class_function(L, "register_task", hlua_register_task); |
| 8747 | hlua_class_function(L, "register_fetches", hlua_register_fetches); |
| 8748 | hlua_class_function(L, "register_converters", hlua_register_converters); |
| 8749 | hlua_class_function(L, "register_action", hlua_register_action); |
| 8750 | hlua_class_function(L, "register_service", hlua_register_service); |
| 8751 | hlua_class_function(L, "register_cli", hlua_register_cli); |
| 8752 | hlua_class_function(L, "yield", hlua_yield); |
| 8753 | hlua_class_function(L, "set_nice", hlua_set_nice); |
| 8754 | hlua_class_function(L, "sleep", hlua_sleep); |
| 8755 | hlua_class_function(L, "msleep", hlua_msleep); |
| 8756 | hlua_class_function(L, "add_acl", hlua_add_acl); |
| 8757 | hlua_class_function(L, "del_acl", hlua_del_acl); |
| 8758 | hlua_class_function(L, "set_map", hlua_set_map); |
| 8759 | hlua_class_function(L, "del_map", hlua_del_map); |
| 8760 | hlua_class_function(L, "tcp", hlua_socket_new); |
| 8761 | hlua_class_function(L, "log", hlua_log); |
| 8762 | hlua_class_function(L, "Debug", hlua_log_debug); |
| 8763 | hlua_class_function(L, "Info", hlua_log_info); |
| 8764 | hlua_class_function(L, "Warning", hlua_log_warning); |
| 8765 | hlua_class_function(L, "Alert", hlua_log_alert); |
| 8766 | hlua_class_function(L, "done", hlua_done); |
| 8767 | hlua_fcn_reg_core_fcn(L); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8768 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8769 | lua_setglobal(L, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8770 | |
| 8771 | /* |
| 8772 | * |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8773 | * Create "act" object. |
| 8774 | * |
| 8775 | */ |
| 8776 | |
| 8777 | /* This table entry is the object "act" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8778 | lua_newtable(L); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8779 | |
| 8780 | /* push action return constants */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8781 | hlua_class_const_int(L, "CONTINUE", ACT_RET_CONT); |
| 8782 | hlua_class_const_int(L, "STOP", ACT_RET_STOP); |
| 8783 | hlua_class_const_int(L, "YIELD", ACT_RET_YIELD); |
| 8784 | hlua_class_const_int(L, "ERROR", ACT_RET_ERR); |
| 8785 | hlua_class_const_int(L, "DONE", ACT_RET_DONE); |
| 8786 | hlua_class_const_int(L, "DENY", ACT_RET_DENY); |
| 8787 | hlua_class_const_int(L, "ABORT", ACT_RET_ABRT); |
| 8788 | hlua_class_const_int(L, "INVALID", ACT_RET_INV); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8789 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8790 | hlua_class_function(L, "wake_time", hlua_set_wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8791 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8792 | lua_setglobal(L, "act"); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8793 | |
| 8794 | /* |
| 8795 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8796 | * Register class Map |
| 8797 | * |
| 8798 | */ |
| 8799 | |
| 8800 | /* This table entry is the object "Map" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8801 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8802 | |
| 8803 | /* register pattern types. */ |
| 8804 | for (i=0; i<PAT_MATCH_NUM; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8805 | hlua_class_const_int(L, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8806 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8807 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8808 | hlua_class_const_int(L, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8809 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8810 | |
| 8811 | /* register constructor. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8812 | hlua_class_function(L, "new", hlua_map_new); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8813 | |
| 8814 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8815 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8816 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8817 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8818 | lua_pushstring(L, "__index"); |
| 8819 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8820 | |
| 8821 | /* Register . */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8822 | hlua_class_function(L, "lookup", hlua_map_lookup); |
| 8823 | hlua_class_function(L, "slookup", hlua_map_slookup); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8824 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8825 | lua_rawset(L, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8826 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8827 | /* Register previous table in the registry with reference and named entry. |
| 8828 | * The function hlua_register_metatable() pops the stack, so we |
| 8829 | * previously create a copy of the table. |
| 8830 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8831 | lua_pushvalue(L, -1); /* Copy the -1 entry and push it on the stack. */ |
| 8832 | class_map_ref = hlua_register_metatable(L, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8833 | |
| 8834 | /* Assign the metatable to the mai Map object. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8835 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8836 | |
| 8837 | /* Set a name to the table. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8838 | lua_setglobal(L, "Map"); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8839 | |
| 8840 | /* |
| 8841 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8842 | * Register class Channel |
| 8843 | * |
| 8844 | */ |
| 8845 | |
| 8846 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8847 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8848 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8849 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8850 | lua_pushstring(L, "__index"); |
| 8851 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8852 | |
| 8853 | /* Register . */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8854 | hlua_class_function(L, "get", hlua_channel_get); |
| 8855 | hlua_class_function(L, "dup", hlua_channel_dup); |
| 8856 | hlua_class_function(L, "getline", hlua_channel_getline); |
| 8857 | hlua_class_function(L, "set", hlua_channel_set); |
| 8858 | hlua_class_function(L, "append", hlua_channel_append); |
| 8859 | hlua_class_function(L, "send", hlua_channel_send); |
| 8860 | hlua_class_function(L, "forward", hlua_channel_forward); |
| 8861 | hlua_class_function(L, "get_in_len", hlua_channel_get_in_len); |
| 8862 | hlua_class_function(L, "get_out_len", hlua_channel_get_out_len); |
| 8863 | hlua_class_function(L, "is_full", hlua_channel_is_full); |
| 8864 | hlua_class_function(L, "is_resp", hlua_channel_is_resp); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8865 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8866 | lua_rawset(L, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8867 | |
| 8868 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8869 | class_channel_ref = hlua_register_metatable(L, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8870 | |
| 8871 | /* |
| 8872 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8873 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8874 | * |
| 8875 | */ |
| 8876 | |
| 8877 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8878 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8879 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8880 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8881 | lua_pushstring(L, "__index"); |
| 8882 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8883 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8884 | /* Browse existing fetches and create the associated |
| 8885 | * object method. |
| 8886 | */ |
| 8887 | sf = NULL; |
| 8888 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8889 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 8890 | * by an underscore. |
| 8891 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8892 | strncpy(trash.area, sf->kw, trash.size); |
| 8893 | trash.area[trash.size - 1] = '\0'; |
| 8894 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8895 | if (*p == '.' || *p == '-' || *p == '+') |
| 8896 | *p = '_'; |
| 8897 | |
| 8898 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8899 | lua_pushstring(L, trash.area); |
| 8900 | lua_pushlightuserdata(L, sf); |
| 8901 | lua_pushcclosure(L, hlua_run_sample_fetch, 1); |
| 8902 | lua_rawset(L, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8903 | } |
| 8904 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8905 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8906 | |
| 8907 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8908 | class_fetches_ref = hlua_register_metatable(L, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8909 | |
| 8910 | /* |
| 8911 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8912 | * Register class Converters |
| 8913 | * |
| 8914 | */ |
| 8915 | |
| 8916 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8917 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8918 | |
| 8919 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8920 | lua_pushstring(L, "__index"); |
| 8921 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8922 | |
| 8923 | /* Browse existing converters and create the associated |
| 8924 | * object method. |
| 8925 | */ |
| 8926 | sc = NULL; |
| 8927 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8928 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 8929 | * by an underscore. |
| 8930 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8931 | strncpy(trash.area, sc->kw, trash.size); |
| 8932 | trash.area[trash.size - 1] = '\0'; |
| 8933 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8934 | if (*p == '.' || *p == '-' || *p == '+') |
| 8935 | *p = '_'; |
| 8936 | |
| 8937 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8938 | lua_pushstring(L, trash.area); |
| 8939 | lua_pushlightuserdata(L, sc); |
| 8940 | lua_pushcclosure(L, hlua_run_sample_conv, 1); |
| 8941 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8942 | } |
| 8943 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8944 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8945 | |
| 8946 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8947 | class_converters_ref = hlua_register_metatable(L, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8948 | |
| 8949 | /* |
| 8950 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8951 | * Register class HTTP |
| 8952 | * |
| 8953 | */ |
| 8954 | |
| 8955 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8956 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8957 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8958 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8959 | lua_pushstring(L, "__index"); |
| 8960 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8961 | |
| 8962 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8963 | hlua_class_function(L, "req_get_headers",hlua_http_req_get_headers); |
| 8964 | hlua_class_function(L, "req_del_header", hlua_http_req_del_hdr); |
| 8965 | hlua_class_function(L, "req_rep_header", hlua_http_req_rep_hdr); |
| 8966 | hlua_class_function(L, "req_rep_value", hlua_http_req_rep_val); |
| 8967 | hlua_class_function(L, "req_add_header", hlua_http_req_add_hdr); |
| 8968 | hlua_class_function(L, "req_set_header", hlua_http_req_set_hdr); |
| 8969 | hlua_class_function(L, "req_set_method", hlua_http_req_set_meth); |
| 8970 | hlua_class_function(L, "req_set_path", hlua_http_req_set_path); |
| 8971 | hlua_class_function(L, "req_set_query", hlua_http_req_set_query); |
| 8972 | hlua_class_function(L, "req_set_uri", hlua_http_req_set_uri); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8973 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8974 | hlua_class_function(L, "res_get_headers",hlua_http_res_get_headers); |
| 8975 | hlua_class_function(L, "res_del_header", hlua_http_res_del_hdr); |
| 8976 | hlua_class_function(L, "res_rep_header", hlua_http_res_rep_hdr); |
| 8977 | hlua_class_function(L, "res_rep_value", hlua_http_res_rep_val); |
| 8978 | hlua_class_function(L, "res_add_header", hlua_http_res_add_hdr); |
| 8979 | hlua_class_function(L, "res_set_header", hlua_http_res_set_hdr); |
| 8980 | hlua_class_function(L, "res_set_status", hlua_http_res_set_status); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8981 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8982 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +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_http_ref = hlua_register_metatable(L, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8986 | |
| 8987 | /* |
| 8988 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8989 | * Register class AppletTCP |
| 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 | f0a64b6 | 2015-09-19 12:36:17 +0200 | [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 | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8999 | |
| 9000 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9001 | hlua_class_function(L, "getline", hlua_applet_tcp_getline); |
| 9002 | hlua_class_function(L, "receive", hlua_applet_tcp_recv); |
| 9003 | hlua_class_function(L, "send", hlua_applet_tcp_send); |
| 9004 | hlua_class_function(L, "set_priv", hlua_applet_tcp_set_priv); |
| 9005 | hlua_class_function(L, "get_priv", hlua_applet_tcp_get_priv); |
| 9006 | hlua_class_function(L, "set_var", hlua_applet_tcp_set_var); |
| 9007 | hlua_class_function(L, "unset_var", hlua_applet_tcp_unset_var); |
| 9008 | hlua_class_function(L, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9009 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9010 | lua_settable(L, -3); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9011 | |
| 9012 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9013 | class_applet_tcp_ref = hlua_register_metatable(L, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9014 | |
| 9015 | /* |
| 9016 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9017 | * Register class AppletHTTP |
| 9018 | * |
| 9019 | */ |
| 9020 | |
| 9021 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9022 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9023 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9024 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9025 | lua_pushstring(L, "__index"); |
| 9026 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9027 | |
| 9028 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9029 | hlua_class_function(L, "set_priv", hlua_applet_http_set_priv); |
| 9030 | hlua_class_function(L, "get_priv", hlua_applet_http_get_priv); |
| 9031 | hlua_class_function(L, "set_var", hlua_applet_http_set_var); |
| 9032 | hlua_class_function(L, "unset_var", hlua_applet_http_unset_var); |
| 9033 | hlua_class_function(L, "get_var", hlua_applet_http_get_var); |
| 9034 | hlua_class_function(L, "getline", hlua_applet_http_getline); |
| 9035 | hlua_class_function(L, "receive", hlua_applet_http_recv); |
| 9036 | hlua_class_function(L, "send", hlua_applet_http_send); |
| 9037 | hlua_class_function(L, "add_header", hlua_applet_http_addheader); |
| 9038 | hlua_class_function(L, "set_status", hlua_applet_http_status); |
| 9039 | hlua_class_function(L, "start_response", hlua_applet_http_start_response); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9040 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9041 | lua_settable(L, -3); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9042 | |
| 9043 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9044 | class_applet_http_ref = hlua_register_metatable(L, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9045 | |
| 9046 | /* |
| 9047 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9048 | * Register class TXN |
| 9049 | * |
| 9050 | */ |
| 9051 | |
| 9052 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9053 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9054 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9055 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9056 | lua_pushstring(L, "__index"); |
| 9057 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9058 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 9059 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9060 | hlua_class_function(L, "set_priv", hlua_set_priv); |
| 9061 | hlua_class_function(L, "get_priv", hlua_get_priv); |
| 9062 | hlua_class_function(L, "set_var", hlua_set_var); |
| 9063 | hlua_class_function(L, "unset_var", hlua_unset_var); |
| 9064 | hlua_class_function(L, "get_var", hlua_get_var); |
| 9065 | hlua_class_function(L, "done", hlua_txn_done); |
| 9066 | hlua_class_function(L, "reply", hlua_txn_reply_new); |
| 9067 | hlua_class_function(L, "set_loglevel", hlua_txn_set_loglevel); |
| 9068 | hlua_class_function(L, "set_tos", hlua_txn_set_tos); |
| 9069 | hlua_class_function(L, "set_mark", hlua_txn_set_mark); |
| 9070 | hlua_class_function(L, "set_priority_class", hlua_txn_set_priority_class); |
| 9071 | hlua_class_function(L, "set_priority_offset", hlua_txn_set_priority_offset); |
| 9072 | hlua_class_function(L, "deflog", hlua_txn_deflog); |
| 9073 | hlua_class_function(L, "log", hlua_txn_log); |
| 9074 | hlua_class_function(L, "Debug", hlua_txn_log_debug); |
| 9075 | hlua_class_function(L, "Info", hlua_txn_log_info); |
| 9076 | hlua_class_function(L, "Warning", hlua_txn_log_warning); |
| 9077 | hlua_class_function(L, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 9078 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9079 | lua_rawset(L, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [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_txn_ref = hlua_register_metatable(L, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9083 | |
| 9084 | /* |
| 9085 | * |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 9086 | * Register class reply |
| 9087 | * |
| 9088 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9089 | lua_newtable(L); |
| 9090 | lua_pushstring(L, "__index"); |
| 9091 | lua_newtable(L); |
| 9092 | hlua_class_function(L, "set_status", hlua_txn_reply_set_status); |
| 9093 | hlua_class_function(L, "add_header", hlua_txn_reply_add_header); |
| 9094 | hlua_class_function(L, "del_header", hlua_txn_reply_del_header); |
| 9095 | hlua_class_function(L, "set_body", hlua_txn_reply_set_body); |
| 9096 | lua_settable(L, -3); /* Sets the __index entry. */ |
| 9097 | class_txn_reply_ref = luaL_ref(L, LUA_REGISTRYINDEX); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 9098 | |
| 9099 | |
| 9100 | /* |
| 9101 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9102 | * Register class Socket |
| 9103 | * |
| 9104 | */ |
| 9105 | |
| 9106 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9107 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9108 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9109 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9110 | lua_pushstring(L, "__index"); |
| 9111 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9112 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 9113 | #ifdef USE_OPENSSL |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9114 | hlua_class_function(L, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 9115 | #endif |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9116 | hlua_class_function(L, "connect", hlua_socket_connect); |
| 9117 | hlua_class_function(L, "send", hlua_socket_send); |
| 9118 | hlua_class_function(L, "receive", hlua_socket_receive); |
| 9119 | hlua_class_function(L, "close", hlua_socket_close); |
| 9120 | hlua_class_function(L, "getpeername", hlua_socket_getpeername); |
| 9121 | hlua_class_function(L, "getsockname", hlua_socket_getsockname); |
| 9122 | hlua_class_function(L, "setoption", hlua_socket_setoption); |
| 9123 | hlua_class_function(L, "settimeout", hlua_socket_settimeout); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9124 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9125 | 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] | 9126 | |
| 9127 | /* Register the garbage collector entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9128 | lua_pushstring(L, "__gc"); |
| 9129 | lua_pushcclosure(L, hlua_socket_gc, 0); |
| 9130 | lua_rawset(L, -3); /* Push the last 2 entries in the table at index -3 */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9131 | |
| 9132 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9133 | class_socket_ref = hlua_register_metatable(L, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9134 | |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 9135 | lua_atpanic(L, hlua_panic_safe); |
| 9136 | |
| 9137 | return L; |
| 9138 | } |
| 9139 | |
| 9140 | void hlua_init(void) { |
| 9141 | int i; |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 9142 | #ifdef USE_OPENSSL |
| 9143 | struct srv_kw *kw; |
| 9144 | int tmp_error; |
| 9145 | char *error; |
| 9146 | char *args[] = { /* SSL client configuration. */ |
| 9147 | "ssl", |
| 9148 | "verify", |
| 9149 | "none", |
| 9150 | NULL |
| 9151 | }; |
| 9152 | #endif |
| 9153 | |
| 9154 | /* Init post init function list head */ |
| 9155 | for (i = 0; i < MAX_THREADS + 1; i++) |
| 9156 | LIST_INIT(&hlua_init_functions[i]); |
| 9157 | |
| 9158 | /* Init state for common/shared lua parts */ |
| 9159 | hlua_state_id = 0; |
| 9160 | ha_set_tid(0); |
| 9161 | hlua_states[0] = hlua_init_state(0); |
| 9162 | |
| 9163 | /* Init state 1 for thread 0. We have at least one thread. */ |
| 9164 | hlua_state_id = 1; |
| 9165 | ha_set_tid(0); |
| 9166 | hlua_states[1] = hlua_init_state(1); |
| 9167 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9168 | /* Proxy and server configuration initialisation. */ |
| 9169 | memset(&socket_proxy, 0, sizeof(socket_proxy)); |
| 9170 | init_new_proxy(&socket_proxy); |
| 9171 | socket_proxy.parent = NULL; |
| 9172 | socket_proxy.last_change = now.tv_sec; |
| 9173 | socket_proxy.id = "LUA-SOCKET"; |
| 9174 | socket_proxy.cap = PR_CAP_FE | PR_CAP_BE; |
| 9175 | socket_proxy.maxconn = 0; |
| 9176 | socket_proxy.accept = NULL; |
| 9177 | socket_proxy.options2 |= PR_O2_INDEPSTR; |
| 9178 | socket_proxy.srv = NULL; |
| 9179 | socket_proxy.conn_retries = 0; |
| 9180 | socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */ |
| 9181 | |
| 9182 | /* Init TCP server: unchanged parameters */ |
| 9183 | memset(&socket_tcp, 0, sizeof(socket_tcp)); |
| 9184 | socket_tcp.next = NULL; |
| 9185 | socket_tcp.proxy = &socket_proxy; |
| 9186 | socket_tcp.obj_type = OBJ_TYPE_SERVER; |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 9187 | socket_tcp.pendconns = EB_ROOT; |
Willy Tarreau | 198e92a | 2021-03-05 10:23:32 +0100 | [diff] [blame] | 9188 | LIST_ADD(&servers_list, &socket_tcp.global_list); |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 9189 | socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9190 | socket_tcp.last_change = 0; |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 9191 | socket_tcp.conf.file = strdup("HLUA_INTERNAL"); |
| 9192 | socket_tcp.conf.line = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9193 | socket_tcp.id = "LUA-TCP-CONN"; |
| 9194 | socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 9195 | socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 9196 | socket_tcp.pp_opts = 0; /* Remove proxy protocol. */ |
| 9197 | |
| 9198 | /* XXX: Copy default parameter from default server, |
| 9199 | * but the default server is not initialized. |
| 9200 | */ |
| 9201 | socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue; |
| 9202 | socket_tcp.minconn = socket_proxy.defsrv.minconn; |
| 9203 | socket_tcp.maxconn = socket_proxy.defsrv.maxconn; |
| 9204 | socket_tcp.slowstart = socket_proxy.defsrv.slowstart; |
| 9205 | socket_tcp.onerror = socket_proxy.defsrv.onerror; |
| 9206 | socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 9207 | socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 9208 | socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 9209 | socket_tcp.uweight = socket_proxy.defsrv.iweight; |
| 9210 | socket_tcp.iweight = socket_proxy.defsrv.iweight; |
| 9211 | |
| 9212 | socket_tcp.check.status = HCHK_STATUS_INI; |
| 9213 | socket_tcp.check.rise = socket_proxy.defsrv.check.rise; |
| 9214 | socket_tcp.check.fall = socket_proxy.defsrv.check.fall; |
| 9215 | socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */ |
| 9216 | socket_tcp.check.server = &socket_tcp; |
| 9217 | |
| 9218 | socket_tcp.agent.status = HCHK_STATUS_INI; |
| 9219 | socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise; |
| 9220 | socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall; |
| 9221 | socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */ |
| 9222 | socket_tcp.agent.server = &socket_tcp; |
| 9223 | |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 9224 | socket_tcp.xprt = xprt_get(XPRT_RAW); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9225 | |
| 9226 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9227 | /* Init TCP server: unchanged parameters */ |
| 9228 | memset(&socket_ssl, 0, sizeof(socket_ssl)); |
| 9229 | socket_ssl.next = NULL; |
| 9230 | socket_ssl.proxy = &socket_proxy; |
| 9231 | socket_ssl.obj_type = OBJ_TYPE_SERVER; |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 9232 | socket_ssl.pendconns = EB_ROOT; |
Willy Tarreau | 198e92a | 2021-03-05 10:23:32 +0100 | [diff] [blame] | 9233 | LIST_ADD(&servers_list, &socket_ssl.global_list); |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 9234 | socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9235 | socket_ssl.last_change = 0; |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 9236 | socket_ssl.conf.file = strdup("HLUA_INTERNAL"); |
| 9237 | socket_ssl.conf.line = 2; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9238 | socket_ssl.id = "LUA-SSL-CONN"; |
| 9239 | socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 9240 | socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 9241 | socket_ssl.pp_opts = 0; /* Remove proxy protocol. */ |
| 9242 | |
| 9243 | /* XXX: Copy default parameter from default server, |
| 9244 | * but the default server is not initialized. |
| 9245 | */ |
| 9246 | socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue; |
| 9247 | socket_ssl.minconn = socket_proxy.defsrv.minconn; |
| 9248 | socket_ssl.maxconn = socket_proxy.defsrv.maxconn; |
| 9249 | socket_ssl.slowstart = socket_proxy.defsrv.slowstart; |
| 9250 | socket_ssl.onerror = socket_proxy.defsrv.onerror; |
| 9251 | socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 9252 | socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 9253 | socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 9254 | socket_ssl.uweight = socket_proxy.defsrv.iweight; |
| 9255 | socket_ssl.iweight = socket_proxy.defsrv.iweight; |
| 9256 | |
| 9257 | socket_ssl.check.status = HCHK_STATUS_INI; |
| 9258 | socket_ssl.check.rise = socket_proxy.defsrv.check.rise; |
| 9259 | socket_ssl.check.fall = socket_proxy.defsrv.check.fall; |
| 9260 | socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */ |
| 9261 | socket_ssl.check.server = &socket_ssl; |
| 9262 | |
| 9263 | socket_ssl.agent.status = HCHK_STATUS_INI; |
| 9264 | socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise; |
| 9265 | socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall; |
| 9266 | socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */ |
| 9267 | socket_ssl.agent.server = &socket_ssl; |
| 9268 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 9269 | socket_ssl.use_ssl = 1; |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 9270 | socket_ssl.xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9271 | |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 9272 | for (i = 0; args[i] != NULL; i++) { |
| 9273 | 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] | 9274 | /* |
| 9275 | * |
| 9276 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9277 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9278 | * features like client certificates and ssl_verify. |
| 9279 | * |
| 9280 | */ |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 9281 | tmp_error = kw->parse(args, &i, &socket_proxy, &socket_ssl, &error); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9282 | if (tmp_error != 0) { |
| 9283 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 9284 | abort(); /* This must be never arrives because the command line |
| 9285 | not editable by the user. */ |
| 9286 | } |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 9287 | i += kw->skip; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9288 | } |
| 9289 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9290 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 9291 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 9292 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 9293 | |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 9294 | static void hlua_deinit() |
| 9295 | { |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 9296 | int thr; |
| 9297 | |
| 9298 | for (thr = 0; thr < MAX_THREADS+1; thr++) { |
| 9299 | if (hlua_states[thr]) |
| 9300 | lua_close(hlua_states[thr]); |
| 9301 | } |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 9302 | |
| 9303 | ha_free(&socket_tcp.per_thr); |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 9304 | ha_free((char**)&socket_tcp.conf.file); |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 9305 | |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 9306 | #ifdef USE_OPENSSL |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 9307 | ha_free(&socket_ssl.per_thr); |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 9308 | ha_free((char**)&socket_ssl.conf.file); |
| 9309 | #endif |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 9310 | } |
| 9311 | |
| 9312 | REGISTER_POST_DEINIT(hlua_deinit); |
| 9313 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9314 | static void hlua_register_build_options(void) |
| 9315 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 9316 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9317 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 9318 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 9319 | hap_register_build_opts(ptr, 1); |
| 9320 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9321 | |
| 9322 | INITCALL0(STG_REGISTER, hlua_register_build_options); |