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. */ |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 204 | static struct proxy *socket_proxy; |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 205 | static struct server *socket_tcp; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 206 | #ifdef USE_OPENSSL |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 207 | static struct server *socket_ssl; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 208 | #endif |
| 209 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 210 | /* List head of the function called at the initialisation time. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 211 | struct list hlua_init_functions[MAX_THREADS + 1]; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 212 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 213 | /* The following variables contains the reference of the different |
| 214 | * Lua classes. These references are useful for identify metadata |
| 215 | * associated with an object. |
| 216 | */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 217 | static int class_txn_ref; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 218 | static int class_socket_ref; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 219 | static int class_channel_ref; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 220 | static int class_fetches_ref; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 221 | static int class_converters_ref; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 222 | static int class_http_ref; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 223 | static int class_map_ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 224 | static int class_applet_tcp_ref; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 225 | static int class_applet_http_ref; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 226 | static int class_txn_reply_ref; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 227 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 228 | /* Global Lua execution timeout. By default Lua, execution linked |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 229 | * with stream (actions, sample-fetches and converters) have a |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 230 | * short timeout. Lua linked with tasks doesn't have a timeout |
| 231 | * because a task may remain alive during all the haproxy execution. |
| 232 | */ |
| 233 | static unsigned int hlua_timeout_session = 4000; /* session timeout. */ |
| 234 | static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 235 | static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 236 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 237 | /* Interrupts the Lua processing each "hlua_nb_instruction" instructions. |
| 238 | * it is used for preventing infinite loops. |
| 239 | * |
| 240 | * I test the scheer with an infinite loop containing one incrementation |
| 241 | * and one test. I run this loop between 10 seconds, I raise a ceil of |
| 242 | * 710M loops from one interrupt each 9000 instructions, so I fix the value |
| 243 | * to one interrupt each 10 000 instructions. |
| 244 | * |
| 245 | * configured | Number of |
| 246 | * instructions | loops executed |
| 247 | * between two | in milions |
| 248 | * forced yields | |
| 249 | * ---------------+--------------- |
| 250 | * 10 | 160 |
| 251 | * 500 | 670 |
| 252 | * 1000 | 680 |
| 253 | * 5000 | 700 |
| 254 | * 7000 | 700 |
| 255 | * 8000 | 700 |
| 256 | * 9000 | 710 <- ceil |
| 257 | * 10000 | 710 |
| 258 | * 100000 | 710 |
| 259 | * 1000000 | 710 |
| 260 | * |
| 261 | */ |
| 262 | static unsigned int hlua_nb_instruction = 10000; |
| 263 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 264 | /* Descriptor for the memory allocation state. The limit is pre-initialised to |
| 265 | * 0 until it is replaced by "tune.lua.maxmem" during the config parsing, or it |
| 266 | * is replaced with ~0 during post_init after everything was loaded. This way |
| 267 | * it is guaranteed that if limit is ~0 the boot is complete and that if it's |
| 268 | * zero it's not yet limited and proper accounting is required. |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 269 | */ |
| 270 | struct hlua_mem_allocator { |
| 271 | size_t allocated; |
| 272 | size_t limit; |
| 273 | }; |
| 274 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 275 | static struct hlua_mem_allocator hlua_global_allocator THREAD_ALIGNED(64); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 276 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 277 | /* These functions converts types between HAProxy internal args or |
| 278 | * sample and LUA types. Another function permits to check if the |
| 279 | * LUA stack contains arguments according with an required ARG_T |
| 280 | * format. |
| 281 | */ |
| 282 | static int hlua_arg2lua(lua_State *L, const struct arg *arg); |
| 283 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 284 | __LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp, |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 285 | uint64_t mask, struct proxy *p); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 286 | static int hlua_smp2lua(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 287 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 288 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp); |
| 289 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 290 | __LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 291 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 292 | struct prepend_path { |
| 293 | struct list l; |
| 294 | char *type; |
| 295 | char *path; |
| 296 | }; |
| 297 | |
| 298 | static struct list prepend_path_list = LIST_HEAD_INIT(prepend_path_list); |
| 299 | |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 300 | #define SEND_ERR(__be, __fmt, __args...) \ |
| 301 | do { \ |
| 302 | send_log(__be, LOG_ERR, __fmt, ## __args); \ |
| 303 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 304 | ha_alert(__fmt, ## __args); \ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 305 | } while (0) |
| 306 | |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 307 | static inline struct hlua_function *new_hlua_function() |
| 308 | { |
| 309 | struct hlua_function *fcn; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 310 | int i; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 311 | |
| 312 | fcn = calloc(1, sizeof(*fcn)); |
| 313 | if (!fcn) |
| 314 | return NULL; |
| 315 | LIST_ADDQ(&referenced_functions, &fcn->l); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 316 | for (i = 0; i < MAX_THREADS + 1; i++) |
| 317 | fcn->function_ref[i] = -1; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 318 | return fcn; |
| 319 | } |
| 320 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 321 | /* If the common state is set, the stack id is 0, otherwise it is the tid + 1 */ |
| 322 | static inline int fcn_ref_to_stack_id(struct hlua_function *fcn) |
| 323 | { |
| 324 | if (fcn->function_ref[0] == -1) |
| 325 | return tid + 1; |
| 326 | return 0; |
| 327 | } |
| 328 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 329 | /* Used to check an Lua function type in the stack. It creates and |
| 330 | * returns a reference of the function. This function throws an |
| 331 | * error if the rgument is not a "function". |
| 332 | */ |
| 333 | __LJMP unsigned int hlua_checkfunction(lua_State *L, int argno) |
| 334 | { |
| 335 | if (!lua_isfunction(L, argno)) { |
Thierry FOURNIER | fd1e955 | 2018-02-23 18:41:18 +0100 | [diff] [blame] | 336 | const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, argno)); |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 337 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 338 | } |
| 339 | lua_pushvalue(L, argno); |
| 340 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 341 | } |
| 342 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 343 | /* Return the string that is of the top of the stack. */ |
| 344 | const char *hlua_get_top_error_string(lua_State *L) |
| 345 | { |
| 346 | if (lua_gettop(L) < 1) |
| 347 | return "unknown error"; |
| 348 | if (lua_type(L, -1) != LUA_TSTRING) |
| 349 | return "unknown error"; |
| 350 | return lua_tostring(L, -1); |
| 351 | } |
| 352 | |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 353 | __LJMP const char *hlua_traceback(lua_State *L, const char* sep) |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 354 | { |
| 355 | lua_Debug ar; |
| 356 | int level = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 357 | struct buffer *msg = get_trash_chunk(); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 358 | |
| 359 | while (lua_getstack(L, level++, &ar)) { |
| 360 | |
| 361 | /* Add separator */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 362 | if (b_data(msg)) |
| 363 | chunk_appendf(msg, "%s", sep); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 364 | |
| 365 | /* Fill fields: |
| 366 | * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what; |
| 367 | * 'l': fills in the field currentline; |
| 368 | * 'n': fills in the field name and namewhat; |
| 369 | * 't': fills in the field istailcall; |
| 370 | */ |
| 371 | lua_getinfo(L, "Slnt", &ar); |
| 372 | |
| 373 | /* Append code localisation */ |
| 374 | if (ar.currentline > 0) |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 375 | chunk_appendf(msg, "%s:%d: ", ar.short_src, ar.currentline); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 376 | else |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 377 | chunk_appendf(msg, "%s: ", ar.short_src); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 378 | |
| 379 | /* |
| 380 | * Get function name |
| 381 | * |
| 382 | * if namewhat is no empty, name is defined. |
| 383 | * what contains "Lua" for Lua function, "C" for C function, |
| 384 | * or "main" for main code. |
| 385 | */ |
| 386 | if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 387 | chunk_appendf(msg, "in %s '%s'", ar.namewhat, ar.name); /* use it */ |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 388 | |
| 389 | else if (*ar.what == 'm') /* "main", the code is not executed in a function */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 390 | chunk_appendf(msg, "in main chunk"); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 391 | |
| 392 | else if (*ar.what != 'C') /* for Lua functions, use <file:line> */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 393 | chunk_appendf(msg, "in function line %d", ar.linedefined); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 394 | |
| 395 | else /* nothing left... */ |
| 396 | chunk_appendf(msg, "?"); |
| 397 | |
| 398 | |
| 399 | /* Display tailed call */ |
| 400 | if (ar.istailcall) |
| 401 | chunk_appendf(msg, " ..."); |
| 402 | } |
| 403 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 404 | return msg->area; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 408 | /* This function check the number of arguments available in the |
| 409 | * stack. If the number of arguments available is not the same |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 410 | * then <nb> an error is thrown. |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 411 | */ |
| 412 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 413 | { |
| 414 | if (lua_gettop(L) == nb) |
| 415 | return; |
| 416 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 417 | } |
| 418 | |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 419 | /* This function pushes an error string prefixed by the file name |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 420 | * and the line number where the error is encountered. |
| 421 | */ |
| 422 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 423 | { |
| 424 | va_list argp; |
| 425 | va_start(argp, fmt); |
| 426 | luaL_where(L, 1); |
| 427 | lua_pushvfstring(L, fmt, argp); |
| 428 | va_end(argp); |
| 429 | lua_concat(L, 2); |
| 430 | return 1; |
| 431 | } |
| 432 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 433 | /* This functions is used with sample fetch and converters. It |
| 434 | * converts the HAProxy configuration argument in a lua stack |
| 435 | * values. |
| 436 | * |
| 437 | * It takes an array of "arg", and each entry of the array is |
| 438 | * converted and pushed in the LUA stack. |
| 439 | */ |
| 440 | static int hlua_arg2lua(lua_State *L, const struct arg *arg) |
| 441 | { |
| 442 | switch (arg->type) { |
| 443 | case ARGT_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 444 | case ARGT_TIME: |
| 445 | case ARGT_SIZE: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 446 | lua_pushinteger(L, arg->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 447 | break; |
| 448 | |
| 449 | case ARGT_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 450 | lua_pushlstring(L, arg->data.str.area, arg->data.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 451 | break; |
| 452 | |
| 453 | case ARGT_IPV4: |
| 454 | case ARGT_IPV6: |
| 455 | case ARGT_MSK4: |
| 456 | case ARGT_MSK6: |
| 457 | case ARGT_FE: |
| 458 | case ARGT_BE: |
| 459 | case ARGT_TAB: |
| 460 | case ARGT_SRV: |
| 461 | case ARGT_USR: |
| 462 | case ARGT_MAP: |
| 463 | default: |
| 464 | lua_pushnil(L); |
| 465 | break; |
| 466 | } |
| 467 | return 1; |
| 468 | } |
| 469 | |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 470 | /* 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] | 471 | * 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] | 472 | * with sample fetch wrappers. The input arguments are given to the |
| 473 | * lua wrapper and converted as arg list by the function. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 474 | */ |
| 475 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg) |
| 476 | { |
| 477 | switch (lua_type(L, ud)) { |
| 478 | |
| 479 | case LUA_TNUMBER: |
| 480 | case LUA_TBOOLEAN: |
| 481 | arg->type = ARGT_SINT; |
| 482 | arg->data.sint = lua_tointeger(L, ud); |
| 483 | break; |
| 484 | |
| 485 | case LUA_TSTRING: |
| 486 | arg->type = ARGT_STR; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 487 | 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] | 488 | /* 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] | 489 | 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] | 490 | arg->data.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 491 | break; |
| 492 | |
| 493 | case LUA_TUSERDATA: |
| 494 | case LUA_TNIL: |
| 495 | case LUA_TTABLE: |
| 496 | case LUA_TFUNCTION: |
| 497 | case LUA_TTHREAD: |
| 498 | case LUA_TLIGHTUSERDATA: |
| 499 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 500 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 501 | break; |
| 502 | } |
| 503 | return 1; |
| 504 | } |
| 505 | |
| 506 | /* the following functions are used to convert a struct sample |
| 507 | * in Lua type. This useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 508 | * fetches or converters. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 509 | */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 510 | static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 511 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 512 | switch (smp->data.type) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 513 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 514 | case SMP_T_BOOL: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 515 | lua_pushinteger(L, smp->data.u.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 516 | break; |
| 517 | |
| 518 | case SMP_T_BIN: |
| 519 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 520 | 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] | 521 | break; |
| 522 | |
| 523 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 524 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 525 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 526 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 527 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 528 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 529 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 530 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 531 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 532 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 533 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 534 | 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] | 535 | break; |
| 536 | default: |
| 537 | lua_pushnil(L); |
| 538 | break; |
| 539 | } |
| 540 | break; |
| 541 | |
| 542 | case SMP_T_IPV4: |
| 543 | case SMP_T_IPV6: |
| 544 | 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] | 545 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 546 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 547 | 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] | 548 | else |
| 549 | lua_pushnil(L); |
| 550 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 551 | default: |
| 552 | lua_pushnil(L); |
| 553 | break; |
| 554 | } |
| 555 | return 1; |
| 556 | } |
| 557 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 558 | /* the following functions are used to convert a struct sample |
| 559 | * in Lua strings. This is useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 560 | * fetches or converters. |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 561 | */ |
| 562 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
| 563 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 564 | switch (smp->data.type) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 565 | |
| 566 | case SMP_T_BIN: |
| 567 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 568 | 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] | 569 | break; |
| 570 | |
| 571 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 572 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 573 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 574 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 575 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 576 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 577 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 578 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 579 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 580 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 581 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 582 | 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] | 583 | break; |
| 584 | default: |
| 585 | lua_pushstring(L, ""); |
| 586 | break; |
| 587 | } |
| 588 | break; |
| 589 | |
| 590 | case SMP_T_SINT: |
| 591 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 592 | case SMP_T_IPV4: |
| 593 | case SMP_T_IPV6: |
| 594 | 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] | 595 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 596 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 597 | 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] | 598 | else |
| 599 | lua_pushstring(L, ""); |
| 600 | break; |
| 601 | default: |
| 602 | lua_pushstring(L, ""); |
| 603 | break; |
| 604 | } |
| 605 | return 1; |
| 606 | } |
| 607 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 608 | /* the following functions are used to convert an Lua type in a |
| 609 | * struct sample. This is useful to provide data from a converter |
| 610 | * to the LUA code. |
| 611 | */ |
| 612 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 613 | { |
| 614 | switch (lua_type(L, ud)) { |
| 615 | |
| 616 | case LUA_TNUMBER: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 617 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 618 | smp->data.u.sint = lua_tointeger(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 619 | break; |
| 620 | |
| 621 | |
| 622 | case LUA_TBOOLEAN: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 623 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 624 | smp->data.u.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 625 | break; |
| 626 | |
| 627 | case LUA_TSTRING: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 628 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 629 | smp->flags |= SMP_F_CONST; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 630 | 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] | 631 | /* 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] | 632 | 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] | 633 | smp->data.u.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 634 | break; |
| 635 | |
| 636 | case LUA_TUSERDATA: |
| 637 | case LUA_TNIL: |
| 638 | case LUA_TTABLE: |
| 639 | case LUA_TFUNCTION: |
| 640 | case LUA_TTHREAD: |
| 641 | case LUA_TLIGHTUSERDATA: |
Thierry FOURNIER | 93405e1 | 2015-08-26 14:19:03 +0200 | [diff] [blame] | 642 | case LUA_TNONE: |
| 643 | default: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 644 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 645 | smp->data.u.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 646 | break; |
| 647 | } |
| 648 | return 1; |
| 649 | } |
| 650 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 651 | /* This function check the "argp" built by another conversion function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 652 | * 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] | 653 | * 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] | 654 | * |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 655 | * This function assumes that the argp argument contains ARGM_NBARGS + 1 |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 656 | * entries. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 657 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 658 | __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] | 659 | uint64_t mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 660 | { |
| 661 | int min_arg; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 662 | int i, idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 663 | struct proxy *px; |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 664 | struct userlist *ul; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 665 | struct my_regex *reg; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 666 | const char *msg = NULL; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 667 | char *sname, *pname, *err = NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 668 | |
| 669 | idx = 0; |
| 670 | min_arg = ARGM(mask); |
| 671 | mask >>= ARGM_BITS; |
| 672 | |
| 673 | while (1) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 674 | struct buffer tmp = BUF_NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 675 | |
| 676 | /* Check oversize. */ |
| 677 | if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 678 | msg = "Malformed argument mask"; |
| 679 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | /* Check for mandatory arguments. */ |
| 683 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 684 | if (idx < min_arg) { |
| 685 | |
| 686 | /* If miss other argument than the first one, we return an error. */ |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 687 | if (idx > 0) { |
| 688 | msg = "Mandatory argument expected"; |
| 689 | goto error; |
| 690 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 691 | |
| 692 | /* If first argument have a certain type, some default values |
| 693 | * may be used. See the function smp_resolve_args(). |
| 694 | */ |
| 695 | switch (mask & ARGT_MASK) { |
| 696 | |
| 697 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 698 | if (!(p->cap & PR_CAP_FE)) { |
| 699 | msg = "Mandatory argument expected"; |
| 700 | goto error; |
| 701 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 702 | argp[idx].data.prx = p; |
| 703 | argp[idx].type = ARGT_FE; |
| 704 | argp[idx+1].type = ARGT_STOP; |
| 705 | break; |
| 706 | |
| 707 | case ARGT_BE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 708 | if (!(p->cap & PR_CAP_BE)) { |
| 709 | msg = "Mandatory argument expected"; |
| 710 | goto error; |
| 711 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 712 | argp[idx].data.prx = p; |
| 713 | argp[idx].type = ARGT_BE; |
| 714 | argp[idx+1].type = ARGT_STOP; |
| 715 | break; |
| 716 | |
| 717 | case ARGT_TAB: |
| 718 | argp[idx].data.prx = p; |
| 719 | argp[idx].type = ARGT_TAB; |
| 720 | argp[idx+1].type = ARGT_STOP; |
| 721 | break; |
| 722 | |
| 723 | default: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 724 | msg = "Mandatory argument expected"; |
| 725 | goto error; |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 726 | break; |
| 727 | } |
| 728 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 729 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 730 | } |
| 731 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 732 | /* Check for exceed the number of required argument. */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 733 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 734 | argp[idx].type != ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 735 | msg = "Last argument expected"; |
| 736 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 740 | argp[idx].type == ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 741 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 742 | } |
| 743 | |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 744 | /* Convert some argument types. All string in argp[] are for not |
| 745 | * duplicated yet. |
| 746 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 747 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 748 | case ARGT_SINT: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 749 | if (argp[idx].type != ARGT_SINT) { |
| 750 | msg = "integer expected"; |
| 751 | goto error; |
| 752 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 753 | argp[idx].type = ARGT_SINT; |
| 754 | break; |
| 755 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 756 | case ARGT_TIME: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 757 | if (argp[idx].type != ARGT_SINT) { |
| 758 | msg = "integer expected"; |
| 759 | goto error; |
| 760 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 761 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 762 | break; |
| 763 | |
| 764 | case ARGT_SIZE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 765 | if (argp[idx].type != ARGT_SINT) { |
| 766 | msg = "integer expected"; |
| 767 | goto error; |
| 768 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 769 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 770 | break; |
| 771 | |
| 772 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 773 | if (argp[idx].type != ARGT_STR) { |
| 774 | msg = "string expected"; |
| 775 | goto error; |
| 776 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 777 | 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] | 778 | if (!argp[idx].data.prx) { |
| 779 | msg = "frontend doesn't exist"; |
| 780 | goto error; |
| 781 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 782 | argp[idx].type = ARGT_FE; |
| 783 | break; |
| 784 | |
| 785 | case ARGT_BE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 786 | if (argp[idx].type != ARGT_STR) { |
| 787 | msg = "string expected"; |
| 788 | goto error; |
| 789 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 790 | 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] | 791 | if (!argp[idx].data.prx) { |
| 792 | msg = "backend doesn't exist"; |
| 793 | goto error; |
| 794 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 795 | argp[idx].type = ARGT_BE; |
| 796 | break; |
| 797 | |
| 798 | case ARGT_TAB: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 799 | if (argp[idx].type != ARGT_STR) { |
| 800 | msg = "string expected"; |
| 801 | goto error; |
| 802 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 803 | 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] | 804 | if (!argp[idx].data.t) { |
| 805 | msg = "table doesn't exist"; |
| 806 | goto error; |
| 807 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 808 | argp[idx].type = ARGT_TAB; |
| 809 | break; |
| 810 | |
| 811 | case ARGT_SRV: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 812 | if (argp[idx].type != ARGT_STR) { |
| 813 | msg = "string expected"; |
| 814 | goto error; |
| 815 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 816 | sname = strrchr(argp[idx].data.str.area, '/'); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 817 | if (sname) { |
| 818 | *sname++ = '\0'; |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 819 | pname = argp[idx].data.str.area; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 820 | px = proxy_be_by_name(pname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 821 | if (!px) { |
| 822 | msg = "backend doesn't exist"; |
| 823 | goto error; |
| 824 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 825 | } |
| 826 | else { |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 827 | sname = argp[idx].data.str.area; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 828 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 829 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 830 | argp[idx].data.srv = findserver(px, sname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 831 | if (!argp[idx].data.srv) { |
| 832 | msg = "server doesn't exist"; |
| 833 | goto error; |
| 834 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 835 | argp[idx].type = ARGT_SRV; |
| 836 | break; |
| 837 | |
| 838 | case ARGT_IPV4: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 839 | if (argp[idx].type != ARGT_STR) { |
| 840 | msg = "string expected"; |
| 841 | goto error; |
| 842 | } |
| 843 | if (inet_pton(AF_INET, argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 844 | msg = "invalid IPv4 address"; |
| 845 | goto error; |
| 846 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 847 | argp[idx].type = ARGT_IPV4; |
| 848 | break; |
| 849 | |
| 850 | case ARGT_MSK4: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 851 | if (argp[idx].type == ARGT_SINT) |
| 852 | len2mask4(argp[idx].data.sint, &argp[idx].data.ipv4); |
| 853 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 854 | if (!str2mask(argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 855 | msg = "invalid IPv4 mask"; |
| 856 | goto error; |
| 857 | } |
| 858 | } |
| 859 | else { |
| 860 | msg = "integer or string expected"; |
| 861 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 862 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 863 | argp[idx].type = ARGT_MSK4; |
| 864 | break; |
| 865 | |
| 866 | case ARGT_IPV6: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 867 | if (argp[idx].type != ARGT_STR) { |
| 868 | msg = "string expected"; |
| 869 | goto error; |
| 870 | } |
| 871 | if (inet_pton(AF_INET6, argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 872 | msg = "invalid IPv6 address"; |
| 873 | goto error; |
| 874 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 875 | argp[idx].type = ARGT_IPV6; |
| 876 | break; |
| 877 | |
| 878 | case ARGT_MSK6: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 879 | if (argp[idx].type == ARGT_SINT) |
| 880 | len2mask6(argp[idx].data.sint, &argp[idx].data.ipv6); |
| 881 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 882 | if (!str2mask6(argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 883 | msg = "invalid IPv6 mask"; |
| 884 | goto error; |
| 885 | } |
| 886 | } |
| 887 | else { |
| 888 | msg = "integer or string expected"; |
| 889 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 890 | } |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 891 | argp[idx].type = ARGT_MSK6; |
| 892 | break; |
| 893 | |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 894 | case ARGT_REG: |
| 895 | if (argp[idx].type != ARGT_STR) { |
| 896 | msg = "string expected"; |
| 897 | goto error; |
| 898 | } |
| 899 | reg = regex_comp(argp[idx].data.str.area, !(argp[idx].type_flags & ARGF_REG_ICASE), 1, &err); |
| 900 | if (!reg) { |
| 901 | msg = lua_pushfstring(L, "error compiling regex '%s' : '%s'", |
| 902 | argp[idx].data.str.area, err); |
| 903 | free(err); |
| 904 | goto error; |
| 905 | } |
| 906 | argp[idx].type = ARGT_REG; |
| 907 | argp[idx].data.reg = reg; |
| 908 | break; |
| 909 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 910 | case ARGT_USR: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 911 | if (argp[idx].type != ARGT_STR) { |
| 912 | msg = "string expected"; |
| 913 | goto error; |
| 914 | } |
| 915 | if (p->uri_auth && p->uri_auth->userlist && |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 916 | strcmp(p->uri_auth->userlist->name, argp[idx].data.str.area) == 0) |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 917 | ul = p->uri_auth->userlist; |
| 918 | else |
| 919 | ul = auth_find_userlist(argp[idx].data.str.area); |
| 920 | |
| 921 | if (!ul) { |
| 922 | msg = lua_pushfstring(L, "unable to find userlist '%s'", argp[idx].data.str.area); |
| 923 | goto error; |
| 924 | } |
| 925 | argp[idx].type = ARGT_USR; |
| 926 | argp[idx].data.usr = ul; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 927 | break; |
| 928 | |
| 929 | case ARGT_STR: |
| 930 | if (!chunk_dup(&tmp, &argp[idx].data.str)) { |
| 931 | msg = "unable to duplicate string arg"; |
| 932 | goto error; |
| 933 | } |
| 934 | argp[idx].data.str = tmp; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 935 | break; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 936 | |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 937 | case ARGT_MAP: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 938 | msg = "type not yet supported"; |
| 939 | goto error; |
| 940 | break; |
| 941 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | /* Check for type of argument. */ |
| 945 | if ((mask & ARGT_MASK) != argp[idx].type) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 946 | msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 947 | arg_type_names[(mask & ARGT_MASK)], |
| 948 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 949 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | /* Next argument. */ |
| 953 | mask >>= ARGT_BITS; |
| 954 | idx++; |
| 955 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 956 | return 0; |
| 957 | |
| 958 | error: |
| 959 | for (i = 0; i < idx; i++) { |
| 960 | if (argp[i].type == ARGT_STR) |
| 961 | chunk_destroy(&argp[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 962 | else if (argp[i].type == ARGT_REG) |
| 963 | regex_free(argp[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 964 | } |
| 965 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 966 | return 0; /* Never reached */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 967 | } |
| 968 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 969 | /* |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 970 | * The following functions are used to make correspondence between the the |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 971 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 972 | * |
| 973 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 974 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 975 | */ |
| 976 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 977 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 978 | struct hlua **hlua = lua_getextraspace(L); |
| 979 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 980 | } |
| 981 | static inline void hlua_sethlua(struct hlua *hlua) |
| 982 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 983 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 984 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 985 | } |
| 986 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 987 | /* This function is used to send logs. It try to send on screen (stderr) |
| 988 | * and on the default syslog server. |
| 989 | */ |
| 990 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 991 | { |
| 992 | struct tm tm; |
| 993 | char *p; |
| 994 | |
| 995 | /* Cleanup the log message. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 996 | p = trash.area; |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 997 | for (; *msg != '\0'; msg++, p++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 998 | if (p >= trash.area + trash.size - 1) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 999 | /* Break the message if exceed the buffer size. */ |
| 1000 | *(p-4) = ' '; |
| 1001 | *(p-3) = '.'; |
| 1002 | *(p-2) = '.'; |
| 1003 | *(p-1) = '.'; |
| 1004 | break; |
| 1005 | } |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 1006 | if (isprint((unsigned char)*msg)) |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1007 | *p = *msg; |
| 1008 | else |
| 1009 | *p = '.'; |
| 1010 | } |
| 1011 | *p = '\0'; |
| 1012 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1013 | send_log(px, level, "%s\n", trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1014 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Christopher Faulet | f98d821 | 2020-10-02 18:13:52 +0200 | [diff] [blame] | 1015 | if (level == LOG_DEBUG && !(global.mode & MODE_DEBUG)) |
| 1016 | return; |
| 1017 | |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 1018 | get_localtime(date.tv_sec, &tm); |
| 1019 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1020 | 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] | 1021 | (int)getpid(), trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1022 | fflush(stderr); |
| 1023 | } |
| 1024 | } |
| 1025 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1026 | /* This function just ensure that the yield will be always |
| 1027 | * returned with a timeout and permit to set some flags |
| 1028 | */ |
| 1029 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1030 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1031 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1032 | struct hlua *hlua; |
| 1033 | |
| 1034 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1035 | hlua = hlua_gethlua(L); |
| 1036 | if (!hlua) { |
| 1037 | return; |
| 1038 | } |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1039 | |
| 1040 | /* Set the wake timeout. If timeout is required, we set |
| 1041 | * the expiration time. |
| 1042 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1043 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1044 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 1045 | hlua->flags |= flags; |
| 1046 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1047 | /* Process the yield. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1048 | MAY_LJMP(lua_yieldk(L, nresults, ctx, k)); |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1049 | } |
| 1050 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1051 | /* This function initialises the Lua environment stored in the stream. |
| 1052 | * 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] | 1053 | * 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] | 1054 | * |
| 1055 | * This function is particular. it initialises a new Lua thread. If the |
| 1056 | * initialisation fails (example: out of memory error), the lua function |
| 1057 | * throws an error (longjmp). |
| 1058 | * |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1059 | * 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] | 1060 | * environment, so we must not initialise it. While the support of |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1061 | * threads appear, the safe environment set a lock to ensure only one |
| 1062 | * Lua execution at a time. If we initialize safe environment in another |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1063 | * safe environment, we have a dead lock. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1064 | * |
| 1065 | * set "already_safe" true if the context is initialized form safe |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1066 | * Lua function. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1067 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1068 | * This function manipulates two Lua stacks: the main and the thread. Only |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 1069 | * the main stack can fail. The thread is not manipulated. This function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1070 | * MUST NOT manipulate the created thread stack state, because it is not |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1071 | * protected against errors thrown by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1072 | */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1073 | 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] | 1074 | { |
| 1075 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1076 | lua->flags = 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1077 | lua->gc_count = 0; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1078 | lua->wake_time = TICK_ETERNITY; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1079 | lua->state_id = state_id; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1080 | LIST_INIT(&lua->com); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1081 | if (!already_safe) { |
| 1082 | if (!SET_SAFE_LJMP_PARENT(lua)) { |
| 1083 | lua->Tref = LUA_REFNIL; |
| 1084 | return 0; |
| 1085 | } |
| 1086 | } |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1087 | lua->T = lua_newthread(hlua_states[state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1088 | if (!lua->T) { |
| 1089 | lua->Tref = LUA_REFNIL; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1090 | if (!already_safe) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1091 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1092 | return 0; |
| 1093 | } |
| 1094 | hlua_sethlua(lua); |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1095 | lua->Tref = luaL_ref(hlua_states[state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1096 | lua->task = task; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1097 | if (!already_safe) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1098 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1099 | return 1; |
| 1100 | } |
| 1101 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1102 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1103 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 1104 | * is not freed. |
| 1105 | */ |
| 1106 | void hlua_ctx_destroy(struct hlua *lua) |
| 1107 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1108 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 1109 | return; |
| 1110 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1111 | if (!lua->T) |
| 1112 | goto end; |
| 1113 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1114 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1115 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1116 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1117 | if (!SET_SAFE_LJMP(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1118 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1119 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1120 | RESET_SAFE_LJMP(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1121 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1122 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1123 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1124 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1125 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1126 | /* Forces a garbage collecting process. If the Lua program is finished |
| 1127 | * without error, we run the GC on the thread pointer. Its freed all |
| 1128 | * the unused memory. |
| 1129 | * If the thread is finnish with an error or is currently yielded, |
| 1130 | * it seems that the GC applied on the thread doesn't clean anything, |
| 1131 | * so e run the GC on the main thread. |
| 1132 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 1133 | * the garbage collection. |
| 1134 | */ |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1135 | if (lua->gc_count) { |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1136 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1137 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1138 | lua_gc(hlua_states[lua->state_id], LUA_GCCOLLECT, 0); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1139 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1140 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1141 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 1142 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1143 | |
| 1144 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1145 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1146 | } |
| 1147 | |
| 1148 | /* This function is used to restore the Lua context when a coroutine |
| 1149 | * fails. This function copy the common memory between old coroutine |
| 1150 | * and the new coroutine. The old coroutine is destroyed, and its |
| 1151 | * replaced by the new coroutine. |
| 1152 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 1153 | * as string error message and it is copied in the new stack. |
| 1154 | */ |
| 1155 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 1156 | { |
| 1157 | lua_State *T; |
| 1158 | int new_ref; |
| 1159 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1160 | /* New Lua coroutine. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1161 | T = lua_newthread(hlua_states[lua->state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1162 | if (!T) |
| 1163 | return 0; |
| 1164 | |
| 1165 | /* Copy last error message. */ |
| 1166 | if (keep_msg) |
| 1167 | lua_xmove(lua->T, T, 1); |
| 1168 | |
| 1169 | /* Copy data between the coroutines. */ |
| 1170 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1171 | lua_xmove(lua->T, T, 1); |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 1172 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */ |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1173 | |
| 1174 | /* Destroy old data. */ |
| 1175 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1176 | |
| 1177 | /* The thread is garbage collected by Lua. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1178 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1179 | |
| 1180 | /* Fill the struct with the new coroutine values. */ |
| 1181 | lua->Mref = new_ref; |
| 1182 | lua->T = T; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1183 | lua->Tref = luaL_ref(hlua_states[lua->state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1184 | |
| 1185 | /* Set context. */ |
| 1186 | hlua_sethlua(lua); |
| 1187 | |
| 1188 | return 1; |
| 1189 | } |
| 1190 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1191 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1192 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1193 | struct hlua *hlua; |
| 1194 | |
| 1195 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1196 | hlua = hlua_gethlua(L); |
| 1197 | if (!hlua) |
| 1198 | return; |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1199 | |
| 1200 | /* Lua cannot yield when its returning from a function, |
| 1201 | * so, we can fix the interrupt hook to 1 instruction, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1202 | * expecting that the function is finished. |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1203 | */ |
| 1204 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1205 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1206 | return; |
| 1207 | } |
| 1208 | |
| 1209 | /* restore the interrupt condition. */ |
| 1210 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1211 | |
| 1212 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1213 | * If the state is not yieldable, trying yield causes an error. |
| 1214 | */ |
| 1215 | if (lua_isyieldable(L)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1216 | MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1217 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 1218 | /* If we cannot yield, update the clock and check the timeout. */ |
| 1219 | tv_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1220 | hlua->run_time += now_ms - hlua->start_time; |
| 1221 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1222 | lua_pushfstring(L, "execution timeout"); |
| 1223 | WILL_LJMP(lua_error(L)); |
| 1224 | } |
| 1225 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1226 | /* Update the start time. */ |
| 1227 | hlua->start_time = now_ms; |
| 1228 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1229 | /* Try to interrupt the process at the end of the current |
| 1230 | * unyieldable function. |
| 1231 | */ |
| 1232 | 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] | 1233 | } |
| 1234 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1235 | /* This function start or resumes the Lua stack execution. If the flag |
| 1236 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1237 | * The function return an error. |
| 1238 | * |
| 1239 | * The function can returns 4 values: |
| 1240 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1241 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1242 | * task wakeup. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1243 | * - 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] | 1244 | * the top of the stack. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1245 | * - HLUA_E_ERR : An error has occurred without error message. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1246 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1247 | * 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] | 1248 | * LUA code. |
| 1249 | */ |
| 1250 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1251 | { |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1252 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 1253 | int nres; |
| 1254 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1255 | int ret; |
| 1256 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1257 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1258 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1259 | /* Initialise run time counter. */ |
| 1260 | if (!HLUA_IS_RUNNING(lua)) |
| 1261 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1262 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1263 | /* Lock the whole Lua execution. This lock must be before the |
| 1264 | * label "resume_execution". |
| 1265 | */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1266 | if (lua->state_id == 0) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1267 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1268 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1269 | resume_execution: |
| 1270 | |
| 1271 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1272 | * instructions. it is used for preventing infinite loops. |
| 1273 | */ |
| 1274 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1275 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1276 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1277 | HLUA_SET_RUN(lua); |
| 1278 | HLUA_CLR_CTRLYIELD(lua); |
| 1279 | HLUA_CLR_WAKERESWR(lua); |
| 1280 | HLUA_CLR_WAKEREQWR(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1281 | |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1282 | /* Update the start time and reset wake_time. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1283 | lua->start_time = now_ms; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1284 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1285 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1286 | /* Call the function. */ |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1287 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1288 | 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] | 1289 | #else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1290 | ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs); |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1291 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1292 | switch (ret) { |
| 1293 | |
| 1294 | case LUA_OK: |
| 1295 | ret = HLUA_E_OK; |
| 1296 | break; |
| 1297 | |
| 1298 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1299 | /* Check if the execution timeout is expired. It it is the case, we |
| 1300 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1301 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1302 | tv_update_date(0, 1); |
| 1303 | lua->run_time += now_ms - lua->start_time; |
| 1304 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1305 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1306 | ret = HLUA_E_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1307 | break; |
| 1308 | } |
| 1309 | /* Process the forced yield. if the general yield is not allowed or |
| 1310 | * if no task were associated this the current Lua execution |
| 1311 | * coroutine, we resume the execution. Else we want to return in the |
| 1312 | * scheduler and we want to be waked up again, to continue the |
| 1313 | * current Lua execution. So we schedule our own task. |
| 1314 | */ |
| 1315 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1316 | if (!yield_allowed || !lua->task) |
| 1317 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1318 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1319 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1320 | if (!yield_allowed) { |
| 1321 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1322 | ret = HLUA_E_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1323 | break; |
| 1324 | } |
| 1325 | ret = HLUA_E_AGAIN; |
| 1326 | break; |
| 1327 | |
| 1328 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1329 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1330 | /* Special exit case. The traditional exit is returned as an error |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1331 | * because the errors ares the only one mean to return immediately |
| 1332 | * from and lua execution. |
| 1333 | */ |
| 1334 | if (lua->flags & HLUA_EXIT) { |
| 1335 | ret = HLUA_E_OK; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 1336 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1337 | break; |
| 1338 | } |
| 1339 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1340 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1341 | if (!lua_checkstack(lua->T, 1)) { |
| 1342 | ret = HLUA_E_ERR; |
| 1343 | break; |
| 1344 | } |
| 1345 | msg = lua_tostring(lua->T, -1); |
| 1346 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1347 | lua_pop(lua->T, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 1348 | trace = hlua_traceback(lua->T, ", "); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1349 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1350 | 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] | 1351 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1352 | 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] | 1353 | ret = HLUA_E_ERRMSG; |
| 1354 | break; |
| 1355 | |
| 1356 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1357 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1358 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1359 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1360 | break; |
| 1361 | |
| 1362 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1363 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1364 | if (!lua_checkstack(lua->T, 1)) { |
| 1365 | ret = HLUA_E_ERR; |
| 1366 | break; |
| 1367 | } |
| 1368 | msg = lua_tostring(lua->T, -1); |
| 1369 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1370 | lua_pop(lua->T, 1); |
| 1371 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1372 | 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] | 1373 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1374 | 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] | 1375 | ret = HLUA_E_ERRMSG; |
| 1376 | break; |
| 1377 | |
| 1378 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1379 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1380 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1381 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1382 | break; |
| 1383 | } |
| 1384 | |
| 1385 | switch (ret) { |
| 1386 | case HLUA_E_AGAIN: |
| 1387 | break; |
| 1388 | |
| 1389 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1390 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1391 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1392 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1393 | break; |
| 1394 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1395 | case HLUA_E_ETMOUT: |
| 1396 | case HLUA_E_NOMEM: |
| 1397 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1398 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1399 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1400 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1401 | hlua_ctx_renew(lua, 0); |
| 1402 | break; |
| 1403 | |
| 1404 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1405 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1406 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1407 | break; |
| 1408 | } |
| 1409 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1410 | /* This is the main exit point, remove the Lua lock. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1411 | if (lua->state_id == 0) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1412 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1413 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1414 | return ret; |
| 1415 | } |
| 1416 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1417 | /* This function exit the current code. */ |
| 1418 | __LJMP static int hlua_done(lua_State *L) |
| 1419 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1420 | struct hlua *hlua; |
| 1421 | |
| 1422 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1423 | hlua = hlua_gethlua(L); |
| 1424 | if (!hlua) |
| 1425 | return 0; |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1426 | |
| 1427 | hlua->flags |= HLUA_EXIT; |
| 1428 | WILL_LJMP(lua_error(L)); |
| 1429 | |
| 1430 | return 0; |
| 1431 | } |
| 1432 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1433 | /* This function is an LUA binding. It provides a function |
| 1434 | * for deleting ACL from a referenced ACL file. |
| 1435 | */ |
| 1436 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1437 | { |
| 1438 | const char *name; |
| 1439 | const char *key; |
| 1440 | struct pat_ref *ref; |
| 1441 | |
| 1442 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1443 | |
| 1444 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1445 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1446 | |
| 1447 | ref = pat_ref_lookup(name); |
| 1448 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1449 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1450 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1451 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1452 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1453 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1454 | return 0; |
| 1455 | } |
| 1456 | |
| 1457 | /* This function is an LUA binding. It provides a function |
| 1458 | * for deleting map entry from a referenced map file. |
| 1459 | */ |
| 1460 | static int hlua_del_map(lua_State *L) |
| 1461 | { |
| 1462 | const char *name; |
| 1463 | const char *key; |
| 1464 | struct pat_ref *ref; |
| 1465 | |
| 1466 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1467 | |
| 1468 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1469 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1470 | |
| 1471 | ref = pat_ref_lookup(name); |
| 1472 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1473 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1474 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1475 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1476 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1477 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1478 | return 0; |
| 1479 | } |
| 1480 | |
| 1481 | /* This function is an LUA binding. It provides a function |
| 1482 | * for adding ACL pattern from a referenced ACL file. |
| 1483 | */ |
| 1484 | static int hlua_add_acl(lua_State *L) |
| 1485 | { |
| 1486 | const char *name; |
| 1487 | const char *key; |
| 1488 | struct pat_ref *ref; |
| 1489 | |
| 1490 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1491 | |
| 1492 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1493 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1494 | |
| 1495 | ref = pat_ref_lookup(name); |
| 1496 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1497 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1498 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1499 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1500 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1501 | pat_ref_add(ref, key, NULL, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1502 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1503 | return 0; |
| 1504 | } |
| 1505 | |
| 1506 | /* This function is an LUA binding. It provides a function |
| 1507 | * for setting map pattern and sample from a referenced map |
| 1508 | * file. |
| 1509 | */ |
| 1510 | static int hlua_set_map(lua_State *L) |
| 1511 | { |
| 1512 | const char *name; |
| 1513 | const char *key; |
| 1514 | const char *value; |
| 1515 | struct pat_ref *ref; |
| 1516 | |
| 1517 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1518 | |
| 1519 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1520 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1521 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1522 | |
| 1523 | ref = pat_ref_lookup(name); |
| 1524 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1525 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1526 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1527 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1528 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1529 | pat_ref_set(ref, key, value, NULL); |
| 1530 | else |
| 1531 | pat_ref_add(ref, key, value, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1532 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1533 | return 0; |
| 1534 | } |
| 1535 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1536 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1537 | * 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] | 1538 | * metatable have an original version registered in the global context with |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1539 | * the name of the object (_G[<name>] = <metable> ). |
| 1540 | * |
| 1541 | * A metable is a table that modify the standard behavior of a standard |
| 1542 | * access to the associated data. The entries of this new metatable are |
| 1543 | * defined as is: |
| 1544 | * |
| 1545 | * http://lua-users.org/wiki/MetatableEvents |
| 1546 | * |
| 1547 | * __index |
| 1548 | * |
| 1549 | * we access an absent field in a table, the result is nil. This is |
| 1550 | * true, but it is not the whole truth. Actually, such access triggers |
| 1551 | * the interpreter to look for an __index metamethod: If there is no |
| 1552 | * such method, as usually happens, then the access results in nil; |
| 1553 | * otherwise, the metamethod will provide the result. |
| 1554 | * |
| 1555 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1556 | * the key does not appear in the table, but the metatable has an __index |
| 1557 | * property: |
| 1558 | * |
| 1559 | * - if the value is a function, the function is called, passing in the |
| 1560 | * table and the key; the return value of that function is returned as |
| 1561 | * the result. |
| 1562 | * |
| 1563 | * - if the value is another table, the value of the key in that table is |
| 1564 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1565 | * table's metatable has an __index property, then it continues on up) |
| 1566 | * |
| 1567 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1568 | * |
| 1569 | * http://www.lua.org/pil/13.4.1.html |
| 1570 | * |
| 1571 | * __newindex |
| 1572 | * |
| 1573 | * Like __index, but control property assignment. |
| 1574 | * |
| 1575 | * __mode - Control weak references. A string value with one or both |
| 1576 | * of the characters 'k' and 'v' which specifies that the the |
| 1577 | * keys and/or values in the table are weak references. |
| 1578 | * |
| 1579 | * __call - Treat a table like a function. When a table is followed by |
| 1580 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1581 | * a __call key pointing to a function, that function is invoked |
| 1582 | * (passing any specified arguments) and the return value is |
| 1583 | * returned. |
| 1584 | * |
| 1585 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1586 | * called, if the metatable for myTable has a __metatable |
| 1587 | * key, the value of that key is returned instead of the |
| 1588 | * actual metatable. |
| 1589 | * |
| 1590 | * __tostring - Control string representation. When the builtin |
| 1591 | * "tostring( myTable )" function is called, if the metatable |
| 1592 | * for myTable has a __tostring property set to a function, |
| 1593 | * that function is invoked (passing myTable to it) and the |
| 1594 | * return value is used as the string representation. |
| 1595 | * |
| 1596 | * __len - Control table length. When the table length is requested using |
| 1597 | * the length operator ( '#' ), if the metatable for myTable has |
| 1598 | * a __len key pointing to a function, that function is invoked |
| 1599 | * (passing myTable to it) and the return value used as the value |
| 1600 | * of "#myTable". |
| 1601 | * |
| 1602 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1603 | * collected, if the metatable has a __gc field pointing to a |
| 1604 | * function, that function is first invoked, passing the userdata |
| 1605 | * to it. The __gc metamethod is not called for tables. |
| 1606 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1607 | * |
| 1608 | * Special metamethods for redefining standard operators: |
| 1609 | * http://www.lua.org/pil/13.1.html |
| 1610 | * |
| 1611 | * __add "+" |
| 1612 | * __sub "-" |
| 1613 | * __mul "*" |
| 1614 | * __div "/" |
| 1615 | * __unm "!" |
| 1616 | * __pow "^" |
| 1617 | * __concat ".." |
| 1618 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1619 | * Special methods for redefining standard relations |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1620 | * http://www.lua.org/pil/13.2.html |
| 1621 | * |
| 1622 | * __eq "==" |
| 1623 | * __lt "<" |
| 1624 | * __le "<=" |
| 1625 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1626 | |
| 1627 | /* |
| 1628 | * |
| 1629 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1630 | * Class Map |
| 1631 | * |
| 1632 | * |
| 1633 | */ |
| 1634 | |
| 1635 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1636 | * a class session, otherwise it throws an error. |
| 1637 | */ |
| 1638 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1639 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1640 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1641 | } |
| 1642 | |
| 1643 | /* This function is the map constructor. It don't need |
| 1644 | * the class Map object. It creates and return a new Map |
| 1645 | * object. It must be called only during "body" or "init" |
| 1646 | * context because it process some filesystem accesses. |
| 1647 | */ |
| 1648 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1649 | { |
| 1650 | const char *fn; |
| 1651 | int match = PAT_MATCH_STR; |
| 1652 | struct sample_conv conv; |
| 1653 | const char *file = ""; |
| 1654 | int line = 0; |
| 1655 | lua_Debug ar; |
| 1656 | char *err = NULL; |
| 1657 | struct arg args[2]; |
| 1658 | |
| 1659 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1660 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1661 | |
| 1662 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1663 | |
| 1664 | if (lua_gettop(L) >= 2) { |
| 1665 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1666 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1667 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1668 | } |
| 1669 | |
| 1670 | /* Get Lua filename and line number. */ |
| 1671 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1672 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1673 | if (ar.currentline > 0) { /* is there info? */ |
| 1674 | file = ar.short_src; |
| 1675 | line = ar.currentline; |
| 1676 | } |
| 1677 | } |
| 1678 | |
| 1679 | /* fill fake sample_conv struct. */ |
| 1680 | conv.kw = ""; /* unused. */ |
| 1681 | conv.process = NULL; /* unused. */ |
| 1682 | conv.arg_mask = 0; /* unused. */ |
| 1683 | conv.val_args = NULL; /* unused. */ |
| 1684 | conv.out_type = SMP_T_STR; |
| 1685 | conv.private = (void *)(long)match; |
| 1686 | switch (match) { |
| 1687 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1688 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1689 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1690 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1691 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1692 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1693 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1694 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1695 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1696 | default: |
| 1697 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1698 | } |
| 1699 | |
| 1700 | /* fill fake args. */ |
| 1701 | args[0].type = ARGT_STR; |
Christopher Faulet | 73292e9 | 2020-08-06 08:40:09 +0200 | [diff] [blame] | 1702 | args[0].data.str.area = strdup(fn); |
| 1703 | args[0].data.str.data = strlen(fn); |
| 1704 | args[0].data.str.size = args[0].data.str.data+1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1705 | args[1].type = ARGT_STOP; |
| 1706 | |
| 1707 | /* load the map. */ |
| 1708 | if (!sample_load_map(args, &conv, file, line, &err)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1709 | /* error case: we can't use luaL_error because we must |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1710 | * free the err variable. |
| 1711 | */ |
| 1712 | luaL_where(L, 1); |
| 1713 | lua_pushfstring(L, "'new': %s.", err); |
| 1714 | lua_concat(L, 2); |
| 1715 | free(err); |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 1716 | chunk_destroy(&args[0].data.str); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1717 | WILL_LJMP(lua_error(L)); |
| 1718 | } |
| 1719 | |
| 1720 | /* create the lua object. */ |
| 1721 | lua_newtable(L); |
| 1722 | lua_pushlightuserdata(L, args[0].data.map); |
| 1723 | lua_rawseti(L, -2, 0); |
| 1724 | |
| 1725 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1726 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1727 | lua_setmetatable(L, -2); |
| 1728 | |
| 1729 | |
| 1730 | return 1; |
| 1731 | } |
| 1732 | |
| 1733 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1734 | { |
| 1735 | struct map_descriptor *desc; |
| 1736 | struct pattern *pat; |
| 1737 | struct sample smp; |
| 1738 | |
| 1739 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1740 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1741 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1742 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1743 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1744 | } |
| 1745 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1746 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1747 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1748 | 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] | 1749 | smp.data.u.str.size = smp.data.u.str.data + 1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1750 | } |
| 1751 | |
| 1752 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1753 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1754 | if (str) |
| 1755 | lua_pushstring(L, ""); |
| 1756 | else |
| 1757 | lua_pushnil(L); |
| 1758 | return 1; |
| 1759 | } |
| 1760 | |
| 1761 | /* 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] | 1762 | 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] | 1763 | return 1; |
| 1764 | } |
| 1765 | |
| 1766 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1767 | { |
| 1768 | return _hlua_map_lookup(L, 0); |
| 1769 | } |
| 1770 | |
| 1771 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1772 | { |
| 1773 | return _hlua_map_lookup(L, 1); |
| 1774 | } |
| 1775 | |
| 1776 | /* |
| 1777 | * |
| 1778 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1779 | * Class Socket |
| 1780 | * |
| 1781 | * |
| 1782 | */ |
| 1783 | |
| 1784 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1785 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1786 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1787 | } |
| 1788 | |
| 1789 | /* 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] | 1790 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1791 | * received. |
| 1792 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1793 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1794 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1795 | struct stream_interface *si = appctx->owner; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1796 | |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1797 | if (appctx->ctx.hlua_cosocket.die) { |
| 1798 | si_shutw(si); |
| 1799 | si_shutr(si); |
| 1800 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1801 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1802 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1803 | stream_shutdown(si_strm(si), SF_ERR_KILLED); |
| 1804 | } |
| 1805 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1806 | /* If we can't write, wakeup the pending write signals. */ |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1807 | if (channel_output_closed(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1808 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1809 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1810 | /* If we can't read, wakeup the pending read signals. */ |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1811 | if (channel_input_closed(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1812 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1813 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1814 | /* if the connection is not established, inform the stream that we want |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1815 | * to be notified whenever the connection completes. |
| 1816 | */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1817 | if (si_opposite(si)->state < SI_ST_EST) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 1818 | si_cant_get(si); |
Willy Tarreau | 12c2423 | 2018-12-06 15:29:50 +0100 | [diff] [blame] | 1819 | si_rx_conn_blk(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1820 | si_rx_endp_more(si); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1821 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1822 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1823 | |
| 1824 | /* This function is called after the connect. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1825 | appctx->ctx.hlua_cosocket.connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1826 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1827 | /* 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] | 1828 | if (channel_may_recv(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1829 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1830 | |
| 1831 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1832 | if (!channel_is_empty(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1833 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1834 | |
| 1835 | /* Some data were injected in the buffer, notify the stream |
| 1836 | * interface. |
| 1837 | */ |
| 1838 | if (!channel_is_empty(si_ic(si))) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 1839 | si_update(si); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1840 | |
| 1841 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1842 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1843 | */ |
| 1844 | if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write)) |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1845 | si_rx_endp_more(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1846 | } |
| 1847 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1848 | /* This function is called when the "struct stream" is destroyed. |
| 1849 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1850 | * Wake all the pending signals. |
| 1851 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1852 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1853 | { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1854 | struct xref *peer; |
| 1855 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1856 | /* Remove my link in the original object. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1857 | peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref); |
| 1858 | if (peer) |
| 1859 | xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1860 | |
| 1861 | /* Wake all the task waiting for me. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1862 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1863 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1864 | } |
| 1865 | |
| 1866 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1867 | * uses this object. If the stream does not exists, just quit. |
| 1868 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1869 | * pending signal can rest in the read and write lists. destroy |
| 1870 | * it. |
| 1871 | */ |
| 1872 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1873 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1874 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1875 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1876 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1877 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1878 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 1879 | |
| 1880 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1881 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1882 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1883 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1884 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1885 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1886 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1887 | appctx->ctx.hlua_cosocket.die = 1; |
| 1888 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1889 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1890 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1891 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1892 | return 0; |
| 1893 | } |
| 1894 | |
| 1895 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1896 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1897 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1898 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1899 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1900 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1901 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1902 | struct xref *peer; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1903 | struct hlua *hlua; |
| 1904 | |
| 1905 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1906 | hlua = hlua_gethlua(L); |
| 1907 | if (!hlua) |
| 1908 | return 0; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1909 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1910 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1911 | |
| 1912 | /* Check if we run on the same thread than the xreator thread. |
| 1913 | * We cannot access to the socket if the thread is different. |
| 1914 | */ |
| 1915 | if (socket->tid != tid) |
| 1916 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1917 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1918 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1919 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1920 | return 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1921 | |
| 1922 | hlua->gc_count--; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1923 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1924 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1925 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1926 | appctx->ctx.hlua_cosocket.die = 1; |
| 1927 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1928 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1929 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1930 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1931 | return 0; |
| 1932 | } |
| 1933 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1934 | /* The close function calls close_helper. |
| 1935 | */ |
| 1936 | __LJMP static int hlua_socket_close(lua_State *L) |
| 1937 | { |
| 1938 | MAY_LJMP(check_args(L, 1, "close")); |
| 1939 | return hlua_socket_close_helper(L); |
| 1940 | } |
| 1941 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1942 | /* This Lua function assumes that the stack contain three parameters. |
| 1943 | * 1 - USERDATA containing a struct socket |
| 1944 | * 2 - INTEGER with values of the macro defined below |
| 1945 | * If the integer is -1, we must read at most one line. |
| 1946 | * If the integer is -2, we ust read all the data until the |
| 1947 | * end of the stream. |
| 1948 | * If the integer is positive value, we must read a number of |
| 1949 | * bytes corresponding to this value. |
| 1950 | */ |
| 1951 | #define HLSR_READ_LINE (-1) |
| 1952 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1953 | __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] | 1954 | { |
| 1955 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1956 | int wanted = lua_tointeger(L, 2); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1957 | struct hlua *hlua; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1958 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1959 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1960 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1961 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1962 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1963 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1964 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1965 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1966 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1967 | struct stream_interface *si; |
| 1968 | struct stream *s; |
| 1969 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1970 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1971 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1972 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1973 | hlua = hlua_gethlua(L); |
| 1974 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1975 | /* Check if this lua stack is schedulable. */ |
| 1976 | if (!hlua || !hlua->task) |
| 1977 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 1978 | "'frontend', 'backend' or 'task'")); |
| 1979 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1980 | /* Check if we run on the same thread than the xreator thread. |
| 1981 | * We cannot access to the socket if the thread is different. |
| 1982 | */ |
| 1983 | if (socket->tid != tid) |
| 1984 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1985 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1986 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1987 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1988 | if (!peer) |
| 1989 | goto no_peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1990 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 1991 | si = appctx->owner; |
| 1992 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1993 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1994 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1995 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1996 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1997 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1998 | if (nblk < 0) /* Connection close. */ |
| 1999 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2000 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2001 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 2002 | |
| 2003 | /* remove final \r\n. */ |
| 2004 | if (nblk == 1) { |
| 2005 | if (blk1[len1-1] == '\n') { |
| 2006 | len1--; |
| 2007 | skip_at_end++; |
| 2008 | if (blk1[len1-1] == '\r') { |
| 2009 | len1--; |
| 2010 | skip_at_end++; |
| 2011 | } |
| 2012 | } |
| 2013 | } |
| 2014 | else { |
| 2015 | if (blk2[len2-1] == '\n') { |
| 2016 | len2--; |
| 2017 | skip_at_end++; |
| 2018 | if (blk2[len2-1] == '\r') { |
| 2019 | len2--; |
| 2020 | skip_at_end++; |
| 2021 | } |
| 2022 | } |
| 2023 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2024 | } |
| 2025 | |
| 2026 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2027 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2028 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2029 | if (nblk < 0) /* Connection close. */ |
| 2030 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2031 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2032 | goto connection_empty; |
| 2033 | } |
| 2034 | |
| 2035 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2036 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2037 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2038 | if (nblk < 0) /* Connection close. */ |
| 2039 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2040 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2041 | goto connection_empty; |
| 2042 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2043 | missing_bytes = wanted - socket->b.n; |
| 2044 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2045 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2046 | len1 = missing_bytes; |
| 2047 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 2048 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2049 | } |
| 2050 | |
| 2051 | len = len1; |
| 2052 | |
| 2053 | luaL_addlstring(&socket->b, blk1, len1); |
| 2054 | if (nblk == 2) { |
| 2055 | len += len2; |
| 2056 | luaL_addlstring(&socket->b, blk2, len2); |
| 2057 | } |
| 2058 | |
| 2059 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2060 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2061 | |
| 2062 | /* Don't wait anything. */ |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 2063 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2064 | |
| 2065 | /* If the pattern reclaim to read all the data |
| 2066 | * in the connection, got out. |
| 2067 | */ |
| 2068 | if (wanted == HLSR_READ_ALL) |
| 2069 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2070 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2071 | goto connection_empty; |
| 2072 | |
| 2073 | /* Return result. */ |
| 2074 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2075 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2076 | return 1; |
| 2077 | |
| 2078 | connection_closed: |
| 2079 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2080 | xref_unlock(&socket->xref, peer); |
| 2081 | |
| 2082 | no_peer: |
| 2083 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2084 | /* If the buffer containds data. */ |
| 2085 | if (socket->b.n > 0) { |
| 2086 | luaL_pushresult(&socket->b); |
| 2087 | return 1; |
| 2088 | } |
| 2089 | lua_pushnil(L); |
| 2090 | lua_pushstring(L, "connection closed."); |
| 2091 | return 2; |
| 2092 | |
| 2093 | connection_empty: |
| 2094 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2095 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) { |
| 2096 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2097 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2098 | } |
| 2099 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2100 | 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] | 2101 | return 0; |
| 2102 | } |
| 2103 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2104 | /* This Lua function gets two parameters. The first one can be string |
| 2105 | * or a number. If the string is "*l", the user requires one line. If |
| 2106 | * 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] | 2107 | * If the value is a number, the user require a number of bytes equal |
| 2108 | * to the value. The default value is "*l" (a line). |
| 2109 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2110 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2111 | * integer takes this values: |
| 2112 | * -1 : read a line |
| 2113 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2114 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2115 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2116 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2117 | * concatenated with the read data. |
| 2118 | */ |
| 2119 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 2120 | { |
| 2121 | int wanted = HLSR_READ_LINE; |
| 2122 | const char *pattern; |
| 2123 | int type; |
| 2124 | char *error; |
| 2125 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2126 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2127 | |
| 2128 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 2129 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 2130 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2131 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2132 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2133 | /* Check if we run on the same thread than the xreator thread. |
| 2134 | * We cannot access to the socket if the thread is different. |
| 2135 | */ |
| 2136 | if (socket->tid != tid) |
| 2137 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2138 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2139 | /* check for pattern. */ |
| 2140 | if (lua_gettop(L) >= 2) { |
| 2141 | type = lua_type(L, 2); |
| 2142 | if (type == LUA_TSTRING) { |
| 2143 | pattern = lua_tostring(L, 2); |
| 2144 | if (strcmp(pattern, "*a") == 0) |
| 2145 | wanted = HLSR_READ_ALL; |
| 2146 | else if (strcmp(pattern, "*l") == 0) |
| 2147 | wanted = HLSR_READ_LINE; |
| 2148 | else { |
| 2149 | wanted = strtoll(pattern, &error, 10); |
| 2150 | if (*error != '\0') |
| 2151 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 2152 | } |
| 2153 | } |
| 2154 | else if (type == LUA_TNUMBER) { |
| 2155 | wanted = lua_tointeger(L, 2); |
| 2156 | if (wanted < 0) |
| 2157 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 2158 | } |
| 2159 | } |
| 2160 | |
| 2161 | /* Set pattern. */ |
| 2162 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 2163 | |
| 2164 | /* Check if we would replace the top by itself. */ |
| 2165 | if (lua_gettop(L) != 2) |
| 2166 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2167 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2168 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2169 | luaL_buffinit(L, &socket->b); |
| 2170 | |
| 2171 | /* Check prefix. */ |
| 2172 | if (lua_gettop(L) >= 3) { |
| 2173 | if (lua_type(L, 3) != LUA_TSTRING) |
| 2174 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 2175 | pattern = lua_tolstring(L, 3, &len); |
| 2176 | luaL_addlstring(&socket->b, pattern, len); |
| 2177 | } |
| 2178 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2179 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2180 | } |
| 2181 | |
| 2182 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 2183 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2184 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2185 | 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] | 2186 | { |
| 2187 | struct hlua_socket *socket; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2188 | struct hlua *hlua; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2189 | struct appctx *appctx; |
| 2190 | size_t buf_len; |
| 2191 | const char *buf; |
| 2192 | int len; |
| 2193 | int send_len; |
| 2194 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2195 | struct xref *peer; |
| 2196 | struct stream_interface *si; |
| 2197 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2198 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2199 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2200 | hlua = hlua_gethlua(L); |
| 2201 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2202 | /* Check if this lua stack is schedulable. */ |
| 2203 | if (!hlua || !hlua->task) |
| 2204 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 2205 | "'frontend', 'backend' or 'task'")); |
| 2206 | |
| 2207 | /* Get object */ |
| 2208 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2209 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2210 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2211 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2212 | /* Check if we run on the same thread than the xreator thread. |
| 2213 | * We cannot access to the socket if the thread is different. |
| 2214 | */ |
| 2215 | if (socket->tid != tid) |
| 2216 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2217 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2218 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2219 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2220 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2221 | lua_pushinteger(L, -1); |
| 2222 | return 1; |
| 2223 | } |
| 2224 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2225 | si = appctx->owner; |
| 2226 | s = si_strm(si); |
| 2227 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2228 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2229 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2230 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2231 | lua_pushinteger(L, -1); |
| 2232 | return 1; |
| 2233 | } |
| 2234 | |
| 2235 | /* Update the input buffer data. */ |
| 2236 | buf += sent; |
| 2237 | send_len = buf_len - sent; |
| 2238 | |
| 2239 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2240 | if (sent >= buf_len) { |
| 2241 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2242 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2243 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2244 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2245 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2246 | * the request buffer if its not required. |
| 2247 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2248 | if (s->req.buf.size == 0) { |
Willy Tarreau | 581abd3 | 2018-10-25 10:21:41 +0200 | [diff] [blame] | 2249 | if (!si_alloc_ibuf(si, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2250 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2251 | } |
| 2252 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2253 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2254 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2255 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2256 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2257 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2258 | |
| 2259 | /* send data */ |
| 2260 | if (len < send_len) |
| 2261 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2262 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2263 | |
| 2264 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2265 | * the data" (-2) are not expected because the available length |
| 2266 | * is tested. |
| 2267 | * Other unknown error are also not expected. |
| 2268 | */ |
| 2269 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2270 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2271 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2272 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2273 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2274 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2275 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2276 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2277 | return 1; |
| 2278 | } |
| 2279 | |
| 2280 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2281 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2282 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2283 | s->req.rex = TICK_ETERNITY; |
| 2284 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2285 | |
| 2286 | /* Update length sent. */ |
| 2287 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2288 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2289 | |
| 2290 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2291 | if (sent + len >= buf_len) { |
| 2292 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2293 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2294 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2295 | |
| 2296 | hlua_socket_write_yield_return: |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2297 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2298 | xref_unlock(&socket->xref, peer); |
| 2299 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2300 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2301 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2302 | 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] | 2303 | return 0; |
| 2304 | } |
| 2305 | |
| 2306 | /* This function initiate the send of data. It just check the input |
| 2307 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2308 | * 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] | 2309 | * "hlua_socket_write_yield" that can yield. |
| 2310 | * |
| 2311 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2312 | * the associated object. The second is a string buffer. The third is |
| 2313 | * a facultative integer that represents where is the buffer position |
| 2314 | * of the start of the data that can send. The first byte is the |
| 2315 | * position "1". The default value is "1". The fourth argument is a |
| 2316 | * facultative integer that represents where is the buffer position |
| 2317 | * of the end of the data that can send. The default is the last byte. |
| 2318 | */ |
| 2319 | static int hlua_socket_send(struct lua_State *L) |
| 2320 | { |
| 2321 | int i; |
| 2322 | int j; |
| 2323 | const char *buf; |
| 2324 | size_t buf_len; |
| 2325 | |
| 2326 | /* Check number of arguments. */ |
| 2327 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2328 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2329 | |
| 2330 | /* Get the string. */ |
| 2331 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2332 | |
| 2333 | /* Get and check j. */ |
| 2334 | if (lua_gettop(L) == 4) { |
| 2335 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2336 | if (j < 0) |
| 2337 | j = buf_len + j + 1; |
| 2338 | if (j > buf_len) |
| 2339 | j = buf_len + 1; |
| 2340 | lua_pop(L, 1); |
| 2341 | } |
| 2342 | else |
| 2343 | j = buf_len; |
| 2344 | |
| 2345 | /* Get and check i. */ |
| 2346 | if (lua_gettop(L) == 3) { |
| 2347 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2348 | if (i < 0) |
| 2349 | i = buf_len + i + 1; |
| 2350 | if (i > buf_len) |
| 2351 | i = buf_len + 1; |
| 2352 | lua_pop(L, 1); |
| 2353 | } else |
| 2354 | i = 1; |
| 2355 | |
| 2356 | /* Check bth i and j. */ |
| 2357 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2358 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2359 | return 1; |
| 2360 | } |
| 2361 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2362 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2363 | return 1; |
| 2364 | } |
| 2365 | if (i == 0) |
| 2366 | i = 1; |
| 2367 | if (j == 0) |
| 2368 | j = 1; |
| 2369 | |
| 2370 | /* Pop the string. */ |
| 2371 | lua_pop(L, 1); |
| 2372 | |
| 2373 | /* Update the buffer length. */ |
| 2374 | buf += i - 1; |
| 2375 | buf_len = j - i + 1; |
| 2376 | lua_pushlstring(L, buf, buf_len); |
| 2377 | |
| 2378 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2379 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2380 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2381 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2382 | } |
| 2383 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2384 | #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] | 2385 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 2386 | { |
| 2387 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2388 | int ret; |
| 2389 | int len; |
| 2390 | char *p; |
| 2391 | |
| 2392 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2393 | if (ret <= 0) { |
| 2394 | lua_pushnil(L); |
| 2395 | return 1; |
| 2396 | } |
| 2397 | |
| 2398 | if (ret == AF_UNIX) { |
| 2399 | lua_pushstring(L, buffer+1); |
| 2400 | return 1; |
| 2401 | } |
| 2402 | else if (ret == AF_INET6) { |
| 2403 | buffer[0] = '['; |
| 2404 | len = strlen(buffer); |
| 2405 | buffer[len] = ']'; |
| 2406 | len++; |
| 2407 | buffer[len] = ':'; |
| 2408 | len++; |
| 2409 | p = buffer; |
| 2410 | } |
| 2411 | else if (ret == AF_INET) { |
| 2412 | p = buffer + 1; |
| 2413 | len = strlen(p); |
| 2414 | p[len] = ':'; |
| 2415 | len++; |
| 2416 | } |
| 2417 | else { |
| 2418 | lua_pushnil(L); |
| 2419 | return 1; |
| 2420 | } |
| 2421 | |
| 2422 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2423 | lua_pushnil(L); |
| 2424 | return 1; |
| 2425 | } |
| 2426 | |
| 2427 | lua_pushstring(L, p); |
| 2428 | return 1; |
| 2429 | } |
| 2430 | |
| 2431 | /* Returns information about the peer of the connection. */ |
| 2432 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2433 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2434 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2435 | struct xref *peer; |
| 2436 | struct appctx *appctx; |
| 2437 | struct stream_interface *si; |
| 2438 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2439 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2440 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2441 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2442 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2443 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2444 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2445 | /* Check if we run on the same thread than the xreator thread. |
| 2446 | * We cannot access to the socket if the thread is different. |
| 2447 | */ |
| 2448 | if (socket->tid != tid) |
| 2449 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2450 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2451 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2452 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2453 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2454 | lua_pushnil(L); |
| 2455 | return 1; |
| 2456 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2457 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2458 | si = appctx->owner; |
| 2459 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2460 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2461 | if (!s->target_addr) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2462 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2463 | lua_pushnil(L); |
| 2464 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2465 | } |
| 2466 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2467 | ret = MAY_LJMP(hlua_socket_info(L, s->target_addr)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2468 | xref_unlock(&socket->xref, peer); |
| 2469 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2470 | } |
| 2471 | |
| 2472 | /* Returns information about my connection side. */ |
| 2473 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2474 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2475 | struct hlua_socket *socket; |
| 2476 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2477 | struct appctx *appctx; |
| 2478 | struct xref *peer; |
| 2479 | struct stream_interface *si; |
| 2480 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2481 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2482 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2483 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2484 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2485 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2486 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2487 | /* Check if we run on the same thread than the xreator thread. |
| 2488 | * We cannot access to the socket if the thread is different. |
| 2489 | */ |
| 2490 | if (socket->tid != tid) |
| 2491 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2492 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2493 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2494 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2495 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2496 | lua_pushnil(L); |
| 2497 | return 1; |
| 2498 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2499 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2500 | si = appctx->owner; |
| 2501 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2502 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2503 | conn = cs_conn(objt_cs(s->si[1].end)); |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2504 | if (!conn || !conn_get_src(conn)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2505 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2506 | lua_pushnil(L); |
| 2507 | return 1; |
| 2508 | } |
| 2509 | |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2510 | ret = hlua_socket_info(L, conn->src); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2511 | xref_unlock(&socket->xref, peer); |
| 2512 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2513 | } |
| 2514 | |
| 2515 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2516 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2517 | .obj_type = OBJ_TYPE_APPLET, |
| 2518 | .name = "<LUA_TCP>", |
| 2519 | .fct = hlua_socket_handler, |
| 2520 | .release = hlua_socket_release, |
| 2521 | }; |
| 2522 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2523 | __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] | 2524 | { |
| 2525 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2526 | struct hlua *hlua; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2527 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2528 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2529 | struct stream_interface *si; |
| 2530 | struct stream *s; |
| 2531 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2532 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2533 | hlua = hlua_gethlua(L); |
| 2534 | if (!hlua) |
| 2535 | return 0; |
| 2536 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2537 | /* Check if we run on the same thread than the xreator thread. |
| 2538 | * We cannot access to the socket if the thread is different. |
| 2539 | */ |
| 2540 | if (socket->tid != tid) |
| 2541 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2542 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2543 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2544 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2545 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2546 | lua_pushnil(L); |
| 2547 | lua_pushstring(L, "Can't connect"); |
| 2548 | return 2; |
| 2549 | } |
| 2550 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2551 | si = appctx->owner; |
| 2552 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2553 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2554 | /* Check if we run on the same thread than the xreator thread. |
| 2555 | * We cannot access to the socket if the thread is different. |
| 2556 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2557 | if (socket->tid != tid) { |
| 2558 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2559 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2560 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2561 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2562 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2563 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2564 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2565 | lua_pushnil(L); |
| 2566 | lua_pushstring(L, "Can't connect"); |
| 2567 | return 2; |
| 2568 | } |
| 2569 | |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2570 | appctx = __objt_appctx(s->si[0].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2571 | |
| 2572 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2573 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2574 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2575 | lua_pushinteger(L, 1); |
| 2576 | return 1; |
| 2577 | } |
| 2578 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2579 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2580 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2581 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2582 | } |
| 2583 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2584 | 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] | 2585 | return 0; |
| 2586 | } |
| 2587 | |
| 2588 | /* This function fail or initite the connection. */ |
| 2589 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2590 | { |
| 2591 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2592 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2593 | const char *ip; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2594 | struct hlua *hlua; |
| 2595 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2596 | int low, high; |
| 2597 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2598 | struct xref *peer; |
| 2599 | struct stream_interface *si; |
| 2600 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2601 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2602 | if (lua_gettop(L) < 2) |
| 2603 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2604 | |
| 2605 | /* Get args. */ |
| 2606 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2607 | |
| 2608 | /* Check if we run on the same thread than the xreator thread. |
| 2609 | * We cannot access to the socket if the thread is different. |
| 2610 | */ |
| 2611 | if (socket->tid != tid) |
| 2612 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2613 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2614 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2615 | if (lua_gettop(L) >= 3) { |
| 2616 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2617 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2618 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2619 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2620 | * that are not enclosed within square brackets. |
| 2621 | */ |
| 2622 | if (port > 0) { |
| 2623 | luaL_buffinit(L, &b); |
| 2624 | luaL_addstring(&b, ip); |
| 2625 | luaL_addchar(&b, ':'); |
| 2626 | luaL_pushresult(&b); |
| 2627 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2628 | } |
| 2629 | } |
| 2630 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2631 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2632 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2633 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2634 | lua_pushnil(L); |
| 2635 | return 1; |
| 2636 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2637 | |
| 2638 | /* Parse ip address. */ |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 2639 | 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] | 2640 | if (!addr) { |
| 2641 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2642 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2643 | } |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2644 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2645 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2646 | if (low == 0) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2647 | if (addr->ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2648 | if (port == -1) { |
| 2649 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2650 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2651 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2652 | ((struct sockaddr_in *)addr)->sin_port = htons(port); |
| 2653 | } else if (addr->ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2654 | if (port == -1) { |
| 2655 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2656 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2657 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2658 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(port); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2659 | } |
| 2660 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2661 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2662 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2663 | si = appctx->owner; |
| 2664 | s = si_strm(si); |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2665 | |
Willy Tarreau | 9b7587a | 2020-10-15 07:32:10 +0200 | [diff] [blame] | 2666 | if (!sockaddr_alloc(&s->target_addr, addr, sizeof(*addr))) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2667 | xref_unlock(&socket->xref, peer); |
| 2668 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
| 2669 | } |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2670 | s->flags |= SF_ADDR_SET; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2671 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2672 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2673 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2674 | if (!hlua) |
| 2675 | return 0; |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2676 | |
| 2677 | /* inform the stream that we want to be notified whenever the |
| 2678 | * connection completes. |
| 2679 | */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 2680 | si_cant_get(&s->si[0]); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2681 | si_rx_endp_more(&s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2682 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2683 | |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2684 | hlua->gc_count++; |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2685 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2686 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2687 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2688 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2689 | } |
| 2690 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2691 | |
| 2692 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2693 | /* Return yield waiting for connection. */ |
| 2694 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2695 | 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] | 2696 | |
| 2697 | return 0; |
| 2698 | } |
| 2699 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2700 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2701 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2702 | { |
| 2703 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2704 | struct xref *peer; |
| 2705 | struct appctx *appctx; |
| 2706 | struct stream_interface *si; |
| 2707 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2708 | |
| 2709 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2710 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2711 | |
| 2712 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2713 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2714 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2715 | lua_pushnil(L); |
| 2716 | return 1; |
| 2717 | } |
| 2718 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2719 | si = appctx->owner; |
| 2720 | s = si_strm(si); |
| 2721 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 2722 | s->target = &socket_ssl->obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2723 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2724 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2725 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2726 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2727 | |
| 2728 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2729 | { |
| 2730 | return 0; |
| 2731 | } |
| 2732 | |
| 2733 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2734 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2735 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2736 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2737 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2738 | struct xref *peer; |
| 2739 | struct appctx *appctx; |
| 2740 | struct stream_interface *si; |
| 2741 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2742 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2743 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2744 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2745 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2746 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2747 | /* convert the timeout to millis */ |
| 2748 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2749 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2750 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2751 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2752 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2753 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2754 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2755 | 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] | 2756 | |
| 2757 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2758 | if (tmout == 0) |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2759 | tmout++; /* very small timeouts are adjusted to a minimum of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2760 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2761 | /* Check if we run on the same thread than the xreator thread. |
| 2762 | * We cannot access to the socket if the thread is different. |
| 2763 | */ |
| 2764 | if (socket->tid != tid) |
| 2765 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2766 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2767 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2768 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2769 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2770 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2771 | WILL_LJMP(lua_error(L)); |
| 2772 | return 0; |
| 2773 | } |
| 2774 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2775 | si = appctx->owner; |
| 2776 | s = si_strm(si); |
| 2777 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2778 | s->sess->fe->timeout.connect = tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2779 | s->req.rto = tmout; |
| 2780 | s->req.wto = tmout; |
| 2781 | s->res.rto = tmout; |
| 2782 | s->res.wto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2783 | s->req.rex = tick_add_ifset(now_ms, tmout); |
| 2784 | s->req.wex = tick_add_ifset(now_ms, tmout); |
| 2785 | s->res.rex = tick_add_ifset(now_ms, tmout); |
| 2786 | s->res.wex = tick_add_ifset(now_ms, tmout); |
| 2787 | |
| 2788 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 2789 | task_queue(s->task); |
| 2790 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2791 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2792 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 2793 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 2794 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2795 | } |
| 2796 | |
| 2797 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2798 | { |
| 2799 | struct hlua_socket *socket; |
| 2800 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2801 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2802 | struct stream *strm; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2803 | |
| 2804 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2805 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2806 | hlua_pusherror(L, "socket: full stack"); |
| 2807 | goto out_fail_conf; |
| 2808 | } |
| 2809 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2810 | /* Create the object: obj[0] = userdata. */ |
| 2811 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2812 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2813 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2814 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2815 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2816 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2817 | /* Check if the various memory pools are initialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2818 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2819 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2820 | goto out_fail_conf; |
| 2821 | } |
| 2822 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2823 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2824 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2825 | lua_setmetatable(L, -2); |
| 2826 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2827 | /* Create the applet context */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 2828 | appctx = appctx_new(&update_applet, tid_bit); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2829 | if (!appctx) { |
| 2830 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2831 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2832 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2833 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2834 | appctx->ctx.hlua_cosocket.connected = 0; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2835 | appctx->ctx.hlua_cosocket.die = 0; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2836 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 2837 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2838 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2839 | /* Now create a session, task and stream for this applet */ |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 2840 | sess = session_new(socket_proxy, NULL, &appctx->obj_type); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2841 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2842 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2843 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2844 | } |
| 2845 | |
Christopher Faulet | 26256f8 | 2020-09-14 11:40:13 +0200 | [diff] [blame] | 2846 | strm = stream_new(sess, &appctx->obj_type, &BUF_NULL); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2847 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2848 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2849 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2850 | } |
| 2851 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2852 | /* Initialise cross reference between stream and Lua socket object. */ |
| 2853 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2854 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2855 | /* Configure "right" stream interface. this "si" is used to connect |
| 2856 | * and retrieve data from the server. The connection is initialized |
| 2857 | * with the "struct server". |
| 2858 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2859 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2860 | |
| 2861 | /* Force destination server. */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2862 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED; |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 2863 | strm->target = &socket_tcp->obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2864 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2865 | return 1; |
| 2866 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2867 | out_fail_stream: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 2868 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2869 | out_fail_sess: |
| 2870 | appctx_free(appctx); |
| 2871 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2872 | WILL_LJMP(lua_error(L)); |
| 2873 | return 0; |
| 2874 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2875 | |
| 2876 | /* |
| 2877 | * |
| 2878 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2879 | * Class Channel |
| 2880 | * |
| 2881 | * |
| 2882 | */ |
| 2883 | |
| 2884 | /* Returns the struct hlua_channel join to the class channel in the |
| 2885 | * stack entry "ud" or throws an argument error. |
| 2886 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2887 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2888 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2889 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2890 | } |
| 2891 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2892 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 2893 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2894 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 2895 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2896 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2897 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2898 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2899 | return 0; |
| 2900 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2901 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2902 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2903 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2904 | |
| 2905 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 2906 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 2907 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2908 | return 1; |
| 2909 | } |
| 2910 | |
| 2911 | /* Duplicate all the data present in the input channel and put it |
| 2912 | * in a string LUA variables. Returns -1 and push a nil value in |
| 2913 | * the stack if the channel is closed and all the data are consumed, |
| 2914 | * returns 0 if no data are available, otherwise it returns the length |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 2915 | * of the built string. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2916 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2917 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2918 | { |
| 2919 | char *blk1; |
| 2920 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2921 | size_t len1; |
| 2922 | size_t len2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2923 | int ret; |
| 2924 | luaL_Buffer b; |
| 2925 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2926 | ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2927 | if (unlikely(ret == 0)) |
| 2928 | return 0; |
| 2929 | |
| 2930 | if (unlikely(ret < 0)) { |
| 2931 | lua_pushnil(L); |
| 2932 | return -1; |
| 2933 | } |
| 2934 | |
| 2935 | luaL_buffinit(L, &b); |
| 2936 | luaL_addlstring(&b, blk1, len1); |
| 2937 | if (unlikely(ret == 2)) |
| 2938 | luaL_addlstring(&b, blk2, len2); |
| 2939 | luaL_pushresult(&b); |
| 2940 | |
| 2941 | if (unlikely(ret == 2)) |
| 2942 | return len1 + len2; |
| 2943 | return len1; |
| 2944 | } |
| 2945 | |
| 2946 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2947 | * a yield. This function keep the data in the buffer. |
| 2948 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2949 | __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] | 2950 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2951 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2952 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2953 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2954 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 2955 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2956 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2957 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2958 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2959 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2960 | if (_hlua_channel_dup(chn, L) == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2961 | 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] | 2962 | return 1; |
| 2963 | } |
| 2964 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2965 | /* Check arguments for the function "hlua_channel_dup_yield". */ |
| 2966 | __LJMP static int hlua_channel_dup(lua_State *L) |
| 2967 | { |
| 2968 | MAY_LJMP(check_args(L, 1, "dup")); |
| 2969 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2970 | return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0)); |
| 2971 | } |
| 2972 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2973 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2974 | * a yield. This function consumes the data in the buffer. It returns |
| 2975 | * a string containing the data or a nil pointer if no data are available |
| 2976 | * and the channel is closed. |
| 2977 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2978 | __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] | 2979 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2980 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2981 | int ret; |
| 2982 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2983 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2984 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 2985 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2986 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2987 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 2988 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2989 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2990 | ret = _hlua_channel_dup(chn, L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2991 | if (unlikely(ret == 0)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2992 | 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] | 2993 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2994 | if (unlikely(ret == -1)) |
| 2995 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2996 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2997 | b_sub(&chn->buf, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2998 | return 1; |
| 2999 | } |
| 3000 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3001 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3002 | __LJMP static int hlua_channel_get(lua_State *L) |
| 3003 | { |
| 3004 | MAY_LJMP(check_args(L, 1, "get")); |
| 3005 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3006 | return MAY_LJMP(hlua_channel_get_yield(L, 0, 0)); |
| 3007 | } |
| 3008 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3009 | /* This functions consumes and returns one line. If the channel is closed, |
| 3010 | * 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] | 3011 | * 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] | 3012 | * value. |
| 3013 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3014 | __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] | 3015 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3016 | char *blk1; |
| 3017 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3018 | size_t len1; |
| 3019 | size_t len2; |
| 3020 | size_t len; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3021 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3022 | int ret; |
| 3023 | luaL_Buffer b; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3024 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3025 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3026 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3027 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3028 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3029 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3030 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3031 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3032 | ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3033 | if (ret == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3034 | 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] | 3035 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3036 | if (ret == -1) { |
| 3037 | lua_pushnil(L); |
| 3038 | return 1; |
| 3039 | } |
| 3040 | |
| 3041 | luaL_buffinit(L, &b); |
| 3042 | luaL_addlstring(&b, blk1, len1); |
| 3043 | len = len1; |
| 3044 | if (unlikely(ret == 2)) { |
| 3045 | luaL_addlstring(&b, blk2, len2); |
| 3046 | len += len2; |
| 3047 | } |
| 3048 | luaL_pushresult(&b); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3049 | 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] | 3050 | return 1; |
| 3051 | } |
| 3052 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3053 | /* Check arguments for the function "hlua_channel_getline_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3054 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 3055 | { |
| 3056 | MAY_LJMP(check_args(L, 1, "getline")); |
| 3057 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3058 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 3059 | } |
| 3060 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3061 | /* This function takes a string as input, and append it at the |
| 3062 | * input side of channel. If the data is too big, but a space |
| 3063 | * is probably available after sending some data, the function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3064 | * yields. If the data is bigger than the buffer, or if the |
| 3065 | * channel is closed, it returns -1. Otherwise, it returns the |
| 3066 | * amount of data written. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3067 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3068 | __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] | 3069 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3070 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3071 | size_t len; |
| 3072 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3073 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3074 | int ret; |
| 3075 | int max; |
| 3076 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3077 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3078 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3079 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3080 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3081 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3082 | /* Check if the buffer is available because HAProxy doesn't allocate |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 3083 | * the request buffer if its not required. |
| 3084 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3085 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 3086 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3087 | 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] | 3088 | } |
| 3089 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3090 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3091 | if (max > len - l) |
| 3092 | max = len - l; |
| 3093 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3094 | ret = ci_putblk(chn, str + l, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3095 | if (ret == -2 || ret == -3) { |
| 3096 | lua_pushinteger(L, -1); |
| 3097 | return 1; |
| 3098 | } |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 3099 | if (ret == -1) { |
| 3100 | chn->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3101 | 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] | 3102 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3103 | l += ret; |
| 3104 | lua_pop(L, 1); |
| 3105 | lua_pushinteger(L, l); |
| 3106 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3107 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3108 | if (max == 0 && co_data(chn) == 0) { |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3109 | /* There are no space available, and the output buffer is empty. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3110 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3111 | * we return the amount of copied data. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3112 | */ |
| 3113 | return 1; |
| 3114 | } |
| 3115 | if (l < len) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3116 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3117 | return 1; |
| 3118 | } |
| 3119 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3120 | /* Just a wrapper of "hlua_channel_append_yield". It returns the length |
| 3121 | * 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] | 3122 | * buffer size is too little for the data. |
| 3123 | */ |
| 3124 | __LJMP static int hlua_channel_append(lua_State *L) |
| 3125 | { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3126 | size_t len; |
| 3127 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3128 | MAY_LJMP(check_args(L, 2, "append")); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3129 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3130 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3131 | MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3132 | lua_pushinteger(L, 0); |
| 3133 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3134 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3135 | } |
| 3136 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3137 | /* Just a wrapper of "hlua_channel_append_yield". This wrapper starts |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3138 | * his process by cleaning the buffer. The result is a replacement |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3139 | * of the current data. It returns the length of the written string, |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3140 | * or -1 if the channel is closed or if the buffer size is too |
| 3141 | * little for the data. |
| 3142 | */ |
| 3143 | __LJMP static int hlua_channel_set(lua_State *L) |
| 3144 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3145 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3146 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3147 | MAY_LJMP(check_args(L, 2, "set")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3148 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3149 | lua_pushinteger(L, 0); |
| 3150 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3151 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3152 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3153 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3154 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3155 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3156 | b_set_data(&chn->buf, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3157 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3158 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3159 | } |
| 3160 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3161 | /* Append data in the output side of the buffer. This data is immediately |
| 3162 | * sent. The function returns the amount of data written. If the buffer |
| 3163 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3164 | * if the channel is closed. |
| 3165 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3166 | __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] | 3167 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3168 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3169 | size_t len; |
| 3170 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3171 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3172 | int max; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3173 | struct hlua *hlua; |
| 3174 | |
| 3175 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3176 | hlua = hlua_gethlua(L); |
| 3177 | if (!hlua) { |
| 3178 | lua_pushnil(L); |
| 3179 | return 1; |
| 3180 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3181 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3182 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3183 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3184 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3185 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3186 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3187 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3188 | lua_pushinteger(L, -1); |
| 3189 | return 1; |
| 3190 | } |
| 3191 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3192 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 3193 | * the request buffer if its not required. |
| 3194 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3195 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 3196 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3197 | 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] | 3198 | } |
| 3199 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3200 | /* The written data will be immediately sent, so we can check |
| 3201 | * the available space without taking in account the reserve. |
| 3202 | * The reserve is guaranteed for the processing of incoming |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3203 | * data, because the buffer will be flushed. |
| 3204 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3205 | max = b_room(&chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3206 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3207 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3208 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3209 | * we return the amount of copied data. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3210 | */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3211 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3212 | return 1; |
| 3213 | |
| 3214 | /* Adjust the real required length. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3215 | if (max > len - l) |
| 3216 | max = len - l; |
| 3217 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3218 | /* The buffer available size may be not contiguous. This test |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3219 | * detects a non contiguous buffer and realign it. |
| 3220 | */ |
Willy Tarreau | 3f67999 | 2018-06-15 15:06:42 +0200 | [diff] [blame] | 3221 | if (ci_space_for_replace(chn) < max) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3222 | channel_slow_realign(chn, trash.area); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3223 | |
| 3224 | /* Copy input data in the buffer. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3225 | 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] | 3226 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3227 | /* buffer replace considers that the input part is filled. |
| 3228 | * so, I must forward these new data in the output part. |
| 3229 | */ |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 3230 | c_adv(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3231 | |
| 3232 | l += max; |
| 3233 | lua_pop(L, 1); |
| 3234 | lua_pushinteger(L, l); |
| 3235 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3236 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3237 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3238 | * we return the amount of copied data. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3239 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3240 | max = b_room(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3241 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3242 | return 1; |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3243 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3244 | if (l < len) { |
| 3245 | /* If we are waiting for space in the response buffer, we |
| 3246 | * must set the flag WAKERESWR. This flag required the task |
| 3247 | * wake up if any activity is detected on the response buffer. |
| 3248 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3249 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3250 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3251 | else |
| 3252 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3253 | 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] | 3254 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3255 | |
| 3256 | return 1; |
| 3257 | } |
| 3258 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3259 | /* Just a wrapper of "_hlua_channel_send". This wrapper permits |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3260 | * yield the LUA process, and resume it without checking the |
| 3261 | * input arguments. |
| 3262 | */ |
| 3263 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3264 | { |
| 3265 | MAY_LJMP(check_args(L, 2, "send")); |
| 3266 | lua_pushinteger(L, 0); |
| 3267 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3268 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3269 | } |
| 3270 | |
| 3271 | /* This function forward and amount of butes. The data pass from |
| 3272 | * the input side of the buffer to the output side, and can be |
| 3273 | * forwarded. This function never fails. |
| 3274 | * |
| 3275 | * The Lua function takes an amount of bytes to be forwarded in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3276 | * input. It returns the number of bytes forwarded. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3277 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3278 | __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] | 3279 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3280 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3281 | int len; |
| 3282 | int l; |
| 3283 | int max; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3284 | struct hlua *hlua; |
| 3285 | |
| 3286 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3287 | hlua = hlua_gethlua(L); |
| 3288 | if (!hlua) |
| 3289 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3290 | |
| 3291 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3292 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3293 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3294 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3295 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3296 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3297 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3298 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3299 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3300 | |
| 3301 | max = len - l; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3302 | if (max > ci_data(chn)) |
| 3303 | max = ci_data(chn); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3304 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3305 | l += max; |
| 3306 | |
| 3307 | lua_pop(L, 1); |
| 3308 | lua_pushinteger(L, l); |
| 3309 | |
| 3310 | /* Check if it miss bytes to forward. */ |
| 3311 | if (l < len) { |
| 3312 | /* The the input channel or the output channel are closed, we |
| 3313 | * must return the amount of data forwarded. |
| 3314 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3315 | if (channel_input_closed(chn) || channel_output_closed(chn)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3316 | return 1; |
| 3317 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3318 | /* If we are waiting for space data in the response buffer, we |
| 3319 | * must set the flag WAKERESWR. This flag required the task |
| 3320 | * wake up if any activity is detected on the response buffer. |
| 3321 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3322 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3323 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3324 | else |
| 3325 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3326 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3327 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3328 | 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] | 3329 | } |
| 3330 | |
| 3331 | return 1; |
| 3332 | } |
| 3333 | |
| 3334 | /* Just check the input and prepare the stack for the previous |
| 3335 | * function "hlua_channel_forward_yield" |
| 3336 | */ |
| 3337 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3338 | { |
| 3339 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3340 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3341 | MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3342 | |
| 3343 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3344 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3345 | } |
| 3346 | |
| 3347 | /* Just returns the number of bytes available in the input |
| 3348 | * side of the buffer. This function never fails. |
| 3349 | */ |
| 3350 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3351 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3352 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3353 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3354 | MAY_LJMP(check_args(L, 1, "get_in_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3355 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3356 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3357 | struct htx *htx = htxbuf(&chn->buf); |
| 3358 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 3359 | } |
| 3360 | else |
| 3361 | lua_pushinteger(L, ci_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3362 | return 1; |
| 3363 | } |
| 3364 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3365 | /* Returns true if the channel is full. */ |
| 3366 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3367 | { |
| 3368 | struct channel *chn; |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3369 | |
| 3370 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3371 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 0ec740e | 2020-02-26 11:59:19 +0100 | [diff] [blame] | 3372 | /* ignore the reserve, we are not on a producer side (ie in an |
| 3373 | * applet). |
| 3374 | */ |
| 3375 | lua_pushboolean(L, channel_full(chn, 0)); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3376 | return 1; |
| 3377 | } |
| 3378 | |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 3379 | /* Returns true if the channel is the response channel. */ |
| 3380 | __LJMP static int hlua_channel_is_resp(lua_State *L) |
| 3381 | { |
| 3382 | struct channel *chn; |
| 3383 | |
| 3384 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 3385 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3386 | |
| 3387 | lua_pushboolean(L, !!(chn->flags & CF_ISRESP)); |
| 3388 | return 1; |
| 3389 | } |
| 3390 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3391 | /* Just returns the number of bytes available in the output |
| 3392 | * side of the buffer. This function never fails. |
| 3393 | */ |
| 3394 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 3395 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3396 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3397 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3398 | MAY_LJMP(check_args(L, 1, "get_out_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3399 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3400 | lua_pushinteger(L, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3401 | return 1; |
| 3402 | } |
| 3403 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3404 | /* |
| 3405 | * |
| 3406 | * |
| 3407 | * Class Fetches |
| 3408 | * |
| 3409 | * |
| 3410 | */ |
| 3411 | |
| 3412 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3413 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3414 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3415 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3416 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3417 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3418 | } |
| 3419 | |
| 3420 | /* This function creates and push in the stack a fetch object according |
| 3421 | * with a current TXN. |
| 3422 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3423 | 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] | 3424 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3425 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3426 | |
| 3427 | /* Check stack size. */ |
| 3428 | if (!lua_checkstack(L, 3)) |
| 3429 | return 0; |
| 3430 | |
| 3431 | /* Create the object: obj[0] = userdata. |
| 3432 | * Note that the base of the Fetches object is the |
| 3433 | * transaction object. |
| 3434 | */ |
| 3435 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3436 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3437 | lua_rawseti(L, -2, 0); |
| 3438 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3439 | hsmp->s = txn->s; |
| 3440 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3441 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3442 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3443 | |
| 3444 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3445 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 3446 | lua_setmetatable(L, -2); |
| 3447 | |
| 3448 | return 1; |
| 3449 | } |
| 3450 | |
| 3451 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 3452 | * It uses closure argument to store the associated sample-fetch. It |
| 3453 | * returns only one argument or throws an error. An error is thrown |
| 3454 | * only if an error is encountered during the argument parsing. If |
| 3455 | * the "sample-fetch" function fails, nil is returned. |
| 3456 | */ |
| 3457 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 3458 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3459 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3460 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3461 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3462 | int i; |
| 3463 | struct sample smp; |
| 3464 | |
| 3465 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3466 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3467 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3468 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3469 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3470 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 3471 | /* Check execution authorization. */ |
| 3472 | if (f->use & SMP_USE_HTTP_ANY && |
| 3473 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 3474 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 3475 | "is not available in Lua services", f->kw); |
| 3476 | WILL_LJMP(lua_error(L)); |
| 3477 | } |
| 3478 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3479 | /* Get extra arguments. */ |
| 3480 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 3481 | if (i >= ARGM_NBARGS) |
| 3482 | break; |
| 3483 | hlua_lua2arg(L, i + 2, &args[i]); |
| 3484 | } |
| 3485 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3486 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3487 | |
| 3488 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3489 | 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] | 3490 | |
| 3491 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3492 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3493 | lua_pushfstring(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3494 | goto error; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3495 | } |
| 3496 | |
| 3497 | /* Initialise the sample. */ |
| 3498 | memset(&smp, 0, sizeof(smp)); |
| 3499 | |
| 3500 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3501 | 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] | 3502 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3503 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3504 | lua_pushstring(L, ""); |
| 3505 | else |
| 3506 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3507 | goto end; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3508 | } |
| 3509 | |
| 3510 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3511 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3512 | hlua_smp2lua_str(L, &smp); |
| 3513 | else |
| 3514 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3515 | |
| 3516 | end: |
| 3517 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3518 | if (args[i].type == ARGT_STR) |
| 3519 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3520 | else if (args[i].type == ARGT_REG) |
| 3521 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3522 | } |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3523 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3524 | |
| 3525 | error: |
| 3526 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3527 | if (args[i].type == ARGT_STR) |
| 3528 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3529 | else if (args[i].type == ARGT_REG) |
| 3530 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3531 | } |
| 3532 | WILL_LJMP(lua_error(L)); |
| 3533 | return 0; /* Never reached */ |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3534 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3535 | |
| 3536 | /* |
| 3537 | * |
| 3538 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3539 | * Class Converters |
| 3540 | * |
| 3541 | * |
| 3542 | */ |
| 3543 | |
| 3544 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3545 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3546 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3547 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3548 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3549 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3550 | } |
| 3551 | |
| 3552 | /* This function creates and push in the stack a Converters object |
| 3553 | * according with a current TXN. |
| 3554 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3555 | 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] | 3556 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3557 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3558 | |
| 3559 | /* Check stack size. */ |
| 3560 | if (!lua_checkstack(L, 3)) |
| 3561 | return 0; |
| 3562 | |
| 3563 | /* Create the object: obj[0] = userdata. |
| 3564 | * Note that the base of the Converters object is the |
| 3565 | * same than the TXN object. |
| 3566 | */ |
| 3567 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3568 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3569 | lua_rawseti(L, -2, 0); |
| 3570 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3571 | hsmp->s = txn->s; |
| 3572 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3573 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3574 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3575 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3576 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3577 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 3578 | lua_setmetatable(L, -2); |
| 3579 | |
| 3580 | return 1; |
| 3581 | } |
| 3582 | |
| 3583 | /* This function is an LUA binding. It is called with each converter. |
| 3584 | * It uses closure argument to store the associated converter. It |
| 3585 | * returns only one argument or throws an error. An error is thrown |
| 3586 | * only if an error is encountered during the argument parsing. If |
| 3587 | * the converter function function fails, nil is returned. |
| 3588 | */ |
| 3589 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 3590 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3591 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3592 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3593 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3594 | int i; |
| 3595 | struct sample smp; |
| 3596 | |
| 3597 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3598 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3599 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3600 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3601 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3602 | |
| 3603 | /* Get extra arguments. */ |
| 3604 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 3605 | if (i >= ARGM_NBARGS) |
| 3606 | break; |
| 3607 | hlua_lua2arg(L, i + 3, &args[i]); |
| 3608 | } |
| 3609 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3610 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3611 | |
| 3612 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3613 | 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] | 3614 | |
| 3615 | /* Run the special args checker. */ |
| 3616 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 3617 | hlua_pusherror(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3618 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3619 | } |
| 3620 | |
| 3621 | /* Initialise the sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 3622 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3623 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 3624 | hlua_pusherror(L, "error in the input argument"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3625 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3626 | } |
| 3627 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3628 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 3629 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3630 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3631 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3632 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3633 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3634 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3635 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3636 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 3637 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3638 | hlua_pusherror(L, "error during the input argument casting"); |
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 | } |
| 3641 | |
| 3642 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3643 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3644 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3645 | lua_pushstring(L, ""); |
| 3646 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3647 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3648 | goto end; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3649 | } |
| 3650 | |
| 3651 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3652 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3653 | hlua_smp2lua_str(L, &smp); |
| 3654 | else |
| 3655 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3656 | end: |
| 3657 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3658 | if (args[i].type == ARGT_STR) |
| 3659 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3660 | else if (args[i].type == ARGT_REG) |
| 3661 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3662 | } |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3663 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3664 | |
| 3665 | error: |
| 3666 | for (i = 0; args[i].type != ARGT_STOP; i++) { |
| 3667 | if (args[i].type == ARGT_STR) |
| 3668 | chunk_destroy(&args[i].data.str); |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 3669 | else if (args[i].type == ARGT_REG) |
| 3670 | regex_free(args[i].data.reg); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 3671 | } |
| 3672 | WILL_LJMP(lua_error(L)); |
| 3673 | return 0; /* Never reached */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3674 | } |
| 3675 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3676 | /* |
| 3677 | * |
| 3678 | * |
| 3679 | * Class AppletTCP |
| 3680 | * |
| 3681 | * |
| 3682 | */ |
| 3683 | |
| 3684 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 3685 | * a class stream, otherwise it throws an error. |
| 3686 | */ |
| 3687 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 3688 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3689 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3690 | } |
| 3691 | |
| 3692 | /* This function creates and push in the stack an Applet object |
| 3693 | * according with a current TXN. |
| 3694 | */ |
| 3695 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 3696 | { |
| 3697 | struct hlua_appctx *appctx; |
| 3698 | struct stream_interface *si = ctx->owner; |
| 3699 | struct stream *s = si_strm(si); |
| 3700 | struct proxy *p = s->be; |
| 3701 | |
| 3702 | /* Check stack size. */ |
| 3703 | if (!lua_checkstack(L, 3)) |
| 3704 | return 0; |
| 3705 | |
| 3706 | /* Create the object: obj[0] = userdata. |
| 3707 | * Note that the base of the Converters object is the |
| 3708 | * same than the TXN object. |
| 3709 | */ |
| 3710 | lua_newtable(L); |
| 3711 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
| 3712 | lua_rawseti(L, -2, 0); |
| 3713 | appctx->appctx = ctx; |
| 3714 | appctx->htxn.s = s; |
| 3715 | appctx->htxn.p = p; |
| 3716 | |
| 3717 | /* Create the "f" field that contains a list of fetches. */ |
| 3718 | lua_pushstring(L, "f"); |
| 3719 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3720 | return 0; |
| 3721 | lua_settable(L, -3); |
| 3722 | |
| 3723 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3724 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3725 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3726 | return 0; |
| 3727 | lua_settable(L, -3); |
| 3728 | |
| 3729 | /* Create the "c" field that contains a list of converters. */ |
| 3730 | lua_pushstring(L, "c"); |
| 3731 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3732 | return 0; |
| 3733 | lua_settable(L, -3); |
| 3734 | |
| 3735 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3736 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3737 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3738 | return 0; |
| 3739 | lua_settable(L, -3); |
| 3740 | |
| 3741 | /* Pop a class stream metatable and affect it to the table. */ |
| 3742 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 3743 | lua_setmetatable(L, -2); |
| 3744 | |
| 3745 | return 1; |
| 3746 | } |
| 3747 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3748 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 3749 | { |
| 3750 | struct hlua_appctx *appctx; |
| 3751 | struct stream *s; |
| 3752 | const char *name; |
| 3753 | size_t len; |
| 3754 | struct sample smp; |
| 3755 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3756 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 3757 | 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] | 3758 | |
| 3759 | /* It is useles to retrieve the stream, but this function |
| 3760 | * runs only in a stream context. |
| 3761 | */ |
| 3762 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3763 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3764 | s = appctx->htxn.s; |
| 3765 | |
| 3766 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 3767 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3768 | hlua_lua2smp(L, 3, &smp); |
| 3769 | |
| 3770 | /* Store the sample in a variable. */ |
| 3771 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3772 | |
| 3773 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 3774 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 3775 | else |
| 3776 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 3777 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 3778 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3779 | } |
| 3780 | |
| 3781 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 3782 | { |
| 3783 | struct hlua_appctx *appctx; |
| 3784 | struct stream *s; |
| 3785 | const char *name; |
| 3786 | size_t len; |
| 3787 | struct sample smp; |
| 3788 | |
| 3789 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3790 | |
| 3791 | /* It is useles to retrieve the stream, but this function |
| 3792 | * runs only in a stream context. |
| 3793 | */ |
| 3794 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3795 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3796 | s = appctx->htxn.s; |
| 3797 | |
| 3798 | /* Unset the variable. */ |
| 3799 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 3800 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 3801 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3802 | } |
| 3803 | |
| 3804 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 3805 | { |
| 3806 | struct hlua_appctx *appctx; |
| 3807 | struct stream *s; |
| 3808 | const char *name; |
| 3809 | size_t len; |
| 3810 | struct sample smp; |
| 3811 | |
| 3812 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3813 | |
| 3814 | /* It is useles to retrieve the stream, but this function |
| 3815 | * runs only in a stream context. |
| 3816 | */ |
| 3817 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3818 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3819 | s = appctx->htxn.s; |
| 3820 | |
| 3821 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3822 | if (!vars_get_by_name(name, len, &smp)) { |
| 3823 | lua_pushnil(L); |
| 3824 | return 1; |
| 3825 | } |
| 3826 | |
| 3827 | return hlua_smp2lua(L, &smp); |
| 3828 | } |
| 3829 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3830 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 3831 | { |
| 3832 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3833 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3834 | struct hlua *hlua; |
| 3835 | |
| 3836 | /* 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] | 3837 | if (!s->hlua) |
| 3838 | return 0; |
| 3839 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3840 | |
| 3841 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3842 | |
| 3843 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 3844 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3845 | |
| 3846 | /* Get and store new value. */ |
| 3847 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3848 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 3849 | |
| 3850 | return 0; |
| 3851 | } |
| 3852 | |
| 3853 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 3854 | { |
| 3855 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3856 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3857 | struct hlua *hlua; |
| 3858 | |
| 3859 | /* 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] | 3860 | if (!s->hlua) { |
| 3861 | lua_pushnil(L); |
| 3862 | return 1; |
| 3863 | } |
| 3864 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3865 | |
| 3866 | /* Push configuration index in the stack. */ |
| 3867 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 3868 | |
| 3869 | return 1; |
| 3870 | } |
| 3871 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3872 | /* If expected data not yet available, it returns a yield. This function |
| 3873 | * consumes the data in the buffer. It returns a string containing the |
| 3874 | * data. This string can be empty. |
| 3875 | */ |
| 3876 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 3877 | { |
| 3878 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3879 | struct stream_interface *si = appctx->appctx->owner; |
| 3880 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3881 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3882 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3883 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3884 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3885 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3886 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3887 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3888 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3889 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3890 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3891 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3892 | 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] | 3893 | } |
| 3894 | |
| 3895 | /* End of data: commit the total strings and return. */ |
| 3896 | if (ret < 0) { |
| 3897 | luaL_pushresult(&appctx->b); |
| 3898 | return 1; |
| 3899 | } |
| 3900 | |
| 3901 | /* Ensure that the block 2 length is usable. */ |
| 3902 | if (ret == 1) |
| 3903 | len2 = 0; |
| 3904 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 3905 | /* don't check the max length read and don't check. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3906 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3907 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3908 | |
| 3909 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3910 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3911 | luaL_pushresult(&appctx->b); |
| 3912 | return 1; |
| 3913 | } |
| 3914 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3915 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3916 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 3917 | { |
| 3918 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3919 | |
| 3920 | /* Initialise the string catenation. */ |
| 3921 | luaL_buffinit(L, &appctx->b); |
| 3922 | |
| 3923 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 3924 | } |
| 3925 | |
| 3926 | /* If expected data not yet available, it returns a yield. This function |
| 3927 | * consumes the data in the buffer. It returns a string containing the |
| 3928 | * data. This string can be empty. |
| 3929 | */ |
| 3930 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 3931 | { |
| 3932 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3933 | struct stream_interface *si = appctx->appctx->owner; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3934 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3935 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3936 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3937 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3938 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3939 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3940 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3941 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3942 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3943 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3944 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3945 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3946 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3947 | 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] | 3948 | } |
| 3949 | |
| 3950 | /* End of data: commit the total strings and return. */ |
| 3951 | if (ret < 0) { |
| 3952 | luaL_pushresult(&appctx->b); |
| 3953 | return 1; |
| 3954 | } |
| 3955 | |
| 3956 | /* Ensure that the block 2 length is usable. */ |
| 3957 | if (ret == 1) |
| 3958 | len2 = 0; |
| 3959 | |
| 3960 | if (len == -1) { |
| 3961 | |
| 3962 | /* If len == -1, catenate all the data avalaile and |
| 3963 | * yield because we want to get all the data until |
| 3964 | * the end of data stream. |
| 3965 | */ |
| 3966 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3967 | luaL_addlstring(&appctx->b, blk2, len2); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3968 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3969 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3970 | 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] | 3971 | |
| 3972 | } else { |
| 3973 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3974 | /* Copy the first block caping to the length required. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3975 | if (len1 > len) |
| 3976 | len1 = len; |
| 3977 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3978 | len -= len1; |
| 3979 | |
| 3980 | /* Copy the second block. */ |
| 3981 | if (len2 > len) |
| 3982 | len2 = len; |
| 3983 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3984 | len -= len2; |
| 3985 | |
| 3986 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3987 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3988 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3989 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3990 | if (len > 0) { |
| 3991 | lua_pushinteger(L, len); |
| 3992 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3993 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3994 | 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] | 3995 | } |
| 3996 | |
| 3997 | /* return the result. */ |
| 3998 | luaL_pushresult(&appctx->b); |
| 3999 | return 1; |
| 4000 | } |
| 4001 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4002 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4003 | hlua_pusherror(L, "Lua: internal error"); |
| 4004 | WILL_LJMP(lua_error(L)); |
| 4005 | return 0; |
| 4006 | } |
| 4007 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4008 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4009 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 4010 | { |
| 4011 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4012 | int len = -1; |
| 4013 | |
| 4014 | if (lua_gettop(L) > 2) |
| 4015 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4016 | if (lua_gettop(L) >= 2) { |
| 4017 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4018 | lua_pop(L, 1); |
| 4019 | } |
| 4020 | |
| 4021 | /* Confirm or set the required length */ |
| 4022 | lua_pushinteger(L, len); |
| 4023 | |
| 4024 | /* Initialise the string catenation. */ |
| 4025 | luaL_buffinit(L, &appctx->b); |
| 4026 | |
| 4027 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 4028 | } |
| 4029 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4030 | /* Append data in the output side of the buffer. This data is immediately |
| 4031 | * sent. The function returns the amount of data written. If the buffer |
| 4032 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4033 | * if the channel is closed. |
| 4034 | */ |
| 4035 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4036 | { |
| 4037 | size_t len; |
| 4038 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4039 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4040 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4041 | struct stream_interface *si = appctx->appctx->owner; |
| 4042 | struct channel *chn = si_ic(si); |
| 4043 | int max; |
| 4044 | |
| 4045 | /* Get the max amount of data which can write as input in the channel. */ |
| 4046 | max = channel_recv_max(chn); |
| 4047 | if (max > (len - l)) |
| 4048 | max = len - l; |
| 4049 | |
| 4050 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4051 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4052 | |
| 4053 | /* update counters. */ |
| 4054 | l += max; |
| 4055 | lua_pop(L, 1); |
| 4056 | lua_pushinteger(L, l); |
| 4057 | |
| 4058 | /* If some data is not send, declares the situation to the |
| 4059 | * applet, and returns a yield. |
| 4060 | */ |
| 4061 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 4062 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4063 | 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] | 4064 | } |
| 4065 | |
| 4066 | return 1; |
| 4067 | } |
| 4068 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4069 | /* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4070 | * yield the LUA process, and resume it without checking the |
| 4071 | * input arguments. |
| 4072 | */ |
| 4073 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 4074 | { |
| 4075 | MAY_LJMP(check_args(L, 2, "send")); |
| 4076 | lua_pushinteger(L, 0); |
| 4077 | |
| 4078 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 4079 | } |
| 4080 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4081 | /* |
| 4082 | * |
| 4083 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4084 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4085 | * |
| 4086 | * |
| 4087 | */ |
| 4088 | |
| 4089 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4090 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4091 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4092 | __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] | 4093 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4094 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4095 | } |
| 4096 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4097 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4098 | * according with a current TXN. |
| 4099 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4100 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4101 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4102 | struct hlua_appctx *appctx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 4103 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4104 | struct stream_interface *si = ctx->owner; |
| 4105 | struct stream *s = si_strm(si); |
| 4106 | struct proxy *px = s->be; |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4107 | struct htx *htx; |
| 4108 | struct htx_blk *blk; |
| 4109 | struct htx_sl *sl; |
| 4110 | struct ist path; |
| 4111 | unsigned long long len = 0; |
| 4112 | int32_t pos; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4113 | |
| 4114 | /* Check stack size. */ |
| 4115 | if (!lua_checkstack(L, 3)) |
| 4116 | return 0; |
| 4117 | |
| 4118 | /* Create the object: obj[0] = userdata. |
| 4119 | * Note that the base of the Converters object is the |
| 4120 | * same than the TXN object. |
| 4121 | */ |
| 4122 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4123 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4124 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4125 | appctx->appctx = ctx; |
| 4126 | appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4127 | 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] | 4128 | appctx->htxn.s = s; |
| 4129 | appctx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4130 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4131 | /* Create the "f" field that contains a list of fetches. */ |
| 4132 | lua_pushstring(L, "f"); |
| 4133 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 4134 | return 0; |
| 4135 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4136 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4137 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4138 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4139 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4140 | return 0; |
| 4141 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4142 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4143 | /* Create the "c" field that contains a list of converters. */ |
| 4144 | lua_pushstring(L, "c"); |
| 4145 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 4146 | return 0; |
| 4147 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4148 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4149 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4150 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4151 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4152 | return 0; |
| 4153 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4154 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4155 | htx = htxbuf(&s->req.buf); |
| 4156 | blk = htx_get_first_blk(htx); |
Christopher Faulet | ea00973 | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 4157 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4158 | sl = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4159 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4160 | /* Stores the request method. */ |
| 4161 | lua_pushstring(L, "method"); |
| 4162 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 4163 | lua_settable(L, -3); |
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 http version. */ |
| 4166 | lua_pushstring(L, "version"); |
| 4167 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(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 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 4171 | * the array on the top of the stack. |
| 4172 | */ |
| 4173 | lua_pushstring(L, "headers"); |
| 4174 | htxn.s = s; |
| 4175 | htxn.p = px; |
| 4176 | htxn.dir = SMP_OPT_DIR_REQ; |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4177 | if (!hlua_http_get_headers(L, &htxn.s->txn->req)) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4178 | return 0; |
| 4179 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4180 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4181 | path = http_get_path(htx_sl_req_uri(sl)); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 4182 | if (isttest(path)) { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4183 | char *p, *q, *end; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4184 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4185 | p = path.ptr; |
| 4186 | end = path.ptr + path.len; |
| 4187 | q = p; |
| 4188 | while (q < end && *q != '?') |
| 4189 | q++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4190 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4191 | /* Stores the request path. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4192 | lua_pushstring(L, "path"); |
| 4193 | lua_pushlstring(L, p, q - p); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4194 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4195 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4196 | /* Stores the query string. */ |
| 4197 | lua_pushstring(L, "qs"); |
| 4198 | if (*q == '?') |
| 4199 | q++; |
| 4200 | lua_pushlstring(L, q, end - q); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4201 | lua_settable(L, -3); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4202 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4203 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4204 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4205 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4206 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4207 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4208 | if (type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4209 | break; |
| 4210 | if (type == HTX_BLK_DATA) |
| 4211 | len += htx_get_blksz(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4212 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4213 | if (htx->extra != ULLONG_MAX) |
| 4214 | len += htx->extra; |
| 4215 | |
| 4216 | /* Stores the request path. */ |
| 4217 | lua_pushstring(L, "length"); |
| 4218 | lua_pushinteger(L, len); |
| 4219 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4220 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4221 | /* Create an empty array of HTTP request headers. */ |
| 4222 | lua_pushstring(L, "response"); |
| 4223 | lua_newtable(L); |
| 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 | /* Pop a class stream metatable and affect it to the table. */ |
| 4227 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 4228 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4229 | |
| 4230 | return 1; |
| 4231 | } |
| 4232 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4233 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 4234 | { |
| 4235 | struct hlua_appctx *appctx; |
| 4236 | struct stream *s; |
| 4237 | const char *name; |
| 4238 | size_t len; |
| 4239 | struct sample smp; |
| 4240 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4241 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 4242 | 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] | 4243 | |
| 4244 | /* It is useles to retrieve the stream, but this function |
| 4245 | * runs only in a stream context. |
| 4246 | */ |
| 4247 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4248 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4249 | s = appctx->htxn.s; |
| 4250 | |
| 4251 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4252 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4253 | hlua_lua2smp(L, 3, &smp); |
| 4254 | |
| 4255 | /* Store the sample in a variable. */ |
| 4256 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4257 | |
| 4258 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 4259 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 4260 | else |
| 4261 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 4262 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4263 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4264 | } |
| 4265 | |
| 4266 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 4267 | { |
| 4268 | struct hlua_appctx *appctx; |
| 4269 | struct stream *s; |
| 4270 | const char *name; |
| 4271 | size_t len; |
| 4272 | struct sample smp; |
| 4273 | |
| 4274 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4275 | |
| 4276 | /* It is useles to retrieve the stream, but this function |
| 4277 | * runs only in a stream context. |
| 4278 | */ |
| 4279 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4280 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4281 | s = appctx->htxn.s; |
| 4282 | |
| 4283 | /* Unset the variable. */ |
| 4284 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4285 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4286 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4287 | } |
| 4288 | |
| 4289 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4290 | { |
| 4291 | struct hlua_appctx *appctx; |
| 4292 | struct stream *s; |
| 4293 | const char *name; |
| 4294 | size_t len; |
| 4295 | struct sample smp; |
| 4296 | |
| 4297 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4298 | |
| 4299 | /* It is useles to retrieve the stream, but this function |
| 4300 | * runs only in a stream context. |
| 4301 | */ |
| 4302 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4303 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4304 | s = appctx->htxn.s; |
| 4305 | |
| 4306 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4307 | if (!vars_get_by_name(name, len, &smp)) { |
| 4308 | lua_pushnil(L); |
| 4309 | return 1; |
| 4310 | } |
| 4311 | |
| 4312 | return hlua_smp2lua(L, &smp); |
| 4313 | } |
| 4314 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4315 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 4316 | { |
| 4317 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4318 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4319 | struct hlua *hlua; |
| 4320 | |
| 4321 | /* 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] | 4322 | if (!s->hlua) |
| 4323 | return 0; |
| 4324 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4325 | |
| 4326 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4327 | |
| 4328 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4329 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4330 | |
| 4331 | /* Get and store new value. */ |
| 4332 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4333 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4334 | |
| 4335 | return 0; |
| 4336 | } |
| 4337 | |
| 4338 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4339 | { |
| 4340 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4341 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4342 | struct hlua *hlua; |
| 4343 | |
| 4344 | /* 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] | 4345 | if (!s->hlua) { |
| 4346 | lua_pushnil(L); |
| 4347 | return 1; |
| 4348 | } |
| 4349 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4350 | |
| 4351 | /* Push configuration index in the stack. */ |
| 4352 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4353 | |
| 4354 | return 1; |
| 4355 | } |
| 4356 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4357 | /* If expected data not yet available, it returns a yield. This function |
| 4358 | * consumes the data in the buffer. It returns a string containing the |
| 4359 | * data. This string can be empty. |
| 4360 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4361 | __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] | 4362 | { |
| 4363 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4364 | struct stream_interface *si = appctx->appctx->owner; |
| 4365 | struct channel *req = si_oc(si); |
| 4366 | struct htx *htx; |
| 4367 | struct htx_blk *blk; |
| 4368 | size_t count; |
| 4369 | int stop = 0; |
| 4370 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4371 | htx = htx_from_buf(&req->buf); |
| 4372 | count = co_data(req); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4373 | blk = htx_get_first_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 4374 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4375 | while (count && !stop && blk) { |
| 4376 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4377 | uint32_t sz = htx_get_blksz(blk); |
| 4378 | struct ist v; |
| 4379 | uint32_t vlen; |
| 4380 | char *nl; |
| 4381 | |
| 4382 | vlen = sz; |
| 4383 | if (vlen > count) { |
| 4384 | if (type != HTX_BLK_DATA) |
| 4385 | break; |
| 4386 | vlen = count; |
| 4387 | } |
| 4388 | |
| 4389 | switch (type) { |
| 4390 | case HTX_BLK_UNUSED: |
| 4391 | break; |
| 4392 | |
| 4393 | case HTX_BLK_DATA: |
| 4394 | v = htx_get_blk_value(htx, blk); |
| 4395 | v.len = vlen; |
| 4396 | nl = istchr(v, '\n'); |
| 4397 | if (nl != NULL) { |
| 4398 | stop = 1; |
| 4399 | vlen = nl - v.ptr + 1; |
| 4400 | } |
| 4401 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4402 | break; |
| 4403 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4404 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4405 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4406 | stop = 1; |
| 4407 | break; |
| 4408 | |
| 4409 | default: |
| 4410 | break; |
| 4411 | } |
| 4412 | |
| 4413 | co_set_data(req, co_data(req) - vlen); |
| 4414 | count -= vlen; |
| 4415 | if (sz == vlen) |
| 4416 | blk = htx_remove_blk(htx, blk); |
| 4417 | else { |
| 4418 | htx_cut_data_blk(htx, blk, vlen); |
| 4419 | break; |
| 4420 | } |
| 4421 | } |
| 4422 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 4423 | /* The message was fully consumed and no more data are expected |
| 4424 | * (EOM flag set). |
| 4425 | */ |
| 4426 | if (htx_is_empty(htx) && (htx->flags & HTX_FL_EOM)) |
| 4427 | stop = 1; |
| 4428 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4429 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4430 | if (!stop) { |
| 4431 | si_cant_get(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4432 | 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] | 4433 | } |
| 4434 | |
| 4435 | /* return the result. */ |
| 4436 | luaL_pushresult(&appctx->b); |
| 4437 | return 1; |
| 4438 | } |
| 4439 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4440 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4441 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4442 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4443 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4444 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4445 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4446 | /* Initialise the string catenation. */ |
| 4447 | luaL_buffinit(L, &appctx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4448 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4449 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4450 | } |
| 4451 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4452 | /* If expected data not yet available, it returns a yield. This function |
| 4453 | * consumes the data in the buffer. It returns a string containing the |
| 4454 | * data. This string can be empty. |
| 4455 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4456 | __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] | 4457 | { |
| 4458 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4459 | struct stream_interface *si = appctx->appctx->owner; |
| 4460 | struct channel *req = si_oc(si); |
| 4461 | struct htx *htx; |
| 4462 | struct htx_blk *blk; |
| 4463 | size_t count; |
| 4464 | int len; |
| 4465 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4466 | htx = htx_from_buf(&req->buf); |
| 4467 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4468 | count = co_data(req); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 4469 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4470 | while (count && len && blk) { |
| 4471 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4472 | uint32_t sz = htx_get_blksz(blk); |
| 4473 | struct ist v; |
| 4474 | uint32_t vlen; |
| 4475 | |
| 4476 | vlen = sz; |
| 4477 | if (len > 0 && vlen > len) |
| 4478 | vlen = len; |
| 4479 | if (vlen > count) { |
| 4480 | if (type != HTX_BLK_DATA) |
| 4481 | break; |
| 4482 | vlen = count; |
| 4483 | } |
| 4484 | |
| 4485 | switch (type) { |
| 4486 | case HTX_BLK_UNUSED: |
| 4487 | break; |
| 4488 | |
| 4489 | case HTX_BLK_DATA: |
| 4490 | v = htx_get_blk_value(htx, blk); |
| 4491 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4492 | break; |
| 4493 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4494 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4495 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4496 | len = 0; |
| 4497 | break; |
| 4498 | |
| 4499 | default: |
| 4500 | break; |
| 4501 | } |
| 4502 | |
| 4503 | co_set_data(req, co_data(req) - vlen); |
| 4504 | count -= vlen; |
| 4505 | if (len > 0) |
| 4506 | len -= vlen; |
| 4507 | if (sz == vlen) |
| 4508 | blk = htx_remove_blk(htx, blk); |
| 4509 | else { |
| 4510 | htx_cut_data_blk(htx, blk, vlen); |
| 4511 | break; |
| 4512 | } |
| 4513 | } |
| 4514 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 4515 | /* The message was fully consumed and no more data are expected |
| 4516 | * (EOM flag set). |
| 4517 | */ |
| 4518 | if (htx_is_empty(htx) && (htx->flags & HTX_FL_EOM)) |
| 4519 | len = 0; |
| 4520 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4521 | htx_to_buf(htx, &req->buf); |
| 4522 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4523 | /* If we are no other data available, yield waiting for new data. */ |
| 4524 | if (len) { |
| 4525 | if (len > 0) { |
| 4526 | lua_pushinteger(L, len); |
| 4527 | lua_replace(L, 2); |
| 4528 | } |
| 4529 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4530 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4531 | } |
| 4532 | |
| 4533 | /* return the result. */ |
| 4534 | luaL_pushresult(&appctx->b); |
| 4535 | return 1; |
| 4536 | } |
| 4537 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4538 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4539 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 4540 | { |
| 4541 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4542 | int len = -1; |
| 4543 | |
| 4544 | /* Check arguments. */ |
| 4545 | if (lua_gettop(L) > 2) |
| 4546 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4547 | if (lua_gettop(L) >= 2) { |
| 4548 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4549 | lua_pop(L, 1); |
| 4550 | } |
| 4551 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4552 | lua_pushinteger(L, len); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4553 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4554 | /* Initialise the string catenation. */ |
| 4555 | luaL_buffinit(L, &appctx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4556 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4557 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4558 | } |
| 4559 | |
| 4560 | /* Append data in the output side of the buffer. This data is immediately |
| 4561 | * sent. The function returns the amount of data written. If the buffer |
| 4562 | * cannot contain the data, the function yields. The function returns -1 |
| 4563 | * if the channel is closed. |
| 4564 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4565 | __LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4566 | { |
| 4567 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4568 | struct stream_interface *si = appctx->appctx->owner; |
| 4569 | struct channel *res = si_ic(si); |
| 4570 | struct htx *htx = htx_from_buf(&res->buf); |
| 4571 | const char *data; |
| 4572 | size_t len; |
| 4573 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4574 | int max; |
| 4575 | |
Christopher Faulet | 9060fc0 | 2019-07-03 11:39:30 +0200 | [diff] [blame] | 4576 | max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx)); |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4577 | if (!max) |
| 4578 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4579 | |
| 4580 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4581 | |
| 4582 | /* Get the max amount of data which can write as input in the channel. */ |
| 4583 | if (max > (len - l)) |
| 4584 | max = len - l; |
| 4585 | |
| 4586 | /* Copy data. */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 4587 | max = htx_add_data(htx, ist2(data + l, max)); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4588 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4589 | |
| 4590 | /* update counters. */ |
| 4591 | l += max; |
| 4592 | lua_pop(L, 1); |
| 4593 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4594 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4595 | /* If some data is not send, declares the situation to the |
| 4596 | * applet, and returns a yield. |
| 4597 | */ |
| 4598 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4599 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4600 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4601 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4602 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4603 | } |
| 4604 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4605 | htx_to_buf(htx, &res->buf); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4606 | return 1; |
| 4607 | } |
| 4608 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4609 | /* Just a wrapper of "hlua_applet_send_yield". This wrapper permits |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4610 | * yield the LUA process, and resume it without checking the |
| 4611 | * input arguments. |
| 4612 | */ |
| 4613 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 4614 | { |
| 4615 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4616 | |
| 4617 | /* We want to send some data. Headers must be sent. */ |
| 4618 | if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 4619 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 4620 | WILL_LJMP(lua_error(L)); |
| 4621 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4622 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4623 | /* This integer is used for followinf the amount of data sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4624 | lua_pushinteger(L, 0); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4625 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4626 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4627 | } |
| 4628 | |
| 4629 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 4630 | { |
| 4631 | const char *name; |
| 4632 | int ret; |
| 4633 | |
| 4634 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4635 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4636 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 4637 | |
| 4638 | /* Push in the stack the "response" entry. */ |
| 4639 | ret = lua_getfield(L, 1, "response"); |
| 4640 | if (ret != LUA_TTABLE) { |
| 4641 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 4642 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 4643 | WILL_LJMP(lua_error(L)); |
| 4644 | } |
| 4645 | |
| 4646 | /* check if the header is already registered if it is not |
| 4647 | * the case, register it. |
| 4648 | */ |
| 4649 | ret = lua_getfield(L, -1, name); |
| 4650 | if (ret == LUA_TNIL) { |
| 4651 | |
| 4652 | /* Entry not found. */ |
| 4653 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 4654 | |
| 4655 | /* Insert the new header name in the array in the top of the stack. |
| 4656 | * It left the new array in the top of the stack. |
| 4657 | */ |
| 4658 | lua_newtable(L); |
| 4659 | lua_pushvalue(L, 2); |
| 4660 | lua_pushvalue(L, -2); |
| 4661 | lua_settable(L, -4); |
| 4662 | |
| 4663 | } else if (ret != LUA_TTABLE) { |
| 4664 | |
| 4665 | /* corruption error. */ |
| 4666 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 4667 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 4668 | WILL_LJMP(lua_error(L)); |
| 4669 | } |
| 4670 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 4671 | /* Now the top of thestack is an array of values. We push |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4672 | * the header value as new entry. |
| 4673 | */ |
| 4674 | lua_pushvalue(L, 3); |
| 4675 | ret = lua_rawlen(L, -2); |
| 4676 | lua_rawseti(L, -2, ret + 1); |
| 4677 | lua_pushboolean(L, 1); |
| 4678 | return 1; |
| 4679 | } |
| 4680 | |
| 4681 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 4682 | { |
| 4683 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4684 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4685 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4686 | |
| 4687 | if (status < 100 || status > 599) { |
| 4688 | lua_pushboolean(L, 0); |
| 4689 | return 1; |
| 4690 | } |
| 4691 | |
| 4692 | appctx->appctx->ctx.hlua_apphttp.status = status; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4693 | appctx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4694 | lua_pushboolean(L, 1); |
| 4695 | return 1; |
| 4696 | } |
| 4697 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4698 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4699 | __LJMP static int hlua_applet_http_send_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4700 | { |
| 4701 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4702 | struct stream_interface *si = appctx->appctx->owner; |
| 4703 | struct channel *res = si_ic(si); |
| 4704 | struct htx *htx; |
| 4705 | struct htx_sl *sl; |
| 4706 | struct h1m h1m; |
| 4707 | const char *status, *reason; |
| 4708 | const char *name, *value; |
| 4709 | size_t nlen, vlen; |
| 4710 | unsigned int flags; |
| 4711 | |
| 4712 | /* Send the message at once. */ |
| 4713 | htx = htx_from_buf(&res->buf); |
| 4714 | h1m_init_res(&h1m); |
| 4715 | |
| 4716 | /* Use the same http version than the request. */ |
| 4717 | status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size); |
| 4718 | reason = appctx->appctx->ctx.hlua_apphttp.reason; |
| 4719 | if (reason == NULL) |
| 4720 | reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status); |
| 4721 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) { |
| 4722 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 4723 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 4724 | } |
| 4725 | else { |
| 4726 | flags = HTX_SL_F_IS_RESP; |
| 4727 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 4728 | } |
| 4729 | if (!sl) { |
| 4730 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 4731 | appctx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4732 | WILL_LJMP(lua_error(L)); |
| 4733 | } |
| 4734 | sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status; |
| 4735 | |
| 4736 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 4737 | lua_pushvalue(L, 0); |
| 4738 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 4739 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 4740 | appctx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4741 | WILL_LJMP(lua_error(L)); |
| 4742 | } |
| 4743 | |
| 4744 | /* Browse the list of headers. */ |
| 4745 | lua_pushnil(L); |
| 4746 | while(lua_next(L, -2) != 0) { |
| 4747 | /* We expect a string as -2. */ |
| 4748 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 4749 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 4750 | appctx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4751 | lua_typename(L, lua_type(L, -2))); |
| 4752 | WILL_LJMP(lua_error(L)); |
| 4753 | } |
| 4754 | name = lua_tolstring(L, -2, &nlen); |
| 4755 | |
| 4756 | /* We expect an array as -1. */ |
| 4757 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 4758 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 4759 | appctx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4760 | name, |
| 4761 | lua_typename(L, lua_type(L, -1))); |
| 4762 | WILL_LJMP(lua_error(L)); |
| 4763 | } |
| 4764 | |
| 4765 | /* Browse the table who is on the top of the stack. */ |
| 4766 | lua_pushnil(L); |
| 4767 | while(lua_next(L, -2) != 0) { |
| 4768 | int id; |
| 4769 | |
| 4770 | /* We expect a number as -2. */ |
| 4771 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 4772 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 4773 | appctx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4774 | name, |
| 4775 | lua_typename(L, lua_type(L, -2))); |
| 4776 | WILL_LJMP(lua_error(L)); |
| 4777 | } |
| 4778 | id = lua_tointeger(L, -2); |
| 4779 | |
| 4780 | /* We expect a string as -2. */ |
| 4781 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 4782 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 4783 | appctx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4784 | name, id, |
| 4785 | lua_typename(L, lua_type(L, -1))); |
| 4786 | WILL_LJMP(lua_error(L)); |
| 4787 | } |
| 4788 | value = lua_tolstring(L, -1, &vlen); |
| 4789 | |
| 4790 | /* Simple Protocol checks. */ |
| 4791 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 4792 | h1_parse_xfer_enc_header(&h1m, ist2(value, vlen)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4793 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 4794 | struct ist v = ist2(value, vlen); |
| 4795 | int ret; |
| 4796 | |
| 4797 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 4798 | if (ret < 0) { |
| 4799 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 4800 | appctx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4801 | name); |
| 4802 | WILL_LJMP(lua_error(L)); |
| 4803 | } |
| 4804 | else if (ret == 0) |
| 4805 | goto next; /* Skip it */ |
| 4806 | } |
| 4807 | |
| 4808 | /* Add a new header */ |
| 4809 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 4810 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 4811 | appctx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4812 | name); |
| 4813 | WILL_LJMP(lua_error(L)); |
| 4814 | } |
| 4815 | next: |
| 4816 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4817 | lua_pop(L, 1); |
| 4818 | } |
| 4819 | |
| 4820 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4821 | lua_pop(L, 1); |
| 4822 | } |
| 4823 | |
| 4824 | if (h1m.flags & H1_MF_CHNK) |
| 4825 | h1m.flags &= ~H1_MF_CLEN; |
| 4826 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 4827 | h1m.flags |= H1_MF_XFER_LEN; |
| 4828 | |
| 4829 | /* Uset HTX start-line flags */ |
| 4830 | if (h1m.flags & H1_MF_XFER_ENC) |
| 4831 | flags |= HTX_SL_F_XFER_ENC; |
| 4832 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 4833 | flags |= HTX_SL_F_XFER_LEN; |
| 4834 | if (h1m.flags & H1_MF_CHNK) |
| 4835 | flags |= HTX_SL_F_CHNK; |
| 4836 | else if (h1m.flags & H1_MF_CLEN) |
| 4837 | flags |= HTX_SL_F_CLEN; |
| 4838 | if (h1m.body_len == 0) |
| 4839 | flags |= HTX_SL_F_BODYLESS; |
| 4840 | } |
| 4841 | sl->flags |= flags; |
| 4842 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 4843 | /* If we don't have a content-length set, and the HTTP version is 1.1 |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4844 | * and the status code implies the presence of a message body, we must |
| 4845 | * announce a transfer encoding chunked. This is required by haproxy |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4846 | * for the keepalive compliance. If the applet announces a transfer-encoding |
| 4847 | * chunked itself, don't do anything. |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4848 | */ |
| 4849 | if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 && |
| 4850 | appctx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 4851 | appctx->appctx->ctx.hlua_apphttp.status != 204 && |
| 4852 | appctx->appctx->ctx.hlua_apphttp.status != 304) { |
| 4853 | /* Add a new header */ |
| 4854 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 4855 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 4856 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 4857 | appctx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4858 | WILL_LJMP(lua_error(L)); |
| 4859 | } |
| 4860 | } |
| 4861 | |
| 4862 | /* Finalize headers. */ |
| 4863 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 4864 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 4865 | appctx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4866 | WILL_LJMP(lua_error(L)); |
| 4867 | } |
| 4868 | |
| 4869 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 4870 | b_reset(&res->buf); |
| 4871 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 4872 | WILL_LJMP(lua_error(L)); |
| 4873 | } |
| 4874 | |
| 4875 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4876 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4877 | |
| 4878 | /* Headers sent, set the flag. */ |
| 4879 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
| 4880 | return 0; |
| 4881 | |
| 4882 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4883 | /* We will build the status line and the headers of the HTTP response. |
| 4884 | * We will try send at once if its not possible, we give back the hand |
| 4885 | * waiting for more room. |
| 4886 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4887 | __LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4888 | { |
| 4889 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4890 | struct stream_interface *si = appctx->appctx->owner; |
| 4891 | struct channel *res = si_ic(si); |
| 4892 | |
| 4893 | if (co_data(res)) { |
| 4894 | si_rx_room_blk(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4895 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4896 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4897 | return MAY_LJMP(hlua_applet_http_send_response(L)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4898 | } |
| 4899 | |
| 4900 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4901 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4902 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4903 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4904 | } |
| 4905 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4906 | /* |
| 4907 | * |
| 4908 | * |
| 4909 | * Class HTTP |
| 4910 | * |
| 4911 | * |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4912 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4913 | |
| 4914 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4915 | * a class stream, otherwise it throws an error. |
| 4916 | */ |
| 4917 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4918 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4919 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
| 4920 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4921 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4922 | /* This function creates and push in the stack a HTTP object |
| 4923 | * according with a current TXN. |
| 4924 | */ |
| 4925 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 4926 | { |
| 4927 | struct hlua_txn *htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4928 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4929 | /* Check stack size. */ |
| 4930 | if (!lua_checkstack(L, 3)) |
| 4931 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4932 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4933 | /* Create the object: obj[0] = userdata. |
| 4934 | * Note that the base of the Converters object is the |
| 4935 | * same than the TXN object. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4936 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4937 | lua_newtable(L); |
| 4938 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 4939 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4940 | |
| 4941 | htxn->s = txn->s; |
| 4942 | htxn->p = txn->p; |
Christopher Faulet | 256b69a | 2019-05-23 11:14:21 +0200 | [diff] [blame] | 4943 | htxn->dir = txn->dir; |
| 4944 | htxn->flags = txn->flags; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4945 | |
| 4946 | /* Pop a class stream metatable and affect it to the table. */ |
| 4947 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 4948 | lua_setmetatable(L, -2); |
| 4949 | |
| 4950 | return 1; |
| 4951 | } |
| 4952 | |
| 4953 | /* This function creates ans returns an array of HTTP headers. |
| 4954 | * This function does not fails. It is used as wrapper with the |
| 4955 | * 2 following functions. |
| 4956 | */ |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4957 | __LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4958 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4959 | struct htx *htx; |
| 4960 | int32_t pos; |
| 4961 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4962 | /* Create the table. */ |
| 4963 | lua_newtable(L); |
| 4964 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4965 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4966 | htx = htxbuf(&msg->chn->buf); |
| 4967 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4968 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4969 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4970 | struct ist n, v; |
| 4971 | int len; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4972 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4973 | if (type == HTX_BLK_HDR) { |
| 4974 | n = htx_get_blk_name(htx,blk); |
| 4975 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4976 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4977 | else if (type == HTX_BLK_EOH) |
| 4978 | break; |
| 4979 | else |
| 4980 | continue; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4981 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4982 | /* Check for existing entry: |
| 4983 | * assume that the table is on the top of the stack, and |
| 4984 | * push the key in the stack, the function lua_gettable() |
| 4985 | * perform the lookup. |
| 4986 | */ |
| 4987 | lua_pushlstring(L, n.ptr, n.len); |
| 4988 | lua_gettable(L, -2); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4989 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4990 | switch (lua_type(L, -1)) { |
| 4991 | case LUA_TNIL: |
| 4992 | /* Table not found, create it. */ |
| 4993 | lua_pop(L, 1); /* remove the nil value. */ |
| 4994 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 4995 | lua_newtable(L); /* create and push empty table. */ |
| 4996 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 4997 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 4998 | lua_rawset(L, -3); /* index new table with header name (pop the values). */ |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4999 | break; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5000 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5001 | case LUA_TTABLE: |
| 5002 | /* Entry found: push the value in the table. */ |
| 5003 | len = lua_rawlen(L, -1); |
| 5004 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5005 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 5006 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 5007 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5008 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5009 | default: |
| 5010 | /* Other cases are errors. */ |
| 5011 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 5012 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5013 | } |
| 5014 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5015 | return 1; |
| 5016 | } |
| 5017 | |
| 5018 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 5019 | { |
| 5020 | struct hlua_txn *htxn; |
| 5021 | |
| 5022 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 5023 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5024 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5025 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5026 | WILL_LJMP(lua_error(L)); |
| 5027 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5028 | return hlua_http_get_headers(L, &htxn->s->txn->req); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5029 | } |
| 5030 | |
| 5031 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 5032 | { |
| 5033 | struct hlua_txn *htxn; |
| 5034 | |
| 5035 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 5036 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5037 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5038 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5039 | WILL_LJMP(lua_error(L)); |
| 5040 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5041 | return hlua_http_get_headers(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5042 | } |
| 5043 | |
| 5044 | /* This function replace full header, or just a value in |
| 5045 | * the request or in the response. It is a wrapper fir the |
| 5046 | * 4 following functions. |
| 5047 | */ |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5048 | __LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct http_msg *msg, int full) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5049 | { |
| 5050 | size_t name_len; |
| 5051 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5052 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5053 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5054 | struct htx *htx; |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5055 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5056 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5057 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5058 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 5059 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5060 | htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5061 | http_replace_hdrs(chn_strm(msg->chn), htx, ist2(name, name_len), value, re, full); |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5062 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5063 | return 0; |
| 5064 | } |
| 5065 | |
| 5066 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 5067 | { |
| 5068 | struct hlua_txn *htxn; |
| 5069 | |
| 5070 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5071 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5072 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5073 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5074 | WILL_LJMP(lua_error(L)); |
| 5075 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5076 | return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5077 | } |
| 5078 | |
| 5079 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 5080 | { |
| 5081 | struct hlua_txn *htxn; |
| 5082 | |
| 5083 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 5084 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5085 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5086 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5087 | WILL_LJMP(lua_error(L)); |
| 5088 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5089 | return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5090 | } |
| 5091 | |
| 5092 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 5093 | { |
| 5094 | struct hlua_txn *htxn; |
| 5095 | |
| 5096 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5097 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5098 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5099 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5100 | WILL_LJMP(lua_error(L)); |
| 5101 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5102 | return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5103 | } |
| 5104 | |
| 5105 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 5106 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5107 | struct hlua_txn *htxn; |
| 5108 | |
| 5109 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 5110 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5111 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5112 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5113 | WILL_LJMP(lua_error(L)); |
| 5114 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5115 | return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5116 | } |
| 5117 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5118 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5119 | * It is a wrapper for the 2 following functions. |
| 5120 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5121 | __LJMP static inline int hlua_http_del_hdr(lua_State *L, struct http_msg *msg) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5122 | { |
| 5123 | size_t len; |
| 5124 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5125 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5126 | struct http_hdr_ctx ctx; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5127 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5128 | ctx.blk = NULL; |
| 5129 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 5130 | http_remove_header(htx, &ctx); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5131 | return 0; |
| 5132 | } |
| 5133 | |
| 5134 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 5135 | { |
| 5136 | struct hlua_txn *htxn; |
| 5137 | |
| 5138 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 5139 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5140 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5141 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5142 | WILL_LJMP(lua_error(L)); |
| 5143 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5144 | return hlua_http_del_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5145 | } |
| 5146 | |
| 5147 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 5148 | { |
| 5149 | struct hlua_txn *htxn; |
| 5150 | |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5151 | MAY_LJMP(check_args(L, 2, "res_del_hdr")); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5152 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5153 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5154 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5155 | WILL_LJMP(lua_error(L)); |
| 5156 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5157 | return hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5158 | } |
| 5159 | |
| 5160 | /* This function adds an header. It is a wrapper used by |
| 5161 | * the 2 following functions. |
| 5162 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5163 | __LJMP static inline int hlua_http_add_hdr(lua_State *L, struct http_msg *msg) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5164 | { |
| 5165 | size_t name_len; |
| 5166 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5167 | size_t value_len; |
| 5168 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5169 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5170 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5171 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 5172 | ist2(value, value_len))); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5173 | return 0; |
| 5174 | } |
| 5175 | |
| 5176 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 5177 | { |
| 5178 | struct hlua_txn *htxn; |
| 5179 | |
| 5180 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 5181 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5182 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5183 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5184 | WILL_LJMP(lua_error(L)); |
| 5185 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5186 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5187 | } |
| 5188 | |
| 5189 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 5190 | { |
| 5191 | struct hlua_txn *htxn; |
| 5192 | |
| 5193 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 5194 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5195 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5196 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5197 | WILL_LJMP(lua_error(L)); |
| 5198 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5199 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5200 | } |
| 5201 | |
| 5202 | static int hlua_http_req_set_hdr(lua_State *L) |
| 5203 | { |
| 5204 | struct hlua_txn *htxn; |
| 5205 | |
| 5206 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 5207 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5208 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5209 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5210 | WILL_LJMP(lua_error(L)); |
| 5211 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5212 | hlua_http_del_hdr(L, &htxn->s->txn->req); |
| 5213 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5214 | } |
| 5215 | |
| 5216 | static int hlua_http_res_set_hdr(lua_State *L) |
| 5217 | { |
| 5218 | struct hlua_txn *htxn; |
| 5219 | |
| 5220 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 5221 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5222 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5223 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5224 | WILL_LJMP(lua_error(L)); |
| 5225 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5226 | hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
| 5227 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5228 | } |
| 5229 | |
| 5230 | /* This function set the method. */ |
| 5231 | static int hlua_http_req_set_meth(lua_State *L) |
| 5232 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5233 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5234 | size_t name_len; |
| 5235 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5236 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5237 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5238 | WILL_LJMP(lua_error(L)); |
| 5239 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5240 | lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5241 | return 1; |
| 5242 | } |
| 5243 | |
| 5244 | /* This function set the method. */ |
| 5245 | static int hlua_http_req_set_path(lua_State *L) |
| 5246 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5247 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5248 | size_t name_len; |
| 5249 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 5250 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5251 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5252 | WILL_LJMP(lua_error(L)); |
| 5253 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5254 | lua_pushboolean(L, http_req_replace_stline(1, name, name_len, htxn->p, htxn->s) != -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5255 | return 1; |
| 5256 | } |
| 5257 | |
| 5258 | /* This function set the query-string. */ |
| 5259 | static int hlua_http_req_set_query(lua_State *L) |
| 5260 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5261 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5262 | size_t name_len; |
| 5263 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5264 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5265 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5266 | WILL_LJMP(lua_error(L)); |
| 5267 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5268 | /* Check length. */ |
| 5269 | if (name_len > trash.size - 1) { |
| 5270 | lua_pushboolean(L, 0); |
| 5271 | return 1; |
| 5272 | } |
| 5273 | |
| 5274 | /* Add the mark question as prefix. */ |
| 5275 | chunk_reset(&trash); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5276 | trash.area[trash.data++] = '?'; |
| 5277 | memcpy(trash.area + trash.data, name, name_len); |
| 5278 | trash.data += name_len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5279 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5280 | lua_pushboolean(L, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5281 | http_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5282 | return 1; |
| 5283 | } |
| 5284 | |
| 5285 | /* This function set the uri. */ |
| 5286 | static int hlua_http_req_set_uri(lua_State *L) |
| 5287 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5288 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5289 | size_t name_len; |
| 5290 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5291 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5292 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5293 | WILL_LJMP(lua_error(L)); |
| 5294 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5295 | lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5296 | return 1; |
| 5297 | } |
| 5298 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5299 | /* This function set the response code & optionally reason. */ |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5300 | static int hlua_http_res_set_status(lua_State *L) |
| 5301 | { |
| 5302 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5303 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 5304 | const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 5305 | const struct ist reason = ist2(str, (str ? strlen(str) : 0)); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5306 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5307 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5308 | WILL_LJMP(lua_error(L)); |
| 5309 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5310 | http_res_set_status(code, reason, htxn->s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5311 | return 0; |
| 5312 | } |
| 5313 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5314 | /* |
| 5315 | * |
| 5316 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5317 | * Class TXN |
| 5318 | * |
| 5319 | * |
| 5320 | */ |
| 5321 | |
| 5322 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5323 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5324 | */ |
| 5325 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 5326 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5327 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5328 | } |
| 5329 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5330 | __LJMP static int hlua_set_var(lua_State *L) |
| 5331 | { |
| 5332 | struct hlua_txn *htxn; |
| 5333 | const char *name; |
| 5334 | size_t len; |
| 5335 | struct sample smp; |
| 5336 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 5337 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 5338 | WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments")); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5339 | |
| 5340 | /* It is useles to retrieve the stream, but this function |
| 5341 | * runs only in a stream context. |
| 5342 | */ |
| 5343 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5344 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5345 | |
| 5346 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 5347 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5348 | hlua_lua2smp(L, 3, &smp); |
| 5349 | |
| 5350 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5351 | smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 5352 | |
| 5353 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 5354 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 5355 | else |
| 5356 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 5357 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 5358 | return 1; |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5359 | } |
| 5360 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5361 | __LJMP static int hlua_unset_var(lua_State *L) |
| 5362 | { |
| 5363 | struct hlua_txn *htxn; |
| 5364 | const char *name; |
| 5365 | size_t len; |
| 5366 | struct sample smp; |
| 5367 | |
| 5368 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 5369 | |
| 5370 | /* It is useles to retrieve the stream, but this function |
| 5371 | * runs only in a stream context. |
| 5372 | */ |
| 5373 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5374 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5375 | |
| 5376 | /* Unset the variable. */ |
| 5377 | smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 5378 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 5379 | return 1; |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5380 | } |
| 5381 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5382 | __LJMP static int hlua_get_var(lua_State *L) |
| 5383 | { |
| 5384 | struct hlua_txn *htxn; |
| 5385 | const char *name; |
| 5386 | size_t len; |
| 5387 | struct sample smp; |
| 5388 | |
| 5389 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 5390 | |
| 5391 | /* It is useles to retrieve the stream, but this function |
| 5392 | * runs only in a stream context. |
| 5393 | */ |
| 5394 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5395 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5396 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5397 | smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR); |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 5398 | if (!vars_get_by_name(name, len, &smp)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5399 | lua_pushnil(L); |
| 5400 | return 1; |
| 5401 | } |
| 5402 | |
| 5403 | return hlua_smp2lua(L, &smp); |
| 5404 | } |
| 5405 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5406 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5407 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5408 | struct hlua *hlua; |
| 5409 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5410 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 5411 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5412 | /* It is useles to retrieve the stream, but this function |
| 5413 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5414 | */ |
| 5415 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5416 | |
| 5417 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5418 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5419 | if (!hlua) |
| 5420 | return 0; |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5421 | |
| 5422 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5423 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5424 | |
| 5425 | /* Get and store new value. */ |
| 5426 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5427 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5428 | |
| 5429 | return 0; |
| 5430 | } |
| 5431 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5432 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5433 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5434 | struct hlua *hlua; |
| 5435 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5436 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 5437 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5438 | /* It is useles to retrieve the stream, but this function |
| 5439 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5440 | */ |
| 5441 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5442 | |
| 5443 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5444 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 5445 | if (!hlua) { |
| 5446 | lua_pushnil(L); |
| 5447 | return 1; |
| 5448 | } |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5449 | |
| 5450 | /* Push configuration index in the stack. */ |
| 5451 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5452 | |
| 5453 | return 1; |
| 5454 | } |
| 5455 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5456 | /* Create stack entry containing a class TXN. This function |
| 5457 | * return 0 if the stack does not contains free slots, |
| 5458 | * otherwise it returns 1. |
| 5459 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5460 | static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p, int dir, int flags) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5461 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5462 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5463 | |
| 5464 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5465 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5466 | return 0; |
| 5467 | |
| 5468 | /* NOTE: The allocation never fails. The failure |
| 5469 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5470 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5471 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5472 | /* Create the object: obj[0] = userdata. */ |
| 5473 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5474 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5475 | lua_rawseti(L, -2, 0); |
| 5476 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5477 | htxn->s = s; |
| 5478 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 5479 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5480 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5481 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5482 | /* Create the "f" field that contains a list of fetches. */ |
| 5483 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5484 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5485 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5486 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5487 | |
| 5488 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 5489 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5490 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP | HLUA_F_AS_STRING)) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5491 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5492 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5493 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5494 | /* Create the "c" field that contains a list of converters. */ |
| 5495 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5496 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5497 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5498 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5499 | |
| 5500 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 5501 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 5502 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5503 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5504 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5505 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5506 | /* Create the "req" field that contains the request channel object. */ |
| 5507 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5508 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5509 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5510 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5511 | |
| 5512 | /* Create the "res" field that contains the response channel object. */ |
| 5513 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5514 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5515 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5516 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5517 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5518 | /* Creates the HTTP object is the current proxy allows http. */ |
| 5519 | lua_pushstring(L, "http"); |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 5520 | if (IS_HTX_STRM(s)) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5521 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5522 | return 0; |
| 5523 | } |
| 5524 | else |
| 5525 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5526 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5527 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5528 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5529 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 5530 | lua_setmetatable(L, -2); |
| 5531 | |
| 5532 | return 1; |
| 5533 | } |
| 5534 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5535 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 5536 | { |
| 5537 | const char *msg; |
| 5538 | struct hlua_txn *htxn; |
| 5539 | |
| 5540 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 5541 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5542 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5543 | |
| 5544 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 5545 | return 0; |
| 5546 | } |
| 5547 | |
| 5548 | __LJMP static int hlua_txn_log(lua_State *L) |
| 5549 | { |
| 5550 | int level; |
| 5551 | const char *msg; |
| 5552 | struct hlua_txn *htxn; |
| 5553 | |
| 5554 | MAY_LJMP(check_args(L, 3, "log")); |
| 5555 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5556 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5557 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5558 | |
| 5559 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5560 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5561 | |
| 5562 | hlua_sendlog(htxn->s->be, level, msg); |
| 5563 | return 0; |
| 5564 | } |
| 5565 | |
| 5566 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 5567 | { |
| 5568 | const char *msg; |
| 5569 | struct hlua_txn *htxn; |
| 5570 | |
| 5571 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 5572 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5573 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5574 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 5575 | return 0; |
| 5576 | } |
| 5577 | |
| 5578 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 5579 | { |
| 5580 | const char *msg; |
| 5581 | struct hlua_txn *htxn; |
| 5582 | |
| 5583 | MAY_LJMP(check_args(L, 2, "Info")); |
| 5584 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5585 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5586 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 5587 | return 0; |
| 5588 | } |
| 5589 | |
| 5590 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 5591 | { |
| 5592 | const char *msg; |
| 5593 | struct hlua_txn *htxn; |
| 5594 | |
| 5595 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 5596 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5597 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5598 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 5599 | return 0; |
| 5600 | } |
| 5601 | |
| 5602 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 5603 | { |
| 5604 | const char *msg; |
| 5605 | struct hlua_txn *htxn; |
| 5606 | |
| 5607 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 5608 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5609 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5610 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 5611 | return 0; |
| 5612 | } |
| 5613 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5614 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 5615 | { |
| 5616 | struct hlua_txn *htxn; |
| 5617 | int ll; |
| 5618 | |
| 5619 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 5620 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5621 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5622 | |
| 5623 | if (ll < 0 || ll > 7) |
| 5624 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 5625 | |
| 5626 | htxn->s->logs.level = ll; |
| 5627 | return 0; |
| 5628 | } |
| 5629 | |
| 5630 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 5631 | { |
| 5632 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5633 | int tos; |
| 5634 | |
| 5635 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 5636 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5637 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5638 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 5639 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5640 | return 0; |
| 5641 | } |
| 5642 | |
| 5643 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 5644 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5645 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5646 | int mark; |
| 5647 | |
| 5648 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 5649 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5650 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5651 | |
Lukas Tribus | 579e3e3 | 2019-08-11 18:03:45 +0200 | [diff] [blame] | 5652 | conn_set_mark(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5653 | return 0; |
| 5654 | } |
| 5655 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 5656 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 5657 | { |
| 5658 | struct hlua_txn *htxn; |
| 5659 | |
| 5660 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 5661 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5662 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5663 | return 0; |
| 5664 | } |
| 5665 | |
| 5666 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 5667 | { |
| 5668 | struct hlua_txn *htxn; |
| 5669 | |
| 5670 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 5671 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5672 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5673 | return 0; |
| 5674 | } |
| 5675 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5676 | /* Forward the Reply object to the client. This function converts the reply in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5677 | * HTX an push it to into the response channel. It is response to forward the |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5678 | * message and terminate the transaction. It returns 1 on success and 0 on |
| 5679 | * error. The Reply must be on top of the stack. |
| 5680 | */ |
| 5681 | __LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s) |
| 5682 | { |
| 5683 | struct htx *htx; |
| 5684 | struct htx_sl *sl; |
| 5685 | struct h1m h1m; |
| 5686 | const char *status, *reason, *body; |
| 5687 | size_t status_len, reason_len, body_len; |
| 5688 | int ret, code, flags; |
| 5689 | |
| 5690 | code = 200; |
| 5691 | status = "200"; |
| 5692 | status_len = 3; |
| 5693 | ret = lua_getfield(L, -1, "status"); |
| 5694 | if (ret == LUA_TNUMBER) { |
| 5695 | code = lua_tointeger(L, -1); |
| 5696 | status = lua_tolstring(L, -1, &status_len); |
| 5697 | } |
| 5698 | lua_pop(L, 1); |
| 5699 | |
| 5700 | reason = http_get_reason(code); |
| 5701 | reason_len = strlen(reason); |
| 5702 | ret = lua_getfield(L, -1, "reason"); |
| 5703 | if (ret == LUA_TSTRING) |
| 5704 | reason = lua_tolstring(L, -1, &reason_len); |
| 5705 | lua_pop(L, 1); |
| 5706 | |
| 5707 | body = NULL; |
| 5708 | body_len = 0; |
| 5709 | ret = lua_getfield(L, -1, "body"); |
| 5710 | if (ret == LUA_TSTRING) |
| 5711 | body = lua_tolstring(L, -1, &body_len); |
| 5712 | lua_pop(L, 1); |
| 5713 | |
| 5714 | /* Prepare the response before inserting the headers */ |
| 5715 | h1m_init_res(&h1m); |
| 5716 | htx = htx_from_buf(&s->res.buf); |
| 5717 | channel_htx_truncate(&s->res, htx); |
| 5718 | if (s->txn->req.flags & HTTP_MSGF_VER_11) { |
| 5719 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 5720 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), |
| 5721 | ist2(status, status_len), ist2(reason, reason_len)); |
| 5722 | } |
| 5723 | else { |
| 5724 | flags = HTX_SL_F_IS_RESP; |
| 5725 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), |
| 5726 | ist2(status, status_len), ist2(reason, reason_len)); |
| 5727 | } |
| 5728 | if (!sl) |
| 5729 | goto fail; |
| 5730 | sl->info.res.status = code; |
| 5731 | |
| 5732 | /* Push in the stack the "headers" entry. */ |
| 5733 | ret = lua_getfield(L, -1, "headers"); |
| 5734 | if (ret != LUA_TTABLE) |
| 5735 | goto skip_headers; |
| 5736 | |
| 5737 | lua_pushnil(L); |
| 5738 | while (lua_next(L, -2) != 0) { |
| 5739 | struct ist name, value; |
| 5740 | const char *n, *v; |
| 5741 | size_t nlen, vlen; |
| 5742 | |
| 5743 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 5744 | /* Skip element if the key is not a string or if the value is not a table */ |
| 5745 | goto next_hdr; |
| 5746 | } |
| 5747 | |
| 5748 | n = lua_tolstring(L, -2, &nlen); |
| 5749 | name = ist2(n, nlen); |
| 5750 | if (isteqi(name, ist("content-length"))) { |
| 5751 | /* Always skip content-length header. It will be added |
| 5752 | * later with the correct len |
| 5753 | */ |
| 5754 | goto next_hdr; |
| 5755 | } |
| 5756 | |
| 5757 | /* Loop on header's values */ |
| 5758 | lua_pushnil(L); |
| 5759 | while (lua_next(L, -2)) { |
| 5760 | if (!lua_isstring(L, -1)) { |
| 5761 | /* Skip the value if it is not a string */ |
| 5762 | goto next_value; |
| 5763 | } |
| 5764 | |
| 5765 | v = lua_tolstring(L, -1, &vlen); |
| 5766 | value = ist2(v, vlen); |
| 5767 | |
| 5768 | if (isteqi(name, ist("transfer-encoding"))) |
| 5769 | h1_parse_xfer_enc_header(&h1m, value); |
| 5770 | if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen))) |
| 5771 | goto fail; |
| 5772 | |
| 5773 | next_value: |
| 5774 | lua_pop(L, 1); |
| 5775 | } |
| 5776 | |
| 5777 | next_hdr: |
| 5778 | lua_pop(L, 1); |
| 5779 | } |
| 5780 | skip_headers: |
| 5781 | lua_pop(L, 1); |
| 5782 | |
| 5783 | /* Update h1m flags: CLEN is set if CHNK is not present */ |
| 5784 | if (!(h1m.flags & H1_MF_CHNK)) { |
| 5785 | const char *clen = ultoa(body_len); |
| 5786 | |
| 5787 | h1m.flags |= H1_MF_CLEN; |
| 5788 | if (!htx_add_header(htx, ist("content-length"), ist(clen))) |
| 5789 | goto fail; |
| 5790 | } |
| 5791 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 5792 | h1m.flags |= H1_MF_XFER_LEN; |
| 5793 | |
| 5794 | /* Update HTX start-line flags */ |
| 5795 | if (h1m.flags & H1_MF_XFER_ENC) |
| 5796 | flags |= HTX_SL_F_XFER_ENC; |
| 5797 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 5798 | flags |= HTX_SL_F_XFER_LEN; |
| 5799 | if (h1m.flags & H1_MF_CHNK) |
| 5800 | flags |= HTX_SL_F_CHNK; |
| 5801 | else if (h1m.flags & H1_MF_CLEN) |
| 5802 | flags |= HTX_SL_F_CLEN; |
| 5803 | if (h1m.body_len == 0) |
| 5804 | flags |= HTX_SL_F_BODYLESS; |
| 5805 | } |
| 5806 | sl->flags |= flags; |
| 5807 | |
| 5808 | |
| 5809 | if (!htx_add_endof(htx, HTX_BLK_EOH) || |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5810 | (body_len && !htx_add_data_atonce(htx, ist2(body, body_len)))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5811 | goto fail; |
| 5812 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5813 | htx->flags |= HTX_FL_EOM; |
| 5814 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5815 | /* Now, forward the response and terminate the transaction */ |
| 5816 | s->txn->status = code; |
| 5817 | htx_to_buf(htx, &s->res.buf); |
| 5818 | if (!http_forward_proxy_resp(s, 1)) |
| 5819 | goto fail; |
| 5820 | |
| 5821 | return 1; |
| 5822 | |
| 5823 | fail: |
| 5824 | channel_htx_truncate(&s->res, htx); |
| 5825 | return 0; |
| 5826 | } |
| 5827 | |
| 5828 | /* Terminate a transaction if called from a lua action. For TCP streams, |
| 5829 | * processing is just aborted. Nothing is returned to the client and all |
| 5830 | * arguments are ignored. For HTTP streams, if a reply is passed as argument, it |
| 5831 | * is forwarded to the client before terminating the transaction. On success, |
| 5832 | * the function exits with ACT_RET_DONE code. If an error occurred, it exits |
| 5833 | * with ACT_RET_ERR code. If this function is not called from a lua action, it |
| 5834 | * just exits without any processing. |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5835 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5836 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5837 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5838 | struct hlua_txn *htxn; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5839 | struct stream *s; |
| 5840 | int finst; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5841 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5842 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5843 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5844 | /* If the flags NOTERM is set, we cannot terminate the session, so we |
| 5845 | * just end the execution of the current lua code. */ |
| 5846 | if (htxn->flags & HLUA_TXN_NOTERM) |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5847 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5848 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5849 | s = htxn->s; |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5850 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5851 | struct channel *req = &s->req; |
| 5852 | struct channel *res = &s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5853 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5854 | channel_auto_read(req); |
| 5855 | channel_abort(req); |
| 5856 | channel_auto_close(req); |
| 5857 | channel_erase(req); |
| 5858 | |
| 5859 | res->wex = tick_add_ifset(now_ms, res->wto); |
| 5860 | channel_auto_read(res); |
| 5861 | channel_auto_close(res); |
| 5862 | channel_shutr_now(res); |
| 5863 | |
| 5864 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D); |
| 5865 | goto done; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5866 | } |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 5867 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5868 | if (lua_gettop(L) == 1 || !lua_istable(L, 2)) { |
| 5869 | /* No reply or invalid reply */ |
| 5870 | s->txn->status = 0; |
| 5871 | http_reply_and_close(s, 0, NULL); |
| 5872 | } |
| 5873 | else { |
| 5874 | /* Remove extra args to have the reply on top of the stack */ |
| 5875 | if (lua_gettop(L) > 2) |
| 5876 | lua_pop(L, lua_gettop(L) - 2); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5877 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5878 | if (!hlua_txn_forward_reply(L, s)) { |
| 5879 | if (!(s->flags & SF_ERR_MASK)) |
| 5880 | s->flags |= SF_ERR_PRXCOND; |
| 5881 | lua_pushinteger(L, ACT_RET_ERR); |
| 5882 | WILL_LJMP(hlua_done(L)); |
| 5883 | return 0; /* Never reached */ |
| 5884 | } |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 5885 | } |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5886 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5887 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H); |
| 5888 | if (htxn->dir == SMP_OPT_DIR_REQ) { |
| 5889 | /* let's log the request time */ |
| 5890 | s->logs.tv_request = now; |
| 5891 | if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */ |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 5892 | _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5893 | } |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5894 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5895 | done: |
| 5896 | if (!(s->flags & SF_ERR_MASK)) |
| 5897 | s->flags |= SF_ERR_LOCAL; |
| 5898 | if (!(s->flags & SF_FINST_MASK)) |
| 5899 | s->flags |= finst; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5900 | |
Christopher Faulet | 4ad7310 | 2020-03-05 11:07:31 +0100 | [diff] [blame] | 5901 | lua_pushinteger(L, ACT_RET_ABRT); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5902 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5903 | return 0; |
| 5904 | } |
| 5905 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5906 | /* |
| 5907 | * |
| 5908 | * |
| 5909 | * Class REPLY |
| 5910 | * |
| 5911 | * |
| 5912 | */ |
| 5913 | |
| 5914 | /* Pushes the TXN reply onto the top of the stack. If the stask does not have a |
| 5915 | * free slots, the function fails and returns 0; |
| 5916 | */ |
| 5917 | static int hlua_txn_reply_new(lua_State *L) |
| 5918 | { |
| 5919 | struct hlua_txn *htxn; |
| 5920 | const char *reason, *body = NULL; |
| 5921 | int ret, status; |
| 5922 | |
| 5923 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5924 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5925 | hlua_pusherror(L, "txn object is not an HTTP transaction."); |
| 5926 | WILL_LJMP(lua_error(L)); |
| 5927 | } |
| 5928 | |
| 5929 | /* Default value */ |
| 5930 | status = 200; |
| 5931 | reason = http_get_reason(status); |
| 5932 | |
| 5933 | if (lua_istable(L, 2)) { |
| 5934 | /* load status and reason from the table argument at index 2 */ |
| 5935 | ret = lua_getfield(L, 2, "status"); |
| 5936 | if (ret == LUA_TNIL) |
| 5937 | goto reason; |
| 5938 | else if (ret != LUA_TNUMBER) { |
| 5939 | /* invalid status: ignore the reason */ |
| 5940 | goto body; |
| 5941 | } |
| 5942 | status = lua_tointeger(L, -1); |
| 5943 | |
| 5944 | reason: |
| 5945 | lua_pop(L, 1); /* restore the stack: remove status */ |
| 5946 | ret = lua_getfield(L, 2, "reason"); |
| 5947 | if (ret == LUA_TSTRING) |
| 5948 | reason = lua_tostring(L, -1); |
| 5949 | |
| 5950 | body: |
| 5951 | lua_pop(L, 1); /* restore the stack: remove invalid status or reason */ |
| 5952 | ret = lua_getfield(L, 2, "body"); |
| 5953 | if (ret == LUA_TSTRING) |
| 5954 | body = lua_tostring(L, -1); |
| 5955 | lua_pop(L, 1); /* restore the stack: remove body */ |
| 5956 | } |
| 5957 | |
| 5958 | /* Create the Reply table */ |
| 5959 | lua_newtable(L); |
| 5960 | |
| 5961 | /* Add status element */ |
| 5962 | lua_pushstring(L, "status"); |
| 5963 | lua_pushinteger(L, status); |
| 5964 | lua_settable(L, -3); |
| 5965 | |
| 5966 | /* Add reason element */ |
| 5967 | reason = http_get_reason(status); |
| 5968 | lua_pushstring(L, "reason"); |
| 5969 | lua_pushstring(L, reason); |
| 5970 | lua_settable(L, -3); |
| 5971 | |
| 5972 | /* Add body element, nil if undefined */ |
| 5973 | lua_pushstring(L, "body"); |
| 5974 | if (body) |
| 5975 | lua_pushstring(L, body); |
| 5976 | else |
| 5977 | lua_pushnil(L); |
| 5978 | lua_settable(L, -3); |
| 5979 | |
| 5980 | /* Add headers element */ |
| 5981 | lua_pushstring(L, "headers"); |
| 5982 | lua_newtable(L); |
| 5983 | |
| 5984 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 5985 | if (lua_istable(L, 2)) { |
| 5986 | /* load headers from the table argument at index 2. If it is a table, copy it. */ |
| 5987 | ret = lua_getfield(L, 2, "headers"); |
| 5988 | if (ret == LUA_TTABLE) { |
| 5989 | /* stack: [ ... <headers:table>, <table> ] */ |
| 5990 | lua_pushnil(L); |
| 5991 | while (lua_next(L, -2) != 0) { |
| 5992 | /* stack: [ ... <headers:table>, <table>, k, v] */ |
| 5993 | if (!lua_isstring(L, -1) && !lua_istable(L, -1)) { |
| 5994 | /* invalid value type, skip it */ |
| 5995 | lua_pop(L, 1); |
| 5996 | continue; |
| 5997 | } |
| 5998 | |
| 5999 | |
| 6000 | /* Duplicate the key and swap it with the value. */ |
| 6001 | lua_pushvalue(L, -2); |
| 6002 | lua_insert(L, -2); |
| 6003 | /* stack: [ ... <headers:table>, <table>, k, k, v ] */ |
| 6004 | |
| 6005 | lua_newtable(L); |
| 6006 | lua_insert(L, -2); |
| 6007 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */ |
| 6008 | |
| 6009 | if (lua_isstring(L, -1)) { |
| 6010 | /* push the value in the inner table */ |
| 6011 | lua_rawseti(L, -2, 1); |
| 6012 | } |
| 6013 | else { /* table */ |
| 6014 | lua_pushnil(L); |
| 6015 | while (lua_next(L, -2) != 0) { |
| 6016 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */ |
| 6017 | if (!lua_isstring(L, -1)) { |
| 6018 | /* invalid value type, skip it*/ |
| 6019 | lua_pop(L, 1); |
| 6020 | continue; |
| 6021 | } |
| 6022 | /* push the value in the inner table */ |
| 6023 | lua_rawseti(L, -4, lua_rawlen(L, -4) + 1); |
| 6024 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */ |
| 6025 | } |
| 6026 | lua_pop(L, 1); |
| 6027 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */ |
| 6028 | } |
| 6029 | |
| 6030 | /* push (k,v) on the stack in the headers table: |
| 6031 | * stack: [ ... <headers:table>, <table>, k, k, v ] |
| 6032 | */ |
| 6033 | lua_settable(L, -5); |
| 6034 | /* stack: [ ... <headers:table>, <table>, k ] */ |
| 6035 | } |
| 6036 | } |
| 6037 | lua_pop(L, 1); |
| 6038 | } |
| 6039 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 6040 | lua_settable(L, -3); |
| 6041 | /* stack: [ txn, <Arg:table>, <Reply:table> ] */ |
| 6042 | |
| 6043 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 6044 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref); |
| 6045 | lua_setmetatable(L, -2); |
| 6046 | return 1; |
| 6047 | } |
| 6048 | |
| 6049 | /* Set the reply status code, and optionally the reason. If no reason is |
| 6050 | * provided, the default one corresponding to the status code is used. |
| 6051 | */ |
| 6052 | __LJMP static int hlua_txn_reply_set_status(lua_State *L) |
| 6053 | { |
| 6054 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6055 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 6056 | |
| 6057 | /* First argument (self) must be a table */ |
| 6058 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6059 | |
| 6060 | if (status < 100 || status > 599) { |
| 6061 | lua_pushboolean(L, 0); |
| 6062 | return 1; |
| 6063 | } |
| 6064 | if (!reason) |
| 6065 | reason = http_get_reason(status); |
| 6066 | |
| 6067 | lua_pushinteger(L, status); |
| 6068 | lua_setfield(L, 1, "status"); |
| 6069 | |
| 6070 | lua_pushstring(L, reason); |
| 6071 | lua_setfield(L, 1, "reason"); |
| 6072 | |
| 6073 | lua_pushboolean(L, 1); |
| 6074 | return 1; |
| 6075 | } |
| 6076 | |
| 6077 | /* Add a header into the reply object. Each header name is associated to an |
| 6078 | * array of values in the "headers" table. If the header name is not found, a |
| 6079 | * new entry is created. |
| 6080 | */ |
| 6081 | __LJMP static int hlua_txn_reply_add_header(lua_State *L) |
| 6082 | { |
| 6083 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6084 | const char *value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 6085 | int ret; |
| 6086 | |
| 6087 | /* First argument (self) must be a table */ |
| 6088 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6089 | |
| 6090 | /* Push in the stack the "headers" entry. */ |
| 6091 | ret = lua_getfield(L, 1, "headers"); |
| 6092 | if (ret != LUA_TTABLE) { |
| 6093 | hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret)); |
| 6094 | WILL_LJMP(lua_error(L)); |
| 6095 | } |
| 6096 | |
| 6097 | /* check if the header is already registered. If not, register it. */ |
| 6098 | ret = lua_getfield(L, -1, name); |
| 6099 | if (ret == LUA_TNIL) { |
| 6100 | /* Entry not found. */ |
| 6101 | lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */ |
| 6102 | |
| 6103 | /* Insert the new header name in the array in the top of the stack. |
| 6104 | * It left the new array in the top of the stack. |
| 6105 | */ |
| 6106 | lua_newtable(L); |
| 6107 | lua_pushstring(L, name); |
| 6108 | lua_pushvalue(L, -2); |
| 6109 | lua_settable(L, -4); |
| 6110 | } |
| 6111 | else if (ret != LUA_TTABLE) { |
| 6112 | hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret)); |
| 6113 | WILL_LJMP(lua_error(L)); |
| 6114 | } |
| 6115 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 6116 | /* Now the top of thestack is an array of values. We push |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 6117 | * the header value as new entry. |
| 6118 | */ |
| 6119 | lua_pushstring(L, value); |
| 6120 | ret = lua_rawlen(L, -2); |
| 6121 | lua_rawseti(L, -2, ret + 1); |
| 6122 | |
| 6123 | lua_pushboolean(L, 1); |
| 6124 | return 1; |
| 6125 | } |
| 6126 | |
| 6127 | /* Remove all occurrences of a given header name. */ |
| 6128 | __LJMP static int hlua_txn_reply_del_header(lua_State *L) |
| 6129 | { |
| 6130 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6131 | int ret; |
| 6132 | |
| 6133 | /* First argument (self) must be a table */ |
| 6134 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6135 | |
| 6136 | /* Push in the stack the "headers" entry. */ |
| 6137 | ret = lua_getfield(L, 1, "headers"); |
| 6138 | if (ret != LUA_TTABLE) { |
| 6139 | hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret)); |
| 6140 | WILL_LJMP(lua_error(L)); |
| 6141 | } |
| 6142 | |
| 6143 | lua_pushstring(L, name); |
| 6144 | lua_pushnil(L); |
| 6145 | lua_settable(L, -3); |
| 6146 | |
| 6147 | lua_pushboolean(L, 1); |
| 6148 | return 1; |
| 6149 | } |
| 6150 | |
| 6151 | /* Set the reply's body. Overwrite any existing entry. */ |
| 6152 | __LJMP static int hlua_txn_reply_set_body(lua_State *L) |
| 6153 | { |
| 6154 | const char *payload = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6155 | |
| 6156 | /* First argument (self) must be a table */ |
| 6157 | luaL_checktype(L, 1, LUA_TTABLE); |
| 6158 | |
| 6159 | lua_pushstring(L, payload); |
| 6160 | lua_setfield(L, 1, "body"); |
| 6161 | |
| 6162 | lua_pushboolean(L, 1); |
| 6163 | return 1; |
| 6164 | } |
| 6165 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 6166 | __LJMP static int hlua_log(lua_State *L) |
| 6167 | { |
| 6168 | int level; |
| 6169 | const char *msg; |
| 6170 | |
| 6171 | MAY_LJMP(check_args(L, 2, "log")); |
| 6172 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 6173 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6174 | |
| 6175 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 6176 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 6177 | |
| 6178 | hlua_sendlog(NULL, level, msg); |
| 6179 | return 0; |
| 6180 | } |
| 6181 | |
| 6182 | __LJMP static int hlua_log_debug(lua_State *L) |
| 6183 | { |
| 6184 | const char *msg; |
| 6185 | |
| 6186 | MAY_LJMP(check_args(L, 1, "debug")); |
| 6187 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6188 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 6189 | return 0; |
| 6190 | } |
| 6191 | |
| 6192 | __LJMP static int hlua_log_info(lua_State *L) |
| 6193 | { |
| 6194 | const char *msg; |
| 6195 | |
| 6196 | MAY_LJMP(check_args(L, 1, "info")); |
| 6197 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6198 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 6199 | return 0; |
| 6200 | } |
| 6201 | |
| 6202 | __LJMP static int hlua_log_warning(lua_State *L) |
| 6203 | { |
| 6204 | const char *msg; |
| 6205 | |
| 6206 | MAY_LJMP(check_args(L, 1, "warning")); |
| 6207 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6208 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 6209 | return 0; |
| 6210 | } |
| 6211 | |
| 6212 | __LJMP static int hlua_log_alert(lua_State *L) |
| 6213 | { |
| 6214 | const char *msg; |
| 6215 | |
| 6216 | MAY_LJMP(check_args(L, 1, "alert")); |
| 6217 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6218 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6219 | return 0; |
| 6220 | } |
| 6221 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6222 | __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] | 6223 | { |
| 6224 | int wakeup_ms = lua_tointeger(L, -1); |
| 6225 | if (now_ms < wakeup_ms) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6226 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0)); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6227 | return 0; |
| 6228 | } |
| 6229 | |
| 6230 | __LJMP static int hlua_sleep(lua_State *L) |
| 6231 | { |
| 6232 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6233 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6234 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6235 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6236 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6237 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6238 | wakeup_ms = tick_add(now_ms, delay); |
| 6239 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6240 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6241 | 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] | 6242 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6243 | } |
| 6244 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6245 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6246 | { |
| 6247 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6248 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6249 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6250 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6251 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6252 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6253 | wakeup_ms = tick_add(now_ms, delay); |
| 6254 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6255 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6256 | 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] | 6257 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6258 | } |
| 6259 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6260 | /* This functionis an LUA binding. it permits to give back |
| 6261 | * the hand at the HAProxy scheduler. It is used when the |
| 6262 | * LUA processing consumes a lot of time. |
| 6263 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6264 | __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] | 6265 | { |
| 6266 | return 0; |
| 6267 | } |
| 6268 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6269 | __LJMP static int hlua_yield(lua_State *L) |
| 6270 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6271 | 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] | 6272 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6273 | } |
| 6274 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6275 | /* This function change the nice of the currently executed |
| 6276 | * task. It is used set low or high priority at the current |
| 6277 | * task. |
| 6278 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 6279 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6280 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6281 | struct hlua *hlua; |
| 6282 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6283 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6284 | MAY_LJMP(check_args(L, 1, "set_nice")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6285 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6286 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 6287 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 6288 | hlua = hlua_gethlua(L); |
| 6289 | |
| 6290 | /* If the task is not set, I'm in a start mode. */ |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6291 | if (!hlua || !hlua->task) |
| 6292 | return 0; |
| 6293 | |
| 6294 | if (nice < -1024) |
| 6295 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6296 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6297 | nice = 1024; |
| 6298 | |
| 6299 | hlua->task->nice = nice; |
| 6300 | return 0; |
| 6301 | } |
| 6302 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6303 | /* 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] | 6304 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 6305 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 6306 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6307 | * |
| 6308 | * Task wrapper are longjmp safe because the only one Lua code |
| 6309 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6310 | */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 6311 | 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] | 6312 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6313 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6314 | enum hlua_exec status; |
| 6315 | |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 6316 | if (task->thread_mask == MAX_THREADS_MASK) |
| 6317 | task_set_affinity(task, tid_bit); |
| 6318 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6319 | /* If it is the first call to the task, we must initialize the |
| 6320 | * execution timeouts. |
| 6321 | */ |
| 6322 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6323 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6324 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6325 | /* Execute the Lua code. */ |
| 6326 | status = hlua_ctx_resume(hlua, 1); |
| 6327 | |
| 6328 | switch (status) { |
| 6329 | /* finished or yield */ |
| 6330 | case HLUA_E_OK: |
| 6331 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6332 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 6333 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6334 | break; |
| 6335 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6336 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 6337 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 6338 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6339 | break; |
| 6340 | |
| 6341 | /* finished with error. */ |
| 6342 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6343 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6344 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6345 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6346 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6347 | break; |
| 6348 | |
| 6349 | case HLUA_E_ERR: |
| 6350 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6351 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6352 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6353 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6354 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6355 | break; |
| 6356 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6357 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6358 | } |
| 6359 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6360 | /* This function is an LUA binding that register LUA function to be |
| 6361 | * executed after the HAProxy configuration parsing and before the |
| 6362 | * HAProxy scheduler starts. This function expect only one LUA |
| 6363 | * argument that is a function. This function returns nothing, but |
| 6364 | * throws if an error is encountered. |
| 6365 | */ |
| 6366 | __LJMP static int hlua_register_init(lua_State *L) |
| 6367 | { |
| 6368 | struct hlua_init_function *init; |
| 6369 | int ref; |
| 6370 | |
| 6371 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 6372 | |
| 6373 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6374 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6375 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6376 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6377 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6378 | |
| 6379 | init->function_ref = ref; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6380 | LIST_ADDQ(&hlua_init_functions[hlua_state_id], &init->l); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6381 | return 0; |
| 6382 | } |
| 6383 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6384 | /* This functio is an LUA binding. It permits to register a task |
| 6385 | * executed in parallel of the main HAroxy activity. The task is |
| 6386 | * created and it is set in the HAProxy scheduler. It can be called |
| 6387 | * from the "init" section, "post init" or during the runtime. |
| 6388 | * |
| 6389 | * Lua prototype: |
| 6390 | * |
| 6391 | * <none> core.register_task(<function>) |
| 6392 | */ |
| 6393 | static int hlua_register_task(lua_State *L) |
| 6394 | { |
| 6395 | struct hlua *hlua; |
| 6396 | struct task *task; |
| 6397 | int ref; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 6398 | int state_id; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6399 | |
| 6400 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 6401 | |
| 6402 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6403 | |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 6404 | /* Get the reference state. If the reference is NULL, L is the master |
| 6405 | * state, otherwise hlua->T is. |
| 6406 | */ |
| 6407 | hlua = hlua_gethlua(L); |
| 6408 | if (hlua) |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 6409 | /* we are in runtime processing */ |
| 6410 | state_id = hlua->state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 6411 | else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 6412 | /* we are in initialization mode */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6413 | state_id = hlua_state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 6414 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6415 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6416 | if (!hlua) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6417 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6418 | HLUA_INIT(hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6419 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 6420 | /* We are in the common lua state, execute the task anywhere, |
| 6421 | * otherwise, inherit the current thread identifier |
| 6422 | */ |
| 6423 | if (state_id == 0) |
| 6424 | task = task_new(MAX_THREADS_MASK); |
| 6425 | else |
| 6426 | task = task_new(tid_bit); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 6427 | if (!task) |
| 6428 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 6429 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6430 | task->context = hlua; |
| 6431 | task->process = hlua_process_task; |
| 6432 | |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 6433 | if (!hlua_ctx_init(hlua, state_id, task, 1)) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6434 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6435 | |
| 6436 | /* Restore the function in the stack. */ |
| 6437 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 6438 | hlua->nargs = 0; |
| 6439 | |
| 6440 | /* Schedule task. */ |
| 6441 | task_schedule(task, now_ms); |
| 6442 | |
| 6443 | return 0; |
| 6444 | } |
| 6445 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6446 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 6447 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 6448 | * resume converters. |
| 6449 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6450 | 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] | 6451 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6452 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6453 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6454 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6455 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6456 | if (!stream) |
| 6457 | return 0; |
| 6458 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6459 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6460 | * Lua context can be not initialized. This behavior |
| 6461 | * permits to save performances because a systematic |
| 6462 | * Lua initialization cause 5% performances loss. |
| 6463 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6464 | if (!stream->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6465 | struct hlua *hlua; |
| 6466 | |
| 6467 | hlua = pool_alloc(pool_head_hlua); |
| 6468 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6469 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6470 | return 0; |
| 6471 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6472 | HLUA_INIT(hlua); |
| 6473 | stream->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6474 | 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] | 6475 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6476 | return 0; |
| 6477 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6478 | } |
| 6479 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6480 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6481 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6482 | |
| 6483 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6484 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6485 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6486 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6487 | else |
| 6488 | error = "critical error"; |
| 6489 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6490 | return 0; |
| 6491 | } |
| 6492 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6493 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6494 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6495 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6496 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6497 | return 0; |
| 6498 | } |
| 6499 | |
| 6500 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6501 | 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] | 6502 | |
| 6503 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6504 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6505 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6506 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6507 | return 0; |
| 6508 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6509 | hlua_smp2lua(stream->hlua->T, smp); |
| 6510 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6511 | |
| 6512 | /* push keywords in the stack. */ |
| 6513 | if (arg_p) { |
| 6514 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6515 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6516 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
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 | return 0; |
| 6519 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6520 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6521 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6522 | } |
| 6523 | } |
| 6524 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6525 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6526 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6527 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6528 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6529 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6530 | } |
| 6531 | |
| 6532 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6533 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6534 | /* finished. */ |
| 6535 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6536 | /* If the stack is empty, the function fails. */ |
| 6537 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6538 | return 0; |
| 6539 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6540 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6541 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6542 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6543 | return 1; |
| 6544 | |
| 6545 | /* yield. */ |
| 6546 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6547 | 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] | 6548 | return 0; |
| 6549 | |
| 6550 | /* finished with error. */ |
| 6551 | case HLUA_E_ERRMSG: |
| 6552 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6553 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6554 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6555 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6556 | return 0; |
| 6557 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6558 | case HLUA_E_ETMOUT: |
| 6559 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 6560 | return 0; |
| 6561 | |
| 6562 | case HLUA_E_NOMEM: |
| 6563 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 6564 | return 0; |
| 6565 | |
| 6566 | case HLUA_E_YIELD: |
| 6567 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 6568 | return 0; |
| 6569 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6570 | case HLUA_E_ERR: |
| 6571 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6572 | 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] | 6573 | /* fall through */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6574 | |
| 6575 | default: |
| 6576 | return 0; |
| 6577 | } |
| 6578 | } |
| 6579 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6580 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 6581 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6582 | * resume sample-fetches. This function will be called by the sample |
| 6583 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6584 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6585 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 6586 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6587 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6588 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6589 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6590 | const char *error; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6591 | unsigned int hflags = HLUA_TXN_NOTERM; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6592 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6593 | if (!stream) |
| 6594 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 6595 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6596 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6597 | * Lua context can be not initialized. This behavior |
| 6598 | * permits to save performances because a systematic |
| 6599 | * Lua initialization cause 5% performances loss. |
| 6600 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6601 | if (!stream->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6602 | struct hlua *hlua; |
| 6603 | |
| 6604 | hlua = pool_alloc(pool_head_hlua); |
| 6605 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6606 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6607 | return 0; |
| 6608 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6609 | hlua->T = NULL; |
| 6610 | stream->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6611 | 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] | 6612 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6613 | return 0; |
| 6614 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6615 | } |
| 6616 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6617 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6618 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6619 | |
| 6620 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6621 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6622 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6623 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6624 | else |
| 6625 | error = "critical error"; |
| 6626 | 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] | 6627 | return 0; |
| 6628 | } |
| 6629 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6630 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6631 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6632 | SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6633 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6634 | return 0; |
| 6635 | } |
| 6636 | |
| 6637 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6638 | 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] | 6639 | |
| 6640 | /* push arguments in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6641 | 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] | 6642 | SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6643 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6644 | return 0; |
| 6645 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6646 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6647 | |
| 6648 | /* push keywords in the stack. */ |
| 6649 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 6650 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6651 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6652 | 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] | 6653 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6654 | return 0; |
| 6655 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6656 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6657 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6658 | } |
| 6659 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6660 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6661 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6662 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6663 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6664 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6665 | } |
| 6666 | |
| 6667 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6668 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6669 | /* finished. */ |
| 6670 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6671 | /* If the stack is empty, the function fails. */ |
| 6672 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6673 | return 0; |
| 6674 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6675 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6676 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6677 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6678 | |
| 6679 | /* Set the end of execution flag. */ |
| 6680 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 6681 | return 1; |
| 6682 | |
| 6683 | /* yield. */ |
| 6684 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6685 | 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] | 6686 | return 0; |
| 6687 | |
| 6688 | /* finished with error. */ |
| 6689 | case HLUA_E_ERRMSG: |
| 6690 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6691 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6692 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6693 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6694 | return 0; |
| 6695 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6696 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6697 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 6698 | return 0; |
| 6699 | |
| 6700 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6701 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 6702 | return 0; |
| 6703 | |
| 6704 | case HLUA_E_YIELD: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6705 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 6706 | return 0; |
| 6707 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6708 | case HLUA_E_ERR: |
| 6709 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6710 | 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] | 6711 | /* fall through */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6712 | |
| 6713 | default: |
| 6714 | return 0; |
| 6715 | } |
| 6716 | } |
| 6717 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6718 | /* This function is an LUA binding used for registering |
| 6719 | * "sample-conv" functions. It expects a converter name used |
| 6720 | * in the haproxy configuration file, and an LUA function. |
| 6721 | */ |
| 6722 | __LJMP static int hlua_register_converters(lua_State *L) |
| 6723 | { |
| 6724 | struct sample_conv_kw_list *sck; |
| 6725 | const char *name; |
| 6726 | int ref; |
| 6727 | int len; |
| 6728 | struct hlua_function *fcn; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6729 | struct sample_conv *sc; |
| 6730 | struct buffer *trash; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6731 | |
| 6732 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 6733 | |
| 6734 | /* First argument : converter name. */ |
| 6735 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6736 | |
| 6737 | /* Second argument : lua function. */ |
| 6738 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6739 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6740 | /* Check if the converter is already registered */ |
| 6741 | trash = get_trash_chunk(); |
| 6742 | chunk_printf(trash, "lua.%s", name); |
| 6743 | sc = find_sample_conv(trash->area, trash->data); |
| 6744 | if (sc != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 6745 | fcn = sc->private; |
| 6746 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 6747 | ha_warning("Trying to register converter 'lua.%s' more than once. " |
| 6748 | "This will become a hard error in version 2.5.\n", name); |
| 6749 | } |
| 6750 | fcn->function_ref[hlua_state_id] = ref; |
| 6751 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6752 | } |
| 6753 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6754 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6755 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6756 | if (!sck) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6757 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 6758 | fcn = new_hlua_function(); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6759 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6760 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6761 | |
| 6762 | /* Fill fcn. */ |
| 6763 | fcn->name = strdup(name); |
| 6764 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6765 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6766 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6767 | |
| 6768 | /* List head */ |
| 6769 | sck->list.n = sck->list.p = NULL; |
| 6770 | |
| 6771 | /* converter keyword. */ |
| 6772 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6773 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6774 | if (!sck->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6775 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6776 | |
| 6777 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 6778 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6779 | 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] | 6780 | sck->kw[0].val_args = NULL; |
| 6781 | sck->kw[0].in_type = SMP_T_STR; |
| 6782 | sck->kw[0].out_type = SMP_T_STR; |
| 6783 | sck->kw[0].private = fcn; |
| 6784 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6785 | /* Register this new converter */ |
| 6786 | sample_register_convs(sck); |
| 6787 | |
| 6788 | return 0; |
| 6789 | } |
| 6790 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6791 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6792 | * "sample-fetch" functions. It expects a converter name used |
| 6793 | * in the haproxy configuration file, and an LUA function. |
| 6794 | */ |
| 6795 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 6796 | { |
| 6797 | const char *name; |
| 6798 | int ref; |
| 6799 | int len; |
| 6800 | struct sample_fetch_kw_list *sfk; |
| 6801 | struct hlua_function *fcn; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6802 | struct sample_fetch *sf; |
| 6803 | struct buffer *trash; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6804 | |
| 6805 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 6806 | |
| 6807 | /* First argument : sample-fetch name. */ |
| 6808 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6809 | |
| 6810 | /* Second argument : lua function. */ |
| 6811 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6812 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6813 | /* Check if the sample-fetch is already registered */ |
| 6814 | trash = get_trash_chunk(); |
| 6815 | chunk_printf(trash, "lua.%s", name); |
| 6816 | sf = find_sample_fetch(trash->area, trash->data); |
| 6817 | if (sf != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 6818 | fcn = sf->private; |
| 6819 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 6820 | ha_warning("Trying to register sample-fetch 'lua.%s' more than once. " |
| 6821 | "This will become a hard error in version 2.5.\n", name); |
| 6822 | } |
| 6823 | fcn->function_ref[hlua_state_id] = ref; |
| 6824 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 6825 | } |
| 6826 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6827 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6828 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6829 | if (!sfk) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6830 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 6831 | fcn = new_hlua_function(); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6832 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6833 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6834 | |
| 6835 | /* Fill fcn. */ |
| 6836 | fcn->name = strdup(name); |
| 6837 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6838 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6839 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6840 | |
| 6841 | /* List head */ |
| 6842 | sfk->list.n = sfk->list.p = NULL; |
| 6843 | |
| 6844 | /* sample-fetch keyword. */ |
| 6845 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6846 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6847 | if (!sfk->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6848 | return luaL_error(L, "Lua out of memory error."); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6849 | |
| 6850 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 6851 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6852 | 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] | 6853 | sfk->kw[0].val_args = NULL; |
| 6854 | sfk->kw[0].out_type = SMP_T_STR; |
| 6855 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 6856 | sfk->kw[0].val = 0; |
| 6857 | sfk->kw[0].private = fcn; |
| 6858 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6859 | /* Register this new fetch. */ |
| 6860 | sample_register_fetches(sfk); |
| 6861 | |
| 6862 | return 0; |
| 6863 | } |
| 6864 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 6865 | /* This function is a lua binding to set the wake_time. |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6866 | */ |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 6867 | __LJMP static int hlua_set_wake_time(lua_State *L) |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6868 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 6869 | struct hlua *hlua; |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6870 | unsigned int delay; |
| 6871 | unsigned int wakeup_ms; |
| 6872 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 6873 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 6874 | hlua = hlua_gethlua(L); |
| 6875 | if (!hlua) { |
| 6876 | return 0; |
| 6877 | } |
| 6878 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6879 | MAY_LJMP(check_args(L, 1, "wake_time")); |
| 6880 | |
| 6881 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 6882 | wakeup_ms = tick_add(now_ms, delay); |
| 6883 | hlua->wake_time = wakeup_ms; |
| 6884 | return 0; |
| 6885 | } |
| 6886 | |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6887 | /* This function is a wrapper to execute each LUA function declared as an action |
| 6888 | * wrapper during the initialisation period. This function may return any |
| 6889 | * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is |
| 6890 | * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the |
| 6891 | * return value is the first element on the stack. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6892 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6893 | 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] | 6894 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6895 | { |
| 6896 | char **arg; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6897 | unsigned int hflags = 0; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6898 | int dir, act_ret = ACT_RET_CONT; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6899 | const char *error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6900 | |
| 6901 | switch (rule->from) { |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 6902 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 6903 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 6904 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 6905 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6906 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6907 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6908 | goto end; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6909 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6910 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6911 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6912 | * Lua context can be not initialized. This behavior |
| 6913 | * permits to save performances because a systematic |
| 6914 | * Lua initialization cause 5% performances loss. |
| 6915 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6916 | if (!s->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6917 | struct hlua *hlua; |
| 6918 | |
| 6919 | hlua = pool_alloc(pool_head_hlua); |
| 6920 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6921 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6922 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6923 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6924 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 6925 | HLUA_INIT(hlua); |
| 6926 | s->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6927 | 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] | 6928 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6929 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6930 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6931 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6932 | } |
| 6933 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6934 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6935 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6936 | |
| 6937 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6938 | if (!SET_SAFE_LJMP(s->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6939 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 6940 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6941 | else |
| 6942 | error = "critical error"; |
| 6943 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6944 | rule->arg.hlua_rule->fcn->name, error); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6945 | goto end; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6946 | } |
| 6947 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6948 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6949 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6950 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6951 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6952 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6953 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6954 | } |
| 6955 | |
| 6956 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 6957 | 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] | 6958 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6959 | /* Create and and push object stream in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6960 | if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6961 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6962 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6963 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6964 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6965 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6966 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6967 | |
| 6968 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6969 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6970 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6971 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 6972 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6973 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6974 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6975 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6976 | lua_pushstring(s->hlua->T, *arg); |
| 6977 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6978 | } |
| 6979 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6980 | /* Now the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 6981 | RESET_SAFE_LJMP(s->hlua); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6982 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6983 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6984 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6985 | } |
| 6986 | |
| 6987 | /* Execute the function. */ |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 6988 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6989 | /* finished. */ |
| 6990 | case HLUA_E_OK: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6991 | /* Catch the return value */ |
| 6992 | if (lua_gettop(s->hlua->T) > 0) |
| 6993 | act_ret = lua_tointeger(s->hlua->T, -1); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6994 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6995 | /* Set timeout in the required channel. */ |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 6996 | if (act_ret == ACT_RET_YIELD) { |
| 6997 | if (flags & ACT_OPT_FINAL) |
| 6998 | goto err_yield; |
| 6999 | |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 7000 | if (dir == SMP_OPT_DIR_REQ) |
| 7001 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 7002 | s->hlua->wake_time); |
| 7003 | else |
| 7004 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 7005 | s->hlua->wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 7006 | } |
| 7007 | goto end; |
| 7008 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7009 | /* yield. */ |
| 7010 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 7011 | /* Set timeout in the required channel. */ |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 7012 | if (dir == SMP_OPT_DIR_REQ) |
| 7013 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 7014 | s->hlua->wake_time); |
| 7015 | else |
| 7016 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 7017 | s->hlua->wake_time); |
| 7018 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7019 | /* Some actions can be wake up when a "write" event |
| 7020 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7021 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7022 | */ |
Christopher Faulet | 51fa358 | 2019-07-26 14:54:52 +0200 | [diff] [blame] | 7023 | if (HLUA_IS_WAKERESWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7024 | s->res.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7025 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 7026 | s->req.flags |= CF_WAKE_WRITE; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7027 | act_ret = ACT_RET_YIELD; |
| 7028 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7029 | |
| 7030 | /* finished with error. */ |
| 7031 | case HLUA_E_ERRMSG: |
| 7032 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7033 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7034 | rule->arg.hlua_rule->fcn->name, lua_tostring(s->hlua->T, -1)); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 7035 | lua_pop(s->hlua->T, 1); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7036 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7037 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7038 | case HLUA_E_ETMOUT: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7039 | 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] | 7040 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7041 | |
| 7042 | case HLUA_E_NOMEM: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7043 | 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] | 7044 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7045 | |
| 7046 | case HLUA_E_YIELD: |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 7047 | err_yield: |
| 7048 | act_ret = ACT_RET_CONT; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7049 | 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] | 7050 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7051 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7052 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7053 | case HLUA_E_ERR: |
| 7054 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 7055 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7056 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7057 | |
| 7058 | default: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7059 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7060 | } |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7061 | |
| 7062 | end: |
Christopher Faulet | 2361fd9 | 2020-07-30 10:40:58 +0200 | [diff] [blame] | 7063 | if (act_ret != ACT_RET_YIELD && s->hlua) |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 7064 | s->hlua->wake_time = TICK_ETERNITY; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 7065 | return act_ret; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7066 | } |
| 7067 | |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 7068 | 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] | 7069 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 7070 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7071 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7072 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 7073 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 7074 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7075 | } |
| 7076 | |
| 7077 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 7078 | { |
| 7079 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7080 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7081 | struct task *task; |
| 7082 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7083 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7084 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7085 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7086 | if (!hlua) { |
| 7087 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7088 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7089 | return 0; |
| 7090 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7091 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7092 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7093 | ctx->ctx.hlua_apptcp.flags = 0; |
| 7094 | |
| 7095 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7096 | task = task_new(tid_bit); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7097 | if (!task) { |
| 7098 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7099 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7100 | return 0; |
| 7101 | } |
| 7102 | task->nice = 0; |
| 7103 | task->context = ctx; |
| 7104 | task->process = hlua_applet_wakeup; |
| 7105 | ctx->ctx.hlua_apptcp.task = task; |
| 7106 | |
| 7107 | /* In the execution wrappers linked with a stream, the |
| 7108 | * Lua context can be not initialized. This behavior |
| 7109 | * permits to save performances because a systematic |
| 7110 | * Lua initialization cause 5% performances loss. |
| 7111 | */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7112 | 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] | 7113 | 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] | 7114 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7115 | return 0; |
| 7116 | } |
| 7117 | |
| 7118 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 7119 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7120 | |
| 7121 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7122 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7123 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7124 | error = lua_tostring(hlua->T, -1); |
| 7125 | else |
| 7126 | error = "critical error"; |
| 7127 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7128 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7129 | return 0; |
| 7130 | } |
| 7131 | |
| 7132 | /* Check stack available size. */ |
| 7133 | if (!lua_checkstack(hlua->T, 1)) { |
| 7134 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7135 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7136 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7137 | return 0; |
| 7138 | } |
| 7139 | |
| 7140 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7141 | 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] | 7142 | |
| 7143 | /* Create and and push object stream in the stack. */ |
| 7144 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 7145 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7146 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7147 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7148 | return 0; |
| 7149 | } |
| 7150 | hlua->nargs = 1; |
| 7151 | |
| 7152 | /* push keywords in the stack. */ |
| 7153 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 7154 | if (!lua_checkstack(hlua->T, 1)) { |
| 7155 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7156 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7157 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7158 | return 0; |
| 7159 | } |
| 7160 | lua_pushstring(hlua->T, *arg); |
| 7161 | hlua->nargs++; |
| 7162 | } |
| 7163 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7164 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7165 | |
| 7166 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7167 | si_cant_get(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 7168 | si_rx_endp_more(si); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7169 | |
| 7170 | return 1; |
| 7171 | } |
| 7172 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7173 | void hlua_applet_tcp_fct(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7174 | { |
| 7175 | struct stream_interface *si = ctx->owner; |
| 7176 | struct stream *strm = si_strm(si); |
| 7177 | struct channel *res = si_ic(si); |
| 7178 | struct act_rule *rule = ctx->rule; |
| 7179 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7180 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7181 | |
| 7182 | /* The applet execution is already done. */ |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 7183 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) { |
| 7184 | /* eat the whole request */ |
| 7185 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7186 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 7187 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7188 | |
| 7189 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7190 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7191 | return; |
| 7192 | |
| 7193 | /* Execute the function. */ |
| 7194 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7195 | /* finished. */ |
| 7196 | case HLUA_E_OK: |
| 7197 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 7198 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7199 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 7200 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7201 | res->flags |= CF_READ_NULL; |
| 7202 | si_shutr(si); |
| 7203 | return; |
| 7204 | |
| 7205 | /* yield. */ |
| 7206 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 7207 | if (hlua->wake_time != TICK_ETERNITY) |
| 7208 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7209 | return; |
| 7210 | |
| 7211 | /* finished with error. */ |
| 7212 | case HLUA_E_ERRMSG: |
| 7213 | /* Display log. */ |
| 7214 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7215 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7216 | lua_pop(hlua->T, 1); |
| 7217 | goto error; |
| 7218 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7219 | case HLUA_E_ETMOUT: |
| 7220 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7221 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7222 | goto error; |
| 7223 | |
| 7224 | case HLUA_E_NOMEM: |
| 7225 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7226 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7227 | goto error; |
| 7228 | |
| 7229 | case HLUA_E_YIELD: /* unexpected */ |
| 7230 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7231 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7232 | goto error; |
| 7233 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7234 | case HLUA_E_ERR: |
| 7235 | /* Display log. */ |
| 7236 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7237 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7238 | goto error; |
| 7239 | |
| 7240 | default: |
| 7241 | goto error; |
| 7242 | } |
| 7243 | |
| 7244 | error: |
| 7245 | |
| 7246 | /* For all other cases, just close the stream. */ |
| 7247 | si_shutw(si); |
| 7248 | si_shutr(si); |
| 7249 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 7250 | } |
| 7251 | |
| 7252 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 7253 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7254 | task_destroy(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7255 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7256 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7257 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7258 | } |
| 7259 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7260 | /* The function returns 1 if the initialisation is complete, 0 if |
| 7261 | * an errors occurs and -1 if more data are required for initializing |
| 7262 | * the applet. |
| 7263 | */ |
| 7264 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 7265 | { |
| 7266 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7267 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7268 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7269 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7270 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7271 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7272 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7273 | txn = strm->txn; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7274 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7275 | if (!hlua) { |
| 7276 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7277 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7278 | return 0; |
| 7279 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7280 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7281 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7282 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 7283 | ctx->ctx.hlua_apphttp.flags = 0; |
| 7284 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 7285 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 7286 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 7287 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7288 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7289 | task = task_new(tid_bit); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7290 | if (!task) { |
| 7291 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7292 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7293 | return 0; |
| 7294 | } |
| 7295 | task->nice = 0; |
| 7296 | task->context = ctx; |
| 7297 | task->process = hlua_applet_wakeup; |
| 7298 | ctx->ctx.hlua_apphttp.task = task; |
| 7299 | |
| 7300 | /* In the execution wrappers linked with a stream, the |
| 7301 | * Lua context can be not initialized. This behavior |
| 7302 | * permits to save performances because a systematic |
| 7303 | * Lua initialization cause 5% performances loss. |
| 7304 | */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7305 | 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] | 7306 | 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] | 7307 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7308 | return 0; |
| 7309 | } |
| 7310 | |
| 7311 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 7312 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7313 | |
| 7314 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7315 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7316 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7317 | error = lua_tostring(hlua->T, -1); |
| 7318 | else |
| 7319 | error = "critical error"; |
| 7320 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7321 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7322 | return 0; |
| 7323 | } |
| 7324 | |
| 7325 | /* Check stack available size. */ |
| 7326 | if (!lua_checkstack(hlua->T, 1)) { |
| 7327 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7328 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7329 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7330 | return 0; |
| 7331 | } |
| 7332 | |
| 7333 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7334 | 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] | 7335 | |
| 7336 | /* Create and and push object stream in the stack. */ |
| 7337 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 7338 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7339 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7340 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7341 | return 0; |
| 7342 | } |
| 7343 | hlua->nargs = 1; |
| 7344 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7345 | /* push keywords in the stack. */ |
| 7346 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 7347 | if (!lua_checkstack(hlua->T, 1)) { |
| 7348 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7349 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7350 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7351 | return 0; |
| 7352 | } |
| 7353 | lua_pushstring(hlua->T, *arg); |
| 7354 | hlua->nargs++; |
| 7355 | } |
| 7356 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7357 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7358 | |
| 7359 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7360 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7361 | |
| 7362 | return 1; |
| 7363 | } |
| 7364 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7365 | void hlua_applet_http_fct(struct appctx *ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7366 | { |
| 7367 | struct stream_interface *si = ctx->owner; |
| 7368 | struct stream *strm = si_strm(si); |
| 7369 | struct channel *req = si_oc(si); |
| 7370 | struct channel *res = si_ic(si); |
| 7371 | struct act_rule *rule = ctx->rule; |
| 7372 | struct proxy *px = strm->be; |
| 7373 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
| 7374 | struct htx *req_htx, *res_htx; |
| 7375 | |
| 7376 | res_htx = htx_from_buf(&res->buf); |
| 7377 | |
| 7378 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7379 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7380 | goto out; |
| 7381 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7382 | /* Check if the input buffer is available. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7383 | if (!b_size(&res->buf)) { |
| 7384 | si_rx_room_blk(si); |
| 7385 | goto out; |
| 7386 | } |
| 7387 | /* check that the output is not closed */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7388 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7389 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7390 | |
| 7391 | /* Set the currently running flag. */ |
| 7392 | if (!HLUA_IS_RUNNING(hlua) && |
| 7393 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7394 | struct htx_blk *blk; |
| 7395 | size_t count = co_data(req); |
| 7396 | |
| 7397 | if (!count) { |
| 7398 | si_cant_get(si); |
| 7399 | goto out; |
| 7400 | } |
| 7401 | |
| 7402 | /* We need to flush the request header. This left the body for |
| 7403 | * the Lua. |
| 7404 | */ |
| 7405 | req_htx = htx_from_buf(&req->buf); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 7406 | blk = htx_get_first_blk(req_htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7407 | while (count && blk) { |
| 7408 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 7409 | uint32_t sz = htx_get_blksz(blk); |
| 7410 | |
| 7411 | if (sz > count) { |
| 7412 | si_cant_get(si); |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 7413 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7414 | goto out; |
| 7415 | } |
| 7416 | |
| 7417 | count -= sz; |
| 7418 | co_set_data(req, co_data(req) - sz); |
| 7419 | blk = htx_remove_blk(req_htx, blk); |
| 7420 | |
| 7421 | if (type == HTX_BLK_EOH) |
| 7422 | break; |
| 7423 | } |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 7424 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7425 | } |
| 7426 | |
| 7427 | /* Executes The applet if it is not done. */ |
| 7428 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7429 | |
| 7430 | /* Execute the function. */ |
| 7431 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7432 | /* finished. */ |
| 7433 | case HLUA_E_OK: |
| 7434 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7435 | break; |
| 7436 | |
| 7437 | /* yield. */ |
| 7438 | case HLUA_E_AGAIN: |
| 7439 | if (hlua->wake_time != TICK_ETERNITY) |
| 7440 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7441 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7442 | |
| 7443 | /* finished with error. */ |
| 7444 | case HLUA_E_ERRMSG: |
| 7445 | /* Display log. */ |
| 7446 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7447 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7448 | lua_pop(hlua->T, 1); |
| 7449 | goto error; |
| 7450 | |
| 7451 | case HLUA_E_ETMOUT: |
| 7452 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7453 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7454 | goto error; |
| 7455 | |
| 7456 | case HLUA_E_NOMEM: |
| 7457 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7458 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7459 | goto error; |
| 7460 | |
| 7461 | case HLUA_E_YIELD: /* unexpected */ |
| 7462 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7463 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7464 | goto error; |
| 7465 | |
| 7466 | case HLUA_E_ERR: |
| 7467 | /* Display log. */ |
| 7468 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7469 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7470 | goto error; |
| 7471 | |
| 7472 | default: |
| 7473 | goto error; |
| 7474 | } |
| 7475 | } |
| 7476 | |
| 7477 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7478 | if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT) |
| 7479 | goto done; |
| 7480 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7481 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) |
| 7482 | goto error; |
| 7483 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 7484 | /* no more data are expected. Don't add TLR because mux-h1 will take care of it */ |
| 7485 | res_htx->flags |= HTX_FL_EOM; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7486 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 7487 | ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7488 | } |
| 7489 | |
| 7490 | done: |
| 7491 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7492 | if (!(res->flags & CF_SHUTR)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7493 | res->flags |= CF_READ_NULL; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7494 | si_shutr(si); |
| 7495 | } |
| 7496 | |
| 7497 | /* eat the whole request */ |
| 7498 | if (co_data(req)) { |
| 7499 | req_htx = htx_from_buf(&req->buf); |
| 7500 | co_htx_skip(req, req_htx, co_data(req)); |
| 7501 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7502 | } |
| 7503 | } |
| 7504 | |
| 7505 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7506 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7507 | return; |
| 7508 | |
| 7509 | error: |
| 7510 | |
| 7511 | /* If we are in HTTP mode, and we are not send any |
| 7512 | * data, return a 500 server error in best effort: |
| 7513 | * if there is no room available in the buffer, |
| 7514 | * just close the connection. |
| 7515 | */ |
| 7516 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 7517 | struct buffer *err = &http_err_chunks[HTTP_ERR_500]; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7518 | |
| 7519 | channel_erase(res); |
| 7520 | res->buf.data = b_data(err); |
| 7521 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 7522 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 7523 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7524 | } |
| 7525 | if (!(strm->flags & SF_ERR_MASK)) |
| 7526 | strm->flags |= SF_ERR_RESOURCE; |
| 7527 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7528 | goto done; |
| 7529 | } |
| 7530 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7531 | static void hlua_applet_http_release(struct appctx *ctx) |
| 7532 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7533 | task_destroy(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7534 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7535 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7536 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7537 | } |
| 7538 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7539 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7540 | * success case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7541 | * |
| 7542 | * This function can fail with an abort() due to an Lua critical error. |
| 7543 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7544 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7545 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7546 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 7547 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7548 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7549 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7550 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7551 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7552 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7553 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7554 | if (!rule->arg.hlua_rule) { |
| 7555 | memprintf(err, "out of memory error"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7556 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7557 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7558 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7559 | /* Memory for arguments. */ |
Tim Duesterhus | e52b6e5 | 2020-09-12 20:26:43 +0200 | [diff] [blame] | 7560 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, |
| 7561 | sizeof(*rule->arg.hlua_rule->args)); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7562 | if (!rule->arg.hlua_rule->args) { |
| 7563 | memprintf(err, "out of memory error"); |
| 7564 | return ACT_RET_PRS_ERR; |
| 7565 | } |
| 7566 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7567 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7568 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7569 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7570 | /* Expect some arguments */ |
| 7571 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7572 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7573 | memprintf(err, "expect %d arguments", fcn->nargs); |
| 7574 | return ACT_RET_PRS_ERR; |
| 7575 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7576 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7577 | if (!rule->arg.hlua_rule->args[i]) { |
| 7578 | memprintf(err, "out of memory error"); |
| 7579 | return ACT_RET_PRS_ERR; |
| 7580 | } |
| 7581 | (*cur_arg)++; |
| 7582 | } |
| 7583 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7584 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 7585 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7586 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7587 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7588 | } |
| 7589 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7590 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 7591 | struct act_rule *rule, char **err) |
| 7592 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7593 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7594 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7595 | /* HTTP applets are forbidden in tcp-request rules. |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7596 | * HTTP applet request requires everything initialized by |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7597 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7598 | * The applet will be immediately initialized, but its before |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7599 | * the call of this analyzer. |
| 7600 | */ |
| 7601 | if (rule->from != ACT_F_HTTP_REQ) { |
| 7602 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 7603 | return ACT_RET_PRS_ERR; |
| 7604 | } |
| 7605 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7606 | /* Memory for the rule. */ |
| 7607 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7608 | if (!rule->arg.hlua_rule) { |
| 7609 | memprintf(err, "out of memory error"); |
| 7610 | return ACT_RET_PRS_ERR; |
| 7611 | } |
| 7612 | |
| 7613 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7614 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7615 | |
| 7616 | /* TODO: later accept arguments. */ |
| 7617 | rule->arg.hlua_rule->args = NULL; |
| 7618 | |
| 7619 | /* Add applet pointer in the rule. */ |
| 7620 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7621 | rule->applet.name = fcn->name; |
| 7622 | rule->applet.init = hlua_applet_http_init; |
| 7623 | rule->applet.fct = hlua_applet_http_fct; |
| 7624 | rule->applet.release = hlua_applet_http_release; |
| 7625 | rule->applet.timeout = hlua_timeout_applet; |
| 7626 | |
| 7627 | return ACT_RET_PRS_OK; |
| 7628 | } |
| 7629 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7630 | /* This function is an LUA binding used for registering |
| 7631 | * "sample-conv" functions. It expects a converter name used |
| 7632 | * in the haproxy configuration file, and an LUA function. |
| 7633 | */ |
| 7634 | __LJMP static int hlua_register_action(lua_State *L) |
| 7635 | { |
| 7636 | struct action_kw_list *akl; |
| 7637 | const char *name; |
| 7638 | int ref; |
| 7639 | int len; |
| 7640 | struct hlua_function *fcn; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7641 | int nargs; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7642 | struct buffer *trash; |
| 7643 | struct action_kw *akw; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7644 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7645 | /* Initialise the number of expected arguments at 0. */ |
| 7646 | nargs = 0; |
| 7647 | |
| 7648 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 7649 | 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] | 7650 | |
| 7651 | /* First argument : converter name. */ |
| 7652 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7653 | |
| 7654 | /* Second argument : environment. */ |
| 7655 | if (lua_type(L, 2) != LUA_TTABLE) |
| 7656 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7657 | |
| 7658 | /* Third argument : lua function. */ |
| 7659 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7660 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7661 | /* 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] | 7662 | if (lua_gettop(L) >= 4) |
| 7663 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 7664 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7665 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7666 | lua_pushnil(L); |
| 7667 | while (lua_next(L, 2) != 0) { |
| 7668 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7669 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7670 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7671 | /* Check if action exists */ |
| 7672 | trash = get_trash_chunk(); |
| 7673 | chunk_printf(trash, "lua.%s", name); |
| 7674 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) { |
| 7675 | akw = tcp_req_cont_action(trash->area); |
| 7676 | } else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) { |
| 7677 | akw = tcp_res_cont_action(trash->area); |
| 7678 | } else if (strcmp(lua_tostring(L, -1), "http-req") == 0) { |
| 7679 | akw = action_http_req_custom(trash->area); |
| 7680 | } else if (strcmp(lua_tostring(L, -1), "http-res") == 0) { |
| 7681 | akw = action_http_res_custom(trash->area); |
| 7682 | } else { |
| 7683 | akw = NULL; |
| 7684 | } |
| 7685 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 7686 | fcn = akw->private; |
| 7687 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 7688 | ha_warning("Trying to register action 'lua.%s' more than once. " |
| 7689 | "This will become a hard error in version 2.5.\n", name); |
| 7690 | } |
| 7691 | fcn->function_ref[hlua_state_id] = ref; |
| 7692 | |
| 7693 | /* pop the environment string. */ |
| 7694 | lua_pop(L, 1); |
| 7695 | continue; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7696 | } |
| 7697 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7698 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 7699 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7700 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7701 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7702 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 7703 | fcn = new_hlua_function(); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7704 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7705 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7706 | |
| 7707 | /* Fill fcn. */ |
| 7708 | fcn->name = strdup(name); |
| 7709 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7710 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7711 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7712 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 7713 | /* Set the expected number of arguments. */ |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7714 | fcn->nargs = nargs; |
| 7715 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7716 | /* List head */ |
| 7717 | akl->list.n = akl->list.p = NULL; |
| 7718 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7719 | /* action keyword. */ |
| 7720 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7721 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7722 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7723 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7724 | |
| 7725 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7726 | |
| 7727 | akl->kw[0].match_pfx = 0; |
| 7728 | akl->kw[0].private = fcn; |
| 7729 | akl->kw[0].parse = action_register_lua; |
| 7730 | |
| 7731 | /* select the action registering point. */ |
| 7732 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 7733 | tcp_req_cont_keywords_register(akl); |
| 7734 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 7735 | tcp_res_cont_keywords_register(akl); |
| 7736 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 7737 | http_req_keywords_register(akl); |
| 7738 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 7739 | http_res_keywords_register(akl); |
| 7740 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7741 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7742 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 7743 | "are expected.", lua_tostring(L, -1))); |
| 7744 | |
| 7745 | /* pop the environment string. */ |
| 7746 | lua_pop(L, 1); |
| 7747 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7748 | return ACT_RET_PRS_OK; |
| 7749 | } |
| 7750 | |
| 7751 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 7752 | struct act_rule *rule, char **err) |
| 7753 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7754 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7755 | |
Christopher Faulet | 280f85b | 2019-07-15 15:02:04 +0200 | [diff] [blame] | 7756 | if (px->mode == PR_MODE_HTTP) { |
| 7757 | memprintf(err, "Lua TCP services cannot be used on HTTP proxies"); |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 7758 | return ACT_RET_PRS_ERR; |
| 7759 | } |
| 7760 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7761 | /* Memory for the rule. */ |
| 7762 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7763 | if (!rule->arg.hlua_rule) { |
| 7764 | memprintf(err, "out of memory error"); |
| 7765 | return ACT_RET_PRS_ERR; |
| 7766 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7767 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7768 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 7769 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7770 | |
| 7771 | /* TODO: later accept arguments. */ |
| 7772 | rule->arg.hlua_rule->args = NULL; |
| 7773 | |
| 7774 | /* Add applet pointer in the rule. */ |
| 7775 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7776 | rule->applet.name = fcn->name; |
| 7777 | rule->applet.init = hlua_applet_tcp_init; |
| 7778 | rule->applet.fct = hlua_applet_tcp_fct; |
| 7779 | rule->applet.release = hlua_applet_tcp_release; |
| 7780 | rule->applet.timeout = hlua_timeout_applet; |
| 7781 | |
| 7782 | return 0; |
| 7783 | } |
| 7784 | |
| 7785 | /* This function is an LUA binding used for registering |
| 7786 | * "sample-conv" functions. It expects a converter name used |
| 7787 | * in the haproxy configuration file, and an LUA function. |
| 7788 | */ |
| 7789 | __LJMP static int hlua_register_service(lua_State *L) |
| 7790 | { |
| 7791 | struct action_kw_list *akl; |
| 7792 | const char *name; |
| 7793 | const char *env; |
| 7794 | int ref; |
| 7795 | int len; |
| 7796 | struct hlua_function *fcn; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7797 | struct buffer *trash; |
| 7798 | struct action_kw *akw; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7799 | |
| 7800 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 7801 | |
| 7802 | /* First argument : converter name. */ |
| 7803 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7804 | |
| 7805 | /* Second argument : environment. */ |
| 7806 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7807 | |
| 7808 | /* Third argument : lua function. */ |
| 7809 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7810 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7811 | /* Check for service already registered */ |
| 7812 | trash = get_trash_chunk(); |
| 7813 | chunk_printf(trash, "lua.%s", name); |
| 7814 | akw = service_find(trash->area); |
| 7815 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 7816 | fcn = akw->private; |
| 7817 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 7818 | ha_warning("Trying to register service 'lua.%s' more than once. " |
| 7819 | "This will become a hard error in version 2.5.\n", name); |
| 7820 | } |
| 7821 | fcn->function_ref[hlua_state_id] = ref; |
| 7822 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 7823 | } |
| 7824 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7825 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7826 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 7827 | if (!akl) |
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 | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 7829 | fcn = new_hlua_function(); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7830 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7831 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7832 | |
| 7833 | /* Fill fcn. */ |
| 7834 | len = strlen("<lua.>") + strlen(name) + 1; |
| 7835 | fcn->name = calloc(1, len); |
| 7836 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7837 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7838 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7839 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7840 | |
| 7841 | /* List head */ |
| 7842 | akl->list.n = akl->list.p = NULL; |
| 7843 | |
| 7844 | /* converter keyword. */ |
| 7845 | len = strlen("lua.") + strlen(name) + 1; |
| 7846 | akl->kw[0].kw = calloc(1, len); |
| 7847 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7848 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7849 | |
| 7850 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7851 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 7852 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7853 | if (strcmp(env, "tcp") == 0) |
| 7854 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7855 | else if (strcmp(env, "http") == 0) |
| 7856 | akl->kw[0].parse = action_register_service_http; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7857 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7858 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 7859 | "'tcp' or 'http' are expected.", env)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7860 | |
| 7861 | akl->kw[0].match_pfx = 0; |
| 7862 | akl->kw[0].private = fcn; |
| 7863 | |
| 7864 | /* End of array. */ |
| 7865 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 7866 | |
| 7867 | /* Register this new converter */ |
| 7868 | service_keywords_register(akl); |
| 7869 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7870 | return 0; |
| 7871 | } |
| 7872 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7873 | /* This function initialises Lua cli handler. It copies the |
| 7874 | * arguments in the Lua stack and create channel IO objects. |
| 7875 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 7876 | 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] | 7877 | { |
| 7878 | struct hlua *hlua; |
| 7879 | struct hlua_function *fcn; |
| 7880 | int i; |
| 7881 | const char *error; |
| 7882 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7883 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7884 | appctx->ctx.hlua_cli.fcn = private; |
| 7885 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7886 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7887 | if (!hlua) { |
| 7888 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7889 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7890 | } |
| 7891 | HLUA_INIT(hlua); |
| 7892 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7893 | |
| 7894 | /* Create task used by signal to wakeup applets. |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 7895 | * 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] | 7896 | * applet_http. It is absolutely compatible. |
| 7897 | */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7898 | appctx->ctx.hlua_cli.task = task_new(tid_bit); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7899 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 7900 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7901 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7902 | } |
| 7903 | appctx->ctx.hlua_cli.task->nice = 0; |
| 7904 | appctx->ctx.hlua_cli.task->context = appctx; |
| 7905 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 7906 | |
| 7907 | /* Initialises the Lua context */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7908 | 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] | 7909 | 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] | 7910 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7911 | } |
| 7912 | |
| 7913 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7914 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7915 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7916 | error = lua_tostring(hlua->T, -1); |
| 7917 | else |
| 7918 | error = "critical error"; |
| 7919 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 7920 | goto error; |
| 7921 | } |
| 7922 | |
| 7923 | /* Check stack available size. */ |
| 7924 | if (!lua_checkstack(hlua->T, 2)) { |
| 7925 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7926 | goto error; |
| 7927 | } |
| 7928 | |
| 7929 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 7930 | 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] | 7931 | |
| 7932 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 7933 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7934 | */ |
| 7935 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 7936 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7937 | goto error; |
| 7938 | } |
| 7939 | hlua->nargs = 1; |
| 7940 | |
| 7941 | /* push keywords in the stack. */ |
| 7942 | for (i = 0; *args[i]; i++) { |
| 7943 | /* Check stack available size. */ |
| 7944 | if (!lua_checkstack(hlua->T, 1)) { |
| 7945 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7946 | goto error; |
| 7947 | } |
| 7948 | lua_pushstring(hlua->T, args[i]); |
| 7949 | hlua->nargs++; |
| 7950 | } |
| 7951 | |
| 7952 | /* We must initialize the execution timeouts. */ |
| 7953 | hlua->max_time = hlua_timeout_session; |
| 7954 | |
| 7955 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7956 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7957 | |
| 7958 | /* It's ok */ |
| 7959 | return 0; |
| 7960 | |
| 7961 | /* It's not ok. */ |
| 7962 | error: |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 7963 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7964 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7965 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7966 | return 1; |
| 7967 | } |
| 7968 | |
| 7969 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 7970 | { |
| 7971 | struct hlua *hlua; |
| 7972 | struct stream_interface *si; |
| 7973 | struct hlua_function *fcn; |
| 7974 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7975 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7976 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 7977 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7978 | |
| 7979 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7980 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7981 | return 1; |
| 7982 | |
| 7983 | /* Execute the function. */ |
| 7984 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7985 | |
| 7986 | /* finished. */ |
| 7987 | case HLUA_E_OK: |
| 7988 | return 1; |
| 7989 | |
| 7990 | /* yield. */ |
| 7991 | case HLUA_E_AGAIN: |
| 7992 | /* We want write. */ |
| 7993 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 7994 | si_rx_room_blk(si); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7995 | /* Set the timeout. */ |
| 7996 | if (hlua->wake_time != TICK_ETERNITY) |
| 7997 | task_schedule(hlua->task, hlua->wake_time); |
| 7998 | return 0; |
| 7999 | |
| 8000 | /* finished with error. */ |
| 8001 | case HLUA_E_ERRMSG: |
| 8002 | /* Display log. */ |
| 8003 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 8004 | fcn->name, lua_tostring(hlua->T, -1)); |
| 8005 | lua_pop(hlua->T, 1); |
| 8006 | return 1; |
| 8007 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8008 | case HLUA_E_ETMOUT: |
| 8009 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 8010 | fcn->name); |
| 8011 | return 1; |
| 8012 | |
| 8013 | case HLUA_E_NOMEM: |
| 8014 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 8015 | fcn->name); |
| 8016 | return 1; |
| 8017 | |
| 8018 | case HLUA_E_YIELD: /* unexpected */ |
| 8019 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 8020 | fcn->name); |
| 8021 | return 1; |
| 8022 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8023 | case HLUA_E_ERR: |
| 8024 | /* Display log. */ |
| 8025 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 8026 | fcn->name); |
| 8027 | return 1; |
| 8028 | |
| 8029 | default: |
| 8030 | return 1; |
| 8031 | } |
| 8032 | |
| 8033 | return 1; |
| 8034 | } |
| 8035 | |
| 8036 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 8037 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8038 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8039 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8040 | } |
| 8041 | |
| 8042 | /* This function is an LUA binding used for registering |
| 8043 | * new keywords in the cli. It expects a list of keywords |
| 8044 | * which are the "path". It is limited to 5 keywords. A |
| 8045 | * description of the command, a function to be executed |
| 8046 | * for the parsing and a function for io handlers. |
| 8047 | */ |
| 8048 | __LJMP static int hlua_register_cli(lua_State *L) |
| 8049 | { |
| 8050 | struct cli_kw_list *cli_kws; |
| 8051 | const char *message; |
| 8052 | int ref_io; |
| 8053 | int len; |
| 8054 | struct hlua_function *fcn; |
| 8055 | int index; |
| 8056 | int i; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8057 | struct buffer *trash; |
| 8058 | const char *kw[5]; |
| 8059 | struct cli_kw *cli_kw; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8060 | |
| 8061 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 8062 | |
| 8063 | /* First argument : an array of maximum 5 keywords. */ |
| 8064 | if (!lua_istable(L, 1)) |
| 8065 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 8066 | |
| 8067 | /* Second argument : string with contextual message. */ |
| 8068 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8069 | |
| 8070 | /* Third and fourth argument : lua function. */ |
| 8071 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 8072 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8073 | /* Check for CLI service already registered */ |
| 8074 | trash = get_trash_chunk(); |
| 8075 | index = 0; |
| 8076 | lua_pushnil(L); |
| 8077 | memset(kw, 0, sizeof(kw)); |
| 8078 | while (lua_next(L, 1) != 0) { |
| 8079 | if (index >= CLI_PREFIX_KW_NB) |
| 8080 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 8081 | if (lua_type(L, -1) != LUA_TSTRING) |
| 8082 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 8083 | kw[index] = lua_tostring(L, -1); |
| 8084 | if (index == 0) |
| 8085 | chunk_printf(trash, "%s", kw[index]); |
| 8086 | else |
| 8087 | chunk_appendf(trash, " %s", kw[index]); |
| 8088 | index++; |
| 8089 | lua_pop(L, 1); |
| 8090 | } |
| 8091 | cli_kw = cli_find_kw_exact((char **)kw); |
| 8092 | if (cli_kw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8093 | fcn = cli_kw->private; |
| 8094 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 8095 | ha_warning("Trying to register CLI keyword 'lua.%s' more than once. " |
| 8096 | "This will become a hard error in version 2.5.\n", trash->area); |
| 8097 | } |
| 8098 | fcn->function_ref[hlua_state_id] = ref_io; |
| 8099 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8100 | } |
| 8101 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8102 | /* Allocate and fill the sample fetch keyword struct. */ |
| 8103 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
| 8104 | if (!cli_kws) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8105 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 8106 | fcn = new_hlua_function(); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8107 | if (!fcn) |
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 path. */ |
| 8111 | index = 0; |
| 8112 | lua_pushnil(L); |
| 8113 | while(lua_next(L, 1) != 0) { |
| 8114 | if (index >= 5) |
| 8115 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 8116 | if (lua_type(L, -1) != LUA_TSTRING) |
| 8117 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 8118 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
| 8119 | if (!cli_kws->kw[0].str_kw[index]) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8120 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8121 | index++; |
| 8122 | lua_pop(L, 1); |
| 8123 | } |
| 8124 | |
| 8125 | /* Copy help message. */ |
| 8126 | cli_kws->kw[0].usage = strdup(message); |
| 8127 | if (!cli_kws->kw[0].usage) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8128 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8129 | |
| 8130 | /* Fill fcn io handler. */ |
| 8131 | len = strlen("<lua.cli>") + 1; |
| 8132 | for (i = 0; i < index; i++) |
| 8133 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 8134 | fcn->name = calloc(1, len); |
| 8135 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8136 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8137 | strncat((char *)fcn->name, "<lua.cli", len); |
| 8138 | for (i = 0; i < index; i++) { |
| 8139 | strncat((char *)fcn->name, ".", len); |
| 8140 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 8141 | } |
| 8142 | strncat((char *)fcn->name, ">", len); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8143 | fcn->function_ref[hlua_state_id] = ref_io; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8144 | |
| 8145 | /* Fill last entries. */ |
| 8146 | cli_kws->kw[0].private = fcn; |
| 8147 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 8148 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 8149 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 8150 | |
| 8151 | /* Register this new converter */ |
| 8152 | cli_register_kw(cli_kws); |
| 8153 | |
| 8154 | return 0; |
| 8155 | } |
| 8156 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8157 | 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] | 8158 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8159 | char **err, unsigned int *timeout) |
| 8160 | { |
| 8161 | const char *error; |
| 8162 | |
| 8163 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 8164 | if (error == PARSE_TIME_OVER) { |
| 8165 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 8166 | args[1], args[0]); |
| 8167 | return -1; |
| 8168 | } |
| 8169 | else if (error == PARSE_TIME_UNDER) { |
| 8170 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 8171 | args[1], args[0]); |
| 8172 | return -1; |
| 8173 | } |
| 8174 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8175 | memprintf(err, "%s: invalid timeout", args[0]); |
| 8176 | return -1; |
| 8177 | } |
| 8178 | return 0; |
| 8179 | } |
| 8180 | |
| 8181 | 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] | 8182 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8183 | char **err) |
| 8184 | { |
| 8185 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8186 | file, line, err, &hlua_timeout_session); |
| 8187 | } |
| 8188 | |
| 8189 | 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] | 8190 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8191 | char **err) |
| 8192 | { |
| 8193 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8194 | file, line, err, &hlua_timeout_task); |
| 8195 | } |
| 8196 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8197 | 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] | 8198 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8199 | char **err) |
| 8200 | { |
| 8201 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8202 | file, line, err, &hlua_timeout_applet); |
| 8203 | } |
| 8204 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8205 | 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] | 8206 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8207 | char **err) |
| 8208 | { |
| 8209 | char *error; |
| 8210 | |
| 8211 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 8212 | if (*error != '\0') { |
| 8213 | memprintf(err, "%s: invalid number", args[0]); |
| 8214 | return -1; |
| 8215 | } |
| 8216 | return 0; |
| 8217 | } |
| 8218 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8219 | 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] | 8220 | const struct proxy *defpx, const char *file, int line, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8221 | char **err) |
| 8222 | { |
| 8223 | char *error; |
| 8224 | |
| 8225 | if (*(args[1]) == 0) { |
| 8226 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 8227 | return -1; |
| 8228 | } |
| 8229 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 8230 | if (*error != '\0') { |
| 8231 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 8232 | return -1; |
| 8233 | } |
| 8234 | return 0; |
| 8235 | } |
| 8236 | |
| 8237 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8238 | /* This function is called by the main configuration key "lua-load". It loads and |
| 8239 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 8240 | * the main lua entry point. |
| 8241 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8242 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 8243 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8244 | * |
| 8245 | * In some error case, LUA set an error message in top of the stack. This function |
| 8246 | * 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] | 8247 | * |
| 8248 | * This function can fail with an abort() due to an Lua critical error. |
| 8249 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 8250 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8251 | */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8252 | static int hlua_load_state(char *filename, lua_State *L, char **err) |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8253 | { |
| 8254 | int error; |
| 8255 | |
| 8256 | /* Just load and compile the file. */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8257 | error = luaL_loadfile(L, filename); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8258 | if (error) { |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8259 | memprintf(err, "error in Lua file '%s': %s", filename, lua_tostring(L, -1)); |
| 8260 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8261 | return -1; |
| 8262 | } |
| 8263 | |
| 8264 | /* If no syntax error where detected, execute the code. */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8265 | error = lua_pcall(L, 0, LUA_MULTRET, 0); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8266 | switch (error) { |
| 8267 | case LUA_OK: |
| 8268 | break; |
| 8269 | case LUA_ERRRUN: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8270 | memprintf(err, "Lua runtime error: %s\n", lua_tostring(L, -1)); |
| 8271 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8272 | return -1; |
| 8273 | case LUA_ERRMEM: |
Thierry Fournier | de6145f | 2020-11-29 00:55:53 +0100 | [diff] [blame] | 8274 | memprintf(err, "Lua out of memory error\n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8275 | return -1; |
| 8276 | case LUA_ERRERR: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8277 | memprintf(err, "Lua message handler error: %s\n", lua_tostring(L, -1)); |
| 8278 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8279 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 8280 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8281 | case LUA_ERRGCMM: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8282 | memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(L, -1)); |
| 8283 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8284 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 8285 | #endif |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8286 | default: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8287 | memprintf(err, "Lua unknown error: %s\n", lua_tostring(L, -1)); |
| 8288 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8289 | return -1; |
| 8290 | } |
| 8291 | |
| 8292 | return 0; |
| 8293 | } |
| 8294 | |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8295 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 8296 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8297 | char **err) |
| 8298 | { |
| 8299 | if (*(args[1]) == 0) { |
| 8300 | memprintf(err, "'%s' expects a file name as parameter.\n", args[0]); |
| 8301 | return -1; |
| 8302 | } |
| 8303 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8304 | /* loading for global state */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8305 | hlua_state_id = 0; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8306 | ha_set_tid(0); |
Thierry Fournier | afc63e2 | 2020-11-28 17:06:51 +0100 | [diff] [blame] | 8307 | return hlua_load_state(args[1], hlua_states[0], err); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 8308 | } |
| 8309 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8310 | 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] | 8311 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8312 | char **err) |
| 8313 | { |
| 8314 | int len; |
| 8315 | |
| 8316 | if (*(args[1]) == 0) { |
| 8317 | memprintf(err, "'%s' expects a file as parameter.\n", args[0]); |
| 8318 | return -1; |
| 8319 | } |
| 8320 | |
| 8321 | if (per_thread_load == NULL) { |
| 8322 | /* allocate the first entry large enough to store the final NULL */ |
| 8323 | per_thread_load = calloc(1, sizeof(*per_thread_load)); |
| 8324 | if (per_thread_load == NULL) { |
| 8325 | memprintf(err, "out of memory error"); |
| 8326 | return -1; |
| 8327 | } |
| 8328 | } |
| 8329 | |
| 8330 | /* count used entries */ |
| 8331 | for (len = 0; per_thread_load[len] != NULL; len++) |
| 8332 | ; |
| 8333 | |
| 8334 | per_thread_load = realloc(per_thread_load, (len + 2) * sizeof(*per_thread_load)); |
| 8335 | if (per_thread_load == NULL) { |
| 8336 | memprintf(err, "out of memory error"); |
| 8337 | return -1; |
| 8338 | } |
| 8339 | |
| 8340 | per_thread_load[len] = strdup(args[1]); |
| 8341 | per_thread_load[len + 1] = NULL; |
| 8342 | |
| 8343 | if (per_thread_load[len] == NULL) { |
| 8344 | memprintf(err, "out of memory error"); |
| 8345 | return -1; |
| 8346 | } |
| 8347 | |
| 8348 | /* loading for thread 1 only */ |
| 8349 | hlua_state_id = 1; |
| 8350 | ha_set_tid(0); |
| 8351 | return hlua_load_state(args[1], hlua_states[1], err); |
| 8352 | } |
| 8353 | |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 8354 | /* Prepend the given <path> followed by a semicolon to the `package.<type>` variable |
| 8355 | * in the given <ctx>. |
| 8356 | */ |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 8357 | static int hlua_prepend_path(lua_State *L, char *type, char *path) |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 8358 | { |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 8359 | lua_getglobal(L, "package"); /* push package variable */ |
| 8360 | lua_pushstring(L, path); /* push given path */ |
| 8361 | lua_pushstring(L, ";"); /* push semicolon */ |
| 8362 | lua_getfield(L, -3, type); /* push old path */ |
| 8363 | lua_concat(L, 3); /* concatenate to new path */ |
| 8364 | lua_setfield(L, -2, type); /* store new path */ |
| 8365 | lua_pop(L, 1); /* pop package variable */ |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 8366 | |
| 8367 | return 0; |
| 8368 | } |
| 8369 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8370 | 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] | 8371 | const struct proxy *defpx, const char *file, int line, |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8372 | char **err) |
| 8373 | { |
| 8374 | char *path; |
| 8375 | char *type = "path"; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8376 | struct prepend_path *p = NULL; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8377 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8378 | if (too_many_args(2, args, err, NULL)) { |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8379 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8380 | } |
| 8381 | |
| 8382 | if (!(*args[1])) { |
| 8383 | memprintf(err, "'%s' expects to receive a <path> as argument", args[0]); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8384 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8385 | } |
| 8386 | path = args[1]; |
| 8387 | |
| 8388 | if (*args[2]) { |
| 8389 | if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) { |
| 8390 | 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] | 8391 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8392 | } |
| 8393 | type = args[2]; |
| 8394 | } |
| 8395 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8396 | p = calloc(1, sizeof(*p)); |
| 8397 | if (p == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 8398 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8399 | goto err; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8400 | } |
| 8401 | p->path = strdup(path); |
| 8402 | if (p->path == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 8403 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8404 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8405 | } |
| 8406 | p->type = strdup(type); |
| 8407 | if (p->type == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 8408 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8409 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8410 | } |
| 8411 | LIST_ADDQ(&prepend_path_list, &p->l); |
| 8412 | |
| 8413 | hlua_prepend_path(hlua_states[0], type, path); |
| 8414 | hlua_prepend_path(hlua_states[1], type, path); |
| 8415 | return 0; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 8416 | |
| 8417 | err2: |
| 8418 | free(p->type); |
| 8419 | free(p->path); |
| 8420 | err: |
| 8421 | free(p); |
| 8422 | return -1; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8423 | } |
| 8424 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8425 | /* configuration keywords declaration */ |
| 8426 | static struct cfg_kw_list cfg_kws = {{ },{ |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 8427 | { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8428 | { CFG_GLOBAL, "lua-load", hlua_load }, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8429 | { CFG_GLOBAL, "lua-load-per-thread", hlua_load_per_thread }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8430 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 8431 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 8432 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8433 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8434 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8435 | { 0, NULL, NULL }, |
| 8436 | }}; |
| 8437 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 8438 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 8439 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 8440 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8441 | /* This function can fail with an abort() due to an Lua critical error. |
| 8442 | * We are in the initialisation process of HAProxy, this abort() is |
| 8443 | * tolerated. |
| 8444 | */ |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8445 | int hlua_post_init_state(lua_State *L) |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8446 | { |
| 8447 | struct hlua_init_function *init; |
| 8448 | const char *msg; |
| 8449 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8450 | const char *error; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8451 | const char *kind; |
| 8452 | const char *trace; |
| 8453 | int return_status = 1; |
| 8454 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 8455 | int nres; |
| 8456 | #endif |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8457 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8458 | /* disable memory limit checks if limit is not set */ |
| 8459 | if (!hlua_global_allocator.limit) |
| 8460 | hlua_global_allocator.limit = ~hlua_global_allocator.limit; |
| 8461 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 8462 | /* Call post initialisation function in safe environment. */ |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8463 | if (setjmp(safe_ljmp_env) != 0) { |
| 8464 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8465 | if (lua_type(L, -1) == LUA_TSTRING) |
| 8466 | error = lua_tostring(L, -1); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8467 | else |
| 8468 | error = "critical error"; |
| 8469 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 8470 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8471 | } else { |
| 8472 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8473 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 8474 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8475 | hlua_fcn_post_init(L); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8476 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8477 | list_for_each_entry(init, &hlua_init_functions[hlua_state_id], l) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8478 | lua_rawgeti(L, LUA_REGISTRYINDEX, init->function_ref); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8479 | |
| 8480 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8481 | ret = lua_resume(L, L, 0, &nres); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8482 | #else |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8483 | ret = lua_resume(L, L, 0); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8484 | #endif |
| 8485 | kind = NULL; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8486 | switch (ret) { |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8487 | |
| 8488 | case LUA_OK: |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8489 | lua_pop(L, -1); |
Thierry Fournier | 13d08b7 | 2020-11-28 11:02:58 +0100 | [diff] [blame] | 8490 | break; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8491 | |
| 8492 | case LUA_ERRERR: |
| 8493 | kind = "message handler error"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8494 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8495 | case LUA_ERRRUN: |
| 8496 | if (!kind) |
| 8497 | kind = "runtime error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8498 | msg = lua_tostring(L, -1); |
| 8499 | lua_settop(L, 0); /* Empty the stack. */ |
| 8500 | lua_pop(L, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 8501 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8502 | if (msg) |
| 8503 | ha_alert("Lua init: %s: '%s' from %s\n", kind, msg, trace); |
| 8504 | else |
| 8505 | ha_alert("Lua init: unknown %s from %s\n", kind, trace); |
| 8506 | return_status = 0; |
| 8507 | break; |
| 8508 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8509 | default: |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8510 | /* Unknown error */ |
| 8511 | kind = "Unknown error"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8512 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8513 | case LUA_YIELD: |
| 8514 | /* yield is not configured at this step, this state doesn't happen */ |
| 8515 | if (!kind) |
| 8516 | kind = "yield not allowed"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8517 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8518 | case LUA_ERRMEM: |
| 8519 | if (!kind) |
| 8520 | kind = "out of memory error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8521 | lua_settop(L, 0); |
| 8522 | lua_pop(L, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 8523 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8524 | ha_alert("Lua init: %s: %s\n", kind, trace); |
| 8525 | return_status = 0; |
| 8526 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8527 | } |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8528 | if (!return_status) |
| 8529 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8530 | } |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8531 | |
| 8532 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 8533 | return return_status; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8534 | } |
| 8535 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8536 | int hlua_post_init() |
| 8537 | { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8538 | int ret; |
| 8539 | int i; |
| 8540 | int errors; |
| 8541 | char *err = NULL; |
| 8542 | struct hlua_function *fcn; |
| 8543 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8544 | #if USE_OPENSSL |
| 8545 | /* Initialize SSL server. */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 8546 | if (socket_ssl->xprt->prepare_srv) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8547 | int saved_used_backed = global.ssl_used_backend; |
| 8548 | // don't affect maxconn automatic computation |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 8549 | socket_ssl->xprt->prepare_srv(socket_ssl); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8550 | global.ssl_used_backend = saved_used_backed; |
| 8551 | } |
| 8552 | #endif |
| 8553 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8554 | /* Perform post init of common thread */ |
| 8555 | hlua_state_id = 0; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8556 | ha_set_tid(0); |
| 8557 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 8558 | if (ret == 0) |
| 8559 | return 0; |
| 8560 | |
| 8561 | /* init remaining lua states and load files */ |
| 8562 | for (hlua_state_id = 2; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 8563 | |
| 8564 | /* set thread context */ |
| 8565 | ha_set_tid(hlua_state_id - 1); |
| 8566 | |
| 8567 | /* Init lua state */ |
| 8568 | hlua_states[hlua_state_id] = hlua_init_state(hlua_state_id); |
| 8569 | |
| 8570 | /* Load lua files */ |
| 8571 | for (i = 0; per_thread_load && per_thread_load[i]; i++) { |
| 8572 | ret = hlua_load_state(per_thread_load[i], hlua_states[hlua_state_id], &err); |
| 8573 | if (ret != 0) { |
| 8574 | ha_alert("Lua init: %s\n", err); |
| 8575 | return 0; |
| 8576 | } |
| 8577 | } |
| 8578 | } |
| 8579 | |
| 8580 | /* Reset thread context */ |
| 8581 | ha_set_tid(0); |
| 8582 | |
| 8583 | /* Execute post init for all states */ |
| 8584 | for (hlua_state_id = 1; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 8585 | |
| 8586 | /* set thread context */ |
| 8587 | ha_set_tid(hlua_state_id - 1); |
| 8588 | |
| 8589 | /* run post init */ |
| 8590 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 8591 | if (ret == 0) |
| 8592 | return 0; |
| 8593 | } |
| 8594 | |
| 8595 | /* Reset thread context */ |
| 8596 | ha_set_tid(0); |
| 8597 | |
| 8598 | /* control functions registering. Each function must have: |
| 8599 | * - only the function_ref[0] set positive and all other to -1 |
| 8600 | * - only the function_ref[0] set to -1 and all other positive |
| 8601 | * This ensure a same reference is not used both in shared |
| 8602 | * lua state and thread dedicated lua state. Note: is the case |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 8603 | * reach, the shared state is priority, but the bug will be |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8604 | * complicated to found for the end user. |
| 8605 | */ |
| 8606 | errors = 0; |
| 8607 | list_for_each_entry(fcn, &referenced_functions, l) { |
| 8608 | ret = 0; |
| 8609 | for (i = 1; i < global.nbthread + 1; i++) { |
| 8610 | if (fcn->function_ref[i] == -1) |
| 8611 | ret--; |
| 8612 | else |
| 8613 | ret++; |
| 8614 | } |
| 8615 | if (abs(ret) != global.nbthread) { |
| 8616 | ha_alert("Lua function '%s' is not referenced in all thread. " |
| 8617 | "Expect function in all thread or in none thread.\n", fcn->name); |
| 8618 | errors++; |
| 8619 | continue; |
| 8620 | } |
| 8621 | |
| 8622 | if ((fcn->function_ref[0] == -1) == (ret < 0)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 8623 | ha_alert("Lua function '%s' is referenced both ins shared Lua context (through lua-load) " |
| 8624 | "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] | 8625 | "exclusive.\n", fcn->name); |
| 8626 | errors++; |
| 8627 | } |
| 8628 | } |
| 8629 | |
| 8630 | if (errors > 0) |
| 8631 | return 0; |
| 8632 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 8633 | /* 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] | 8634 | * -1 in order to have probably a segfault if someone use it |
| 8635 | */ |
| 8636 | hlua_state_id = -1; |
| 8637 | |
| 8638 | return 1; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 8639 | } |
| 8640 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8641 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 8642 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 8643 | * 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] | 8644 | * allocation. <nsize> is the requested new size. A new allocation is |
| 8645 | * indicated by <ptr> being NULL. A free is indicated by <nsize> being |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8646 | * zero. This one verifies that the limits are respected but is optimized |
| 8647 | * 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] | 8648 | */ |
| 8649 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 8650 | { |
| 8651 | struct hlua_mem_allocator *zone = ud; |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8652 | size_t limit, old, new; |
| 8653 | |
Tim Duesterhus | 2258652 | 2021-01-08 10:35:33 +0100 | [diff] [blame] | 8654 | if (unlikely(!ptr && !nsize)) |
| 8655 | return NULL; |
| 8656 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8657 | /* a limit of ~0 means unlimited and boot complete, so there's no need |
| 8658 | * for accounting anymore. |
| 8659 | */ |
Christopher Faulet | cc2c4f8 | 2021-03-24 14:52:24 +0100 | [diff] [blame] | 8660 | if (likely(~zone->limit == 0)) |
| 8661 | return realloc(ptr, nsize); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8662 | |
Willy Tarreau | d36c7fa | 2020-12-02 12:26:29 +0100 | [diff] [blame] | 8663 | if (!ptr) |
| 8664 | osize = 0; |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8665 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8666 | /* enforce strict limits across all threads */ |
| 8667 | limit = zone->limit; |
| 8668 | old = _HA_ATOMIC_LOAD(&zone->allocated); |
| 8669 | do { |
| 8670 | new = old + nsize - osize; |
| 8671 | if (unlikely(nsize && limit && new > limit)) |
| 8672 | return NULL; |
| 8673 | } while (!_HA_ATOMIC_CAS(&zone->allocated, &old, new)); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8674 | |
| 8675 | ptr = realloc(ptr, nsize); |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 8676 | |
| 8677 | if (unlikely(!ptr && nsize)) // failed |
| 8678 | _HA_ATOMIC_SUB(&zone->allocated, nsize - osize); |
| 8679 | |
| 8680 | __ha_barrier_atomic_store(); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8681 | return ptr; |
| 8682 | } |
| 8683 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 8684 | /* 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] | 8685 | * We are in the initialisation process of HAProxy, this abort() is |
| 8686 | * tolerated. |
| 8687 | */ |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 8688 | lua_State *hlua_init_state(int thread_num) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8689 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8690 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8691 | int idx; |
| 8692 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8693 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8694 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8695 | const char *error_msg; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8696 | void **context; |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8697 | lua_State *L; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8698 | struct prepend_path *pp; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8699 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8700 | /* Init main lua stack. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8701 | L = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8702 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8703 | /* Initialise Lua context to NULL */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8704 | context = lua_getextraspace(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8705 | *context = NULL; |
| 8706 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8707 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8708 | * the Lua function can fail with an abort. We are in the initialisation |
| 8709 | * process of HAProxy, this abort() is tolerated. |
| 8710 | */ |
| 8711 | |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8712 | /* Call post initialisation function in safe environment. */ |
| 8713 | if (setjmp(safe_ljmp_env) != 0) { |
| 8714 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8715 | if (lua_type(L, -1) == LUA_TSTRING) |
| 8716 | error_msg = lua_tostring(L, -1); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 8717 | else |
| 8718 | error_msg = "critical error"; |
| 8719 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
| 8720 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 8721 | } else { |
| 8722 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 8723 | } |
| 8724 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8725 | /* Initialise lua. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8726 | luaL_openlibs(L); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 8727 | #define HLUA_PREPEND_PATH_TOSTRING1(x) #x |
| 8728 | #define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x) |
| 8729 | #ifdef HLUA_PREPEND_PATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8730 | hlua_prepend_path(L, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 8731 | #endif |
| 8732 | #ifdef HLUA_PREPEND_CPATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8733 | hlua_prepend_path(L, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 8734 | #endif |
| 8735 | #undef HLUA_PREPEND_PATH_TOSTRING |
| 8736 | #undef HLUA_PREPEND_PATH_TOSTRING1 |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8737 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8738 | /* Apply configured prepend path */ |
| 8739 | list_for_each_entry(pp, &prepend_path_list, l) |
| 8740 | hlua_prepend_path(L, pp->type, pp->path); |
| 8741 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8742 | /* |
| 8743 | * |
| 8744 | * Create "core" object. |
| 8745 | * |
| 8746 | */ |
| 8747 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 8748 | /* This table entry is the object "core" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8749 | lua_newtable(L); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8750 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 8751 | /* set the thread id */ |
| 8752 | hlua_class_const_int(L, "thread", thread_num); |
| 8753 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8754 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8755 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8756 | hlua_class_const_int(L, log_levels[i], i); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8757 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8758 | /* Register special functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8759 | hlua_class_function(L, "register_init", hlua_register_init); |
| 8760 | hlua_class_function(L, "register_task", hlua_register_task); |
| 8761 | hlua_class_function(L, "register_fetches", hlua_register_fetches); |
| 8762 | hlua_class_function(L, "register_converters", hlua_register_converters); |
| 8763 | hlua_class_function(L, "register_action", hlua_register_action); |
| 8764 | hlua_class_function(L, "register_service", hlua_register_service); |
| 8765 | hlua_class_function(L, "register_cli", hlua_register_cli); |
| 8766 | hlua_class_function(L, "yield", hlua_yield); |
| 8767 | hlua_class_function(L, "set_nice", hlua_set_nice); |
| 8768 | hlua_class_function(L, "sleep", hlua_sleep); |
| 8769 | hlua_class_function(L, "msleep", hlua_msleep); |
| 8770 | hlua_class_function(L, "add_acl", hlua_add_acl); |
| 8771 | hlua_class_function(L, "del_acl", hlua_del_acl); |
| 8772 | hlua_class_function(L, "set_map", hlua_set_map); |
| 8773 | hlua_class_function(L, "del_map", hlua_del_map); |
| 8774 | hlua_class_function(L, "tcp", hlua_socket_new); |
| 8775 | hlua_class_function(L, "log", hlua_log); |
| 8776 | hlua_class_function(L, "Debug", hlua_log_debug); |
| 8777 | hlua_class_function(L, "Info", hlua_log_info); |
| 8778 | hlua_class_function(L, "Warning", hlua_log_warning); |
| 8779 | hlua_class_function(L, "Alert", hlua_log_alert); |
| 8780 | hlua_class_function(L, "done", hlua_done); |
| 8781 | hlua_fcn_reg_core_fcn(L); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8782 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8783 | lua_setglobal(L, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8784 | |
| 8785 | /* |
| 8786 | * |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8787 | * Create "act" object. |
| 8788 | * |
| 8789 | */ |
| 8790 | |
| 8791 | /* This table entry is the object "act" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8792 | lua_newtable(L); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8793 | |
| 8794 | /* push action return constants */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8795 | hlua_class_const_int(L, "CONTINUE", ACT_RET_CONT); |
| 8796 | hlua_class_const_int(L, "STOP", ACT_RET_STOP); |
| 8797 | hlua_class_const_int(L, "YIELD", ACT_RET_YIELD); |
| 8798 | hlua_class_const_int(L, "ERROR", ACT_RET_ERR); |
| 8799 | hlua_class_const_int(L, "DONE", ACT_RET_DONE); |
| 8800 | hlua_class_const_int(L, "DENY", ACT_RET_DENY); |
| 8801 | hlua_class_const_int(L, "ABORT", ACT_RET_ABRT); |
| 8802 | hlua_class_const_int(L, "INVALID", ACT_RET_INV); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8803 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8804 | hlua_class_function(L, "wake_time", hlua_set_wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8805 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8806 | lua_setglobal(L, "act"); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8807 | |
| 8808 | /* |
| 8809 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8810 | * Register class Map |
| 8811 | * |
| 8812 | */ |
| 8813 | |
| 8814 | /* This table entry is the object "Map" base. */ |
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 | |
| 8817 | /* register pattern types. */ |
| 8818 | for (i=0; i<PAT_MATCH_NUM; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8819 | hlua_class_const_int(L, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8820 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8821 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8822 | hlua_class_const_int(L, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8823 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8824 | |
| 8825 | /* register constructor. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8826 | hlua_class_function(L, "new", hlua_map_new); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8827 | |
| 8828 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8829 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8830 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8831 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8832 | lua_pushstring(L, "__index"); |
| 8833 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8834 | |
| 8835 | /* Register . */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8836 | hlua_class_function(L, "lookup", hlua_map_lookup); |
| 8837 | hlua_class_function(L, "slookup", hlua_map_slookup); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8838 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8839 | lua_rawset(L, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8840 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8841 | /* Register previous table in the registry with reference and named entry. |
| 8842 | * The function hlua_register_metatable() pops the stack, so we |
| 8843 | * previously create a copy of the table. |
| 8844 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8845 | lua_pushvalue(L, -1); /* Copy the -1 entry and push it on the stack. */ |
| 8846 | class_map_ref = hlua_register_metatable(L, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8847 | |
| 8848 | /* Assign the metatable to the mai Map object. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8849 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8850 | |
| 8851 | /* Set a name to the table. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8852 | lua_setglobal(L, "Map"); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8853 | |
| 8854 | /* |
| 8855 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8856 | * Register class Channel |
| 8857 | * |
| 8858 | */ |
| 8859 | |
| 8860 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8861 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8862 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8863 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8864 | lua_pushstring(L, "__index"); |
| 8865 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8866 | |
| 8867 | /* Register . */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8868 | hlua_class_function(L, "get", hlua_channel_get); |
| 8869 | hlua_class_function(L, "dup", hlua_channel_dup); |
| 8870 | hlua_class_function(L, "getline", hlua_channel_getline); |
| 8871 | hlua_class_function(L, "set", hlua_channel_set); |
| 8872 | hlua_class_function(L, "append", hlua_channel_append); |
| 8873 | hlua_class_function(L, "send", hlua_channel_send); |
| 8874 | hlua_class_function(L, "forward", hlua_channel_forward); |
| 8875 | hlua_class_function(L, "get_in_len", hlua_channel_get_in_len); |
| 8876 | hlua_class_function(L, "get_out_len", hlua_channel_get_out_len); |
| 8877 | hlua_class_function(L, "is_full", hlua_channel_is_full); |
| 8878 | hlua_class_function(L, "is_resp", hlua_channel_is_resp); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8879 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8880 | lua_rawset(L, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8881 | |
| 8882 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8883 | class_channel_ref = hlua_register_metatable(L, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8884 | |
| 8885 | /* |
| 8886 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8887 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8888 | * |
| 8889 | */ |
| 8890 | |
| 8891 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8892 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8893 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8894 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8895 | lua_pushstring(L, "__index"); |
| 8896 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8897 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8898 | /* Browse existing fetches and create the associated |
| 8899 | * object method. |
| 8900 | */ |
| 8901 | sf = NULL; |
| 8902 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8903 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 8904 | * by an underscore. |
| 8905 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8906 | strncpy(trash.area, sf->kw, trash.size); |
| 8907 | trash.area[trash.size - 1] = '\0'; |
| 8908 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8909 | if (*p == '.' || *p == '-' || *p == '+') |
| 8910 | *p = '_'; |
| 8911 | |
| 8912 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8913 | lua_pushstring(L, trash.area); |
| 8914 | lua_pushlightuserdata(L, sf); |
| 8915 | lua_pushcclosure(L, hlua_run_sample_fetch, 1); |
| 8916 | lua_rawset(L, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8917 | } |
| 8918 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8919 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8920 | |
| 8921 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8922 | class_fetches_ref = hlua_register_metatable(L, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8923 | |
| 8924 | /* |
| 8925 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8926 | * Register class Converters |
| 8927 | * |
| 8928 | */ |
| 8929 | |
| 8930 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8931 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8932 | |
| 8933 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8934 | lua_pushstring(L, "__index"); |
| 8935 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8936 | |
| 8937 | /* Browse existing converters and create the associated |
| 8938 | * object method. |
| 8939 | */ |
| 8940 | sc = NULL; |
| 8941 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8942 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 8943 | * by an underscore. |
| 8944 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8945 | strncpy(trash.area, sc->kw, trash.size); |
| 8946 | trash.area[trash.size - 1] = '\0'; |
| 8947 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8948 | if (*p == '.' || *p == '-' || *p == '+') |
| 8949 | *p = '_'; |
| 8950 | |
| 8951 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8952 | lua_pushstring(L, trash.area); |
| 8953 | lua_pushlightuserdata(L, sc); |
| 8954 | lua_pushcclosure(L, hlua_run_sample_conv, 1); |
| 8955 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8956 | } |
| 8957 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8958 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8959 | |
| 8960 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8961 | class_converters_ref = hlua_register_metatable(L, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8962 | |
| 8963 | /* |
| 8964 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8965 | * Register class HTTP |
| 8966 | * |
| 8967 | */ |
| 8968 | |
| 8969 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8970 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8971 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8972 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8973 | lua_pushstring(L, "__index"); |
| 8974 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8975 | |
| 8976 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8977 | hlua_class_function(L, "req_get_headers",hlua_http_req_get_headers); |
| 8978 | hlua_class_function(L, "req_del_header", hlua_http_req_del_hdr); |
| 8979 | hlua_class_function(L, "req_rep_header", hlua_http_req_rep_hdr); |
| 8980 | hlua_class_function(L, "req_rep_value", hlua_http_req_rep_val); |
| 8981 | hlua_class_function(L, "req_add_header", hlua_http_req_add_hdr); |
| 8982 | hlua_class_function(L, "req_set_header", hlua_http_req_set_hdr); |
| 8983 | hlua_class_function(L, "req_set_method", hlua_http_req_set_meth); |
| 8984 | hlua_class_function(L, "req_set_path", hlua_http_req_set_path); |
| 8985 | hlua_class_function(L, "req_set_query", hlua_http_req_set_query); |
| 8986 | hlua_class_function(L, "req_set_uri", hlua_http_req_set_uri); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8987 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8988 | hlua_class_function(L, "res_get_headers",hlua_http_res_get_headers); |
| 8989 | hlua_class_function(L, "res_del_header", hlua_http_res_del_hdr); |
| 8990 | hlua_class_function(L, "res_rep_header", hlua_http_res_rep_hdr); |
| 8991 | hlua_class_function(L, "res_rep_value", hlua_http_res_rep_val); |
| 8992 | hlua_class_function(L, "res_add_header", hlua_http_res_add_hdr); |
| 8993 | hlua_class_function(L, "res_set_header", hlua_http_res_set_hdr); |
| 8994 | hlua_class_function(L, "res_set_status", hlua_http_res_set_status); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8995 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8996 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8997 | |
| 8998 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 8999 | class_http_ref = hlua_register_metatable(L, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 9000 | |
| 9001 | /* |
| 9002 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9003 | * Register class AppletTCP |
| 9004 | * |
| 9005 | */ |
| 9006 | |
| 9007 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9008 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9009 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9010 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9011 | lua_pushstring(L, "__index"); |
| 9012 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9013 | |
| 9014 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9015 | hlua_class_function(L, "getline", hlua_applet_tcp_getline); |
| 9016 | hlua_class_function(L, "receive", hlua_applet_tcp_recv); |
| 9017 | hlua_class_function(L, "send", hlua_applet_tcp_send); |
| 9018 | hlua_class_function(L, "set_priv", hlua_applet_tcp_set_priv); |
| 9019 | hlua_class_function(L, "get_priv", hlua_applet_tcp_get_priv); |
| 9020 | hlua_class_function(L, "set_var", hlua_applet_tcp_set_var); |
| 9021 | hlua_class_function(L, "unset_var", hlua_applet_tcp_unset_var); |
| 9022 | hlua_class_function(L, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9023 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9024 | lua_settable(L, -3); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9025 | |
| 9026 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9027 | class_applet_tcp_ref = hlua_register_metatable(L, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9028 | |
| 9029 | /* |
| 9030 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9031 | * Register class AppletHTTP |
| 9032 | * |
| 9033 | */ |
| 9034 | |
| 9035 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9036 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9037 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9038 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9039 | lua_pushstring(L, "__index"); |
| 9040 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9041 | |
| 9042 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9043 | hlua_class_function(L, "set_priv", hlua_applet_http_set_priv); |
| 9044 | hlua_class_function(L, "get_priv", hlua_applet_http_get_priv); |
| 9045 | hlua_class_function(L, "set_var", hlua_applet_http_set_var); |
| 9046 | hlua_class_function(L, "unset_var", hlua_applet_http_unset_var); |
| 9047 | hlua_class_function(L, "get_var", hlua_applet_http_get_var); |
| 9048 | hlua_class_function(L, "getline", hlua_applet_http_getline); |
| 9049 | hlua_class_function(L, "receive", hlua_applet_http_recv); |
| 9050 | hlua_class_function(L, "send", hlua_applet_http_send); |
| 9051 | hlua_class_function(L, "add_header", hlua_applet_http_addheader); |
| 9052 | hlua_class_function(L, "set_status", hlua_applet_http_status); |
| 9053 | hlua_class_function(L, "start_response", hlua_applet_http_start_response); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9054 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9055 | lua_settable(L, -3); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9056 | |
| 9057 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9058 | class_applet_http_ref = hlua_register_metatable(L, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9059 | |
| 9060 | /* |
| 9061 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9062 | * Register class TXN |
| 9063 | * |
| 9064 | */ |
| 9065 | |
| 9066 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9067 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9068 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9069 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9070 | lua_pushstring(L, "__index"); |
| 9071 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 9072 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 9073 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9074 | hlua_class_function(L, "set_priv", hlua_set_priv); |
| 9075 | hlua_class_function(L, "get_priv", hlua_get_priv); |
| 9076 | hlua_class_function(L, "set_var", hlua_set_var); |
| 9077 | hlua_class_function(L, "unset_var", hlua_unset_var); |
| 9078 | hlua_class_function(L, "get_var", hlua_get_var); |
| 9079 | hlua_class_function(L, "done", hlua_txn_done); |
| 9080 | hlua_class_function(L, "reply", hlua_txn_reply_new); |
| 9081 | hlua_class_function(L, "set_loglevel", hlua_txn_set_loglevel); |
| 9082 | hlua_class_function(L, "set_tos", hlua_txn_set_tos); |
| 9083 | hlua_class_function(L, "set_mark", hlua_txn_set_mark); |
| 9084 | hlua_class_function(L, "set_priority_class", hlua_txn_set_priority_class); |
| 9085 | hlua_class_function(L, "set_priority_offset", hlua_txn_set_priority_offset); |
| 9086 | hlua_class_function(L, "deflog", hlua_txn_deflog); |
| 9087 | hlua_class_function(L, "log", hlua_txn_log); |
| 9088 | hlua_class_function(L, "Debug", hlua_txn_log_debug); |
| 9089 | hlua_class_function(L, "Info", hlua_txn_log_info); |
| 9090 | hlua_class_function(L, "Warning", hlua_txn_log_warning); |
| 9091 | hlua_class_function(L, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 9092 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9093 | lua_rawset(L, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 9094 | |
| 9095 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9096 | class_txn_ref = hlua_register_metatable(L, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9097 | |
| 9098 | /* |
| 9099 | * |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 9100 | * Register class reply |
| 9101 | * |
| 9102 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9103 | lua_newtable(L); |
| 9104 | lua_pushstring(L, "__index"); |
| 9105 | lua_newtable(L); |
| 9106 | hlua_class_function(L, "set_status", hlua_txn_reply_set_status); |
| 9107 | hlua_class_function(L, "add_header", hlua_txn_reply_add_header); |
| 9108 | hlua_class_function(L, "del_header", hlua_txn_reply_del_header); |
| 9109 | hlua_class_function(L, "set_body", hlua_txn_reply_set_body); |
| 9110 | lua_settable(L, -3); /* Sets the __index entry. */ |
| 9111 | class_txn_reply_ref = luaL_ref(L, LUA_REGISTRYINDEX); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 9112 | |
| 9113 | |
| 9114 | /* |
| 9115 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9116 | * Register class Socket |
| 9117 | * |
| 9118 | */ |
| 9119 | |
| 9120 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9121 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9122 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9123 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9124 | lua_pushstring(L, "__index"); |
| 9125 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9126 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 9127 | #ifdef USE_OPENSSL |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9128 | hlua_class_function(L, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 9129 | #endif |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9130 | hlua_class_function(L, "connect", hlua_socket_connect); |
| 9131 | hlua_class_function(L, "send", hlua_socket_send); |
| 9132 | hlua_class_function(L, "receive", hlua_socket_receive); |
| 9133 | hlua_class_function(L, "close", hlua_socket_close); |
| 9134 | hlua_class_function(L, "getpeername", hlua_socket_getpeername); |
| 9135 | hlua_class_function(L, "getsockname", hlua_socket_getsockname); |
| 9136 | hlua_class_function(L, "setoption", hlua_socket_setoption); |
| 9137 | hlua_class_function(L, "settimeout", hlua_socket_settimeout); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9138 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9139 | 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] | 9140 | |
| 9141 | /* Register the garbage collector entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9142 | lua_pushstring(L, "__gc"); |
| 9143 | lua_pushcclosure(L, hlua_socket_gc, 0); |
| 9144 | 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] | 9145 | |
| 9146 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 9147 | class_socket_ref = hlua_register_metatable(L, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9148 | |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 9149 | lua_atpanic(L, hlua_panic_safe); |
| 9150 | |
| 9151 | return L; |
| 9152 | } |
| 9153 | |
| 9154 | void hlua_init(void) { |
| 9155 | int i; |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 9156 | char *errmsg; |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 9157 | #ifdef USE_OPENSSL |
| 9158 | struct srv_kw *kw; |
| 9159 | int tmp_error; |
| 9160 | char *error; |
| 9161 | char *args[] = { /* SSL client configuration. */ |
| 9162 | "ssl", |
| 9163 | "verify", |
| 9164 | "none", |
| 9165 | NULL |
| 9166 | }; |
| 9167 | #endif |
| 9168 | |
| 9169 | /* Init post init function list head */ |
| 9170 | for (i = 0; i < MAX_THREADS + 1; i++) |
| 9171 | LIST_INIT(&hlua_init_functions[i]); |
| 9172 | |
| 9173 | /* Init state for common/shared lua parts */ |
| 9174 | hlua_state_id = 0; |
| 9175 | ha_set_tid(0); |
| 9176 | hlua_states[0] = hlua_init_state(0); |
| 9177 | |
| 9178 | /* Init state 1 for thread 0. We have at least one thread. */ |
| 9179 | hlua_state_id = 1; |
| 9180 | ha_set_tid(0); |
| 9181 | hlua_states[1] = hlua_init_state(1); |
| 9182 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9183 | /* Proxy and server configuration initialisation. */ |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 9184 | socket_proxy = alloc_new_proxy("LUA-SOCKET", PR_CAP_FE|PR_CAP_BE|PR_CAP_LUA, &errmsg); |
| 9185 | if (!socket_proxy) { |
| 9186 | fprintf(stderr, "Lua init: %s\n", errmsg); |
| 9187 | exit(1); |
| 9188 | } |
| 9189 | proxy_preset_defaults(socket_proxy); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9190 | |
| 9191 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9192 | socket_tcp = new_server(socket_proxy); |
| 9193 | if (!socket_tcp) { |
| 9194 | fprintf(stderr, "Lua init: failed to allocate tcp server socket\n"); |
| 9195 | exit(1); |
| 9196 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9197 | |
| 9198 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9199 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9200 | socket_ssl = new_server(socket_proxy); |
| 9201 | if (!socket_ssl) { |
| 9202 | fprintf(stderr, "Lua init: failed to allocate ssl server socket\n"); |
| 9203 | exit(1); |
| 9204 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9205 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9206 | socket_ssl->use_ssl = 1; |
| 9207 | socket_ssl->xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9208 | |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 9209 | for (i = 0; args[i] != NULL; i++) { |
| 9210 | 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] | 9211 | /* |
| 9212 | * |
| 9213 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9214 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9215 | * features like client certificates and ssl_verify. |
| 9216 | * |
| 9217 | */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9218 | tmp_error = kw->parse(args, &i, socket_proxy, socket_ssl, &error); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9219 | if (tmp_error != 0) { |
| 9220 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 9221 | abort(); /* This must be never arrives because the command line |
| 9222 | not editable by the user. */ |
| 9223 | } |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 9224 | i += kw->skip; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9225 | } |
| 9226 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 9227 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 9228 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 9229 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 9230 | |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 9231 | static void hlua_deinit() |
| 9232 | { |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 9233 | int thr; |
| 9234 | |
| 9235 | for (thr = 0; thr < MAX_THREADS+1; thr++) { |
| 9236 | if (hlua_states[thr]) |
| 9237 | lua_close(hlua_states[thr]); |
| 9238 | } |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 9239 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9240 | free_server(socket_tcp); |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 9241 | |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 9242 | #ifdef USE_OPENSSL |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 9243 | free_server(socket_ssl); |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 9244 | #endif |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 9245 | |
| 9246 | free_proxy(socket_proxy); |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 9247 | } |
| 9248 | |
| 9249 | REGISTER_POST_DEINIT(hlua_deinit); |
| 9250 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9251 | static void hlua_register_build_options(void) |
| 9252 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 9253 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9254 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 9255 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 9256 | hap_register_build_opts(ptr, 1); |
| 9257 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9258 | |
| 9259 | INITCALL0(STG_REGISTER, hlua_register_build_options); |