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> |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 36 | #include <haproxy/filters.h> |
Willy Tarreau | 5413a87 | 2020-06-02 19:33:08 +0200 | [diff] [blame] | 37 | #include <haproxy/h1.h> |
Willy Tarreau | 8641605 | 2020-06-04 09:20:54 +0200 | [diff] [blame] | 38 | #include <haproxy/hlua.h> |
Willy Tarreau | 8c79400 | 2020-06-04 10:05:25 +0200 | [diff] [blame] | 39 | #include <haproxy/hlua_fcn.h> |
Willy Tarreau | c2b1ff0 | 2020-06-04 21:21:03 +0200 | [diff] [blame] | 40 | #include <haproxy/http_ana.h> |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 41 | #include <haproxy/http_client.h> |
Willy Tarreau | 126ba3a | 2020-06-04 18:26:43 +0200 | [diff] [blame] | 42 | #include <haproxy/http_fetch.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 43 | #include <haproxy/http_htx.h> |
Willy Tarreau | c761f84 | 2020-06-04 11:40:28 +0200 | [diff] [blame] | 44 | #include <haproxy/http_rules.h> |
Willy Tarreau | 36979d9 | 2020-06-05 17:27:29 +0200 | [diff] [blame] | 45 | #include <haproxy/log.h> |
Willy Tarreau | 2cd5809 | 2020-06-04 15:10:43 +0200 | [diff] [blame] | 46 | #include <haproxy/map.h> |
Willy Tarreau | 8efbdfb | 2020-06-04 11:29:21 +0200 | [diff] [blame] | 47 | #include <haproxy/obj_type.h> |
Willy Tarreau | 225a90a | 2020-06-04 15:06:28 +0200 | [diff] [blame] | 48 | #include <haproxy/pattern.h> |
Willy Tarreau | 469509b | 2020-06-04 15:13:30 +0200 | [diff] [blame] | 49 | #include <haproxy/payload.h> |
Willy Tarreau | 3d6ee40 | 2021-05-08 20:28:07 +0200 | [diff] [blame] | 50 | #include <haproxy/proxy.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 51 | #include <haproxy/regex.h> |
Willy Tarreau | e6ce10b | 2020-06-04 15:33:47 +0200 | [diff] [blame] | 52 | #include <haproxy/sample.h> |
Willy Tarreau | 198e92a | 2021-03-05 10:23:32 +0100 | [diff] [blame] | 53 | #include <haproxy/server.h> |
Willy Tarreau | 48d25b3 | 2020-06-04 18:58:52 +0200 | [diff] [blame] | 54 | #include <haproxy/session.h> |
Willy Tarreau | 2eec9b5 | 2020-06-04 19:58:55 +0200 | [diff] [blame] | 55 | #include <haproxy/stats-t.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 56 | #include <haproxy/stream.h> |
Willy Tarreau | 5e539c9 | 2020-06-04 20:45:39 +0200 | [diff] [blame] | 57 | #include <haproxy/stream_interface.h> |
Willy Tarreau | cea0e1b | 2020-06-04 17:25:40 +0200 | [diff] [blame] | 58 | #include <haproxy/task.h> |
Willy Tarreau | 8b550af | 2020-06-04 17:42:48 +0200 | [diff] [blame] | 59 | #include <haproxy/tcp_rules.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 60 | #include <haproxy/thread.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 61 | #include <haproxy/tools.h> |
Willy Tarreau | a171892 | 2020-06-04 16:25:31 +0200 | [diff] [blame] | 62 | #include <haproxy/vars.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 63 | #include <haproxy/xref.h> |
| 64 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 65 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 66 | /* Lua uses longjmp to perform yield or throwing errors. This |
| 67 | * macro is used only for identifying the function that can |
| 68 | * not return because a longjmp is executed. |
| 69 | * __LJMP marks a prototype of hlua file that can use longjmp. |
| 70 | * WILL_LJMP() marks an lua function that will use longjmp. |
| 71 | * MAY_LJMP() marks an lua function that may use longjmp. |
| 72 | */ |
| 73 | #define __LJMP |
Willy Tarreau | 4e7cc33 | 2018-10-20 17:45:48 +0200 | [diff] [blame] | 74 | #define WILL_LJMP(func) do { func; my_unreachable(); } while(0) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 75 | #define MAY_LJMP(func) func |
| 76 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 77 | /* This couple of function executes securely some Lua calls outside of |
| 78 | * the lua runtime environment. Each Lua call can return a longjmp |
| 79 | * if it encounter a memory error. |
| 80 | * |
| 81 | * Lua documentation extract: |
| 82 | * |
| 83 | * If an error happens outside any protected environment, Lua calls |
| 84 | * a panic function (see lua_atpanic) and then calls abort, thus |
| 85 | * exiting the host application. Your panic function can avoid this |
| 86 | * exit by never returning (e.g., doing a long jump to your own |
| 87 | * recovery point outside Lua). |
| 88 | * |
| 89 | * The panic function runs as if it were a message handler (see |
| 90 | * §2.3); in particular, the error message is at the top of the |
| 91 | * stack. However, there is no guarantee about stack space. To push |
| 92 | * anything on the stack, the panic function must first check the |
| 93 | * available space (see §4.2). |
| 94 | * |
| 95 | * We must check all the Lua entry point. This includes: |
| 96 | * - The include/proto/hlua.h exported functions |
| 97 | * - the task wrapper function |
| 98 | * - The action wrapper function |
| 99 | * - The converters wrapper function |
| 100 | * - The sample-fetch wrapper functions |
| 101 | * |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 102 | * It is tolerated that the initialisation function returns an abort. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 103 | * Before each Lua abort, an error message is written on stderr. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 104 | * |
| 105 | * The macro SET_SAFE_LJMP initialise the longjmp. The Macro |
| 106 | * RESET_SAFE_LJMP reset the longjmp. These function must be macro |
| 107 | * because they must be exists in the program stack when the longjmp |
| 108 | * is called. |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 109 | * |
| 110 | * Note that the Lua processing is not really thread safe. It provides |
| 111 | * heavy system which consists to add our own lock function in the Lua |
| 112 | * code and recompile the library. This system will probably not accepted |
| 113 | * by maintainers of various distribs. |
| 114 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 115 | * 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] | 116 | * quick looking on the Lua sources displays a lua_lock() a the start |
| 117 | * of function and a lua_unlock() at the end of the function. So I |
| 118 | * conclude that the Lua thread safe mode just perform a mutex around |
| 119 | * all execution. So I prefer to do this in the HAProxy code, it will be |
| 120 | * easier for distro maintainers. |
| 121 | * |
| 122 | * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP |
| 123 | * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful |
| 124 | * to set mutex around these functions. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 125 | */ |
Willy Tarreau | 86abe44 | 2018-11-25 20:12:18 +0100 | [diff] [blame] | 126 | __decl_spinlock(hlua_global_lock); |
Thierry FOURNIER | ffbad79 | 2017-07-12 11:39:04 +0200 | [diff] [blame] | 127 | THREAD_LOCAL jmp_buf safe_ljmp_env; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 128 | static int hlua_panic_safe(lua_State *L) { return 0; } |
Willy Tarreau | 6a51090 | 2021-07-14 19:41:25 +0200 | [diff] [blame] | 129 | static int hlua_panic_ljmp(lua_State *L) { WILL_LJMP(longjmp(safe_ljmp_env, 1)); return 0; } |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 130 | |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 131 | /* This is the chained list of struct hlua_function referenced |
| 132 | * for haproxy action, sample-fetches, converters, cli and |
| 133 | * applet bindings. It is used for a post-initialisation control. |
| 134 | */ |
| 135 | static struct list referenced_functions = LIST_HEAD_INIT(referenced_functions); |
| 136 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 137 | /* This variable is used only during initialization to identify the Lua state |
| 138 | * currently being initialized. 0 is the common lua state, 1 to n are the Lua |
| 139 | * states dedicated to each thread (in this case hlua_state_id==tid+1). |
| 140 | */ |
| 141 | static int hlua_state_id; |
| 142 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 143 | /* This is a NULL-terminated list of lua file which are referenced to load per thread */ |
| 144 | static char **per_thread_load = NULL; |
| 145 | |
| 146 | lua_State *hlua_init_state(int thread_id); |
| 147 | |
Willy Tarreau | 1e7bef1 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 148 | /* This function takes the Lua global lock. Keep this function's visibility |
| 149 | * global so that it can appear in stack dumps and performance profiles! |
| 150 | */ |
| 151 | void lua_take_global_lock() |
| 152 | { |
| 153 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); |
| 154 | } |
| 155 | |
| 156 | static inline void lua_drop_global_lock() |
| 157 | { |
| 158 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); |
| 159 | } |
| 160 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 161 | #define SET_SAFE_LJMP_L(__L, __HLUA) \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 162 | ({ \ |
| 163 | int ret; \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 164 | if ((__HLUA)->state_id == 0) \ |
Willy Tarreau | 1e7bef1 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 165 | lua_take_global_lock(); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 166 | if (setjmp(safe_ljmp_env) != 0) { \ |
| 167 | lua_atpanic(__L, hlua_panic_safe); \ |
| 168 | ret = 0; \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 169 | if ((__HLUA)->state_id == 0) \ |
Willy Tarreau | 1e7bef1 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 170 | lua_drop_global_lock(); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 171 | } else { \ |
| 172 | lua_atpanic(__L, hlua_panic_ljmp); \ |
| 173 | ret = 1; \ |
| 174 | } \ |
| 175 | ret; \ |
| 176 | }) |
| 177 | |
| 178 | /* If we are the last function catching Lua errors, we |
| 179 | * must reset the panic function. |
| 180 | */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 181 | #define RESET_SAFE_LJMP_L(__L, __HLUA) \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 182 | do { \ |
| 183 | lua_atpanic(__L, hlua_panic_safe); \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 184 | if ((__HLUA)->state_id == 0) \ |
Willy Tarreau | 1e7bef1 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 185 | lua_drop_global_lock(); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 186 | } while(0) |
| 187 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 188 | #define SET_SAFE_LJMP(__HLUA) \ |
| 189 | SET_SAFE_LJMP_L((__HLUA)->T, __HLUA) |
| 190 | |
| 191 | #define RESET_SAFE_LJMP(__HLUA) \ |
| 192 | RESET_SAFE_LJMP_L((__HLUA)->T, __HLUA) |
| 193 | |
| 194 | #define SET_SAFE_LJMP_PARENT(__HLUA) \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 195 | SET_SAFE_LJMP_L(hlua_states[(__HLUA)->state_id], __HLUA) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 196 | |
| 197 | #define RESET_SAFE_LJMP_PARENT(__HLUA) \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 198 | RESET_SAFE_LJMP_L(hlua_states[(__HLUA)->state_id], __HLUA) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 199 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 200 | /* Applet status flags */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 201 | #define APPLET_DONE 0x01 /* applet processing is done. */ |
Christopher Faulet | 18c2e8d | 2019-03-01 12:02:08 +0100 | [diff] [blame] | 202 | /* unused: 0x02 */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 203 | #define APPLET_HDR_SENT 0x04 /* Response header sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 204 | /* unused: 0x08, 0x10 */ |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 205 | #define APPLET_HTTP11 0x20 /* Last chunk sent. */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 206 | #define APPLET_RSP_SENT 0x40 /* The response was fully sent */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 207 | |
Thierry Fournier | afc63e2 | 2020-11-28 17:06:51 +0100 | [diff] [blame] | 208 | /* The main Lua execution context. The 0 index is the |
| 209 | * common state shared by all threads. |
| 210 | */ |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 211 | static lua_State *hlua_states[MAX_THREADS + 1]; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 212 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 213 | #define HLUA_FLT_CB_FINAL 0x00000001 |
| 214 | #define HLUA_FLT_CB_RETVAL 0x00000002 |
| 215 | #define HLUA_FLT_CB_ARG_CHN 0x00000004 |
| 216 | #define HLUA_FLT_CB_ARG_HTTP_MSG 0x00000008 |
| 217 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 218 | #define HLUA_FLT_CTX_FL_PAYLOAD 0x00000001 |
| 219 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 220 | struct hlua_reg_filter { |
| 221 | char *name; |
| 222 | int flt_ref[MAX_THREADS + 1]; |
| 223 | int fun_ref[MAX_THREADS + 1]; |
| 224 | struct list l; |
| 225 | }; |
| 226 | |
| 227 | struct hlua_flt_config { |
| 228 | struct hlua_reg_filter *reg; |
| 229 | int ref[MAX_THREADS + 1]; |
| 230 | char **args; |
| 231 | }; |
| 232 | |
| 233 | struct hlua_flt_ctx { |
| 234 | int ref; /* ref to the filter lua object */ |
| 235 | struct hlua *hlua[2]; /* lua runtime context (0: request, 1: response) */ |
| 236 | unsigned int cur_off[2]; /* current offset (0: request, 1: response) */ |
| 237 | unsigned int cur_len[2]; /* current forwardable length (0: request, 1: response) */ |
| 238 | unsigned int flags; /* HLUA_FLT_CTX_FL_* */ |
| 239 | }; |
| 240 | |
| 241 | DECLARE_STATIC_POOL(pool_head_hlua_flt_ctx, "hlua_flt_ctx", sizeof(struct hlua_flt_ctx)); |
| 242 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 243 | static int hlua_filter_from_payload(struct filter *filter); |
| 244 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 245 | /* This is the chained list of struct hlua_flt referenced |
| 246 | * for haproxy filters. It is used for a post-initialisation control. |
| 247 | */ |
| 248 | static struct list referenced_filters = LIST_HEAD_INIT(referenced_filters); |
| 249 | |
| 250 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 251 | /* This is the memory pool containing struct lua for applets |
| 252 | * (including cli). |
| 253 | */ |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 254 | DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua)); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 255 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 256 | /* Used for Socket connection. */ |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 257 | static struct proxy *socket_proxy; |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 258 | static struct server *socket_tcp; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 259 | #ifdef USE_OPENSSL |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 260 | static struct server *socket_ssl; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 261 | #endif |
| 262 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 263 | /* List head of the function called at the initialisation time. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 264 | struct list hlua_init_functions[MAX_THREADS + 1]; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 265 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 266 | /* The following variables contains the reference of the different |
| 267 | * Lua classes. These references are useful for identify metadata |
| 268 | * associated with an object. |
| 269 | */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 270 | static int class_txn_ref; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 271 | static int class_socket_ref; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 272 | static int class_channel_ref; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 273 | static int class_fetches_ref; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 274 | static int class_converters_ref; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 275 | static int class_http_ref; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 276 | static int class_http_msg_ref; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 277 | static int class_httpclient_ref; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 278 | static int class_map_ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 279 | static int class_applet_tcp_ref; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 280 | static int class_applet_http_ref; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 281 | static int class_txn_reply_ref; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 282 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 283 | /* Global Lua execution timeout. By default Lua, execution linked |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 284 | * with stream (actions, sample-fetches and converters) have a |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 285 | * short timeout. Lua linked with tasks doesn't have a timeout |
| 286 | * because a task may remain alive during all the haproxy execution. |
| 287 | */ |
| 288 | static unsigned int hlua_timeout_session = 4000; /* session timeout. */ |
| 289 | static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 290 | static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 291 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 292 | /* Interrupts the Lua processing each "hlua_nb_instruction" instructions. |
| 293 | * it is used for preventing infinite loops. |
| 294 | * |
| 295 | * I test the scheer with an infinite loop containing one incrementation |
| 296 | * and one test. I run this loop between 10 seconds, I raise a ceil of |
| 297 | * 710M loops from one interrupt each 9000 instructions, so I fix the value |
| 298 | * to one interrupt each 10 000 instructions. |
| 299 | * |
| 300 | * configured | Number of |
| 301 | * instructions | loops executed |
| 302 | * between two | in milions |
| 303 | * forced yields | |
| 304 | * ---------------+--------------- |
| 305 | * 10 | 160 |
| 306 | * 500 | 670 |
| 307 | * 1000 | 680 |
| 308 | * 5000 | 700 |
| 309 | * 7000 | 700 |
| 310 | * 8000 | 700 |
| 311 | * 9000 | 710 <- ceil |
| 312 | * 10000 | 710 |
| 313 | * 100000 | 710 |
| 314 | * 1000000 | 710 |
| 315 | * |
| 316 | */ |
| 317 | static unsigned int hlua_nb_instruction = 10000; |
| 318 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 319 | /* Descriptor for the memory allocation state. The limit is pre-initialised to |
| 320 | * 0 until it is replaced by "tune.lua.maxmem" during the config parsing, or it |
| 321 | * is replaced with ~0 during post_init after everything was loaded. This way |
| 322 | * it is guaranteed that if limit is ~0 the boot is complete and that if it's |
| 323 | * zero it's not yet limited and proper accounting is required. |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 324 | */ |
| 325 | struct hlua_mem_allocator { |
| 326 | size_t allocated; |
| 327 | size_t limit; |
| 328 | }; |
| 329 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 330 | static struct hlua_mem_allocator hlua_global_allocator THREAD_ALIGNED(64); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 331 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 332 | /* These functions converts types between HAProxy internal args or |
| 333 | * sample and LUA types. Another function permits to check if the |
| 334 | * LUA stack contains arguments according with an required ARG_T |
| 335 | * format. |
| 336 | */ |
| 337 | static int hlua_arg2lua(lua_State *L, const struct arg *arg); |
| 338 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 339 | __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] | 340 | uint64_t mask, struct proxy *p); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 341 | static int hlua_smp2lua(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 342 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 343 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp); |
| 344 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 345 | __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] | 346 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 347 | struct prepend_path { |
| 348 | struct list l; |
| 349 | char *type; |
| 350 | char *path; |
| 351 | }; |
| 352 | |
| 353 | static struct list prepend_path_list = LIST_HEAD_INIT(prepend_path_list); |
| 354 | |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 355 | #define SEND_ERR(__be, __fmt, __args...) \ |
| 356 | do { \ |
| 357 | send_log(__be, LOG_ERR, __fmt, ## __args); \ |
| 358 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 359 | ha_alert(__fmt, ## __args); \ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 360 | } while (0) |
| 361 | |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 362 | static inline struct hlua_function *new_hlua_function() |
| 363 | { |
| 364 | struct hlua_function *fcn; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 365 | int i; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 366 | |
| 367 | fcn = calloc(1, sizeof(*fcn)); |
| 368 | if (!fcn) |
| 369 | return NULL; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 370 | LIST_APPEND(&referenced_functions, &fcn->l); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 371 | for (i = 0; i < MAX_THREADS + 1; i++) |
| 372 | fcn->function_ref[i] = -1; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 373 | return fcn; |
| 374 | } |
| 375 | |
Christopher Faulet | dda4444 | 2021-04-12 14:05:43 +0200 | [diff] [blame] | 376 | static inline void release_hlua_function(struct hlua_function *fcn) |
| 377 | { |
| 378 | if (!fcn) |
| 379 | return; |
| 380 | if (fcn->name) |
| 381 | ha_free(&fcn->name); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 382 | LIST_DELETE(&fcn->l); |
Christopher Faulet | dda4444 | 2021-04-12 14:05:43 +0200 | [diff] [blame] | 383 | ha_free(&fcn); |
| 384 | } |
| 385 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 386 | /* If the common state is set, the stack id is 0, otherwise it is the tid + 1 */ |
| 387 | static inline int fcn_ref_to_stack_id(struct hlua_function *fcn) |
| 388 | { |
| 389 | if (fcn->function_ref[0] == -1) |
| 390 | return tid + 1; |
| 391 | return 0; |
| 392 | } |
| 393 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 394 | /* Create a new registered filter. Only its name is filled */ |
| 395 | static inline struct hlua_reg_filter *new_hlua_reg_filter(const char *name) |
| 396 | { |
| 397 | struct hlua_reg_filter *reg_flt; |
| 398 | int i; |
| 399 | |
| 400 | reg_flt = calloc(1, sizeof(*reg_flt)); |
| 401 | if (!reg_flt) |
| 402 | return NULL; |
| 403 | reg_flt->name = strdup(name); |
| 404 | if (!reg_flt->name) { |
| 405 | free(reg_flt); |
| 406 | return NULL; |
| 407 | } |
| 408 | LIST_APPEND(&referenced_filters, ®_flt->l); |
| 409 | for (i = 0; i < MAX_THREADS + 1; i++) { |
| 410 | reg_flt->flt_ref[i] = -1; |
| 411 | reg_flt->fun_ref[i] = -1; |
| 412 | } |
| 413 | return reg_flt; |
| 414 | } |
| 415 | |
| 416 | /* Release a registered filter */ |
| 417 | static inline void release_hlua_reg_filter(struct hlua_reg_filter *reg_flt) |
| 418 | { |
| 419 | if (!reg_flt) |
| 420 | return; |
| 421 | if (reg_flt->name) |
| 422 | ha_free(®_flt->name); |
| 423 | LIST_DELETE(®_flt->l); |
| 424 | ha_free(®_flt); |
| 425 | } |
| 426 | |
| 427 | /* If the common state is set, the stack id is 0, otherwise it is the tid + 1 */ |
| 428 | static inline int reg_flt_to_stack_id(struct hlua_reg_filter *reg_flt) |
| 429 | { |
| 430 | if (reg_flt->fun_ref[0] == -1) |
| 431 | return tid + 1; |
| 432 | return 0; |
| 433 | } |
| 434 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 435 | /* Used to check an Lua function type in the stack. It creates and |
| 436 | * returns a reference of the function. This function throws an |
| 437 | * error if the rgument is not a "function". |
| 438 | */ |
| 439 | __LJMP unsigned int hlua_checkfunction(lua_State *L, int argno) |
| 440 | { |
| 441 | if (!lua_isfunction(L, argno)) { |
Thierry FOURNIER | fd1e955 | 2018-02-23 18:41:18 +0100 | [diff] [blame] | 442 | 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] | 443 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 444 | } |
| 445 | lua_pushvalue(L, argno); |
| 446 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 447 | } |
| 448 | |
Christopher Faulet | ba9e21d | 2020-02-25 10:20:04 +0100 | [diff] [blame] | 449 | /* Used to check an Lua table type in the stack. It creates and |
| 450 | * returns a reference of the table. This function throws an |
| 451 | * error if the rgument is not a "table". |
| 452 | */ |
| 453 | __LJMP unsigned int hlua_checktable(lua_State *L, int argno) |
| 454 | { |
| 455 | if (!lua_istable(L, argno)) { |
| 456 | const char *msg = lua_pushfstring(L, "table expected, got %s", luaL_typename(L, argno)); |
| 457 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 458 | } |
| 459 | lua_pushvalue(L, argno); |
| 460 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 461 | } |
| 462 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 463 | /* Return the string that is of the top of the stack. */ |
| 464 | const char *hlua_get_top_error_string(lua_State *L) |
| 465 | { |
| 466 | if (lua_gettop(L) < 1) |
| 467 | return "unknown error"; |
| 468 | if (lua_type(L, -1) != LUA_TSTRING) |
| 469 | return "unknown error"; |
| 470 | return lua_tostring(L, -1); |
| 471 | } |
| 472 | |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 473 | __LJMP const char *hlua_traceback(lua_State *L, const char* sep) |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 474 | { |
| 475 | lua_Debug ar; |
| 476 | int level = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 477 | struct buffer *msg = get_trash_chunk(); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 478 | |
| 479 | while (lua_getstack(L, level++, &ar)) { |
| 480 | |
| 481 | /* Add separator */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 482 | if (b_data(msg)) |
| 483 | chunk_appendf(msg, "%s", sep); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 484 | |
| 485 | /* Fill fields: |
| 486 | * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what; |
| 487 | * 'l': fills in the field currentline; |
| 488 | * 'n': fills in the field name and namewhat; |
| 489 | * 't': fills in the field istailcall; |
| 490 | */ |
| 491 | lua_getinfo(L, "Slnt", &ar); |
| 492 | |
| 493 | /* Append code localisation */ |
| 494 | if (ar.currentline > 0) |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 495 | chunk_appendf(msg, "%s:%d: ", ar.short_src, ar.currentline); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 496 | else |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 497 | chunk_appendf(msg, "%s: ", ar.short_src); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 498 | |
| 499 | /* |
| 500 | * Get function name |
| 501 | * |
| 502 | * if namewhat is no empty, name is defined. |
| 503 | * what contains "Lua" for Lua function, "C" for C function, |
| 504 | * or "main" for main code. |
| 505 | */ |
| 506 | 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] | 507 | chunk_appendf(msg, "in %s '%s'", ar.namewhat, ar.name); /* use it */ |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 508 | |
| 509 | 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] | 510 | chunk_appendf(msg, "in main chunk"); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 511 | |
| 512 | else if (*ar.what != 'C') /* for Lua functions, use <file:line> */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 513 | chunk_appendf(msg, "in function line %d", ar.linedefined); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 514 | |
| 515 | else /* nothing left... */ |
| 516 | chunk_appendf(msg, "?"); |
| 517 | |
| 518 | |
| 519 | /* Display tailed call */ |
| 520 | if (ar.istailcall) |
| 521 | chunk_appendf(msg, " ..."); |
| 522 | } |
| 523 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 524 | return msg->area; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 528 | /* This function check the number of arguments available in the |
| 529 | * stack. If the number of arguments available is not the same |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 530 | * then <nb> an error is thrown. |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 531 | */ |
| 532 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 533 | { |
| 534 | if (lua_gettop(L) == nb) |
| 535 | return; |
| 536 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 537 | } |
| 538 | |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 539 | /* This function pushes an error string prefixed by the file name |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 540 | * and the line number where the error is encountered. |
| 541 | */ |
| 542 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 543 | { |
| 544 | va_list argp; |
| 545 | va_start(argp, fmt); |
| 546 | luaL_where(L, 1); |
| 547 | lua_pushvfstring(L, fmt, argp); |
| 548 | va_end(argp); |
| 549 | lua_concat(L, 2); |
| 550 | return 1; |
| 551 | } |
| 552 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 553 | /* This functions is used with sample fetch and converters. It |
| 554 | * converts the HAProxy configuration argument in a lua stack |
| 555 | * values. |
| 556 | * |
| 557 | * It takes an array of "arg", and each entry of the array is |
| 558 | * converted and pushed in the LUA stack. |
| 559 | */ |
| 560 | static int hlua_arg2lua(lua_State *L, const struct arg *arg) |
| 561 | { |
| 562 | switch (arg->type) { |
| 563 | case ARGT_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 564 | case ARGT_TIME: |
| 565 | case ARGT_SIZE: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 566 | lua_pushinteger(L, arg->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 567 | break; |
| 568 | |
| 569 | case ARGT_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 570 | lua_pushlstring(L, arg->data.str.area, arg->data.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 571 | break; |
| 572 | |
| 573 | case ARGT_IPV4: |
| 574 | case ARGT_IPV6: |
| 575 | case ARGT_MSK4: |
| 576 | case ARGT_MSK6: |
| 577 | case ARGT_FE: |
| 578 | case ARGT_BE: |
| 579 | case ARGT_TAB: |
| 580 | case ARGT_SRV: |
| 581 | case ARGT_USR: |
| 582 | case ARGT_MAP: |
| 583 | default: |
| 584 | lua_pushnil(L); |
| 585 | break; |
| 586 | } |
| 587 | return 1; |
| 588 | } |
| 589 | |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 590 | /* 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] | 591 | * 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] | 592 | * with sample fetch wrappers. The input arguments are given to the |
| 593 | * lua wrapper and converted as arg list by the function. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 594 | */ |
| 595 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg) |
| 596 | { |
| 597 | switch (lua_type(L, ud)) { |
| 598 | |
| 599 | case LUA_TNUMBER: |
| 600 | case LUA_TBOOLEAN: |
| 601 | arg->type = ARGT_SINT; |
| 602 | arg->data.sint = lua_tointeger(L, ud); |
| 603 | break; |
| 604 | |
| 605 | case LUA_TSTRING: |
| 606 | arg->type = ARGT_STR; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 607 | 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] | 608 | /* 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] | 609 | 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] | 610 | arg->data.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 611 | break; |
| 612 | |
| 613 | case LUA_TUSERDATA: |
| 614 | case LUA_TNIL: |
| 615 | case LUA_TTABLE: |
| 616 | case LUA_TFUNCTION: |
| 617 | case LUA_TTHREAD: |
| 618 | case LUA_TLIGHTUSERDATA: |
| 619 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 620 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 621 | break; |
| 622 | } |
| 623 | return 1; |
| 624 | } |
| 625 | |
| 626 | /* the following functions are used to convert a struct sample |
| 627 | * in Lua type. This useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 628 | * fetches or converters. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 629 | */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 630 | static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 631 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 632 | switch (smp->data.type) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 633 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 634 | case SMP_T_BOOL: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 635 | lua_pushinteger(L, smp->data.u.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 636 | break; |
| 637 | |
| 638 | case SMP_T_BIN: |
| 639 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 640 | 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] | 641 | break; |
| 642 | |
| 643 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 644 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 645 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 646 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 647 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 648 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 649 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 650 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 651 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 652 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 653 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 654 | 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] | 655 | break; |
| 656 | default: |
| 657 | lua_pushnil(L); |
| 658 | break; |
| 659 | } |
| 660 | break; |
| 661 | |
| 662 | case SMP_T_IPV4: |
| 663 | case SMP_T_IPV6: |
| 664 | 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] | 665 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 666 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 667 | 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] | 668 | else |
| 669 | lua_pushnil(L); |
| 670 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 671 | default: |
| 672 | lua_pushnil(L); |
| 673 | break; |
| 674 | } |
| 675 | return 1; |
| 676 | } |
| 677 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 678 | /* the following functions are used to convert a struct sample |
| 679 | * in Lua strings. This is useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 680 | * fetches or converters. |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 681 | */ |
| 682 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
| 683 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 684 | switch (smp->data.type) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 685 | |
| 686 | case SMP_T_BIN: |
| 687 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 688 | 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] | 689 | break; |
| 690 | |
| 691 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 692 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 693 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 694 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 695 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 696 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 697 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 698 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 699 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 700 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 701 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 702 | 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] | 703 | break; |
| 704 | default: |
| 705 | lua_pushstring(L, ""); |
| 706 | break; |
| 707 | } |
| 708 | break; |
| 709 | |
| 710 | case SMP_T_SINT: |
| 711 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 712 | case SMP_T_IPV4: |
| 713 | case SMP_T_IPV6: |
| 714 | 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] | 715 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 716 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 717 | 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] | 718 | else |
| 719 | lua_pushstring(L, ""); |
| 720 | break; |
| 721 | default: |
| 722 | lua_pushstring(L, ""); |
| 723 | break; |
| 724 | } |
| 725 | return 1; |
| 726 | } |
| 727 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 728 | /* the following functions are used to convert an Lua type in a |
| 729 | * struct sample. This is useful to provide data from a converter |
| 730 | * to the LUA code. |
| 731 | */ |
| 732 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 733 | { |
| 734 | switch (lua_type(L, ud)) { |
| 735 | |
| 736 | case LUA_TNUMBER: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 737 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 738 | smp->data.u.sint = lua_tointeger(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 739 | break; |
| 740 | |
| 741 | |
| 742 | case LUA_TBOOLEAN: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 743 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 744 | smp->data.u.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 745 | break; |
| 746 | |
| 747 | case LUA_TSTRING: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 748 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 749 | smp->flags |= SMP_F_CONST; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 750 | 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] | 751 | /* 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] | 752 | 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] | 753 | smp->data.u.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 754 | break; |
| 755 | |
| 756 | case LUA_TUSERDATA: |
| 757 | case LUA_TNIL: |
| 758 | case LUA_TTABLE: |
| 759 | case LUA_TFUNCTION: |
| 760 | case LUA_TTHREAD: |
| 761 | case LUA_TLIGHTUSERDATA: |
Thierry FOURNIER | 93405e1 | 2015-08-26 14:19:03 +0200 | [diff] [blame] | 762 | case LUA_TNONE: |
| 763 | default: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 764 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 765 | smp->data.u.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 766 | break; |
| 767 | } |
| 768 | return 1; |
| 769 | } |
| 770 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 771 | /* This function check the "argp" built by another conversion function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 772 | * 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] | 773 | * 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] | 774 | * |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 775 | * This function assumes that the argp argument contains ARGM_NBARGS + 1 |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 776 | * entries and that there is at least one stop at the last position. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 777 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 778 | __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] | 779 | uint64_t mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 780 | { |
| 781 | int min_arg; |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 782 | int idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 783 | struct proxy *px; |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 784 | struct userlist *ul; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 785 | struct my_regex *reg; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 786 | const char *msg = NULL; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 787 | char *sname, *pname, *err = NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 788 | |
| 789 | idx = 0; |
| 790 | min_arg = ARGM(mask); |
| 791 | mask >>= ARGM_BITS; |
| 792 | |
| 793 | while (1) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 794 | struct buffer tmp = BUF_NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 795 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 796 | /* Check for mandatory arguments. */ |
| 797 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 798 | if (idx < min_arg) { |
| 799 | |
| 800 | /* If miss other argument than the first one, we return an error. */ |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 801 | if (idx > 0) { |
| 802 | msg = "Mandatory argument expected"; |
| 803 | goto error; |
| 804 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 805 | |
| 806 | /* If first argument have a certain type, some default values |
| 807 | * may be used. See the function smp_resolve_args(). |
| 808 | */ |
| 809 | switch (mask & ARGT_MASK) { |
| 810 | |
| 811 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 812 | if (!(p->cap & PR_CAP_FE)) { |
| 813 | msg = "Mandatory argument expected"; |
| 814 | goto error; |
| 815 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 816 | argp[idx].data.prx = p; |
| 817 | argp[idx].type = ARGT_FE; |
| 818 | argp[idx+1].type = ARGT_STOP; |
| 819 | break; |
| 820 | |
| 821 | case ARGT_BE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 822 | if (!(p->cap & PR_CAP_BE)) { |
| 823 | msg = "Mandatory argument expected"; |
| 824 | goto error; |
| 825 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 826 | argp[idx].data.prx = p; |
| 827 | argp[idx].type = ARGT_BE; |
| 828 | argp[idx+1].type = ARGT_STOP; |
| 829 | break; |
| 830 | |
| 831 | case ARGT_TAB: |
| 832 | argp[idx].data.prx = p; |
| 833 | argp[idx].type = ARGT_TAB; |
| 834 | argp[idx+1].type = ARGT_STOP; |
| 835 | break; |
| 836 | |
| 837 | default: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 838 | msg = "Mandatory argument expected"; |
| 839 | goto error; |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 840 | break; |
| 841 | } |
| 842 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 843 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 844 | } |
| 845 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 846 | /* Check for exceed the number of required argument. */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 847 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 848 | argp[idx].type != ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 849 | msg = "Last argument expected"; |
| 850 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 854 | argp[idx].type == ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 855 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 856 | } |
| 857 | |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 858 | /* Convert some argument types. All string in argp[] are for not |
| 859 | * duplicated yet. |
| 860 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 861 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 862 | case ARGT_SINT: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 863 | if (argp[idx].type != ARGT_SINT) { |
| 864 | msg = "integer expected"; |
| 865 | goto error; |
| 866 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 867 | argp[idx].type = ARGT_SINT; |
| 868 | break; |
| 869 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 870 | case ARGT_TIME: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 871 | if (argp[idx].type != ARGT_SINT) { |
| 872 | msg = "integer expected"; |
| 873 | goto error; |
| 874 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 875 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 876 | break; |
| 877 | |
| 878 | case ARGT_SIZE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 879 | if (argp[idx].type != ARGT_SINT) { |
| 880 | msg = "integer expected"; |
| 881 | goto error; |
| 882 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 883 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 884 | break; |
| 885 | |
| 886 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 887 | if (argp[idx].type != ARGT_STR) { |
| 888 | msg = "string expected"; |
| 889 | goto error; |
| 890 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 891 | 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] | 892 | if (!argp[idx].data.prx) { |
| 893 | msg = "frontend doesn't exist"; |
| 894 | goto error; |
| 895 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 896 | argp[idx].type = ARGT_FE; |
| 897 | break; |
| 898 | |
| 899 | case ARGT_BE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 900 | if (argp[idx].type != ARGT_STR) { |
| 901 | msg = "string expected"; |
| 902 | goto error; |
| 903 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 904 | 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] | 905 | if (!argp[idx].data.prx) { |
| 906 | msg = "backend doesn't exist"; |
| 907 | goto error; |
| 908 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 909 | argp[idx].type = ARGT_BE; |
| 910 | break; |
| 911 | |
| 912 | case ARGT_TAB: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 913 | if (argp[idx].type != ARGT_STR) { |
| 914 | msg = "string expected"; |
| 915 | goto error; |
| 916 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 917 | 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] | 918 | if (!argp[idx].data.t) { |
| 919 | msg = "table doesn't exist"; |
| 920 | goto error; |
| 921 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 922 | argp[idx].type = ARGT_TAB; |
| 923 | break; |
| 924 | |
| 925 | case ARGT_SRV: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 926 | if (argp[idx].type != ARGT_STR) { |
| 927 | msg = "string expected"; |
| 928 | goto error; |
| 929 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 930 | sname = strrchr(argp[idx].data.str.area, '/'); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 931 | if (sname) { |
| 932 | *sname++ = '\0'; |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 933 | pname = argp[idx].data.str.area; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 934 | px = proxy_be_by_name(pname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 935 | if (!px) { |
| 936 | msg = "backend doesn't exist"; |
| 937 | goto error; |
| 938 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 939 | } |
| 940 | else { |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 941 | sname = argp[idx].data.str.area; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 942 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 943 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 944 | argp[idx].data.srv = findserver(px, sname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 945 | if (!argp[idx].data.srv) { |
| 946 | msg = "server doesn't exist"; |
| 947 | goto error; |
| 948 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 949 | argp[idx].type = ARGT_SRV; |
| 950 | break; |
| 951 | |
| 952 | case ARGT_IPV4: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 953 | if (argp[idx].type != ARGT_STR) { |
| 954 | msg = "string expected"; |
| 955 | goto error; |
| 956 | } |
| 957 | if (inet_pton(AF_INET, argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 958 | msg = "invalid IPv4 address"; |
| 959 | goto error; |
| 960 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 961 | argp[idx].type = ARGT_IPV4; |
| 962 | break; |
| 963 | |
| 964 | case ARGT_MSK4: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 965 | if (argp[idx].type == ARGT_SINT) |
| 966 | len2mask4(argp[idx].data.sint, &argp[idx].data.ipv4); |
| 967 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 968 | if (!str2mask(argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 969 | msg = "invalid IPv4 mask"; |
| 970 | goto error; |
| 971 | } |
| 972 | } |
| 973 | else { |
| 974 | msg = "integer or string expected"; |
| 975 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 976 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 977 | argp[idx].type = ARGT_MSK4; |
| 978 | break; |
| 979 | |
| 980 | case ARGT_IPV6: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 981 | if (argp[idx].type != ARGT_STR) { |
| 982 | msg = "string expected"; |
| 983 | goto error; |
| 984 | } |
| 985 | if (inet_pton(AF_INET6, argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 986 | msg = "invalid IPv6 address"; |
| 987 | goto error; |
| 988 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 989 | argp[idx].type = ARGT_IPV6; |
| 990 | break; |
| 991 | |
| 992 | case ARGT_MSK6: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 993 | if (argp[idx].type == ARGT_SINT) |
| 994 | len2mask6(argp[idx].data.sint, &argp[idx].data.ipv6); |
| 995 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 996 | if (!str2mask6(argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 997 | msg = "invalid IPv6 mask"; |
| 998 | goto error; |
| 999 | } |
| 1000 | } |
| 1001 | else { |
| 1002 | msg = "integer or string expected"; |
| 1003 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 1004 | } |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 1005 | argp[idx].type = ARGT_MSK6; |
| 1006 | break; |
| 1007 | |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 1008 | case ARGT_REG: |
| 1009 | if (argp[idx].type != ARGT_STR) { |
| 1010 | msg = "string expected"; |
| 1011 | goto error; |
| 1012 | } |
| 1013 | reg = regex_comp(argp[idx].data.str.area, !(argp[idx].type_flags & ARGF_REG_ICASE), 1, &err); |
| 1014 | if (!reg) { |
| 1015 | msg = lua_pushfstring(L, "error compiling regex '%s' : '%s'", |
| 1016 | argp[idx].data.str.area, err); |
| 1017 | free(err); |
| 1018 | goto error; |
| 1019 | } |
| 1020 | argp[idx].type = ARGT_REG; |
| 1021 | argp[idx].data.reg = reg; |
| 1022 | break; |
| 1023 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1024 | case ARGT_USR: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1025 | if (argp[idx].type != ARGT_STR) { |
| 1026 | msg = "string expected"; |
| 1027 | goto error; |
| 1028 | } |
| 1029 | if (p->uri_auth && p->uri_auth->userlist && |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1030 | strcmp(p->uri_auth->userlist->name, argp[idx].data.str.area) == 0) |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1031 | ul = p->uri_auth->userlist; |
| 1032 | else |
| 1033 | ul = auth_find_userlist(argp[idx].data.str.area); |
| 1034 | |
| 1035 | if (!ul) { |
| 1036 | msg = lua_pushfstring(L, "unable to find userlist '%s'", argp[idx].data.str.area); |
| 1037 | goto error; |
| 1038 | } |
| 1039 | argp[idx].type = ARGT_USR; |
| 1040 | argp[idx].data.usr = ul; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1041 | break; |
| 1042 | |
| 1043 | case ARGT_STR: |
| 1044 | if (!chunk_dup(&tmp, &argp[idx].data.str)) { |
| 1045 | msg = "unable to duplicate string arg"; |
| 1046 | goto error; |
| 1047 | } |
| 1048 | argp[idx].data.str = tmp; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1049 | break; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1050 | |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1051 | case ARGT_MAP: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1052 | msg = "type not yet supported"; |
| 1053 | goto error; |
| 1054 | break; |
| 1055 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1056 | } |
| 1057 | |
| 1058 | /* Check for type of argument. */ |
| 1059 | if ((mask & ARGT_MASK) != argp[idx].type) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1060 | msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 1061 | arg_type_names[(mask & ARGT_MASK)], |
| 1062 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 1063 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1064 | } |
| 1065 | |
| 1066 | /* Next argument. */ |
| 1067 | mask >>= ARGT_BITS; |
| 1068 | idx++; |
| 1069 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1070 | return 0; |
| 1071 | |
| 1072 | error: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 1073 | free_args(argp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1074 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 1075 | return 0; /* Never reached */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1076 | } |
| 1077 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1078 | /* |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 1079 | * The following functions are used to make correspondence between the the |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1080 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1081 | * |
| 1082 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1083 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 1084 | */ |
| 1085 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 1086 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 1087 | struct hlua **hlua = lua_getextraspace(L); |
| 1088 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1089 | } |
| 1090 | static inline void hlua_sethlua(struct hlua *hlua) |
| 1091 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 1092 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 1093 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1094 | } |
| 1095 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1096 | /* This function is used to send logs. It try to send on screen (stderr) |
| 1097 | * and on the default syslog server. |
| 1098 | */ |
| 1099 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 1100 | { |
| 1101 | struct tm tm; |
| 1102 | char *p; |
| 1103 | |
| 1104 | /* Cleanup the log message. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1105 | p = trash.area; |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1106 | for (; *msg != '\0'; msg++, p++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1107 | if (p >= trash.area + trash.size - 1) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 1108 | /* Break the message if exceed the buffer size. */ |
| 1109 | *(p-4) = ' '; |
| 1110 | *(p-3) = '.'; |
| 1111 | *(p-2) = '.'; |
| 1112 | *(p-1) = '.'; |
| 1113 | break; |
| 1114 | } |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 1115 | if (isprint((unsigned char)*msg)) |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1116 | *p = *msg; |
| 1117 | else |
| 1118 | *p = '.'; |
| 1119 | } |
| 1120 | *p = '\0'; |
| 1121 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1122 | send_log(px, level, "%s\n", trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1123 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Christopher Faulet | f98d821 | 2020-10-02 18:13:52 +0200 | [diff] [blame] | 1124 | if (level == LOG_DEBUG && !(global.mode & MODE_DEBUG)) |
| 1125 | return; |
| 1126 | |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 1127 | get_localtime(date.tv_sec, &tm); |
| 1128 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1129 | 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] | 1130 | (int)getpid(), trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1131 | fflush(stderr); |
| 1132 | } |
| 1133 | } |
| 1134 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1135 | /* This function just ensure that the yield will be always |
| 1136 | * returned with a timeout and permit to set some flags |
| 1137 | */ |
| 1138 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1139 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1140 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1141 | struct hlua *hlua; |
| 1142 | |
| 1143 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1144 | hlua = hlua_gethlua(L); |
| 1145 | if (!hlua) { |
| 1146 | return; |
| 1147 | } |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1148 | |
| 1149 | /* Set the wake timeout. If timeout is required, we set |
| 1150 | * the expiration time. |
| 1151 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1152 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1153 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 1154 | hlua->flags |= flags; |
| 1155 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1156 | /* Process the yield. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1157 | MAY_LJMP(lua_yieldk(L, nresults, ctx, k)); |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1158 | } |
| 1159 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1160 | /* This function initialises the Lua environment stored in the stream. |
| 1161 | * 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] | 1162 | * 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] | 1163 | * |
| 1164 | * This function is particular. it initialises a new Lua thread. If the |
| 1165 | * initialisation fails (example: out of memory error), the lua function |
| 1166 | * throws an error (longjmp). |
| 1167 | * |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1168 | * 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] | 1169 | * environment, so we must not initialise it. While the support of |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1170 | * threads appear, the safe environment set a lock to ensure only one |
| 1171 | * Lua execution at a time. If we initialize safe environment in another |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1172 | * safe environment, we have a dead lock. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1173 | * |
| 1174 | * set "already_safe" true if the context is initialized form safe |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1175 | * Lua function. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1176 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1177 | * This function manipulates two Lua stacks: the main and the thread. Only |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 1178 | * the main stack can fail. The thread is not manipulated. This function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1179 | * MUST NOT manipulate the created thread stack state, because it is not |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1180 | * protected against errors thrown by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1181 | */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1182 | 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] | 1183 | { |
| 1184 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1185 | lua->flags = 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1186 | lua->gc_count = 0; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1187 | lua->wake_time = TICK_ETERNITY; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1188 | lua->state_id = state_id; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1189 | LIST_INIT(&lua->com); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1190 | if (!already_safe) { |
| 1191 | if (!SET_SAFE_LJMP_PARENT(lua)) { |
| 1192 | lua->Tref = LUA_REFNIL; |
| 1193 | return 0; |
| 1194 | } |
| 1195 | } |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1196 | lua->T = lua_newthread(hlua_states[state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1197 | if (!lua->T) { |
| 1198 | lua->Tref = LUA_REFNIL; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1199 | if (!already_safe) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1200 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1201 | return 0; |
| 1202 | } |
| 1203 | hlua_sethlua(lua); |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1204 | lua->Tref = luaL_ref(hlua_states[state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1205 | lua->task = task; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1206 | if (!already_safe) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1207 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1208 | return 1; |
| 1209 | } |
| 1210 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1211 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1212 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 1213 | * is not freed. |
| 1214 | */ |
| 1215 | void hlua_ctx_destroy(struct hlua *lua) |
| 1216 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1217 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 1218 | return; |
| 1219 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1220 | if (!lua->T) |
| 1221 | goto end; |
| 1222 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1223 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1224 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1225 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1226 | if (!SET_SAFE_LJMP(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1227 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1228 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1229 | RESET_SAFE_LJMP(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1230 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1231 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1232 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1233 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1234 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1235 | /* Forces a garbage collecting process. If the Lua program is finished |
| 1236 | * without error, we run the GC on the thread pointer. Its freed all |
| 1237 | * the unused memory. |
| 1238 | * If the thread is finnish with an error or is currently yielded, |
| 1239 | * it seems that the GC applied on the thread doesn't clean anything, |
| 1240 | * so e run the GC on the main thread. |
| 1241 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 1242 | * the garbage collection. |
| 1243 | */ |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1244 | if (lua->gc_count) { |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1245 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1246 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1247 | lua_gc(hlua_states[lua->state_id], LUA_GCCOLLECT, 0); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1248 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1249 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1250 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 1251 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1252 | |
| 1253 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1254 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1255 | } |
| 1256 | |
| 1257 | /* This function is used to restore the Lua context when a coroutine |
| 1258 | * fails. This function copy the common memory between old coroutine |
| 1259 | * and the new coroutine. The old coroutine is destroyed, and its |
| 1260 | * replaced by the new coroutine. |
| 1261 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 1262 | * as string error message and it is copied in the new stack. |
| 1263 | */ |
| 1264 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 1265 | { |
| 1266 | lua_State *T; |
| 1267 | int new_ref; |
| 1268 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1269 | /* New Lua coroutine. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1270 | T = lua_newthread(hlua_states[lua->state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1271 | if (!T) |
| 1272 | return 0; |
| 1273 | |
| 1274 | /* Copy last error message. */ |
| 1275 | if (keep_msg) |
| 1276 | lua_xmove(lua->T, T, 1); |
| 1277 | |
| 1278 | /* Copy data between the coroutines. */ |
| 1279 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1280 | lua_xmove(lua->T, T, 1); |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 1281 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */ |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1282 | |
| 1283 | /* Destroy old data. */ |
| 1284 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1285 | |
| 1286 | /* The thread is garbage collected by Lua. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1287 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1288 | |
| 1289 | /* Fill the struct with the new coroutine values. */ |
| 1290 | lua->Mref = new_ref; |
| 1291 | lua->T = T; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1292 | lua->Tref = luaL_ref(hlua_states[lua->state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1293 | |
| 1294 | /* Set context. */ |
| 1295 | hlua_sethlua(lua); |
| 1296 | |
| 1297 | return 1; |
| 1298 | } |
| 1299 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1300 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1301 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1302 | struct hlua *hlua; |
| 1303 | |
| 1304 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1305 | hlua = hlua_gethlua(L); |
| 1306 | if (!hlua) |
| 1307 | return; |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1308 | |
| 1309 | /* Lua cannot yield when its returning from a function, |
| 1310 | * so, we can fix the interrupt hook to 1 instruction, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1311 | * expecting that the function is finished. |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1312 | */ |
| 1313 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1314 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1315 | return; |
| 1316 | } |
| 1317 | |
| 1318 | /* restore the interrupt condition. */ |
| 1319 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1320 | |
| 1321 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1322 | * If the state is not yieldable, trying yield causes an error. |
| 1323 | */ |
| 1324 | if (lua_isyieldable(L)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1325 | MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1326 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 1327 | /* If we cannot yield, update the clock and check the timeout. */ |
| 1328 | tv_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1329 | hlua->run_time += now_ms - hlua->start_time; |
| 1330 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1331 | lua_pushfstring(L, "execution timeout"); |
| 1332 | WILL_LJMP(lua_error(L)); |
| 1333 | } |
| 1334 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1335 | /* Update the start time. */ |
| 1336 | hlua->start_time = now_ms; |
| 1337 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1338 | /* Try to interrupt the process at the end of the current |
| 1339 | * unyieldable function. |
| 1340 | */ |
| 1341 | 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] | 1342 | } |
| 1343 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1344 | /* This function start or resumes the Lua stack execution. If the flag |
| 1345 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1346 | * The function return an error. |
| 1347 | * |
| 1348 | * The function can returns 4 values: |
| 1349 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1350 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1351 | * task wakeup. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1352 | * - 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] | 1353 | * the top of the stack. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1354 | * - HLUA_E_ERR : An error has occurred without error message. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1355 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1356 | * 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] | 1357 | * LUA code. |
| 1358 | */ |
| 1359 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1360 | { |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1361 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 1362 | int nres; |
| 1363 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1364 | int ret; |
| 1365 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1366 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1367 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1368 | /* Initialise run time counter. */ |
| 1369 | if (!HLUA_IS_RUNNING(lua)) |
| 1370 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1371 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1372 | /* Lock the whole Lua execution. This lock must be before the |
| 1373 | * label "resume_execution". |
| 1374 | */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1375 | if (lua->state_id == 0) |
Willy Tarreau | 1e7bef1 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 1376 | lua_take_global_lock(); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1377 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1378 | resume_execution: |
| 1379 | |
| 1380 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1381 | * instructions. it is used for preventing infinite loops. |
| 1382 | */ |
| 1383 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1384 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1385 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1386 | HLUA_SET_RUN(lua); |
| 1387 | HLUA_CLR_CTRLYIELD(lua); |
| 1388 | HLUA_CLR_WAKERESWR(lua); |
| 1389 | HLUA_CLR_WAKEREQWR(lua); |
Christopher Faulet | 1f43a34 | 2021-08-04 17:58:21 +0200 | [diff] [blame] | 1390 | HLUA_CLR_NOYIELD(lua); |
| 1391 | if (!yield_allowed) |
| 1392 | HLUA_SET_NOYIELD(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1393 | |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1394 | /* Update the start time and reset wake_time. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1395 | lua->start_time = now_ms; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1396 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1397 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1398 | /* Call the function. */ |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1399 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1400 | 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] | 1401 | #else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1402 | ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs); |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1403 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1404 | switch (ret) { |
| 1405 | |
| 1406 | case LUA_OK: |
| 1407 | ret = HLUA_E_OK; |
| 1408 | break; |
| 1409 | |
| 1410 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1411 | /* Check if the execution timeout is expired. It it is the case, we |
| 1412 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1413 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1414 | tv_update_date(0, 1); |
| 1415 | lua->run_time += now_ms - lua->start_time; |
| 1416 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1417 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1418 | ret = HLUA_E_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1419 | break; |
| 1420 | } |
| 1421 | /* Process the forced yield. if the general yield is not allowed or |
| 1422 | * if no task were associated this the current Lua execution |
| 1423 | * coroutine, we resume the execution. Else we want to return in the |
| 1424 | * scheduler and we want to be waked up again, to continue the |
| 1425 | * current Lua execution. So we schedule our own task. |
| 1426 | */ |
| 1427 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1428 | if (!yield_allowed || !lua->task) |
| 1429 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1430 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1431 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1432 | if (!yield_allowed) { |
| 1433 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1434 | ret = HLUA_E_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1435 | break; |
| 1436 | } |
| 1437 | ret = HLUA_E_AGAIN; |
| 1438 | break; |
| 1439 | |
| 1440 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1441 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1442 | /* Special exit case. The traditional exit is returned as an error |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1443 | * because the errors ares the only one mean to return immediately |
| 1444 | * from and lua execution. |
| 1445 | */ |
| 1446 | if (lua->flags & HLUA_EXIT) { |
| 1447 | ret = HLUA_E_OK; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 1448 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1449 | break; |
| 1450 | } |
| 1451 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1452 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1453 | if (!lua_checkstack(lua->T, 1)) { |
| 1454 | ret = HLUA_E_ERR; |
| 1455 | break; |
| 1456 | } |
| 1457 | msg = lua_tostring(lua->T, -1); |
| 1458 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1459 | lua_pop(lua->T, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 1460 | trace = hlua_traceback(lua->T, ", "); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1461 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1462 | 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] | 1463 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1464 | 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] | 1465 | ret = HLUA_E_ERRMSG; |
| 1466 | break; |
| 1467 | |
| 1468 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1469 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1470 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1471 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1472 | break; |
| 1473 | |
| 1474 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1475 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1476 | if (!lua_checkstack(lua->T, 1)) { |
| 1477 | ret = HLUA_E_ERR; |
| 1478 | break; |
| 1479 | } |
| 1480 | msg = lua_tostring(lua->T, -1); |
| 1481 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1482 | lua_pop(lua->T, 1); |
| 1483 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1484 | 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] | 1485 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1486 | 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] | 1487 | ret = HLUA_E_ERRMSG; |
| 1488 | break; |
| 1489 | |
| 1490 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1491 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1492 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1493 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1494 | break; |
| 1495 | } |
| 1496 | |
| 1497 | switch (ret) { |
| 1498 | case HLUA_E_AGAIN: |
| 1499 | break; |
| 1500 | |
| 1501 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1502 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1503 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1504 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1505 | break; |
| 1506 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1507 | case HLUA_E_ETMOUT: |
| 1508 | case HLUA_E_NOMEM: |
| 1509 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1510 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1511 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1512 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1513 | hlua_ctx_renew(lua, 0); |
| 1514 | break; |
| 1515 | |
| 1516 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1517 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1518 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1519 | break; |
| 1520 | } |
| 1521 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1522 | /* This is the main exit point, remove the Lua lock. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1523 | if (lua->state_id == 0) |
Willy Tarreau | 1e7bef1 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 1524 | lua_drop_global_lock(); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1525 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1526 | return ret; |
| 1527 | } |
| 1528 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1529 | /* This function exit the current code. */ |
| 1530 | __LJMP static int hlua_done(lua_State *L) |
| 1531 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1532 | struct hlua *hlua; |
| 1533 | |
| 1534 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1535 | hlua = hlua_gethlua(L); |
| 1536 | if (!hlua) |
| 1537 | return 0; |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1538 | |
| 1539 | hlua->flags |= HLUA_EXIT; |
| 1540 | WILL_LJMP(lua_error(L)); |
| 1541 | |
| 1542 | return 0; |
| 1543 | } |
| 1544 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1545 | /* This function is an LUA binding. It provides a function |
| 1546 | * for deleting ACL from a referenced ACL file. |
| 1547 | */ |
| 1548 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1549 | { |
| 1550 | const char *name; |
| 1551 | const char *key; |
| 1552 | struct pat_ref *ref; |
| 1553 | |
| 1554 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1555 | |
| 1556 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1557 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1558 | |
| 1559 | ref = pat_ref_lookup(name); |
| 1560 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1561 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1562 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1563 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1564 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1565 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1566 | return 0; |
| 1567 | } |
| 1568 | |
| 1569 | /* This function is an LUA binding. It provides a function |
| 1570 | * for deleting map entry from a referenced map file. |
| 1571 | */ |
| 1572 | static int hlua_del_map(lua_State *L) |
| 1573 | { |
| 1574 | const char *name; |
| 1575 | const char *key; |
| 1576 | struct pat_ref *ref; |
| 1577 | |
| 1578 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1579 | |
| 1580 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1581 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1582 | |
| 1583 | ref = pat_ref_lookup(name); |
| 1584 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1585 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1586 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1587 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1588 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1589 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1590 | return 0; |
| 1591 | } |
| 1592 | |
| 1593 | /* This function is an LUA binding. It provides a function |
| 1594 | * for adding ACL pattern from a referenced ACL file. |
| 1595 | */ |
| 1596 | static int hlua_add_acl(lua_State *L) |
| 1597 | { |
| 1598 | const char *name; |
| 1599 | const char *key; |
| 1600 | struct pat_ref *ref; |
| 1601 | |
| 1602 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1603 | |
| 1604 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1605 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1606 | |
| 1607 | ref = pat_ref_lookup(name); |
| 1608 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1609 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1610 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1611 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1612 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1613 | pat_ref_add(ref, key, NULL, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1614 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1615 | return 0; |
| 1616 | } |
| 1617 | |
| 1618 | /* This function is an LUA binding. It provides a function |
| 1619 | * for setting map pattern and sample from a referenced map |
| 1620 | * file. |
| 1621 | */ |
| 1622 | static int hlua_set_map(lua_State *L) |
| 1623 | { |
| 1624 | const char *name; |
| 1625 | const char *key; |
| 1626 | const char *value; |
| 1627 | struct pat_ref *ref; |
| 1628 | |
| 1629 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1630 | |
| 1631 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1632 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1633 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1634 | |
| 1635 | ref = pat_ref_lookup(name); |
| 1636 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1637 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1638 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1639 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1640 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1641 | pat_ref_set(ref, key, value, NULL); |
| 1642 | else |
| 1643 | pat_ref_add(ref, key, value, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1644 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1645 | return 0; |
| 1646 | } |
| 1647 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1648 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1649 | * 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] | 1650 | * metatable have an original version registered in the global context with |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1651 | * the name of the object (_G[<name>] = <metable> ). |
| 1652 | * |
| 1653 | * A metable is a table that modify the standard behavior of a standard |
| 1654 | * access to the associated data. The entries of this new metatable are |
| 1655 | * defined as is: |
| 1656 | * |
| 1657 | * http://lua-users.org/wiki/MetatableEvents |
| 1658 | * |
| 1659 | * __index |
| 1660 | * |
| 1661 | * we access an absent field in a table, the result is nil. This is |
| 1662 | * true, but it is not the whole truth. Actually, such access triggers |
| 1663 | * the interpreter to look for an __index metamethod: If there is no |
| 1664 | * such method, as usually happens, then the access results in nil; |
| 1665 | * otherwise, the metamethod will provide the result. |
| 1666 | * |
| 1667 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1668 | * the key does not appear in the table, but the metatable has an __index |
| 1669 | * property: |
| 1670 | * |
| 1671 | * - if the value is a function, the function is called, passing in the |
| 1672 | * table and the key; the return value of that function is returned as |
| 1673 | * the result. |
| 1674 | * |
| 1675 | * - if the value is another table, the value of the key in that table is |
| 1676 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1677 | * table's metatable has an __index property, then it continues on up) |
| 1678 | * |
| 1679 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1680 | * |
| 1681 | * http://www.lua.org/pil/13.4.1.html |
| 1682 | * |
| 1683 | * __newindex |
| 1684 | * |
| 1685 | * Like __index, but control property assignment. |
| 1686 | * |
| 1687 | * __mode - Control weak references. A string value with one or both |
| 1688 | * of the characters 'k' and 'v' which specifies that the the |
| 1689 | * keys and/or values in the table are weak references. |
| 1690 | * |
| 1691 | * __call - Treat a table like a function. When a table is followed by |
| 1692 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1693 | * a __call key pointing to a function, that function is invoked |
| 1694 | * (passing any specified arguments) and the return value is |
| 1695 | * returned. |
| 1696 | * |
| 1697 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1698 | * called, if the metatable for myTable has a __metatable |
| 1699 | * key, the value of that key is returned instead of the |
| 1700 | * actual metatable. |
| 1701 | * |
| 1702 | * __tostring - Control string representation. When the builtin |
| 1703 | * "tostring( myTable )" function is called, if the metatable |
| 1704 | * for myTable has a __tostring property set to a function, |
| 1705 | * that function is invoked (passing myTable to it) and the |
| 1706 | * return value is used as the string representation. |
| 1707 | * |
| 1708 | * __len - Control table length. When the table length is requested using |
| 1709 | * the length operator ( '#' ), if the metatable for myTable has |
| 1710 | * a __len key pointing to a function, that function is invoked |
| 1711 | * (passing myTable to it) and the return value used as the value |
| 1712 | * of "#myTable". |
| 1713 | * |
| 1714 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1715 | * collected, if the metatable has a __gc field pointing to a |
| 1716 | * function, that function is first invoked, passing the userdata |
| 1717 | * to it. The __gc metamethod is not called for tables. |
| 1718 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1719 | * |
| 1720 | * Special metamethods for redefining standard operators: |
| 1721 | * http://www.lua.org/pil/13.1.html |
| 1722 | * |
| 1723 | * __add "+" |
| 1724 | * __sub "-" |
| 1725 | * __mul "*" |
| 1726 | * __div "/" |
| 1727 | * __unm "!" |
| 1728 | * __pow "^" |
| 1729 | * __concat ".." |
| 1730 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1731 | * Special methods for redefining standard relations |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1732 | * http://www.lua.org/pil/13.2.html |
| 1733 | * |
| 1734 | * __eq "==" |
| 1735 | * __lt "<" |
| 1736 | * __le "<=" |
| 1737 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1738 | |
| 1739 | /* |
| 1740 | * |
| 1741 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1742 | * Class Map |
| 1743 | * |
| 1744 | * |
| 1745 | */ |
| 1746 | |
| 1747 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1748 | * a class session, otherwise it throws an error. |
| 1749 | */ |
| 1750 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1751 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1752 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1753 | } |
| 1754 | |
| 1755 | /* This function is the map constructor. It don't need |
| 1756 | * the class Map object. It creates and return a new Map |
| 1757 | * object. It must be called only during "body" or "init" |
| 1758 | * context because it process some filesystem accesses. |
| 1759 | */ |
| 1760 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1761 | { |
| 1762 | const char *fn; |
| 1763 | int match = PAT_MATCH_STR; |
| 1764 | struct sample_conv conv; |
| 1765 | const char *file = ""; |
| 1766 | int line = 0; |
| 1767 | lua_Debug ar; |
| 1768 | char *err = NULL; |
| 1769 | struct arg args[2]; |
| 1770 | |
| 1771 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1772 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1773 | |
| 1774 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1775 | |
| 1776 | if (lua_gettop(L) >= 2) { |
| 1777 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1778 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1779 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1780 | } |
| 1781 | |
| 1782 | /* Get Lua filename and line number. */ |
| 1783 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1784 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1785 | if (ar.currentline > 0) { /* is there info? */ |
| 1786 | file = ar.short_src; |
| 1787 | line = ar.currentline; |
| 1788 | } |
| 1789 | } |
| 1790 | |
| 1791 | /* fill fake sample_conv struct. */ |
| 1792 | conv.kw = ""; /* unused. */ |
| 1793 | conv.process = NULL; /* unused. */ |
| 1794 | conv.arg_mask = 0; /* unused. */ |
| 1795 | conv.val_args = NULL; /* unused. */ |
| 1796 | conv.out_type = SMP_T_STR; |
| 1797 | conv.private = (void *)(long)match; |
| 1798 | switch (match) { |
| 1799 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1800 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1801 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1802 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1803 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1804 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1805 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1806 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1807 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1808 | default: |
| 1809 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1810 | } |
| 1811 | |
| 1812 | /* fill fake args. */ |
| 1813 | args[0].type = ARGT_STR; |
Christopher Faulet | 73292e9 | 2020-08-06 08:40:09 +0200 | [diff] [blame] | 1814 | args[0].data.str.area = strdup(fn); |
| 1815 | args[0].data.str.data = strlen(fn); |
| 1816 | args[0].data.str.size = args[0].data.str.data+1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1817 | args[1].type = ARGT_STOP; |
| 1818 | |
| 1819 | /* load the map. */ |
| 1820 | if (!sample_load_map(args, &conv, file, line, &err)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1821 | /* error case: we can't use luaL_error because we must |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1822 | * free the err variable. |
| 1823 | */ |
| 1824 | luaL_where(L, 1); |
| 1825 | lua_pushfstring(L, "'new': %s.", err); |
| 1826 | lua_concat(L, 2); |
| 1827 | free(err); |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 1828 | chunk_destroy(&args[0].data.str); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1829 | WILL_LJMP(lua_error(L)); |
| 1830 | } |
| 1831 | |
| 1832 | /* create the lua object. */ |
| 1833 | lua_newtable(L); |
| 1834 | lua_pushlightuserdata(L, args[0].data.map); |
| 1835 | lua_rawseti(L, -2, 0); |
| 1836 | |
| 1837 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1838 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1839 | lua_setmetatable(L, -2); |
| 1840 | |
| 1841 | |
| 1842 | return 1; |
| 1843 | } |
| 1844 | |
| 1845 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1846 | { |
| 1847 | struct map_descriptor *desc; |
| 1848 | struct pattern *pat; |
| 1849 | struct sample smp; |
| 1850 | |
| 1851 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1852 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1853 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1854 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1855 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1856 | } |
| 1857 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1858 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1859 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1860 | 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] | 1861 | smp.data.u.str.size = smp.data.u.str.data + 1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1862 | } |
| 1863 | |
| 1864 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1865 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1866 | if (str) |
| 1867 | lua_pushstring(L, ""); |
| 1868 | else |
| 1869 | lua_pushnil(L); |
| 1870 | return 1; |
| 1871 | } |
| 1872 | |
| 1873 | /* 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] | 1874 | 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] | 1875 | return 1; |
| 1876 | } |
| 1877 | |
| 1878 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1879 | { |
| 1880 | return _hlua_map_lookup(L, 0); |
| 1881 | } |
| 1882 | |
| 1883 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1884 | { |
| 1885 | return _hlua_map_lookup(L, 1); |
| 1886 | } |
| 1887 | |
| 1888 | /* |
| 1889 | * |
| 1890 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1891 | * Class Socket |
| 1892 | * |
| 1893 | * |
| 1894 | */ |
| 1895 | |
| 1896 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1897 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1898 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1899 | } |
| 1900 | |
| 1901 | /* 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] | 1902 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1903 | * received. |
| 1904 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1905 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1906 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1907 | struct stream_interface *si = appctx->owner; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1908 | |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1909 | if (appctx->ctx.hlua_cosocket.die) { |
| 1910 | si_shutw(si); |
| 1911 | si_shutr(si); |
| 1912 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1913 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1914 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1915 | stream_shutdown(si_strm(si), SF_ERR_KILLED); |
| 1916 | } |
| 1917 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1918 | /* If we can't write, wakeup the pending write signals. */ |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1919 | if (channel_output_closed(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1920 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1921 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1922 | /* If we can't read, wakeup the pending read signals. */ |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1923 | if (channel_input_closed(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1924 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1925 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1926 | /* if the connection is not established, inform the stream that we want |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1927 | * to be notified whenever the connection completes. |
| 1928 | */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1929 | if (si_opposite(si)->state < SI_ST_EST) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 1930 | si_cant_get(si); |
Willy Tarreau | 12c2423 | 2018-12-06 15:29:50 +0100 | [diff] [blame] | 1931 | si_rx_conn_blk(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1932 | si_rx_endp_more(si); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1933 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1934 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1935 | |
| 1936 | /* This function is called after the connect. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1937 | appctx->ctx.hlua_cosocket.connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1938 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1939 | /* 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] | 1940 | if (channel_may_recv(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1941 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1942 | |
| 1943 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1944 | if (!channel_is_empty(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1945 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1946 | |
| 1947 | /* Some data were injected in the buffer, notify the stream |
| 1948 | * interface. |
| 1949 | */ |
| 1950 | if (!channel_is_empty(si_ic(si))) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 1951 | si_update(si); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1952 | |
| 1953 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1954 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1955 | */ |
| 1956 | if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write)) |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1957 | si_rx_endp_more(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1958 | } |
| 1959 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1960 | /* This function is called when the "struct stream" is destroyed. |
| 1961 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1962 | * Wake all the pending signals. |
| 1963 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1964 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1965 | { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1966 | struct xref *peer; |
| 1967 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1968 | /* Remove my link in the original object. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1969 | peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref); |
| 1970 | if (peer) |
| 1971 | xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1972 | |
| 1973 | /* Wake all the task waiting for me. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1974 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1975 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1976 | } |
| 1977 | |
| 1978 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1979 | * uses this object. If the stream does not exists, just quit. |
| 1980 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1981 | * pending signal can rest in the read and write lists. destroy |
| 1982 | * it. |
| 1983 | */ |
| 1984 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1985 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1986 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1987 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1988 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1989 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1990 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 1991 | |
| 1992 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1993 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1994 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1995 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1996 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1997 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1998 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1999 | appctx->ctx.hlua_cosocket.die = 1; |
| 2000 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2001 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2002 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2003 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2004 | return 0; |
| 2005 | } |
| 2006 | |
| 2007 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2008 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2009 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2010 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2011 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2012 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2013 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2014 | struct xref *peer; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2015 | struct hlua *hlua; |
| 2016 | |
| 2017 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2018 | hlua = hlua_gethlua(L); |
| 2019 | if (!hlua) |
| 2020 | return 0; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2021 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2022 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2023 | |
| 2024 | /* Check if we run on the same thread than the xreator thread. |
| 2025 | * We cannot access to the socket if the thread is different. |
| 2026 | */ |
| 2027 | if (socket->tid != tid) |
| 2028 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2029 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2030 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2031 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2032 | return 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2033 | |
| 2034 | hlua->gc_count--; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2035 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2036 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2037 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2038 | appctx->ctx.hlua_cosocket.die = 1; |
| 2039 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2040 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2041 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2042 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2043 | return 0; |
| 2044 | } |
| 2045 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2046 | /* The close function calls close_helper. |
| 2047 | */ |
| 2048 | __LJMP static int hlua_socket_close(lua_State *L) |
| 2049 | { |
| 2050 | MAY_LJMP(check_args(L, 1, "close")); |
| 2051 | return hlua_socket_close_helper(L); |
| 2052 | } |
| 2053 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2054 | /* This Lua function assumes that the stack contain three parameters. |
| 2055 | * 1 - USERDATA containing a struct socket |
| 2056 | * 2 - INTEGER with values of the macro defined below |
| 2057 | * If the integer is -1, we must read at most one line. |
| 2058 | * If the integer is -2, we ust read all the data until the |
| 2059 | * end of the stream. |
| 2060 | * If the integer is positive value, we must read a number of |
| 2061 | * bytes corresponding to this value. |
| 2062 | */ |
| 2063 | #define HLSR_READ_LINE (-1) |
| 2064 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2065 | __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] | 2066 | { |
| 2067 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2068 | int wanted = lua_tointeger(L, 2); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2069 | struct hlua *hlua; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2070 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2071 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2072 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 2073 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2074 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 2075 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2076 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 2077 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 2078 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2079 | struct stream_interface *si; |
| 2080 | struct stream *s; |
| 2081 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2082 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2083 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2084 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2085 | hlua = hlua_gethlua(L); |
| 2086 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2087 | /* Check if this lua stack is schedulable. */ |
| 2088 | if (!hlua || !hlua->task) |
| 2089 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 2090 | "'frontend', 'backend' or 'task'")); |
| 2091 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2092 | /* Check if we run on the same thread than the xreator thread. |
| 2093 | * We cannot access to the socket if the thread is different. |
| 2094 | */ |
| 2095 | if (socket->tid != tid) |
| 2096 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2097 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2098 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2099 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2100 | if (!peer) |
| 2101 | goto no_peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2102 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2103 | si = appctx->owner; |
| 2104 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2105 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2106 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2107 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2108 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2109 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2110 | if (nblk < 0) /* Connection close. */ |
| 2111 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2112 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2113 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 2114 | |
| 2115 | /* remove final \r\n. */ |
| 2116 | if (nblk == 1) { |
| 2117 | if (blk1[len1-1] == '\n') { |
| 2118 | len1--; |
| 2119 | skip_at_end++; |
| 2120 | if (blk1[len1-1] == '\r') { |
| 2121 | len1--; |
| 2122 | skip_at_end++; |
| 2123 | } |
| 2124 | } |
| 2125 | } |
| 2126 | else { |
| 2127 | if (blk2[len2-1] == '\n') { |
| 2128 | len2--; |
| 2129 | skip_at_end++; |
| 2130 | if (blk2[len2-1] == '\r') { |
| 2131 | len2--; |
| 2132 | skip_at_end++; |
| 2133 | } |
| 2134 | } |
| 2135 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2136 | } |
| 2137 | |
| 2138 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2139 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2140 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2141 | if (nblk < 0) /* Connection close. */ |
| 2142 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2143 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2144 | goto connection_empty; |
| 2145 | } |
| 2146 | |
| 2147 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2148 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2149 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2150 | if (nblk < 0) /* Connection close. */ |
| 2151 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2152 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2153 | goto connection_empty; |
| 2154 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2155 | missing_bytes = wanted - socket->b.n; |
| 2156 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2157 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2158 | len1 = missing_bytes; |
| 2159 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 2160 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2161 | } |
| 2162 | |
| 2163 | len = len1; |
| 2164 | |
| 2165 | luaL_addlstring(&socket->b, blk1, len1); |
| 2166 | if (nblk == 2) { |
| 2167 | len += len2; |
| 2168 | luaL_addlstring(&socket->b, blk2, len2); |
| 2169 | } |
| 2170 | |
| 2171 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2172 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2173 | |
| 2174 | /* Don't wait anything. */ |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 2175 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2176 | |
| 2177 | /* If the pattern reclaim to read all the data |
| 2178 | * in the connection, got out. |
| 2179 | */ |
| 2180 | if (wanted == HLSR_READ_ALL) |
| 2181 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2182 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2183 | goto connection_empty; |
| 2184 | |
| 2185 | /* Return result. */ |
| 2186 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2187 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2188 | return 1; |
| 2189 | |
| 2190 | connection_closed: |
| 2191 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2192 | xref_unlock(&socket->xref, peer); |
| 2193 | |
| 2194 | no_peer: |
| 2195 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2196 | /* If the buffer containds data. */ |
| 2197 | if (socket->b.n > 0) { |
| 2198 | luaL_pushresult(&socket->b); |
| 2199 | return 1; |
| 2200 | } |
| 2201 | lua_pushnil(L); |
| 2202 | lua_pushstring(L, "connection closed."); |
| 2203 | return 2; |
| 2204 | |
| 2205 | connection_empty: |
| 2206 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2207 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) { |
| 2208 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2209 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2210 | } |
| 2211 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2212 | 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] | 2213 | return 0; |
| 2214 | } |
| 2215 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2216 | /* This Lua function gets two parameters. The first one can be string |
| 2217 | * or a number. If the string is "*l", the user requires one line. If |
| 2218 | * 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] | 2219 | * If the value is a number, the user require a number of bytes equal |
| 2220 | * to the value. The default value is "*l" (a line). |
| 2221 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2222 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2223 | * integer takes this values: |
| 2224 | * -1 : read a line |
| 2225 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2226 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2227 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2228 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2229 | * concatenated with the read data. |
| 2230 | */ |
| 2231 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 2232 | { |
| 2233 | int wanted = HLSR_READ_LINE; |
| 2234 | const char *pattern; |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2235 | int lastarg, type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2236 | char *error; |
| 2237 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2238 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2239 | |
| 2240 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 2241 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 2242 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2243 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2244 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2245 | /* Check if we run on the same thread than the xreator thread. |
| 2246 | * We cannot access to the socket if the thread is different. |
| 2247 | */ |
| 2248 | if (socket->tid != tid) |
| 2249 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2250 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2251 | /* check for pattern. */ |
| 2252 | if (lua_gettop(L) >= 2) { |
| 2253 | type = lua_type(L, 2); |
| 2254 | if (type == LUA_TSTRING) { |
| 2255 | pattern = lua_tostring(L, 2); |
| 2256 | if (strcmp(pattern, "*a") == 0) |
| 2257 | wanted = HLSR_READ_ALL; |
| 2258 | else if (strcmp(pattern, "*l") == 0) |
| 2259 | wanted = HLSR_READ_LINE; |
| 2260 | else { |
| 2261 | wanted = strtoll(pattern, &error, 10); |
| 2262 | if (*error != '\0') |
| 2263 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 2264 | } |
| 2265 | } |
| 2266 | else if (type == LUA_TNUMBER) { |
| 2267 | wanted = lua_tointeger(L, 2); |
| 2268 | if (wanted < 0) |
| 2269 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 2270 | } |
| 2271 | } |
| 2272 | |
| 2273 | /* Set pattern. */ |
| 2274 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 2275 | |
| 2276 | /* Check if we would replace the top by itself. */ |
| 2277 | if (lua_gettop(L) != 2) |
| 2278 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2279 | |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2280 | /* Save index of the top of the stack because since buffers are used, it |
| 2281 | * may change |
| 2282 | */ |
| 2283 | lastarg = lua_gettop(L); |
| 2284 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2285 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2286 | luaL_buffinit(L, &socket->b); |
| 2287 | |
| 2288 | /* Check prefix. */ |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2289 | if (lastarg >= 3) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2290 | if (lua_type(L, 3) != LUA_TSTRING) |
| 2291 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 2292 | pattern = lua_tolstring(L, 3, &len); |
| 2293 | luaL_addlstring(&socket->b, pattern, len); |
| 2294 | } |
| 2295 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2296 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2297 | } |
| 2298 | |
| 2299 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 2300 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2301 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2302 | 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] | 2303 | { |
| 2304 | struct hlua_socket *socket; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2305 | struct hlua *hlua; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2306 | struct appctx *appctx; |
| 2307 | size_t buf_len; |
| 2308 | const char *buf; |
| 2309 | int len; |
| 2310 | int send_len; |
| 2311 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2312 | struct xref *peer; |
| 2313 | struct stream_interface *si; |
| 2314 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2315 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2316 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2317 | hlua = hlua_gethlua(L); |
| 2318 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2319 | /* Check if this lua stack is schedulable. */ |
| 2320 | if (!hlua || !hlua->task) |
| 2321 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 2322 | "'frontend', 'backend' or 'task'")); |
| 2323 | |
| 2324 | /* Get object */ |
| 2325 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2326 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2327 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2328 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2329 | /* Check if we run on the same thread than the xreator thread. |
| 2330 | * We cannot access to the socket if the thread is different. |
| 2331 | */ |
| 2332 | if (socket->tid != tid) |
| 2333 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2334 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2335 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2336 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2337 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2338 | lua_pushinteger(L, -1); |
| 2339 | return 1; |
| 2340 | } |
| 2341 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2342 | si = appctx->owner; |
| 2343 | s = si_strm(si); |
| 2344 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2345 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2346 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2347 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2348 | lua_pushinteger(L, -1); |
| 2349 | return 1; |
| 2350 | } |
| 2351 | |
| 2352 | /* Update the input buffer data. */ |
| 2353 | buf += sent; |
| 2354 | send_len = buf_len - sent; |
| 2355 | |
| 2356 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2357 | if (sent >= buf_len) { |
| 2358 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2359 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2360 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2361 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2362 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2363 | * the request buffer if its not required. |
| 2364 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2365 | if (s->req.buf.size == 0) { |
Willy Tarreau | 581abd3 | 2018-10-25 10:21:41 +0200 | [diff] [blame] | 2366 | if (!si_alloc_ibuf(si, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2367 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2368 | } |
| 2369 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2370 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2371 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2372 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2373 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2374 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2375 | |
| 2376 | /* send data */ |
| 2377 | if (len < send_len) |
| 2378 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2379 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2380 | |
| 2381 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2382 | * the data" (-2) are not expected because the available length |
| 2383 | * is tested. |
| 2384 | * Other unknown error are also not expected. |
| 2385 | */ |
| 2386 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2387 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2388 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2389 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2390 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2391 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2392 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2393 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2394 | return 1; |
| 2395 | } |
| 2396 | |
| 2397 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2398 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2399 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2400 | s->req.rex = TICK_ETERNITY; |
| 2401 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2402 | |
| 2403 | /* Update length sent. */ |
| 2404 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2405 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2406 | |
| 2407 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2408 | if (sent + len >= buf_len) { |
| 2409 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2410 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2411 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2412 | |
| 2413 | hlua_socket_write_yield_return: |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2414 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2415 | xref_unlock(&socket->xref, peer); |
| 2416 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2417 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2418 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2419 | 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] | 2420 | return 0; |
| 2421 | } |
| 2422 | |
| 2423 | /* This function initiate the send of data. It just check the input |
| 2424 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2425 | * 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] | 2426 | * "hlua_socket_write_yield" that can yield. |
| 2427 | * |
| 2428 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2429 | * the associated object. The second is a string buffer. The third is |
| 2430 | * a facultative integer that represents where is the buffer position |
| 2431 | * of the start of the data that can send. The first byte is the |
| 2432 | * position "1". The default value is "1". The fourth argument is a |
| 2433 | * facultative integer that represents where is the buffer position |
| 2434 | * of the end of the data that can send. The default is the last byte. |
| 2435 | */ |
| 2436 | static int hlua_socket_send(struct lua_State *L) |
| 2437 | { |
| 2438 | int i; |
| 2439 | int j; |
| 2440 | const char *buf; |
| 2441 | size_t buf_len; |
| 2442 | |
| 2443 | /* Check number of arguments. */ |
| 2444 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2445 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2446 | |
| 2447 | /* Get the string. */ |
| 2448 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2449 | |
| 2450 | /* Get and check j. */ |
| 2451 | if (lua_gettop(L) == 4) { |
| 2452 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2453 | if (j < 0) |
| 2454 | j = buf_len + j + 1; |
| 2455 | if (j > buf_len) |
| 2456 | j = buf_len + 1; |
| 2457 | lua_pop(L, 1); |
| 2458 | } |
| 2459 | else |
| 2460 | j = buf_len; |
| 2461 | |
| 2462 | /* Get and check i. */ |
| 2463 | if (lua_gettop(L) == 3) { |
| 2464 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2465 | if (i < 0) |
| 2466 | i = buf_len + i + 1; |
| 2467 | if (i > buf_len) |
| 2468 | i = buf_len + 1; |
| 2469 | lua_pop(L, 1); |
| 2470 | } else |
| 2471 | i = 1; |
| 2472 | |
| 2473 | /* Check bth i and j. */ |
| 2474 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2475 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2476 | return 1; |
| 2477 | } |
| 2478 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2479 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2480 | return 1; |
| 2481 | } |
| 2482 | if (i == 0) |
| 2483 | i = 1; |
| 2484 | if (j == 0) |
| 2485 | j = 1; |
| 2486 | |
| 2487 | /* Pop the string. */ |
| 2488 | lua_pop(L, 1); |
| 2489 | |
| 2490 | /* Update the buffer length. */ |
| 2491 | buf += i - 1; |
| 2492 | buf_len = j - i + 1; |
| 2493 | lua_pushlstring(L, buf, buf_len); |
| 2494 | |
| 2495 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2496 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2497 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2498 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2499 | } |
| 2500 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2501 | #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] | 2502 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 2503 | { |
| 2504 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2505 | int ret; |
| 2506 | int len; |
| 2507 | char *p; |
| 2508 | |
| 2509 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2510 | if (ret <= 0) { |
| 2511 | lua_pushnil(L); |
| 2512 | return 1; |
| 2513 | } |
| 2514 | |
| 2515 | if (ret == AF_UNIX) { |
| 2516 | lua_pushstring(L, buffer+1); |
| 2517 | return 1; |
| 2518 | } |
| 2519 | else if (ret == AF_INET6) { |
| 2520 | buffer[0] = '['; |
| 2521 | len = strlen(buffer); |
| 2522 | buffer[len] = ']'; |
| 2523 | len++; |
| 2524 | buffer[len] = ':'; |
| 2525 | len++; |
| 2526 | p = buffer; |
| 2527 | } |
| 2528 | else if (ret == AF_INET) { |
| 2529 | p = buffer + 1; |
| 2530 | len = strlen(p); |
| 2531 | p[len] = ':'; |
| 2532 | len++; |
| 2533 | } |
| 2534 | else { |
| 2535 | lua_pushnil(L); |
| 2536 | return 1; |
| 2537 | } |
| 2538 | |
| 2539 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2540 | lua_pushnil(L); |
| 2541 | return 1; |
| 2542 | } |
| 2543 | |
| 2544 | lua_pushstring(L, p); |
| 2545 | return 1; |
| 2546 | } |
| 2547 | |
| 2548 | /* Returns information about the peer of the connection. */ |
| 2549 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2550 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2551 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2552 | struct xref *peer; |
| 2553 | struct appctx *appctx; |
| 2554 | struct stream_interface *si; |
| 2555 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2556 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2557 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2558 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2559 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2560 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2561 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2562 | /* Check if we run on the same thread than the xreator thread. |
| 2563 | * We cannot access to the socket if the thread is different. |
| 2564 | */ |
| 2565 | if (socket->tid != tid) |
| 2566 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2567 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2568 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2569 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2570 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2571 | lua_pushnil(L); |
| 2572 | return 1; |
| 2573 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2574 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2575 | si = appctx->owner; |
| 2576 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2577 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2578 | if (!s->target_addr) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2579 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2580 | lua_pushnil(L); |
| 2581 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2582 | } |
| 2583 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2584 | ret = MAY_LJMP(hlua_socket_info(L, s->target_addr)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2585 | xref_unlock(&socket->xref, peer); |
| 2586 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2587 | } |
| 2588 | |
| 2589 | /* Returns information about my connection side. */ |
| 2590 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2591 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2592 | struct hlua_socket *socket; |
| 2593 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2594 | struct appctx *appctx; |
| 2595 | struct xref *peer; |
| 2596 | struct stream_interface *si; |
| 2597 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2598 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2599 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2600 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2601 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2602 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2603 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2604 | /* Check if we run on the same thread than the xreator thread. |
| 2605 | * We cannot access to the socket if the thread is different. |
| 2606 | */ |
| 2607 | if (socket->tid != tid) |
| 2608 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2609 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2610 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2611 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2612 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2613 | lua_pushnil(L); |
| 2614 | return 1; |
| 2615 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2616 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2617 | si = appctx->owner; |
| 2618 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2619 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2620 | conn = cs_conn(objt_cs(s->si[1].end)); |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2621 | if (!conn || !conn_get_src(conn)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2622 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2623 | lua_pushnil(L); |
| 2624 | return 1; |
| 2625 | } |
| 2626 | |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2627 | ret = hlua_socket_info(L, conn->src); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2628 | xref_unlock(&socket->xref, peer); |
| 2629 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2630 | } |
| 2631 | |
| 2632 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2633 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2634 | .obj_type = OBJ_TYPE_APPLET, |
| 2635 | .name = "<LUA_TCP>", |
| 2636 | .fct = hlua_socket_handler, |
| 2637 | .release = hlua_socket_release, |
| 2638 | }; |
| 2639 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2640 | __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] | 2641 | { |
| 2642 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2643 | struct hlua *hlua; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2644 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2645 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2646 | struct stream_interface *si; |
| 2647 | struct stream *s; |
| 2648 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2649 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2650 | hlua = hlua_gethlua(L); |
| 2651 | if (!hlua) |
| 2652 | return 0; |
| 2653 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2654 | /* Check if we run on the same thread than the xreator thread. |
| 2655 | * We cannot access to the socket if the thread is different. |
| 2656 | */ |
| 2657 | if (socket->tid != tid) |
| 2658 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2659 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2660 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2661 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2662 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2663 | lua_pushnil(L); |
| 2664 | lua_pushstring(L, "Can't connect"); |
| 2665 | return 2; |
| 2666 | } |
| 2667 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2668 | si = appctx->owner; |
| 2669 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2670 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2671 | /* Check if we run on the same thread than the xreator thread. |
| 2672 | * We cannot access to the socket if the thread is different. |
| 2673 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2674 | if (socket->tid != tid) { |
| 2675 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2676 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2677 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2678 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2679 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2680 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2681 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2682 | lua_pushnil(L); |
| 2683 | lua_pushstring(L, "Can't connect"); |
| 2684 | return 2; |
| 2685 | } |
| 2686 | |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2687 | appctx = __objt_appctx(s->si[0].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2688 | |
| 2689 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2690 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2691 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2692 | lua_pushinteger(L, 1); |
| 2693 | return 1; |
| 2694 | } |
| 2695 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2696 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2697 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2698 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2699 | } |
| 2700 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2701 | 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] | 2702 | return 0; |
| 2703 | } |
| 2704 | |
| 2705 | /* This function fail or initite the connection. */ |
| 2706 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2707 | { |
| 2708 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2709 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2710 | const char *ip; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2711 | struct hlua *hlua; |
| 2712 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2713 | int low, high; |
| 2714 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2715 | struct xref *peer; |
| 2716 | struct stream_interface *si; |
| 2717 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2718 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2719 | if (lua_gettop(L) < 2) |
| 2720 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2721 | |
| 2722 | /* Get args. */ |
| 2723 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2724 | |
| 2725 | /* Check if we run on the same thread than the xreator thread. |
| 2726 | * We cannot access to the socket if the thread is different. |
| 2727 | */ |
| 2728 | if (socket->tid != tid) |
| 2729 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2730 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2731 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2732 | if (lua_gettop(L) >= 3) { |
| 2733 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2734 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2735 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2736 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2737 | * that are not enclosed within square brackets. |
| 2738 | */ |
| 2739 | if (port > 0) { |
| 2740 | luaL_buffinit(L, &b); |
| 2741 | luaL_addstring(&b, ip); |
| 2742 | luaL_addchar(&b, ':'); |
| 2743 | luaL_pushresult(&b); |
| 2744 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2745 | } |
| 2746 | } |
| 2747 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2748 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2749 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2750 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2751 | lua_pushnil(L); |
| 2752 | return 1; |
| 2753 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2754 | |
| 2755 | /* Parse ip address. */ |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 2756 | 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] | 2757 | if (!addr) { |
| 2758 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2759 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2760 | } |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2761 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2762 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2763 | if (low == 0) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2764 | if (addr->ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2765 | if (port == -1) { |
| 2766 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2767 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2768 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2769 | ((struct sockaddr_in *)addr)->sin_port = htons(port); |
| 2770 | } else if (addr->ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2771 | if (port == -1) { |
| 2772 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2773 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2774 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2775 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(port); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2776 | } |
| 2777 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2778 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2779 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2780 | si = appctx->owner; |
| 2781 | s = si_strm(si); |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2782 | |
Willy Tarreau | 9b7587a | 2020-10-15 07:32:10 +0200 | [diff] [blame] | 2783 | if (!sockaddr_alloc(&s->target_addr, addr, sizeof(*addr))) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2784 | xref_unlock(&socket->xref, peer); |
| 2785 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
| 2786 | } |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2787 | s->flags |= SF_ADDR_SET; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2788 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2789 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2790 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2791 | if (!hlua) |
| 2792 | return 0; |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2793 | |
| 2794 | /* inform the stream that we want to be notified whenever the |
| 2795 | * connection completes. |
| 2796 | */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 2797 | si_cant_get(&s->si[0]); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2798 | si_rx_endp_more(&s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2799 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2800 | |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2801 | hlua->gc_count++; |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2802 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2803 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2804 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2805 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2806 | } |
| 2807 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2808 | |
| 2809 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2810 | /* Return yield waiting for connection. */ |
| 2811 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2812 | 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] | 2813 | |
| 2814 | return 0; |
| 2815 | } |
| 2816 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2817 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2818 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2819 | { |
| 2820 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2821 | struct xref *peer; |
| 2822 | struct appctx *appctx; |
| 2823 | struct stream_interface *si; |
| 2824 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2825 | |
| 2826 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2827 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2828 | |
| 2829 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2830 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2831 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2832 | lua_pushnil(L); |
| 2833 | return 1; |
| 2834 | } |
| 2835 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2836 | si = appctx->owner; |
| 2837 | s = si_strm(si); |
| 2838 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 2839 | s->target = &socket_ssl->obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2840 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2841 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2842 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2843 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2844 | |
| 2845 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2846 | { |
| 2847 | return 0; |
| 2848 | } |
| 2849 | |
| 2850 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2851 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2852 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2853 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2854 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2855 | struct xref *peer; |
| 2856 | struct appctx *appctx; |
| 2857 | struct stream_interface *si; |
| 2858 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2859 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2860 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2861 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2862 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2863 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2864 | /* convert the timeout to millis */ |
| 2865 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2866 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2867 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2868 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2869 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2870 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2871 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2872 | 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] | 2873 | |
| 2874 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2875 | if (tmout == 0) |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2876 | tmout++; /* very small timeouts are adjusted to a minimum of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2877 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2878 | /* Check if we run on the same thread than the xreator thread. |
| 2879 | * We cannot access to the socket if the thread is different. |
| 2880 | */ |
| 2881 | if (socket->tid != tid) |
| 2882 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2883 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2884 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2885 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2886 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2887 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2888 | WILL_LJMP(lua_error(L)); |
| 2889 | return 0; |
| 2890 | } |
| 2891 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2892 | si = appctx->owner; |
| 2893 | s = si_strm(si); |
| 2894 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2895 | s->sess->fe->timeout.connect = tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2896 | s->req.rto = tmout; |
| 2897 | s->req.wto = tmout; |
| 2898 | s->res.rto = tmout; |
| 2899 | s->res.wto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2900 | s->req.rex = tick_add_ifset(now_ms, tmout); |
| 2901 | s->req.wex = tick_add_ifset(now_ms, tmout); |
| 2902 | s->res.rex = tick_add_ifset(now_ms, tmout); |
| 2903 | s->res.wex = tick_add_ifset(now_ms, tmout); |
| 2904 | |
| 2905 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 2906 | task_queue(s->task); |
| 2907 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2908 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2909 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 2910 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 2911 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2912 | } |
| 2913 | |
| 2914 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2915 | { |
| 2916 | struct hlua_socket *socket; |
| 2917 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2918 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2919 | struct stream *strm; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2920 | |
| 2921 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2922 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2923 | hlua_pusherror(L, "socket: full stack"); |
| 2924 | goto out_fail_conf; |
| 2925 | } |
| 2926 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2927 | /* Create the object: obj[0] = userdata. */ |
| 2928 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2929 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2930 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2931 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2932 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2933 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2934 | /* Check if the various memory pools are initialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2935 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2936 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2937 | goto out_fail_conf; |
| 2938 | } |
| 2939 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2940 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2941 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2942 | lua_setmetatable(L, -2); |
| 2943 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2944 | /* Create the applet context */ |
Willy Tarreau | e612446 | 2021-09-13 10:07:38 +0200 | [diff] [blame] | 2945 | appctx = appctx_new(&update_applet); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2946 | if (!appctx) { |
| 2947 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2948 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2949 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2950 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2951 | appctx->ctx.hlua_cosocket.connected = 0; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2952 | appctx->ctx.hlua_cosocket.die = 0; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2953 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 2954 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2955 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2956 | /* Now create a session, task and stream for this applet */ |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 2957 | sess = session_new(socket_proxy, NULL, &appctx->obj_type); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2958 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2959 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2960 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2961 | } |
| 2962 | |
Christopher Faulet | 26256f8 | 2020-09-14 11:40:13 +0200 | [diff] [blame] | 2963 | strm = stream_new(sess, &appctx->obj_type, &BUF_NULL); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2964 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2965 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2966 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2967 | } |
| 2968 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2969 | /* Initialise cross reference between stream and Lua socket object. */ |
| 2970 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2971 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2972 | /* Configure "right" stream interface. this "si" is used to connect |
| 2973 | * and retrieve data from the server. The connection is initialized |
| 2974 | * with the "struct server". |
| 2975 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2976 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2977 | |
| 2978 | /* Force destination server. */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2979 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED; |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 2980 | strm->target = &socket_tcp->obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2981 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2982 | return 1; |
| 2983 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2984 | out_fail_stream: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 2985 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2986 | out_fail_sess: |
| 2987 | appctx_free(appctx); |
| 2988 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2989 | WILL_LJMP(lua_error(L)); |
| 2990 | return 0; |
| 2991 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2992 | |
| 2993 | /* |
| 2994 | * |
| 2995 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2996 | * Class Channel |
| 2997 | * |
| 2998 | * |
| 2999 | */ |
| 3000 | |
| 3001 | /* Returns the struct hlua_channel join to the class channel in the |
| 3002 | * stack entry "ud" or throws an argument error. |
| 3003 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3004 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3005 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3006 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3007 | } |
| 3008 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3009 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 3010 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3011 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 3012 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3013 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3014 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3015 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3016 | return 0; |
| 3017 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3018 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3019 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3020 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3021 | |
| 3022 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3023 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 3024 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3025 | return 1; |
| 3026 | } |
| 3027 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 3028 | /* Helper function returning a filter attached to a channel at the position <ud> |
| 3029 | * in the stack, filling the current offset and length of the filter. If no |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 3030 | * filter is attached, NULL is returned and <offset> and <len> are not |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 3031 | * initialized. |
| 3032 | */ |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3033 | static struct filter *hlua_channel_filter(lua_State *L, int ud, struct channel *chn, size_t *offset, size_t *len) |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 3034 | { |
| 3035 | struct filter *filter = NULL; |
| 3036 | |
| 3037 | if (lua_getfield(L, ud, "__filter") == LUA_TLIGHTUSERDATA) { |
| 3038 | struct hlua_flt_ctx *flt_ctx; |
| 3039 | |
| 3040 | filter = lua_touserdata (L, -1); |
| 3041 | flt_ctx = filter->ctx; |
| 3042 | if (hlua_filter_from_payload(filter)) { |
| 3043 | *offset = flt_ctx->cur_off[CHN_IDX(chn)]; |
| 3044 | *len = flt_ctx->cur_len[CHN_IDX(chn)]; |
| 3045 | } |
| 3046 | } |
| 3047 | |
| 3048 | lua_pop(L, 1); |
| 3049 | return filter; |
| 3050 | } |
| 3051 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3052 | /* Copies <len> bytes of data present in the channel's buffer, starting at the |
| 3053 | * offset <offset>, and put it in a LUA string variable. It is the caller |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3054 | * responsibility to ensure <len> and <offset> are valid. It always return the |
| 3055 | * length of the built string. <len> may be 0, in this case, an empty string is |
| 3056 | * created and 0 is returned. |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3057 | */ |
| 3058 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L, size_t offset, size_t len) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3059 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3060 | size_t block1, block2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3061 | luaL_Buffer b; |
| 3062 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3063 | block1 = len; |
| 3064 | if (block1 > b_contig_data(&chn->buf, b_peek_ofs(&chn->buf, offset))) |
| 3065 | block1 = b_contig_data(&chn->buf, b_peek_ofs(&chn->buf, offset)); |
| 3066 | block2 = len - block1; |
| 3067 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3068 | luaL_buffinit(L, &b); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3069 | luaL_addlstring(&b, b_peek(&chn->buf, offset), block1); |
| 3070 | if (block2) |
| 3071 | luaL_addlstring(&b, b_orig(&chn->buf), block2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3072 | luaL_pushresult(&b); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3073 | return len; |
| 3074 | } |
| 3075 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3076 | /* Inserts the string <str> to the channel's buffer at the offset <offset>. This |
| 3077 | * function returns -1 if data cannot be copied. Otherwise, it returns the |
| 3078 | * number of bytes copied. |
| 3079 | */ |
| 3080 | static int _hlua_channel_insert(struct channel *chn, lua_State *L, struct ist str, size_t offset) |
| 3081 | { |
| 3082 | int ret = 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3083 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3084 | /* Nothing to do, just return */ |
| 3085 | if (unlikely(istlen(str) == 0)) |
| 3086 | goto end; |
| 3087 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3088 | if (istlen(str) > c_room(chn)) { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3089 | ret = -1; |
| 3090 | goto end; |
| 3091 | } |
| 3092 | ret = b_insert_blk(&chn->buf, offset, istptr(str), istlen(str)); |
| 3093 | |
| 3094 | end: |
| 3095 | return ret; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3096 | } |
| 3097 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3098 | /* Removes <len> bytes of data at the absolute position <offset>. |
| 3099 | */ |
| 3100 | static void _hlua_channel_delete(struct channel *chn, size_t offset, size_t len) |
| 3101 | { |
| 3102 | size_t end = offset + len; |
| 3103 | |
| 3104 | if (b_peek(&chn->buf, end) != b_tail(&chn->buf)) |
| 3105 | b_move(&chn->buf, b_peek_ofs(&chn->buf, end), |
| 3106 | b_data(&chn->buf) - end, -len); |
| 3107 | b_sub(&chn->buf, len); |
| 3108 | } |
| 3109 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3110 | /* Copies input data in the channel's buffer. It is possible to set a specific |
| 3111 | * offset (0 by default) and a length (all remaining input data starting for the |
| 3112 | * offset by default). If there is not enough input data and more data can be |
| 3113 | * received, this function yields. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3114 | * |
| 3115 | * From an action, All input data are considered. For a filter, the offset and |
| 3116 | * the length of input data to consider are retrieved from the filter context. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3117 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3118 | __LJMP static int hlua_channel_get_data_yield(lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3119 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3120 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3121 | struct filter *filter; |
| 3122 | size_t input, output; |
| 3123 | int offset, len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3124 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3125 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3126 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3127 | output = co_data(chn); |
| 3128 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3129 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3130 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3131 | if (filter && !hlua_filter_from_payload(filter)) |
| 3132 | WILL_LJMP(lua_error(L)); |
| 3133 | |
| 3134 | offset = output; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3135 | if (lua_gettop(L) > 1) { |
| 3136 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3137 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3138 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3139 | offset += output; |
| 3140 | if (offset < output || offset > input + output) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3141 | lua_pushfstring(L, "offset out of range."); |
| 3142 | WILL_LJMP(lua_error(L)); |
| 3143 | } |
| 3144 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3145 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3146 | if (lua_gettop(L) == 3) { |
| 3147 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3148 | if (!len) |
| 3149 | goto dup; |
| 3150 | if (len == -1) |
| 3151 | len = global.tune.bufsize; |
| 3152 | if (len < 0) { |
| 3153 | lua_pushfstring(L, "length out of range."); |
| 3154 | WILL_LJMP(lua_error(L)); |
| 3155 | } |
| 3156 | } |
| 3157 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3158 | if (offset + len > output + input) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3159 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3160 | /* Yield waiting for more data, as requested */ |
| 3161 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_data_yield, TICK_ETERNITY, 0)); |
| 3162 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3163 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3164 | } |
| 3165 | |
| 3166 | dup: |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3167 | _hlua_channel_dup(chn, L, offset, len); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3168 | return 1; |
| 3169 | } |
| 3170 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3171 | /* Copies the first line (including the trailing LF) of input data in the |
| 3172 | * channel's buffer. It is possible to set a specific offset (0 by default) and |
| 3173 | * a length (all remaining input data starting for the offset by default). If |
| 3174 | * there is not enough input data and more data can be received, the function |
| 3175 | * yields. If a length is explicitly specified, no more data are |
| 3176 | * copied. Otherwise, if no LF is found and more data can be received, this |
| 3177 | * function yields. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3178 | * |
| 3179 | * From an action, All input data are considered. For a filter, the offset and |
| 3180 | * the length of input data to consider are retrieved from the filter context. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3181 | */ |
| 3182 | __LJMP static int hlua_channel_get_line_yield(lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3183 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3184 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3185 | struct filter *filter; |
| 3186 | size_t l, input, output; |
| 3187 | int offset, len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3188 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3189 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3190 | output = co_data(chn); |
| 3191 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3192 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3193 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3194 | if (filter && !hlua_filter_from_payload(filter)) |
| 3195 | WILL_LJMP(lua_error(L)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3196 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3197 | offset = output; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3198 | if (lua_gettop(L) > 1) { |
| 3199 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3200 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3201 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3202 | offset += output; |
| 3203 | if (offset < output || offset > input + output) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3204 | lua_pushfstring(L, "offset out of range."); |
| 3205 | WILL_LJMP(lua_error(L)); |
| 3206 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3207 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3208 | |
| 3209 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3210 | if (lua_gettop(L) == 3) { |
| 3211 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3212 | if (!len) |
| 3213 | goto dup; |
| 3214 | if (len == -1) |
| 3215 | len = global.tune.bufsize; |
| 3216 | if (len < 0) { |
| 3217 | lua_pushfstring(L, "length out of range."); |
| 3218 | WILL_LJMP(lua_error(L)); |
| 3219 | } |
| 3220 | } |
| 3221 | |
| 3222 | for (l = 0; l < len; l++) { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3223 | if (l + offset >= output + input) |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3224 | break; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3225 | if (*(b_peek(&chn->buf, offset + l)) == '\n') { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3226 | len = l+1; |
| 3227 | goto dup; |
| 3228 | } |
| 3229 | } |
| 3230 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3231 | if (offset + len > output + input) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3232 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3233 | /* Yield waiting for more data */ |
| 3234 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_line_yield, TICK_ETERNITY, 0)); |
| 3235 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3236 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3237 | } |
| 3238 | |
| 3239 | dup: |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3240 | _hlua_channel_dup(chn, L, offset, len); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3241 | return 1; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3242 | } |
| 3243 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3244 | /* [ DEPRECATED ] |
| 3245 | * |
| 3246 | * Duplicate all input data foud in the channel's buffer. The data are not |
| 3247 | * removed from the buffer. This function relies on _hlua_channel_dup(). |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3248 | * |
| 3249 | * From an action, All input data are considered. For a filter, the offset and |
| 3250 | * the length of input data to consider are retrieved from the filter context. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3251 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3252 | __LJMP static int hlua_channel_dup(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3253 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3254 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3255 | struct filter *filter; |
| 3256 | size_t offset, len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3257 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3258 | MAY_LJMP(check_args(L, 1, "dup")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3259 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3260 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3261 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3262 | WILL_LJMP(lua_error(L)); |
| 3263 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3264 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3265 | offset = co_data(chn); |
| 3266 | len = ci_data(chn); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3267 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3268 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3269 | if (filter && !hlua_filter_from_payload(filter)) |
| 3270 | WILL_LJMP(lua_error(L)); |
| 3271 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3272 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3273 | lua_pushnil(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3274 | return 1; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3275 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3276 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3277 | _hlua_channel_dup(chn, L, offset, len); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3278 | return 1; |
| 3279 | } |
| 3280 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3281 | /* [ DEPRECATED ] |
| 3282 | * |
| 3283 | * Get all input data foud in the channel's buffer. The data are removed from |
| 3284 | * the buffer after the copy. This function relies on _hlua_channel_dup() and |
| 3285 | * _hlua_channel_delete(). |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3286 | * |
| 3287 | * From an action, All input data are considered. For a filter, the offset and |
| 3288 | * the length of input data to consider are retrieved from the filter context. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3289 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3290 | __LJMP static int hlua_channel_get(lua_State *L) |
| 3291 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3292 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3293 | struct filter *filter; |
| 3294 | size_t offset, len; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3295 | int ret; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3296 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3297 | MAY_LJMP(check_args(L, 1, "get")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3298 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3299 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3300 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3301 | WILL_LJMP(lua_error(L)); |
| 3302 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3303 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3304 | offset = co_data(chn); |
| 3305 | len = ci_data(chn); |
| 3306 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3307 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3308 | if (filter && !hlua_filter_from_payload(filter)) |
| 3309 | WILL_LJMP(lua_error(L)); |
| 3310 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3311 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3312 | lua_pushnil(L); |
| 3313 | return 1; |
| 3314 | } |
| 3315 | |
| 3316 | ret = _hlua_channel_dup(chn, L, offset, len); |
| 3317 | _hlua_channel_delete(chn, offset, ret); |
| 3318 | return 1; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3319 | } |
| 3320 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3321 | /* This functions consumes and returns one line. If the channel is closed, |
| 3322 | * 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] | 3323 | * 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] | 3324 | * value. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3325 | * |
| 3326 | * From an action, All input data are considered. For a filter, the offset and |
| 3327 | * the length of input data to consider are retrieved from the filter context. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3328 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3329 | __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] | 3330 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3331 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3332 | struct filter *filter; |
| 3333 | size_t l, offset, len; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3334 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3335 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3336 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3337 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3338 | offset = co_data(chn); |
| 3339 | len = ci_data(chn); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3340 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3341 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3342 | if (filter && !hlua_filter_from_payload(filter)) |
| 3343 | WILL_LJMP(lua_error(L)); |
| 3344 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3345 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3346 | lua_pushnil(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3347 | return 1; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3348 | } |
| 3349 | |
| 3350 | for (l = 0; l < len; l++) { |
| 3351 | if (*(b_peek(&chn->buf, offset+l)) == '\n') { |
| 3352 | len = l+1; |
| 3353 | goto dup; |
| 3354 | } |
| 3355 | } |
| 3356 | |
| 3357 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3358 | /* Yield waiting for more data */ |
| 3359 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0)); |
| 3360 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3361 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3362 | dup: |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3363 | ret = _hlua_channel_dup(chn, L, offset, len); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3364 | _hlua_channel_delete(chn, offset, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3365 | return 1; |
| 3366 | } |
| 3367 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3368 | /* [ DEPRECATED ] |
| 3369 | * |
| 3370 | * Check arguments for the function "hlua_channel_getline_yield". |
| 3371 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3372 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 3373 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3374 | struct channel *chn; |
| 3375 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3376 | MAY_LJMP(check_args(L, 1, "getline")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3377 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3378 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3379 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3380 | WILL_LJMP(lua_error(L)); |
| 3381 | } |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3382 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 3383 | } |
| 3384 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3385 | /* Retrieves a given amount of input data at the given offset. By default all |
| 3386 | * available input data are returned. The offset may be negactive to start from |
| 3387 | * the end of input data. The length may be -1 to set it to the maximum buffer |
| 3388 | * size. |
| 3389 | */ |
| 3390 | __LJMP static int hlua_channel_get_data(lua_State *L) |
| 3391 | { |
| 3392 | struct channel *chn; |
| 3393 | |
| 3394 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3395 | WILL_LJMP(luaL_error(L, "'data' expects at most 2 arguments")); |
| 3396 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3397 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3398 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3399 | WILL_LJMP(lua_error(L)); |
| 3400 | } |
| 3401 | return MAY_LJMP(hlua_channel_get_data_yield(L, 0, 0)); |
| 3402 | } |
| 3403 | |
| 3404 | /* Retrieves a given amount of input data at the given offset. By default all |
| 3405 | * available input data are returned. The offset may be negactive to start from |
| 3406 | * the end of input data. The length may be -1 to set it to the maximum buffer |
| 3407 | * size. |
| 3408 | */ |
| 3409 | __LJMP static int hlua_channel_get_line(lua_State *L) |
| 3410 | { |
| 3411 | struct channel *chn; |
| 3412 | |
| 3413 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3414 | WILL_LJMP(luaL_error(L, "'line' expects at most 2 arguments")); |
| 3415 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3416 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3417 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3418 | WILL_LJMP(lua_error(L)); |
| 3419 | } |
| 3420 | return MAY_LJMP(hlua_channel_get_line_yield(L, 0, 0)); |
| 3421 | } |
| 3422 | |
| 3423 | /* Appends a string into the input side of channel. It returns the length of the |
| 3424 | * written string, or -1 if the channel is closed or if the buffer size is too |
| 3425 | * little for the data. 0 may be returned if nothing is copied. This function |
| 3426 | * does not yield. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3427 | * |
| 3428 | * For a filter, the context is updated on success. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3429 | */ |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3430 | __LJMP static int hlua_channel_append(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3431 | { |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3432 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3433 | struct filter *filter; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3434 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3435 | size_t sz, offset, len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3436 | int ret; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3437 | |
| 3438 | MAY_LJMP(check_args(L, 2, "append")); |
| 3439 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3440 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3441 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3442 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3443 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3444 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3445 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3446 | offset = co_data(chn); |
| 3447 | len = ci_data(chn); |
| 3448 | |
| 3449 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3450 | if (filter && !hlua_filter_from_payload(filter)) |
| 3451 | WILL_LJMP(lua_error(L)); |
| 3452 | |
| 3453 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3454 | if (ret > 0 && filter) { |
| 3455 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3456 | |
| 3457 | flt_update_offsets(filter, chn, ret); |
| 3458 | flt_ctx->cur_len[CHN_IDX(chn)] += ret; |
| 3459 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3460 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3461 | return 1; |
| 3462 | } |
| 3463 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3464 | /* Prepends a string into the input side of channel. It returns the length of the |
| 3465 | * written string, or -1 if the channel is closed or if the buffer size is too |
| 3466 | * little for the data. 0 may be returned if nothing is copied. This function |
| 3467 | * does not yield. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3468 | * |
| 3469 | * For a filter, the context is updated on success. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3470 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3471 | __LJMP static int hlua_channel_prepend(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3472 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3473 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3474 | struct filter *filter; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3475 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3476 | size_t sz, offset, len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3477 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3478 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3479 | MAY_LJMP(check_args(L, 2, "prepend")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3480 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3481 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 3482 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3483 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3484 | WILL_LJMP(lua_error(L)); |
| 3485 | } |
| 3486 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3487 | offset = co_data(chn); |
| 3488 | len = ci_data(chn); |
| 3489 | |
| 3490 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3491 | if (filter && !hlua_filter_from_payload(filter)) |
| 3492 | WILL_LJMP(lua_error(L)); |
| 3493 | |
| 3494 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3495 | if (ret > 0 && filter) { |
| 3496 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3497 | |
| 3498 | flt_update_offsets(filter, chn, ret); |
| 3499 | flt_ctx->cur_len[CHN_IDX(chn)] += ret; |
| 3500 | } |
| 3501 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3502 | lua_pushinteger(L, ret); |
| 3503 | return 1; |
| 3504 | } |
| 3505 | |
| 3506 | /* Inserts a given amount of input data at the given offset by a string |
| 3507 | * content. By default the string is appended at the end of input data. It |
| 3508 | * returns the length of the written string, or -1 if the channel is closed or |
| 3509 | * if the buffer size is too little for the data. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3510 | * |
| 3511 | * For a filter, the context is updated on success. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3512 | */ |
| 3513 | __LJMP static int hlua_channel_insert_data(lua_State *L) |
| 3514 | { |
| 3515 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3516 | struct filter *filter; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3517 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3518 | size_t sz, input, output; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3519 | int ret, offset; |
| 3520 | |
| 3521 | if (lua_gettop(L) < 2 || lua_gettop(L) > 3) |
| 3522 | WILL_LJMP(luaL_error(L, "'insert' expects at least 1 argument and at most 2 arguments")); |
| 3523 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3524 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3525 | |
| 3526 | output = co_data(chn); |
| 3527 | input = ci_data(chn); |
| 3528 | |
| 3529 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3530 | if (filter && !hlua_filter_from_payload(filter)) |
| 3531 | WILL_LJMP(lua_error(L)); |
| 3532 | |
| 3533 | offset = input + output; |
| 3534 | if (lua_gettop(L) > 2) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3535 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3536 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3537 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3538 | offset += output; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3539 | if (offset < output || offset > output + input) { |
| 3540 | lua_pushfstring(L, "offset out of range."); |
| 3541 | WILL_LJMP(lua_error(L)); |
| 3542 | } |
| 3543 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3544 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3545 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3546 | WILL_LJMP(lua_error(L)); |
| 3547 | } |
| 3548 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3549 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3550 | if (ret > 0 && filter) { |
| 3551 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3552 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3553 | flt_update_offsets(filter, chn, ret); |
| 3554 | flt_ctx->cur_len[CHN_IDX(chn)] += ret; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3555 | } |
| 3556 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3557 | lua_pushinteger(L, ret); |
| 3558 | return 1; |
| 3559 | } |
| 3560 | /* Replaces a given amount of input data at the given offset by a string |
| 3561 | * content. By default all remaining data are removed (offset = 0 and len = |
| 3562 | * -1). It returns the length of the written string, or -1 if the channel is |
| 3563 | * closed or if the buffer size is too little for the data. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3564 | * |
| 3565 | * For a filter, the context is updated on success. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3566 | */ |
| 3567 | __LJMP static int hlua_channel_set_data(lua_State *L) |
| 3568 | { |
| 3569 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3570 | struct filter *filter; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3571 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3572 | size_t sz, input, output; |
| 3573 | int ret, offset, len; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3574 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3575 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 3576 | WILL_LJMP(luaL_error(L, "'set' expects at least 1 argument and at most 3 arguments")); |
| 3577 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3578 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3579 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3580 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3581 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3582 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3583 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3584 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3585 | output = co_data(chn); |
| 3586 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3587 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3588 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3589 | if (filter && !hlua_filter_from_payload(filter)) |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3590 | WILL_LJMP(lua_error(L)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3591 | |
| 3592 | offset = output; |
| 3593 | if (lua_gettop(L) > 2) { |
| 3594 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3595 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3596 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3597 | offset += output; |
| 3598 | if (offset < output || offset > input + output) { |
| 3599 | lua_pushfstring(L, "offset out of range."); |
| 3600 | WILL_LJMP(lua_error(L)); |
| 3601 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3602 | } |
| 3603 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3604 | len = output + input - offset; |
| 3605 | if (lua_gettop(L) == 4) { |
| 3606 | len = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 3607 | if (!len) |
| 3608 | goto set; |
| 3609 | if (len == -1) |
| 3610 | len = output + input - offset; |
| 3611 | if (len < 0 || offset + len > output + input) { |
| 3612 | lua_pushfstring(L, "length out of range."); |
| 3613 | WILL_LJMP(lua_error(L)); |
| 3614 | } |
| 3615 | } |
| 3616 | |
| 3617 | set: |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3618 | /* Be sure we can copied the string once input data will be removed. */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3619 | if (sz > c_room(chn) + len) |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3620 | lua_pushinteger(L, -1); |
| 3621 | else { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3622 | _hlua_channel_delete(chn, offset, len); |
| 3623 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3624 | if (filter) { |
| 3625 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3626 | |
| 3627 | len -= (ret > 0 ? ret : 0); |
| 3628 | flt_update_offsets(filter, chn, -len); |
| 3629 | flt_ctx->cur_len[CHN_IDX(chn)] -= len; |
| 3630 | } |
| 3631 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3632 | lua_pushinteger(L, ret); |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3633 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3634 | return 1; |
| 3635 | } |
| 3636 | |
| 3637 | /* Removes a given amount of input data at the given offset. By default all |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3638 | * input data are removed (offset = 0 and len = -1). It returns the amount of |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3639 | * the removed data. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3640 | * |
| 3641 | * For a filter, the context is updated on success. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3642 | */ |
| 3643 | __LJMP static int hlua_channel_del_data(lua_State *L) |
| 3644 | { |
| 3645 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3646 | struct filter *filter; |
| 3647 | size_t input, output; |
| 3648 | int offset, len; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3649 | |
| 3650 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3651 | WILL_LJMP(luaL_error(L, "'remove' expects at most 2 arguments")); |
| 3652 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3653 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3654 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3655 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3656 | WILL_LJMP(lua_error(L)); |
| 3657 | } |
| 3658 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3659 | output = co_data(chn); |
| 3660 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3661 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3662 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3663 | if (filter && !hlua_filter_from_payload(filter)) |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3664 | WILL_LJMP(lua_error(L)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3665 | |
| 3666 | offset = output; |
| 3667 | if (lua_gettop(L) > 2) { |
| 3668 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3669 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3670 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3671 | offset += output; |
| 3672 | if (offset < output || offset > input + output) { |
| 3673 | lua_pushfstring(L, "offset out of range."); |
| 3674 | WILL_LJMP(lua_error(L)); |
| 3675 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3676 | } |
| 3677 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3678 | len = output + input - offset; |
| 3679 | if (lua_gettop(L) == 4) { |
| 3680 | len = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 3681 | if (!len) |
| 3682 | goto end; |
| 3683 | if (len == -1) |
| 3684 | len = output + input - offset; |
| 3685 | if (len < 0 || offset + len > output + input) { |
| 3686 | lua_pushfstring(L, "length out of range."); |
| 3687 | WILL_LJMP(lua_error(L)); |
| 3688 | } |
| 3689 | } |
| 3690 | |
| 3691 | _hlua_channel_delete(chn, offset, len); |
| 3692 | if (filter) { |
| 3693 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3694 | |
| 3695 | flt_update_offsets(filter, chn, -len); |
| 3696 | flt_ctx->cur_len[CHN_IDX(chn)] -= len; |
| 3697 | } |
| 3698 | |
| 3699 | end: |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3700 | lua_pushinteger(L, len); |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3701 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3702 | } |
| 3703 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3704 | /* Append data in the output side of the buffer. This data is immediately |
| 3705 | * sent. The function returns the amount of data written. If the buffer |
| 3706 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3707 | * if the channel is closed. |
| 3708 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3709 | __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] | 3710 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3711 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3712 | struct filter *filter; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3713 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3714 | size_t offset, len, sz; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3715 | int l, ret; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3716 | struct hlua *hlua; |
| 3717 | |
| 3718 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3719 | hlua = hlua_gethlua(L); |
| 3720 | if (!hlua) { |
| 3721 | lua_pushnil(L); |
| 3722 | return 1; |
| 3723 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3724 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3725 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3726 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 3727 | l = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3728 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3729 | offset = co_data(chn); |
| 3730 | len = ci_data(chn); |
| 3731 | |
| 3732 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3733 | if (filter && !hlua_filter_from_payload(filter)) |
| 3734 | WILL_LJMP(lua_error(L)); |
| 3735 | |
| 3736 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3737 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3738 | lua_pushinteger(L, -1); |
| 3739 | return 1; |
| 3740 | } |
| 3741 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3742 | len = c_room(chn); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3743 | if (len > sz -l) { |
| 3744 | if (filter) { |
| 3745 | lua_pushinteger(L, -1); |
| 3746 | return 1; |
| 3747 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3748 | len = sz - l; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3749 | } |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3750 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3751 | ret = _hlua_channel_insert(chn, L, ist2(str, len), offset); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3752 | if (ret == -1) { |
| 3753 | lua_pop(L, 1); |
| 3754 | lua_pushinteger(L, -1); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3755 | return 1; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3756 | } |
| 3757 | if (ret) { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3758 | if (filter) { |
| 3759 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3760 | |
| 3761 | |
| 3762 | flt_update_offsets(filter, chn, ret); |
| 3763 | FLT_OFF(filter, chn) += ret; |
| 3764 | flt_ctx->cur_off[CHN_IDX(chn)] += ret; |
| 3765 | } |
| 3766 | else |
| 3767 | c_adv(chn, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3768 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3769 | l += ret; |
| 3770 | lua_pop(L, 1); |
| 3771 | lua_pushinteger(L, l); |
| 3772 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3773 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3774 | if (l < sz) { |
| 3775 | /* Yield only if the channel's output is not empty. |
| 3776 | * Otherwise it means we cannot add more data. */ |
| 3777 | if (co_data(chn) == 0 || HLUA_CANT_YIELD(hlua_gethlua(L))) |
Christopher Faulet | 2e60aa4 | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3778 | return 1; |
| 3779 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3780 | /* If we are waiting for space in the response buffer, we |
| 3781 | * must set the flag WAKERESWR. This flag required the task |
| 3782 | * wake up if any activity is detected on the response buffer. |
| 3783 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3784 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3785 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3786 | else |
| 3787 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3788 | 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] | 3789 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3790 | |
| 3791 | return 1; |
| 3792 | } |
| 3793 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3794 | /* Just a wrapper of "_hlua_channel_send". This wrapper permits |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3795 | * yield the LUA process, and resume it without checking the |
| 3796 | * input arguments. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3797 | * |
| 3798 | * This function cannot be called from a filter. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3799 | */ |
| 3800 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3801 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3802 | struct channel *chn; |
| 3803 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3804 | MAY_LJMP(check_args(L, 2, "send")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3805 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3806 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3807 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3808 | WILL_LJMP(lua_error(L)); |
| 3809 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3810 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3811 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3812 | } |
| 3813 | |
| 3814 | /* This function forward and amount of butes. The data pass from |
| 3815 | * the input side of the buffer to the output side, and can be |
| 3816 | * forwarded. This function never fails. |
| 3817 | * |
| 3818 | * The Lua function takes an amount of bytes to be forwarded in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3819 | * input. It returns the number of bytes forwarded. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3820 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3821 | __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] | 3822 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3823 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3824 | struct filter *filter; |
| 3825 | size_t offset, len, fwd; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3826 | int l, max; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3827 | struct hlua *hlua; |
| 3828 | |
| 3829 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3830 | hlua = hlua_gethlua(L); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3831 | if (!hlua) { |
| 3832 | lua_pushnil(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3833 | return 1; |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3834 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3835 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3836 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3837 | fwd = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3838 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3839 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3840 | offset = co_data(chn); |
| 3841 | len = ci_data(chn); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3842 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3843 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3844 | if (filter && !hlua_filter_from_payload(filter)) |
| 3845 | WILL_LJMP(lua_error(L)); |
| 3846 | |
| 3847 | max = fwd - l; |
| 3848 | if (max > len) |
| 3849 | max = len; |
| 3850 | |
| 3851 | if (filter) { |
| 3852 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3853 | |
| 3854 | FLT_OFF(filter, chn) += max; |
| 3855 | flt_ctx->cur_off[CHN_IDX(chn)] += max; |
| 3856 | flt_ctx->cur_len[CHN_IDX(chn)] -= max; |
| 3857 | } |
| 3858 | else |
| 3859 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3860 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3861 | l += max; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3862 | lua_pop(L, 1); |
| 3863 | lua_pushinteger(L, l); |
| 3864 | |
| 3865 | /* Check if it miss bytes to forward. */ |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3866 | if (l < fwd) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3867 | /* The the input channel or the output channel are closed, we |
| 3868 | * must return the amount of data forwarded. |
| 3869 | */ |
Christopher Faulet | 2e60aa4 | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3870 | if (channel_input_closed(chn) || channel_output_closed(chn) || HLUA_CANT_YIELD(hlua_gethlua(L))) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3871 | return 1; |
| 3872 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3873 | /* If we are waiting for space data in the response buffer, we |
| 3874 | * must set the flag WAKERESWR. This flag required the task |
| 3875 | * wake up if any activity is detected on the response buffer. |
| 3876 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3877 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3878 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3879 | else |
| 3880 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3881 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3882 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3883 | 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] | 3884 | } |
| 3885 | |
| 3886 | return 1; |
| 3887 | } |
| 3888 | |
| 3889 | /* Just check the input and prepare the stack for the previous |
| 3890 | * function "hlua_channel_forward_yield" |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3891 | * |
| 3892 | * This function cannot be called from a filter. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3893 | */ |
| 3894 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3895 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3896 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3897 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3898 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3899 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3900 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3901 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3902 | WILL_LJMP(lua_error(L)); |
| 3903 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3904 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3905 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3906 | } |
| 3907 | |
| 3908 | /* Just returns the number of bytes available in the input |
| 3909 | * side of the buffer. This function never fails. |
| 3910 | */ |
| 3911 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3912 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3913 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3914 | struct filter *filter; |
| 3915 | size_t output, input; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3916 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3917 | MAY_LJMP(check_args(L, 1, "input")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3918 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3919 | |
| 3920 | output = co_data(chn); |
| 3921 | input = ci_data(chn); |
| 3922 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3923 | if (filter || !IS_HTX_STRM(chn_strm(chn))) |
| 3924 | lua_pushinteger(L, input); |
| 3925 | else { |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3926 | struct htx *htx = htxbuf(&chn->buf); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3927 | |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3928 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 3929 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3930 | return 1; |
| 3931 | } |
| 3932 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3933 | /* Returns true if the channel is full. */ |
| 3934 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3935 | { |
| 3936 | struct channel *chn; |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3937 | |
| 3938 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3939 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 0ec740e | 2020-02-26 11:59:19 +0100 | [diff] [blame] | 3940 | /* ignore the reserve, we are not on a producer side (ie in an |
| 3941 | * applet). |
| 3942 | */ |
| 3943 | lua_pushboolean(L, channel_full(chn, 0)); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3944 | return 1; |
| 3945 | } |
| 3946 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3947 | /* Returns true if the channel may still receive data. */ |
| 3948 | __LJMP static int hlua_channel_may_recv(lua_State *L) |
| 3949 | { |
| 3950 | struct channel *chn; |
| 3951 | |
| 3952 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 3953 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3954 | lua_pushboolean(L, (!channel_input_closed(chn) && channel_may_recv(chn))); |
| 3955 | return 1; |
| 3956 | } |
| 3957 | |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 3958 | /* Returns true if the channel is the response channel. */ |
| 3959 | __LJMP static int hlua_channel_is_resp(lua_State *L) |
| 3960 | { |
| 3961 | struct channel *chn; |
| 3962 | |
| 3963 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 3964 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3965 | |
| 3966 | lua_pushboolean(L, !!(chn->flags & CF_ISRESP)); |
| 3967 | return 1; |
| 3968 | } |
| 3969 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3970 | /* Just returns the number of bytes available in the output |
| 3971 | * side of the buffer. This function never fails. |
| 3972 | */ |
| 3973 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 3974 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3975 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3976 | size_t output, input; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3977 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3978 | MAY_LJMP(check_args(L, 1, "output")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3979 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3980 | |
| 3981 | output = co_data(chn); |
| 3982 | input = ci_data(chn); |
| 3983 | hlua_channel_filter(L, 1, chn, &output, &input); |
| 3984 | |
| 3985 | lua_pushinteger(L, output); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3986 | return 1; |
| 3987 | } |
| 3988 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3989 | /* |
| 3990 | * |
| 3991 | * |
| 3992 | * Class Fetches |
| 3993 | * |
| 3994 | * |
| 3995 | */ |
| 3996 | |
| 3997 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3998 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3999 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4000 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4001 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4002 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4003 | } |
| 4004 | |
| 4005 | /* This function creates and push in the stack a fetch object according |
| 4006 | * with a current TXN. |
| 4007 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4008 | 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] | 4009 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4010 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4011 | |
| 4012 | /* Check stack size. */ |
| 4013 | if (!lua_checkstack(L, 3)) |
| 4014 | return 0; |
| 4015 | |
| 4016 | /* Create the object: obj[0] = userdata. |
| 4017 | * Note that the base of the Fetches object is the |
| 4018 | * transaction object. |
| 4019 | */ |
| 4020 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4021 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4022 | lua_rawseti(L, -2, 0); |
| 4023 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4024 | hsmp->s = txn->s; |
| 4025 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 4026 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4027 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4028 | |
| 4029 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 4030 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 4031 | lua_setmetatable(L, -2); |
| 4032 | |
| 4033 | return 1; |
| 4034 | } |
| 4035 | |
| 4036 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 4037 | * It uses closure argument to store the associated sample-fetch. It |
| 4038 | * returns only one argument or throws an error. An error is thrown |
| 4039 | * only if an error is encountered during the argument parsing. If |
| 4040 | * the "sample-fetch" function fails, nil is returned. |
| 4041 | */ |
| 4042 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 4043 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4044 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 4045 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 4046 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4047 | int i; |
| 4048 | struct sample smp; |
| 4049 | |
| 4050 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4051 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4052 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4053 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4054 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4055 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 4056 | /* Check execution authorization. */ |
| 4057 | if (f->use & SMP_USE_HTTP_ANY && |
| 4058 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 4059 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 4060 | "is not available in Lua services", f->kw); |
| 4061 | WILL_LJMP(lua_error(L)); |
| 4062 | } |
| 4063 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4064 | /* Get extra arguments. */ |
| 4065 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 4066 | if (i >= ARGM_NBARGS) |
| 4067 | break; |
| 4068 | hlua_lua2arg(L, i + 2, &args[i]); |
| 4069 | } |
| 4070 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4071 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4072 | |
| 4073 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4074 | 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] | 4075 | |
| 4076 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 4077 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4078 | lua_pushfstring(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4079 | goto error; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4080 | } |
| 4081 | |
| 4082 | /* Initialise the sample. */ |
| 4083 | memset(&smp, 0, sizeof(smp)); |
| 4084 | |
| 4085 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 4086 | 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] | 4087 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4088 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4089 | lua_pushstring(L, ""); |
| 4090 | else |
| 4091 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4092 | goto end; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4093 | } |
| 4094 | |
| 4095 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4096 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4097 | hlua_smp2lua_str(L, &smp); |
| 4098 | else |
| 4099 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4100 | |
| 4101 | end: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4102 | free_args(args); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4103 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4104 | |
| 4105 | error: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4106 | free_args(args); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4107 | WILL_LJMP(lua_error(L)); |
| 4108 | return 0; /* Never reached */ |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4109 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4110 | |
| 4111 | /* |
| 4112 | * |
| 4113 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4114 | * Class Converters |
| 4115 | * |
| 4116 | * |
| 4117 | */ |
| 4118 | |
| 4119 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4120 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4121 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4122 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4123 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4124 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4125 | } |
| 4126 | |
| 4127 | /* This function creates and push in the stack a Converters object |
| 4128 | * according with a current TXN. |
| 4129 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4130 | 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] | 4131 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4132 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4133 | |
| 4134 | /* Check stack size. */ |
| 4135 | if (!lua_checkstack(L, 3)) |
| 4136 | return 0; |
| 4137 | |
| 4138 | /* Create the object: obj[0] = userdata. |
| 4139 | * Note that the base of the Converters object is the |
| 4140 | * same than the TXN object. |
| 4141 | */ |
| 4142 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4143 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4144 | lua_rawseti(L, -2, 0); |
| 4145 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4146 | hsmp->s = txn->s; |
| 4147 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 4148 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4149 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4150 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4151 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4152 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 4153 | lua_setmetatable(L, -2); |
| 4154 | |
| 4155 | return 1; |
| 4156 | } |
| 4157 | |
| 4158 | /* This function is an LUA binding. It is called with each converter. |
| 4159 | * It uses closure argument to store the associated converter. It |
| 4160 | * returns only one argument or throws an error. An error is thrown |
| 4161 | * only if an error is encountered during the argument parsing. If |
| 4162 | * the converter function function fails, nil is returned. |
| 4163 | */ |
| 4164 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 4165 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 4166 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4167 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 4168 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4169 | int i; |
| 4170 | struct sample smp; |
| 4171 | |
| 4172 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4173 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4174 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4175 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 4176 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4177 | |
| 4178 | /* Get extra arguments. */ |
| 4179 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 4180 | if (i >= ARGM_NBARGS) |
| 4181 | break; |
| 4182 | hlua_lua2arg(L, i + 3, &args[i]); |
| 4183 | } |
| 4184 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4185 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4186 | |
| 4187 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 4188 | 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] | 4189 | |
| 4190 | /* Run the special args checker. */ |
| 4191 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 4192 | hlua_pusherror(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4193 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4194 | } |
| 4195 | |
| 4196 | /* Initialise the sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4197 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4198 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 4199 | hlua_pusherror(L, "error in the input argument"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4200 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4201 | } |
| 4202 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 4203 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 4204 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4205 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4206 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4207 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4208 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4209 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4210 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4211 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 4212 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4213 | hlua_pusherror(L, "error during the input argument casting"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4214 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4215 | } |
| 4216 | |
| 4217 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4218 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4219 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4220 | lua_pushstring(L, ""); |
| 4221 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 4222 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4223 | goto end; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4224 | } |
| 4225 | |
| 4226 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4227 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4228 | hlua_smp2lua_str(L, &smp); |
| 4229 | else |
| 4230 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4231 | end: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4232 | free_args(args); |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 4233 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4234 | |
| 4235 | error: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4236 | free_args(args); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4237 | WILL_LJMP(lua_error(L)); |
| 4238 | return 0; /* Never reached */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4239 | } |
| 4240 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4241 | /* |
| 4242 | * |
| 4243 | * |
| 4244 | * Class AppletTCP |
| 4245 | * |
| 4246 | * |
| 4247 | */ |
| 4248 | |
| 4249 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4250 | * a class stream, otherwise it throws an error. |
| 4251 | */ |
| 4252 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 4253 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4254 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4255 | } |
| 4256 | |
| 4257 | /* This function creates and push in the stack an Applet object |
| 4258 | * according with a current TXN. |
| 4259 | */ |
| 4260 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 4261 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4262 | struct hlua_appctx *luactx; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4263 | struct stream_interface *si = ctx->owner; |
| 4264 | struct stream *s = si_strm(si); |
| 4265 | struct proxy *p = s->be; |
| 4266 | |
| 4267 | /* Check stack size. */ |
| 4268 | if (!lua_checkstack(L, 3)) |
| 4269 | return 0; |
| 4270 | |
| 4271 | /* Create the object: obj[0] = userdata. |
| 4272 | * Note that the base of the Converters object is the |
| 4273 | * same than the TXN object. |
| 4274 | */ |
| 4275 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4276 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4277 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4278 | luactx->appctx = ctx; |
| 4279 | luactx->htxn.s = s; |
| 4280 | luactx->htxn.p = p; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4281 | |
| 4282 | /* Create the "f" field that contains a list of fetches. */ |
| 4283 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4284 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4285 | return 0; |
| 4286 | lua_settable(L, -3); |
| 4287 | |
| 4288 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4289 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4290 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4291 | return 0; |
| 4292 | lua_settable(L, -3); |
| 4293 | |
| 4294 | /* Create the "c" field that contains a list of converters. */ |
| 4295 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4296 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4297 | return 0; |
| 4298 | lua_settable(L, -3); |
| 4299 | |
| 4300 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4301 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4302 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4303 | return 0; |
| 4304 | lua_settable(L, -3); |
| 4305 | |
| 4306 | /* Pop a class stream metatable and affect it to the table. */ |
| 4307 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 4308 | lua_setmetatable(L, -2); |
| 4309 | |
| 4310 | return 1; |
| 4311 | } |
| 4312 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4313 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 4314 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4315 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4316 | struct stream *s; |
| 4317 | const char *name; |
| 4318 | size_t len; |
| 4319 | struct sample smp; |
| 4320 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4321 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 4322 | 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] | 4323 | |
| 4324 | /* It is useles to retrieve the stream, but this function |
| 4325 | * runs only in a stream context. |
| 4326 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4327 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4328 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4329 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4330 | |
| 4331 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4332 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4333 | hlua_lua2smp(L, 3, &smp); |
| 4334 | |
| 4335 | /* Store the sample in a variable. */ |
| 4336 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4337 | |
| 4338 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 4339 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 4340 | else |
| 4341 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 4342 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4343 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4344 | } |
| 4345 | |
| 4346 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 4347 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4348 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4349 | struct stream *s; |
| 4350 | const char *name; |
| 4351 | size_t len; |
| 4352 | struct sample smp; |
| 4353 | |
| 4354 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4355 | |
| 4356 | /* It is useles to retrieve the stream, but this function |
| 4357 | * runs only in a stream context. |
| 4358 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4359 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4360 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4361 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4362 | |
| 4363 | /* Unset the variable. */ |
| 4364 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4365 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4366 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4367 | } |
| 4368 | |
| 4369 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 4370 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4371 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4372 | struct stream *s; |
| 4373 | const char *name; |
| 4374 | size_t len; |
| 4375 | struct sample smp; |
| 4376 | |
| 4377 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4378 | |
| 4379 | /* It is useles to retrieve the stream, but this function |
| 4380 | * runs only in a stream context. |
| 4381 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4382 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4383 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4384 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4385 | |
| 4386 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Willy Tarreau | e352b9d | 2021-09-03 11:52:38 +0200 | [diff] [blame] | 4387 | if (!vars_get_by_name(name, len, &smp, NULL)) { |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4388 | lua_pushnil(L); |
| 4389 | return 1; |
| 4390 | } |
| 4391 | |
| 4392 | return hlua_smp2lua(L, &smp); |
| 4393 | } |
| 4394 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4395 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 4396 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4397 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4398 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4399 | struct hlua *hlua; |
| 4400 | |
| 4401 | /* 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] | 4402 | if (!s->hlua) |
| 4403 | return 0; |
| 4404 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4405 | |
| 4406 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4407 | |
| 4408 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4409 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4410 | |
| 4411 | /* Get and store new value. */ |
| 4412 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4413 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4414 | |
| 4415 | return 0; |
| 4416 | } |
| 4417 | |
| 4418 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 4419 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4420 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4421 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4422 | struct hlua *hlua; |
| 4423 | |
| 4424 | /* 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] | 4425 | if (!s->hlua) { |
| 4426 | lua_pushnil(L); |
| 4427 | return 1; |
| 4428 | } |
| 4429 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4430 | |
| 4431 | /* Push configuration index in the stack. */ |
| 4432 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4433 | |
| 4434 | return 1; |
| 4435 | } |
| 4436 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4437 | /* If expected data not yet available, it returns a yield. This function |
| 4438 | * consumes the data in the buffer. It returns a string containing the |
| 4439 | * data. This string can be empty. |
| 4440 | */ |
| 4441 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 4442 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4443 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4444 | struct stream_interface *si = luactx->appctx->owner; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4445 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4446 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4447 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4448 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4449 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4450 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4451 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4452 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4453 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4454 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4455 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4456 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4457 | 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] | 4458 | } |
| 4459 | |
| 4460 | /* End of data: commit the total strings and return. */ |
| 4461 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4462 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4463 | return 1; |
| 4464 | } |
| 4465 | |
| 4466 | /* Ensure that the block 2 length is usable. */ |
| 4467 | if (ret == 1) |
| 4468 | len2 = 0; |
| 4469 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 4470 | /* don't check the max length read and don't check. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4471 | luaL_addlstring(&luactx->b, blk1, len1); |
| 4472 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4473 | |
| 4474 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4475 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4476 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4477 | return 1; |
| 4478 | } |
| 4479 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4480 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4481 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 4482 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4483 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4484 | |
| 4485 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4486 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4487 | |
| 4488 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 4489 | } |
| 4490 | |
| 4491 | /* If expected data not yet available, it returns a yield. This function |
| 4492 | * consumes the data in the buffer. It returns a string containing the |
| 4493 | * data. This string can be empty. |
| 4494 | */ |
| 4495 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 4496 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4497 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4498 | struct stream_interface *si = luactx->appctx->owner; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4499 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4500 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4501 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4502 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4503 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4504 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4505 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4506 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4507 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4508 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4509 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4510 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4511 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4512 | 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] | 4513 | } |
| 4514 | |
| 4515 | /* End of data: commit the total strings and return. */ |
| 4516 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4517 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4518 | return 1; |
| 4519 | } |
| 4520 | |
| 4521 | /* Ensure that the block 2 length is usable. */ |
| 4522 | if (ret == 1) |
| 4523 | len2 = 0; |
| 4524 | |
| 4525 | if (len == -1) { |
| 4526 | |
| 4527 | /* If len == -1, catenate all the data avalaile and |
| 4528 | * yield because we want to get all the data until |
| 4529 | * the end of data stream. |
| 4530 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4531 | luaL_addlstring(&luactx->b, blk1, len1); |
| 4532 | luaL_addlstring(&luactx->b, blk2, len2); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4533 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4534 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4535 | 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] | 4536 | |
| 4537 | } else { |
| 4538 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4539 | /* Copy the first block caping to the length required. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4540 | if (len1 > len) |
| 4541 | len1 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4542 | luaL_addlstring(&luactx->b, blk1, len1); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4543 | len -= len1; |
| 4544 | |
| 4545 | /* Copy the second block. */ |
| 4546 | if (len2 > len) |
| 4547 | len2 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4548 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4549 | len -= len2; |
| 4550 | |
| 4551 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4552 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4553 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4554 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4555 | if (len > 0) { |
| 4556 | lua_pushinteger(L, len); |
| 4557 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4558 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4559 | 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] | 4560 | } |
| 4561 | |
| 4562 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4563 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4564 | return 1; |
| 4565 | } |
| 4566 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4567 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4568 | hlua_pusherror(L, "Lua: internal error"); |
| 4569 | WILL_LJMP(lua_error(L)); |
| 4570 | return 0; |
| 4571 | } |
| 4572 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4573 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4574 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 4575 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4576 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4577 | int len = -1; |
| 4578 | |
| 4579 | if (lua_gettop(L) > 2) |
| 4580 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4581 | if (lua_gettop(L) >= 2) { |
| 4582 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4583 | lua_pop(L, 1); |
| 4584 | } |
| 4585 | |
| 4586 | /* Confirm or set the required length */ |
| 4587 | lua_pushinteger(L, len); |
| 4588 | |
| 4589 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4590 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4591 | |
| 4592 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 4593 | } |
| 4594 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4595 | /* Append data in the output side of the buffer. This data is immediately |
| 4596 | * sent. The function returns the amount of data written. If the buffer |
| 4597 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4598 | * if the channel is closed. |
| 4599 | */ |
| 4600 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4601 | { |
| 4602 | size_t len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4603 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4604 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4605 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4606 | struct stream_interface *si = luactx->appctx->owner; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4607 | struct channel *chn = si_ic(si); |
| 4608 | int max; |
| 4609 | |
| 4610 | /* Get the max amount of data which can write as input in the channel. */ |
| 4611 | max = channel_recv_max(chn); |
| 4612 | if (max > (len - l)) |
| 4613 | max = len - l; |
| 4614 | |
| 4615 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4616 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4617 | |
| 4618 | /* update counters. */ |
| 4619 | l += max; |
| 4620 | lua_pop(L, 1); |
| 4621 | lua_pushinteger(L, l); |
| 4622 | |
| 4623 | /* If some data is not send, declares the situation to the |
| 4624 | * applet, and returns a yield. |
| 4625 | */ |
| 4626 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 4627 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4628 | 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] | 4629 | } |
| 4630 | |
| 4631 | return 1; |
| 4632 | } |
| 4633 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4634 | /* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4635 | * yield the LUA process, and resume it without checking the |
| 4636 | * input arguments. |
| 4637 | */ |
| 4638 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 4639 | { |
| 4640 | MAY_LJMP(check_args(L, 2, "send")); |
| 4641 | lua_pushinteger(L, 0); |
| 4642 | |
| 4643 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 4644 | } |
| 4645 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4646 | /* |
| 4647 | * |
| 4648 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4649 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4650 | * |
| 4651 | * |
| 4652 | */ |
| 4653 | |
| 4654 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4655 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4656 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4657 | __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] | 4658 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4659 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4660 | } |
| 4661 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4662 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4663 | * according with a current TXN. |
| 4664 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4665 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4666 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4667 | struct hlua_appctx *luactx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 4668 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4669 | struct stream_interface *si = ctx->owner; |
| 4670 | struct stream *s = si_strm(si); |
| 4671 | struct proxy *px = s->be; |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4672 | struct htx *htx; |
| 4673 | struct htx_blk *blk; |
| 4674 | struct htx_sl *sl; |
| 4675 | struct ist path; |
| 4676 | unsigned long long len = 0; |
| 4677 | int32_t pos; |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 4678 | struct http_uri_parser parser; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4679 | |
| 4680 | /* Check stack size. */ |
| 4681 | if (!lua_checkstack(L, 3)) |
| 4682 | return 0; |
| 4683 | |
| 4684 | /* Create the object: obj[0] = userdata. |
| 4685 | * Note that the base of the Converters object is the |
| 4686 | * same than the TXN object. |
| 4687 | */ |
| 4688 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4689 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4690 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4691 | luactx->appctx = ctx; |
| 4692 | luactx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
| 4693 | luactx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */ |
| 4694 | luactx->htxn.s = s; |
| 4695 | luactx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4696 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4697 | /* Create the "f" field that contains a list of fetches. */ |
| 4698 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4699 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4700 | return 0; |
| 4701 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4702 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4703 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4704 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4705 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4706 | return 0; |
| 4707 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4708 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4709 | /* Create the "c" field that contains a list of converters. */ |
| 4710 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4711 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4712 | return 0; |
| 4713 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4714 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4715 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4716 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4717 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4718 | return 0; |
| 4719 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4720 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4721 | htx = htxbuf(&s->req.buf); |
| 4722 | blk = htx_get_first_blk(htx); |
Christopher Faulet | ea00973 | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 4723 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4724 | sl = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4725 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4726 | /* Stores the request method. */ |
| 4727 | lua_pushstring(L, "method"); |
| 4728 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 4729 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4730 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4731 | /* Stores the http version. */ |
| 4732 | lua_pushstring(L, "version"); |
| 4733 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 4734 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4735 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4736 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 4737 | * the array on the top of the stack. |
| 4738 | */ |
| 4739 | lua_pushstring(L, "headers"); |
| 4740 | htxn.s = s; |
| 4741 | htxn.p = px; |
| 4742 | htxn.dir = SMP_OPT_DIR_REQ; |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4743 | if (!hlua_http_get_headers(L, &htxn.s->txn->req)) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4744 | return 0; |
| 4745 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4746 | |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 4747 | parser = http_uri_parser_init(htx_sl_req_uri(sl)); |
| 4748 | path = http_parse_path(&parser); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 4749 | if (isttest(path)) { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4750 | char *p, *q, *end; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4751 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4752 | p = path.ptr; |
| 4753 | end = path.ptr + path.len; |
| 4754 | q = p; |
| 4755 | while (q < end && *q != '?') |
| 4756 | q++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4757 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4758 | /* Stores the request path. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4759 | lua_pushstring(L, "path"); |
| 4760 | lua_pushlstring(L, p, q - p); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4761 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4762 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4763 | /* Stores the query string. */ |
| 4764 | lua_pushstring(L, "qs"); |
| 4765 | if (*q == '?') |
| 4766 | q++; |
| 4767 | lua_pushlstring(L, q, end - q); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4768 | lua_settable(L, -3); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4769 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4770 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4771 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4772 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4773 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4774 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4775 | if (type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4776 | break; |
| 4777 | if (type == HTX_BLK_DATA) |
| 4778 | len += htx_get_blksz(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4779 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4780 | if (htx->extra != ULLONG_MAX) |
| 4781 | len += htx->extra; |
| 4782 | |
| 4783 | /* Stores the request path. */ |
| 4784 | lua_pushstring(L, "length"); |
| 4785 | lua_pushinteger(L, len); |
| 4786 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4787 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4788 | /* Create an empty array of HTTP request headers. */ |
| 4789 | lua_pushstring(L, "response"); |
| 4790 | lua_newtable(L); |
| 4791 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4792 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4793 | /* Pop a class stream metatable and affect it to the table. */ |
| 4794 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 4795 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4796 | |
| 4797 | return 1; |
| 4798 | } |
| 4799 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4800 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 4801 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4802 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4803 | struct stream *s; |
| 4804 | const char *name; |
| 4805 | size_t len; |
| 4806 | struct sample smp; |
| 4807 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4808 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 4809 | 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] | 4810 | |
| 4811 | /* It is useles to retrieve the stream, but this function |
| 4812 | * runs only in a stream context. |
| 4813 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4814 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4815 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4816 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4817 | |
| 4818 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4819 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4820 | hlua_lua2smp(L, 3, &smp); |
| 4821 | |
| 4822 | /* Store the sample in a variable. */ |
| 4823 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4824 | |
| 4825 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 4826 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 4827 | else |
| 4828 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 4829 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4830 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4831 | } |
| 4832 | |
| 4833 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 4834 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4835 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4836 | struct stream *s; |
| 4837 | const char *name; |
| 4838 | size_t len; |
| 4839 | struct sample smp; |
| 4840 | |
| 4841 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4842 | |
| 4843 | /* It is useles to retrieve the stream, but this function |
| 4844 | * runs only in a stream context. |
| 4845 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4846 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4847 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4848 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4849 | |
| 4850 | /* Unset the variable. */ |
| 4851 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4852 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4853 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4854 | } |
| 4855 | |
| 4856 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4857 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4858 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4859 | struct stream *s; |
| 4860 | const char *name; |
| 4861 | size_t len; |
| 4862 | struct sample smp; |
| 4863 | |
| 4864 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4865 | |
| 4866 | /* It is useles to retrieve the stream, but this function |
| 4867 | * runs only in a stream context. |
| 4868 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4869 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4870 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4871 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4872 | |
| 4873 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Willy Tarreau | e352b9d | 2021-09-03 11:52:38 +0200 | [diff] [blame] | 4874 | if (!vars_get_by_name(name, len, &smp, NULL)) { |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4875 | lua_pushnil(L); |
| 4876 | return 1; |
| 4877 | } |
| 4878 | |
| 4879 | return hlua_smp2lua(L, &smp); |
| 4880 | } |
| 4881 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4882 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 4883 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4884 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4885 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4886 | struct hlua *hlua; |
| 4887 | |
| 4888 | /* 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] | 4889 | if (!s->hlua) |
| 4890 | return 0; |
| 4891 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4892 | |
| 4893 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4894 | |
| 4895 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4896 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4897 | |
| 4898 | /* Get and store new value. */ |
| 4899 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4900 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4901 | |
| 4902 | return 0; |
| 4903 | } |
| 4904 | |
| 4905 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4906 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4907 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4908 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4909 | struct hlua *hlua; |
| 4910 | |
| 4911 | /* 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] | 4912 | if (!s->hlua) { |
| 4913 | lua_pushnil(L); |
| 4914 | return 1; |
| 4915 | } |
| 4916 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4917 | |
| 4918 | /* Push configuration index in the stack. */ |
| 4919 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4920 | |
| 4921 | return 1; |
| 4922 | } |
| 4923 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4924 | /* If expected data not yet available, it returns a yield. This function |
| 4925 | * consumes the data in the buffer. It returns a string containing the |
| 4926 | * data. This string can be empty. |
| 4927 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4928 | __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] | 4929 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4930 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4931 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4932 | struct channel *req = si_oc(si); |
| 4933 | struct htx *htx; |
| 4934 | struct htx_blk *blk; |
| 4935 | size_t count; |
| 4936 | int stop = 0; |
| 4937 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4938 | htx = htx_from_buf(&req->buf); |
| 4939 | count = co_data(req); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4940 | blk = htx_get_first_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 4941 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4942 | while (count && !stop && blk) { |
| 4943 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4944 | uint32_t sz = htx_get_blksz(blk); |
| 4945 | struct ist v; |
| 4946 | uint32_t vlen; |
| 4947 | char *nl; |
| 4948 | |
| 4949 | vlen = sz; |
| 4950 | if (vlen > count) { |
| 4951 | if (type != HTX_BLK_DATA) |
| 4952 | break; |
| 4953 | vlen = count; |
| 4954 | } |
| 4955 | |
| 4956 | switch (type) { |
| 4957 | case HTX_BLK_UNUSED: |
| 4958 | break; |
| 4959 | |
| 4960 | case HTX_BLK_DATA: |
| 4961 | v = htx_get_blk_value(htx, blk); |
| 4962 | v.len = vlen; |
| 4963 | nl = istchr(v, '\n'); |
| 4964 | if (nl != NULL) { |
| 4965 | stop = 1; |
| 4966 | vlen = nl - v.ptr + 1; |
| 4967 | } |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4968 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4969 | break; |
| 4970 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4971 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4972 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4973 | stop = 1; |
| 4974 | break; |
| 4975 | |
| 4976 | default: |
| 4977 | break; |
| 4978 | } |
| 4979 | |
| 4980 | co_set_data(req, co_data(req) - vlen); |
| 4981 | count -= vlen; |
| 4982 | if (sz == vlen) |
| 4983 | blk = htx_remove_blk(htx, blk); |
| 4984 | else { |
| 4985 | htx_cut_data_blk(htx, blk, vlen); |
| 4986 | break; |
| 4987 | } |
| 4988 | } |
| 4989 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 4990 | /* The message was fully consumed and no more data are expected |
| 4991 | * (EOM flag set). |
| 4992 | */ |
| 4993 | if (htx_is_empty(htx) && (htx->flags & HTX_FL_EOM)) |
| 4994 | stop = 1; |
| 4995 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4996 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4997 | if (!stop) { |
| 4998 | si_cant_get(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4999 | 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] | 5000 | } |
| 5001 | |
| 5002 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5003 | luaL_pushresult(&luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5004 | return 1; |
| 5005 | } |
| 5006 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5007 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5008 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5009 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5010 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5011 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5012 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5013 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5014 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5015 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5016 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5017 | } |
| 5018 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5019 | /* If expected data not yet available, it returns a yield. This function |
| 5020 | * consumes the data in the buffer. It returns a string containing the |
| 5021 | * data. This string can be empty. |
| 5022 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5023 | __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] | 5024 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5025 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 5026 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5027 | struct channel *req = si_oc(si); |
| 5028 | struct htx *htx; |
| 5029 | struct htx_blk *blk; |
| 5030 | size_t count; |
| 5031 | int len; |
| 5032 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5033 | htx = htx_from_buf(&req->buf); |
| 5034 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5035 | count = co_data(req); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 5036 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5037 | while (count && len && blk) { |
| 5038 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5039 | uint32_t sz = htx_get_blksz(blk); |
| 5040 | struct ist v; |
| 5041 | uint32_t vlen; |
| 5042 | |
| 5043 | vlen = sz; |
| 5044 | if (len > 0 && vlen > len) |
| 5045 | vlen = len; |
| 5046 | if (vlen > count) { |
| 5047 | if (type != HTX_BLK_DATA) |
| 5048 | break; |
| 5049 | vlen = count; |
| 5050 | } |
| 5051 | |
| 5052 | switch (type) { |
| 5053 | case HTX_BLK_UNUSED: |
| 5054 | break; |
| 5055 | |
| 5056 | case HTX_BLK_DATA: |
| 5057 | v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5058 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5059 | break; |
| 5060 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5061 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5062 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5063 | len = 0; |
| 5064 | break; |
| 5065 | |
| 5066 | default: |
| 5067 | break; |
| 5068 | } |
| 5069 | |
| 5070 | co_set_data(req, co_data(req) - vlen); |
| 5071 | count -= vlen; |
| 5072 | if (len > 0) |
| 5073 | len -= vlen; |
| 5074 | if (sz == vlen) |
| 5075 | blk = htx_remove_blk(htx, blk); |
| 5076 | else { |
| 5077 | htx_cut_data_blk(htx, blk, vlen); |
| 5078 | break; |
| 5079 | } |
| 5080 | } |
| 5081 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 5082 | /* The message was fully consumed and no more data are expected |
| 5083 | * (EOM flag set). |
| 5084 | */ |
| 5085 | if (htx_is_empty(htx) && (htx->flags & HTX_FL_EOM)) |
| 5086 | len = 0; |
| 5087 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 5088 | htx_to_buf(htx, &req->buf); |
| 5089 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5090 | /* If we are no other data available, yield waiting for new data. */ |
| 5091 | if (len) { |
| 5092 | if (len > 0) { |
| 5093 | lua_pushinteger(L, len); |
| 5094 | lua_replace(L, 2); |
| 5095 | } |
| 5096 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5097 | 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] | 5098 | } |
| 5099 | |
| 5100 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5101 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5102 | return 1; |
| 5103 | } |
| 5104 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5105 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5106 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 5107 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5108 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5109 | int len = -1; |
| 5110 | |
| 5111 | /* Check arguments. */ |
| 5112 | if (lua_gettop(L) > 2) |
| 5113 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 5114 | if (lua_gettop(L) >= 2) { |
| 5115 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5116 | lua_pop(L, 1); |
| 5117 | } |
| 5118 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5119 | lua_pushinteger(L, len); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5120 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5121 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5122 | luaL_buffinit(L, &luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5123 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5124 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5125 | } |
| 5126 | |
| 5127 | /* Append data in the output side of the buffer. This data is immediately |
| 5128 | * sent. The function returns the amount of data written. If the buffer |
| 5129 | * cannot contain the data, the function yields. The function returns -1 |
| 5130 | * if the channel is closed. |
| 5131 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5132 | __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] | 5133 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5134 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 5135 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5136 | struct channel *res = si_ic(si); |
| 5137 | struct htx *htx = htx_from_buf(&res->buf); |
| 5138 | const char *data; |
| 5139 | size_t len; |
| 5140 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 5141 | int max; |
| 5142 | |
Christopher Faulet | 9060fc0 | 2019-07-03 11:39:30 +0200 | [diff] [blame] | 5143 | max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx)); |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 5144 | if (!max) |
| 5145 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5146 | |
| 5147 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5148 | |
| 5149 | /* Get the max amount of data which can write as input in the channel. */ |
| 5150 | if (max > (len - l)) |
| 5151 | max = len - l; |
| 5152 | |
| 5153 | /* Copy data. */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 5154 | max = htx_add_data(htx, ist2(data + l, max)); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 5155 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5156 | |
| 5157 | /* update counters. */ |
| 5158 | l += max; |
| 5159 | lua_pop(L, 1); |
| 5160 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5161 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5162 | /* If some data is not send, declares the situation to the |
| 5163 | * applet, and returns a yield. |
| 5164 | */ |
| 5165 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 5166 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 5167 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5168 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5169 | 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] | 5170 | } |
| 5171 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5172 | htx_to_buf(htx, &res->buf); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5173 | return 1; |
| 5174 | } |
| 5175 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5176 | /* Just a wrapper of "hlua_applet_send_yield". This wrapper permits |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5177 | * yield the LUA process, and resume it without checking the |
| 5178 | * input arguments. |
| 5179 | */ |
| 5180 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 5181 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5182 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5183 | |
| 5184 | /* We want to send some data. Headers must be sent. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5185 | if (!(luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5186 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 5187 | WILL_LJMP(lua_error(L)); |
| 5188 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5189 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5190 | /* This integer is used for followinf the amount of data sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5191 | lua_pushinteger(L, 0); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5192 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5193 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5194 | } |
| 5195 | |
| 5196 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 5197 | { |
| 5198 | const char *name; |
| 5199 | int ret; |
| 5200 | |
| 5201 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 5202 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5203 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 5204 | |
| 5205 | /* Push in the stack the "response" entry. */ |
| 5206 | ret = lua_getfield(L, 1, "response"); |
| 5207 | if (ret != LUA_TTABLE) { |
| 5208 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 5209 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 5210 | WILL_LJMP(lua_error(L)); |
| 5211 | } |
| 5212 | |
| 5213 | /* check if the header is already registered if it is not |
| 5214 | * the case, register it. |
| 5215 | */ |
| 5216 | ret = lua_getfield(L, -1, name); |
| 5217 | if (ret == LUA_TNIL) { |
| 5218 | |
| 5219 | /* Entry not found. */ |
| 5220 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 5221 | |
| 5222 | /* Insert the new header name in the array in the top of the stack. |
| 5223 | * It left the new array in the top of the stack. |
| 5224 | */ |
| 5225 | lua_newtable(L); |
| 5226 | lua_pushvalue(L, 2); |
| 5227 | lua_pushvalue(L, -2); |
| 5228 | lua_settable(L, -4); |
| 5229 | |
| 5230 | } else if (ret != LUA_TTABLE) { |
| 5231 | |
| 5232 | /* corruption error. */ |
| 5233 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 5234 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 5235 | WILL_LJMP(lua_error(L)); |
| 5236 | } |
| 5237 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 5238 | /* Now the top of thestack is an array of values. We push |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5239 | * the header value as new entry. |
| 5240 | */ |
| 5241 | lua_pushvalue(L, 3); |
| 5242 | ret = lua_rawlen(L, -2); |
| 5243 | lua_rawseti(L, -2, ret + 1); |
| 5244 | lua_pushboolean(L, 1); |
| 5245 | return 1; |
| 5246 | } |
| 5247 | |
| 5248 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 5249 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5250 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5251 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5252 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5253 | |
| 5254 | if (status < 100 || status > 599) { |
| 5255 | lua_pushboolean(L, 0); |
| 5256 | return 1; |
| 5257 | } |
| 5258 | |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5259 | luactx->appctx->ctx.hlua_apphttp.status = status; |
| 5260 | luactx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5261 | lua_pushboolean(L, 1); |
| 5262 | return 1; |
| 5263 | } |
| 5264 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5265 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5266 | __LJMP static int hlua_applet_http_send_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5267 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5268 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 5269 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5270 | struct channel *res = si_ic(si); |
| 5271 | struct htx *htx; |
| 5272 | struct htx_sl *sl; |
| 5273 | struct h1m h1m; |
| 5274 | const char *status, *reason; |
| 5275 | const char *name, *value; |
| 5276 | size_t nlen, vlen; |
| 5277 | unsigned int flags; |
| 5278 | |
| 5279 | /* Send the message at once. */ |
| 5280 | htx = htx_from_buf(&res->buf); |
| 5281 | h1m_init_res(&h1m); |
| 5282 | |
| 5283 | /* Use the same http version than the request. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5284 | status = ultoa_r(luactx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size); |
| 5285 | reason = luactx->appctx->ctx.hlua_apphttp.reason; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5286 | if (reason == NULL) |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5287 | reason = http_get_reason(luactx->appctx->ctx.hlua_apphttp.status); |
| 5288 | if (luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5289 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 5290 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 5291 | } |
| 5292 | else { |
| 5293 | flags = HTX_SL_F_IS_RESP; |
| 5294 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 5295 | } |
| 5296 | if (!sl) { |
| 5297 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5298 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5299 | WILL_LJMP(lua_error(L)); |
| 5300 | } |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5301 | sl->info.res.status = luactx->appctx->ctx.hlua_apphttp.status; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5302 | |
| 5303 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 5304 | lua_pushvalue(L, 0); |
| 5305 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 5306 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5307 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5308 | WILL_LJMP(lua_error(L)); |
| 5309 | } |
| 5310 | |
| 5311 | /* Browse the list of headers. */ |
| 5312 | lua_pushnil(L); |
| 5313 | while(lua_next(L, -2) != 0) { |
| 5314 | /* We expect a string as -2. */ |
| 5315 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 5316 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5317 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5318 | lua_typename(L, lua_type(L, -2))); |
| 5319 | WILL_LJMP(lua_error(L)); |
| 5320 | } |
| 5321 | name = lua_tolstring(L, -2, &nlen); |
| 5322 | |
| 5323 | /* We expect an array as -1. */ |
| 5324 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 5325 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5326 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5327 | name, |
| 5328 | lua_typename(L, lua_type(L, -1))); |
| 5329 | WILL_LJMP(lua_error(L)); |
| 5330 | } |
| 5331 | |
| 5332 | /* Browse the table who is on the top of the stack. */ |
| 5333 | lua_pushnil(L); |
| 5334 | while(lua_next(L, -2) != 0) { |
| 5335 | int id; |
| 5336 | |
| 5337 | /* We expect a number as -2. */ |
| 5338 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 5339 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5340 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5341 | name, |
| 5342 | lua_typename(L, lua_type(L, -2))); |
| 5343 | WILL_LJMP(lua_error(L)); |
| 5344 | } |
| 5345 | id = lua_tointeger(L, -2); |
| 5346 | |
| 5347 | /* We expect a string as -2. */ |
| 5348 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 5349 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5350 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5351 | name, id, |
| 5352 | lua_typename(L, lua_type(L, -1))); |
| 5353 | WILL_LJMP(lua_error(L)); |
| 5354 | } |
| 5355 | value = lua_tolstring(L, -1, &vlen); |
| 5356 | |
| 5357 | /* Simple Protocol checks. */ |
| 5358 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5359 | h1_parse_xfer_enc_header(&h1m, ist2(value, vlen)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5360 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 5361 | struct ist v = ist2(value, vlen); |
| 5362 | int ret; |
| 5363 | |
| 5364 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 5365 | if (ret < 0) { |
| 5366 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5367 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5368 | name); |
| 5369 | WILL_LJMP(lua_error(L)); |
| 5370 | } |
| 5371 | else if (ret == 0) |
| 5372 | goto next; /* Skip it */ |
| 5373 | } |
| 5374 | |
| 5375 | /* Add a new header */ |
| 5376 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 5377 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5378 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5379 | name); |
| 5380 | WILL_LJMP(lua_error(L)); |
| 5381 | } |
| 5382 | next: |
| 5383 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5384 | lua_pop(L, 1); |
| 5385 | } |
| 5386 | |
| 5387 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5388 | lua_pop(L, 1); |
| 5389 | } |
| 5390 | |
| 5391 | if (h1m.flags & H1_MF_CHNK) |
| 5392 | h1m.flags &= ~H1_MF_CLEN; |
| 5393 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 5394 | h1m.flags |= H1_MF_XFER_LEN; |
| 5395 | |
| 5396 | /* Uset HTX start-line flags */ |
| 5397 | if (h1m.flags & H1_MF_XFER_ENC) |
| 5398 | flags |= HTX_SL_F_XFER_ENC; |
| 5399 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 5400 | flags |= HTX_SL_F_XFER_LEN; |
| 5401 | if (h1m.flags & H1_MF_CHNK) |
| 5402 | flags |= HTX_SL_F_CHNK; |
| 5403 | else if (h1m.flags & H1_MF_CLEN) |
| 5404 | flags |= HTX_SL_F_CLEN; |
| 5405 | if (h1m.body_len == 0) |
| 5406 | flags |= HTX_SL_F_BODYLESS; |
| 5407 | } |
| 5408 | sl->flags |= flags; |
| 5409 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 5410 | /* 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] | 5411 | * and the status code implies the presence of a message body, we must |
| 5412 | * announce a transfer encoding chunked. This is required by haproxy |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5413 | * for the keepalive compliance. If the applet announces a transfer-encoding |
| 5414 | * chunked itself, don't do anything. |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5415 | */ |
| 5416 | if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 && |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5417 | luactx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 5418 | luactx->appctx->ctx.hlua_apphttp.status != 204 && |
| 5419 | luactx->appctx->ctx.hlua_apphttp.status != 304) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5420 | /* Add a new header */ |
| 5421 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 5422 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 5423 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5424 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5425 | WILL_LJMP(lua_error(L)); |
| 5426 | } |
| 5427 | } |
| 5428 | |
| 5429 | /* Finalize headers. */ |
| 5430 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 5431 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5432 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5433 | WILL_LJMP(lua_error(L)); |
| 5434 | } |
| 5435 | |
| 5436 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 5437 | b_reset(&res->buf); |
| 5438 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 5439 | WILL_LJMP(lua_error(L)); |
| 5440 | } |
| 5441 | |
| 5442 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 5443 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5444 | |
| 5445 | /* Headers sent, set the flag. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5446 | luactx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5447 | return 0; |
| 5448 | |
| 5449 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5450 | /* We will build the status line and the headers of the HTTP response. |
| 5451 | * We will try send at once if its not possible, we give back the hand |
| 5452 | * waiting for more room. |
| 5453 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5454 | __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] | 5455 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5456 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 5457 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5458 | struct channel *res = si_ic(si); |
| 5459 | |
| 5460 | if (co_data(res)) { |
| 5461 | si_rx_room_blk(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5462 | 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] | 5463 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5464 | return MAY_LJMP(hlua_applet_http_send_response(L)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5465 | } |
| 5466 | |
| 5467 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5468 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5469 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5470 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5471 | } |
| 5472 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5473 | /* |
| 5474 | * |
| 5475 | * |
| 5476 | * Class HTTP |
| 5477 | * |
| 5478 | * |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5479 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5480 | |
| 5481 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 5482 | * a class stream, otherwise it throws an error. |
| 5483 | */ |
| 5484 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5485 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5486 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
| 5487 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5488 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5489 | /* This function creates and push in the stack a HTTP object |
| 5490 | * according with a current TXN. |
| 5491 | */ |
| 5492 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 5493 | { |
| 5494 | struct hlua_txn *htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5495 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5496 | /* Check stack size. */ |
| 5497 | if (!lua_checkstack(L, 3)) |
| 5498 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5499 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5500 | /* Create the object: obj[0] = userdata. |
| 5501 | * Note that the base of the Converters object is the |
| 5502 | * same than the TXN object. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5503 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5504 | lua_newtable(L); |
| 5505 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 5506 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5507 | |
| 5508 | htxn->s = txn->s; |
| 5509 | htxn->p = txn->p; |
Christopher Faulet | 256b69a | 2019-05-23 11:14:21 +0200 | [diff] [blame] | 5510 | htxn->dir = txn->dir; |
| 5511 | htxn->flags = txn->flags; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5512 | |
| 5513 | /* Pop a class stream metatable and affect it to the table. */ |
| 5514 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 5515 | lua_setmetatable(L, -2); |
| 5516 | |
| 5517 | return 1; |
| 5518 | } |
| 5519 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 5520 | /* This function creates and returns an array containing the status-line |
| 5521 | * elements. This function does not fails. |
| 5522 | */ |
| 5523 | __LJMP static int hlua_http_get_stline(lua_State *L, struct htx_sl *sl) |
| 5524 | { |
| 5525 | /* Create the table. */ |
| 5526 | lua_newtable(L); |
| 5527 | |
| 5528 | if (sl->flags & HTX_SL_F_IS_RESP) { |
| 5529 | lua_pushstring(L, "version"); |
| 5530 | lua_pushlstring(L, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); |
| 5531 | lua_settable(L, -3); |
| 5532 | lua_pushstring(L, "code"); |
| 5533 | lua_pushlstring(L, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl)); |
| 5534 | lua_settable(L, -3); |
| 5535 | lua_pushstring(L, "reason"); |
| 5536 | lua_pushlstring(L, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl)); |
| 5537 | lua_settable(L, -3); |
| 5538 | } |
| 5539 | else { |
| 5540 | lua_pushstring(L, "method"); |
| 5541 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 5542 | lua_settable(L, -3); |
| 5543 | lua_pushstring(L, "uri"); |
| 5544 | lua_pushlstring(L, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl)); |
| 5545 | lua_settable(L, -3); |
| 5546 | lua_pushstring(L, "version"); |
| 5547 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 5548 | lua_settable(L, -3); |
| 5549 | } |
| 5550 | return 1; |
| 5551 | } |
| 5552 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5553 | /* This function creates ans returns an array of HTTP headers. |
| 5554 | * This function does not fails. It is used as wrapper with the |
| 5555 | * 2 following functions. |
| 5556 | */ |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5557 | __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] | 5558 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5559 | struct htx *htx; |
| 5560 | int32_t pos; |
| 5561 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5562 | /* Create the table. */ |
| 5563 | lua_newtable(L); |
| 5564 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5565 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5566 | htx = htxbuf(&msg->chn->buf); |
| 5567 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 5568 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 5569 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5570 | struct ist n, v; |
| 5571 | int len; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5572 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5573 | if (type == HTX_BLK_HDR) { |
| 5574 | n = htx_get_blk_name(htx,blk); |
| 5575 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5576 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5577 | else if (type == HTX_BLK_EOH) |
| 5578 | break; |
| 5579 | else |
| 5580 | continue; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5581 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5582 | /* Check for existing entry: |
| 5583 | * assume that the table is on the top of the stack, and |
| 5584 | * push the key in the stack, the function lua_gettable() |
| 5585 | * perform the lookup. |
| 5586 | */ |
| 5587 | lua_pushlstring(L, n.ptr, n.len); |
| 5588 | lua_gettable(L, -2); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5589 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5590 | switch (lua_type(L, -1)) { |
| 5591 | case LUA_TNIL: |
| 5592 | /* Table not found, create it. */ |
| 5593 | lua_pop(L, 1); /* remove the nil value. */ |
| 5594 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 5595 | lua_newtable(L); /* create and push empty table. */ |
| 5596 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5597 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 5598 | 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] | 5599 | break; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5600 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5601 | case LUA_TTABLE: |
| 5602 | /* Entry found: push the value in the table. */ |
| 5603 | len = lua_rawlen(L, -1); |
| 5604 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5605 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 5606 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 5607 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5608 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5609 | default: |
| 5610 | /* Other cases are errors. */ |
| 5611 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 5612 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5613 | } |
| 5614 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5615 | return 1; |
| 5616 | } |
| 5617 | |
| 5618 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 5619 | { |
| 5620 | struct hlua_txn *htxn; |
| 5621 | |
| 5622 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 5623 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5624 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5625 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5626 | WILL_LJMP(lua_error(L)); |
| 5627 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5628 | return hlua_http_get_headers(L, &htxn->s->txn->req); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5629 | } |
| 5630 | |
| 5631 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 5632 | { |
| 5633 | struct hlua_txn *htxn; |
| 5634 | |
| 5635 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 5636 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5637 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5638 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5639 | WILL_LJMP(lua_error(L)); |
| 5640 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5641 | return hlua_http_get_headers(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5642 | } |
| 5643 | |
| 5644 | /* This function replace full header, or just a value in |
| 5645 | * the request or in the response. It is a wrapper fir the |
| 5646 | * 4 following functions. |
| 5647 | */ |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5648 | __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] | 5649 | { |
| 5650 | size_t name_len; |
| 5651 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5652 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5653 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5654 | struct htx *htx; |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5655 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5656 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5657 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5658 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 5659 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5660 | htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5661 | 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] | 5662 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5663 | return 0; |
| 5664 | } |
| 5665 | |
| 5666 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 5667 | { |
| 5668 | struct hlua_txn *htxn; |
| 5669 | |
| 5670 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5671 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5672 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5673 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5674 | WILL_LJMP(lua_error(L)); |
| 5675 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5676 | 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] | 5677 | } |
| 5678 | |
| 5679 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 5680 | { |
| 5681 | struct hlua_txn *htxn; |
| 5682 | |
| 5683 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 5684 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5685 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5686 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5687 | WILL_LJMP(lua_error(L)); |
| 5688 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5689 | 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] | 5690 | } |
| 5691 | |
| 5692 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 5693 | { |
| 5694 | struct hlua_txn *htxn; |
| 5695 | |
| 5696 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5697 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5698 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5699 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5700 | WILL_LJMP(lua_error(L)); |
| 5701 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5702 | 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] | 5703 | } |
| 5704 | |
| 5705 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 5706 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5707 | struct hlua_txn *htxn; |
| 5708 | |
| 5709 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 5710 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5711 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5712 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5713 | WILL_LJMP(lua_error(L)); |
| 5714 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5715 | 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] | 5716 | } |
| 5717 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5718 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5719 | * It is a wrapper for the 2 following functions. |
| 5720 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5721 | __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] | 5722 | { |
| 5723 | size_t len; |
| 5724 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5725 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5726 | struct http_hdr_ctx ctx; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5727 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5728 | ctx.blk = NULL; |
| 5729 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 5730 | http_remove_header(htx, &ctx); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5731 | return 0; |
| 5732 | } |
| 5733 | |
| 5734 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 5735 | { |
| 5736 | struct hlua_txn *htxn; |
| 5737 | |
| 5738 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 5739 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5740 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5741 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5742 | WILL_LJMP(lua_error(L)); |
| 5743 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5744 | return hlua_http_del_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5745 | } |
| 5746 | |
| 5747 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 5748 | { |
| 5749 | struct hlua_txn *htxn; |
| 5750 | |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5751 | MAY_LJMP(check_args(L, 2, "res_del_hdr")); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5752 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5753 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5754 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5755 | WILL_LJMP(lua_error(L)); |
| 5756 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5757 | return hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5758 | } |
| 5759 | |
| 5760 | /* This function adds an header. It is a wrapper used by |
| 5761 | * the 2 following functions. |
| 5762 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5763 | __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] | 5764 | { |
| 5765 | size_t name_len; |
| 5766 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5767 | size_t value_len; |
| 5768 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5769 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5770 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5771 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 5772 | ist2(value, value_len))); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5773 | return 0; |
| 5774 | } |
| 5775 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 5776 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 5777 | { |
| 5778 | struct hlua_txn *htxn; |
| 5779 | |
| 5780 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 5781 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5782 | |
| 5783 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5784 | WILL_LJMP(lua_error(L)); |
| 5785 | |
| 5786 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
| 5787 | } |
| 5788 | |
| 5789 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 5790 | { |
| 5791 | struct hlua_txn *htxn; |
| 5792 | |
| 5793 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 5794 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5795 | |
| 5796 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
| 5797 | WILL_LJMP(lua_error(L)); |
| 5798 | |
| 5799 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
| 5800 | } |
| 5801 | |
| 5802 | static int hlua_http_req_set_hdr(lua_State *L) |
| 5803 | { |
| 5804 | struct hlua_txn *htxn; |
| 5805 | |
| 5806 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 5807 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5808 | |
| 5809 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5810 | WILL_LJMP(lua_error(L)); |
| 5811 | |
| 5812 | hlua_http_del_hdr(L, &htxn->s->txn->req); |
| 5813 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
| 5814 | } |
| 5815 | |
| 5816 | static int hlua_http_res_set_hdr(lua_State *L) |
| 5817 | { |
| 5818 | struct hlua_txn *htxn; |
| 5819 | |
| 5820 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 5821 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5822 | |
| 5823 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
| 5824 | WILL_LJMP(lua_error(L)); |
| 5825 | |
| 5826 | hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
| 5827 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
| 5828 | } |
| 5829 | |
| 5830 | /* This function set the method. */ |
| 5831 | static int hlua_http_req_set_meth(lua_State *L) |
| 5832 | { |
| 5833 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5834 | size_t name_len; |
| 5835 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5836 | |
| 5837 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5838 | WILL_LJMP(lua_error(L)); |
| 5839 | |
| 5840 | lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1); |
| 5841 | return 1; |
| 5842 | } |
| 5843 | |
| 5844 | /* This function set the method. */ |
| 5845 | static int hlua_http_req_set_path(lua_State *L) |
| 5846 | { |
| 5847 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5848 | size_t name_len; |
| 5849 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5850 | |
| 5851 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5852 | WILL_LJMP(lua_error(L)); |
| 5853 | |
| 5854 | lua_pushboolean(L, http_req_replace_stline(1, name, name_len, htxn->p, htxn->s) != -1); |
| 5855 | return 1; |
| 5856 | } |
| 5857 | |
| 5858 | /* This function set the query-string. */ |
| 5859 | static int hlua_http_req_set_query(lua_State *L) |
| 5860 | { |
| 5861 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5862 | size_t name_len; |
| 5863 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5864 | |
| 5865 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5866 | WILL_LJMP(lua_error(L)); |
| 5867 | |
| 5868 | /* Check length. */ |
| 5869 | if (name_len > trash.size - 1) { |
| 5870 | lua_pushboolean(L, 0); |
| 5871 | return 1; |
| 5872 | } |
| 5873 | |
| 5874 | /* Add the mark question as prefix. */ |
| 5875 | chunk_reset(&trash); |
| 5876 | trash.area[trash.data++] = '?'; |
| 5877 | memcpy(trash.area + trash.data, name, name_len); |
| 5878 | trash.data += name_len; |
| 5879 | |
| 5880 | lua_pushboolean(L, |
| 5881 | http_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1); |
| 5882 | return 1; |
| 5883 | } |
| 5884 | |
| 5885 | /* This function set the uri. */ |
| 5886 | static int hlua_http_req_set_uri(lua_State *L) |
| 5887 | { |
| 5888 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5889 | size_t name_len; |
| 5890 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5891 | |
| 5892 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5893 | WILL_LJMP(lua_error(L)); |
| 5894 | |
| 5895 | lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1); |
| 5896 | return 1; |
| 5897 | } |
| 5898 | |
| 5899 | /* This function set the response code & optionally reason. */ |
| 5900 | static int hlua_http_res_set_status(lua_State *L) |
| 5901 | { |
| 5902 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5903 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5904 | const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 5905 | const struct ist reason = ist2(str, (str ? strlen(str) : 0)); |
| 5906 | |
| 5907 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
| 5908 | WILL_LJMP(lua_error(L)); |
| 5909 | |
| 5910 | http_res_set_status(code, reason, htxn->s); |
| 5911 | return 0; |
| 5912 | } |
| 5913 | |
| 5914 | /* |
| 5915 | * |
| 5916 | * |
| 5917 | * Class HTTPMessage |
| 5918 | * |
| 5919 | * |
| 5920 | */ |
| 5921 | |
| 5922 | /* Returns a struct http_msg if the stack entry "ud" is a class HTTPMessage, |
| 5923 | * otherwise it throws an error. |
| 5924 | */ |
| 5925 | __LJMP static struct http_msg *hlua_checkhttpmsg(lua_State *L, int ud) |
| 5926 | { |
| 5927 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_msg_ref)); |
| 5928 | } |
| 5929 | |
| 5930 | /* Creates and pushes on the stack a HTTP object according with a current TXN. |
| 5931 | */ |
Christopher Faulet | 78c3547 | 2020-02-26 17:14:08 +0100 | [diff] [blame] | 5932 | static int hlua_http_msg_new(lua_State *L, struct http_msg *msg) |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 5933 | { |
| 5934 | /* Check stack size. */ |
| 5935 | if (!lua_checkstack(L, 3)) |
| 5936 | return 0; |
| 5937 | |
| 5938 | lua_newtable(L); |
| 5939 | lua_pushlightuserdata(L, msg); |
| 5940 | lua_rawseti(L, -2, 0); |
| 5941 | |
| 5942 | /* Create the "channel" field that contains the request channel object. */ |
| 5943 | lua_pushstring(L, "channel"); |
| 5944 | if (!hlua_channel_new(L, msg->chn)) |
| 5945 | return 0; |
| 5946 | lua_rawset(L, -3); |
| 5947 | |
| 5948 | /* Pop a class stream metatable and affect it to the table. */ |
| 5949 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_msg_ref); |
| 5950 | lua_setmetatable(L, -2); |
| 5951 | |
| 5952 | return 1; |
| 5953 | } |
| 5954 | |
| 5955 | /* Helper function returning a filter attached to the HTTP message at the |
| 5956 | * position <ud> in the stack, filling the current offset and length of the |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 5957 | * filter. If no filter is attached, NULL is returned and <offset> and <len> are |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 5958 | * filled with output and input length respectively. |
| 5959 | */ |
| 5960 | static struct filter *hlua_http_msg_filter(lua_State *L, int ud, struct http_msg *msg, size_t *offset, size_t *len) |
| 5961 | { |
| 5962 | struct channel *chn = msg->chn; |
| 5963 | struct htx *htx = htxbuf(&chn->buf); |
| 5964 | struct filter *filter = NULL; |
| 5965 | |
| 5966 | *offset = co_data(msg->chn); |
| 5967 | *len = htx->data - co_data(msg->chn); |
| 5968 | |
| 5969 | if (lua_getfield(L, ud, "__filter") == LUA_TLIGHTUSERDATA) { |
| 5970 | filter = lua_touserdata (L, -1); |
| 5971 | if (msg->msg_state >= HTTP_MSG_DATA) { |
| 5972 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 5973 | |
| 5974 | *offset = flt_ctx->cur_off[CHN_IDX(chn)]; |
| 5975 | *len = flt_ctx->cur_len[CHN_IDX(chn)]; |
| 5976 | } |
| 5977 | } |
| 5978 | |
| 5979 | lua_pop(L, 1); |
| 5980 | return filter; |
| 5981 | } |
| 5982 | |
| 5983 | /* Returns true if the channel attached to the HTTP message is the response |
| 5984 | * channel. |
| 5985 | */ |
| 5986 | __LJMP static int hlua_http_msg_is_resp(lua_State *L) |
| 5987 | { |
| 5988 | struct http_msg *msg; |
| 5989 | |
| 5990 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 5991 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 5992 | |
| 5993 | lua_pushboolean(L, !!(msg->chn->flags & CF_ISRESP)); |
| 5994 | return 1; |
| 5995 | } |
| 5996 | |
| 5997 | /* Returns an array containing the elements status-line of the HTTP message. It relies |
| 5998 | * on hlua_http_get_stline(). |
| 5999 | */ |
| 6000 | __LJMP static int hlua_http_msg_get_stline(lua_State *L) |
| 6001 | { |
| 6002 | struct http_msg *msg; |
| 6003 | struct htx *htx; |
| 6004 | struct htx_sl *sl; |
| 6005 | |
| 6006 | MAY_LJMP(check_args(L, 1, "get_stline")); |
| 6007 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6008 | |
| 6009 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6010 | WILL_LJMP(lua_error(L)); |
| 6011 | |
| 6012 | htx = htxbuf(&msg->chn->buf); |
| 6013 | sl = http_get_stline(htx); |
| 6014 | if (!sl) |
| 6015 | return 0; |
| 6016 | return hlua_http_get_stline(L, sl); |
| 6017 | } |
| 6018 | |
| 6019 | /* Returns an array containing all headers of the HTTP message. it relies on |
| 6020 | * hlua_http_get_headers(). |
| 6021 | */ |
| 6022 | __LJMP static int hlua_http_msg_get_headers(lua_State *L) |
| 6023 | { |
| 6024 | struct http_msg *msg; |
| 6025 | |
| 6026 | MAY_LJMP(check_args(L, 1, "get_headers")); |
| 6027 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6028 | |
| 6029 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6030 | WILL_LJMP(lua_error(L)); |
| 6031 | |
| 6032 | return hlua_http_get_headers(L, msg); |
| 6033 | } |
| 6034 | |
| 6035 | /* Deletes all occurrences of an header in the HTTP message matching on its |
| 6036 | * name. It relies on hlua_http_del_hdr(). |
| 6037 | */ |
| 6038 | __LJMP static int hlua_http_msg_del_hdr(lua_State *L) |
| 6039 | { |
| 6040 | struct http_msg *msg; |
| 6041 | |
| 6042 | MAY_LJMP(check_args(L, 2, "del_header")); |
| 6043 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6044 | |
| 6045 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6046 | WILL_LJMP(lua_error(L)); |
| 6047 | |
| 6048 | return hlua_http_del_hdr(L, msg); |
| 6049 | } |
| 6050 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6051 | /* Matches the full value line of all occurrences of an header in the HTTP |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6052 | * message given its name against a regex and replaces it if it matches. It |
| 6053 | * relies on hlua_http_rep_hdr(). |
| 6054 | */ |
| 6055 | __LJMP static int hlua_http_msg_rep_hdr(lua_State *L) |
| 6056 | { |
| 6057 | struct http_msg *msg; |
| 6058 | |
| 6059 | MAY_LJMP(check_args(L, 4, "rep_header")); |
| 6060 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6061 | |
| 6062 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6063 | WILL_LJMP(lua_error(L)); |
| 6064 | |
| 6065 | return hlua_http_rep_hdr(L, msg, 1); |
| 6066 | } |
| 6067 | |
| 6068 | /* Matches all comma-separated values of all occurences of an header in the HTTP |
| 6069 | * message given its name against a regex and replaces it if it matches. It |
| 6070 | * relies on hlua_http_rep_hdr(). |
| 6071 | */ |
| 6072 | __LJMP static int hlua_http_msg_rep_val(lua_State *L) |
| 6073 | { |
| 6074 | struct http_msg *msg; |
| 6075 | |
| 6076 | MAY_LJMP(check_args(L, 4, "rep_value")); |
| 6077 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6078 | |
| 6079 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6080 | WILL_LJMP(lua_error(L)); |
| 6081 | |
| 6082 | return hlua_http_rep_hdr(L, msg, 0); |
| 6083 | } |
| 6084 | |
| 6085 | /* Add an header in the HTTP message. It relies on hlua_http_add_hdr() */ |
| 6086 | __LJMP static int hlua_http_msg_add_hdr(lua_State *L) |
| 6087 | { |
| 6088 | struct http_msg *msg; |
| 6089 | |
| 6090 | MAY_LJMP(check_args(L, 3, "add_header")); |
| 6091 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6092 | |
| 6093 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6094 | WILL_LJMP(lua_error(L)); |
| 6095 | |
| 6096 | return hlua_http_add_hdr(L, msg); |
| 6097 | } |
| 6098 | |
| 6099 | /* Add an header in the HTTP message removing existing headers with the same |
| 6100 | * name. It relies on hlua_http_del_hdr() and hlua_http_add_hdr(). |
| 6101 | */ |
| 6102 | __LJMP static int hlua_http_msg_set_hdr(lua_State *L) |
| 6103 | { |
| 6104 | struct http_msg *msg; |
| 6105 | |
| 6106 | MAY_LJMP(check_args(L, 3, "set_header")); |
| 6107 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6108 | |
| 6109 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6110 | WILL_LJMP(lua_error(L)); |
| 6111 | |
| 6112 | hlua_http_del_hdr(L, msg); |
| 6113 | return hlua_http_add_hdr(L, msg); |
| 6114 | } |
| 6115 | |
| 6116 | /* Rewrites the request method. It relies on http_req_replace_stline(). */ |
| 6117 | __LJMP static int hlua_http_msg_set_meth(lua_State *L) |
| 6118 | { |
| 6119 | struct stream *s; |
| 6120 | struct http_msg *msg; |
| 6121 | const char *name; |
| 6122 | size_t name_len; |
| 6123 | |
| 6124 | MAY_LJMP(check_args(L, 2, "set_method")); |
| 6125 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6126 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6127 | |
| 6128 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6129 | WILL_LJMP(lua_error(L)); |
| 6130 | |
| 6131 | s = chn_strm(msg->chn); |
| 6132 | lua_pushboolean(L, http_req_replace_stline(0, name, name_len, s->be, s) != -1); |
| 6133 | return 1; |
| 6134 | } |
| 6135 | |
| 6136 | /* Rewrites the request path. It relies on http_req_replace_stline(). */ |
| 6137 | __LJMP static int hlua_http_msg_set_path(lua_State *L) |
| 6138 | { |
| 6139 | struct stream *s; |
| 6140 | struct http_msg *msg; |
| 6141 | const char *name; |
| 6142 | size_t name_len; |
| 6143 | |
| 6144 | MAY_LJMP(check_args(L, 2, "set_path")); |
| 6145 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6146 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6147 | |
| 6148 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6149 | WILL_LJMP(lua_error(L)); |
| 6150 | |
| 6151 | s = chn_strm(msg->chn); |
| 6152 | lua_pushboolean(L, http_req_replace_stline(1, name, name_len, s->be, s) != -1); |
| 6153 | return 1; |
| 6154 | } |
| 6155 | |
| 6156 | /* Rewrites the request query-string. It relies on http_req_replace_stline(). */ |
| 6157 | __LJMP static int hlua_http_msg_set_query(lua_State *L) |
| 6158 | { |
| 6159 | struct stream *s; |
| 6160 | struct http_msg *msg; |
| 6161 | const char *name; |
| 6162 | size_t name_len; |
| 6163 | |
| 6164 | MAY_LJMP(check_args(L, 2, "set_query")); |
| 6165 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6166 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6167 | |
| 6168 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6169 | WILL_LJMP(lua_error(L)); |
| 6170 | |
| 6171 | /* Check length. */ |
| 6172 | if (name_len > trash.size - 1) { |
| 6173 | lua_pushboolean(L, 0); |
| 6174 | return 1; |
| 6175 | } |
| 6176 | |
| 6177 | /* Add the mark question as prefix. */ |
| 6178 | chunk_reset(&trash); |
| 6179 | trash.area[trash.data++] = '?'; |
| 6180 | memcpy(trash.area + trash.data, name, name_len); |
| 6181 | trash.data += name_len; |
| 6182 | |
| 6183 | s = chn_strm(msg->chn); |
| 6184 | lua_pushboolean(L, http_req_replace_stline(2, trash.area, trash.data, s->be, s) != -1); |
| 6185 | return 1; |
| 6186 | } |
| 6187 | |
| 6188 | /* Rewrites the request URI. It relies on http_req_replace_stline(). */ |
| 6189 | __LJMP static int hlua_http_msg_set_uri(lua_State *L) |
| 6190 | { |
| 6191 | struct stream *s; |
| 6192 | struct http_msg *msg; |
| 6193 | const char *name; |
| 6194 | size_t name_len; |
| 6195 | |
| 6196 | MAY_LJMP(check_args(L, 2, "set_uri")); |
| 6197 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6198 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6199 | |
| 6200 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6201 | WILL_LJMP(lua_error(L)); |
| 6202 | |
| 6203 | s = chn_strm(msg->chn); |
| 6204 | lua_pushboolean(L, http_req_replace_stline(3, name, name_len, s->be, s) != -1); |
| 6205 | return 1; |
| 6206 | } |
| 6207 | |
| 6208 | /* Rewrites the response status code. It relies on http_res_set_status(). */ |
| 6209 | __LJMP static int hlua_http_msg_set_status(lua_State *L) |
| 6210 | { |
| 6211 | struct http_msg *msg; |
| 6212 | unsigned int code; |
| 6213 | const char *reason; |
| 6214 | size_t reason_len; |
| 6215 | |
| 6216 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6217 | code = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6218 | reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, &reason_len)); |
| 6219 | |
| 6220 | if (!(msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6221 | WILL_LJMP(lua_error(L)); |
| 6222 | |
| 6223 | lua_pushboolean(L, http_res_set_status(code, ist2(reason, reason_len), chn_strm(msg->chn)) != -1); |
| 6224 | return 1; |
| 6225 | } |
| 6226 | |
| 6227 | /* Returns true if the HTTP message is full. */ |
| 6228 | __LJMP static int hlua_http_msg_is_full(lua_State *L) |
| 6229 | { |
| 6230 | struct http_msg *msg; |
| 6231 | |
| 6232 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 6233 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6234 | lua_pushboolean(L, channel_full(msg->chn, 0)); |
| 6235 | return 1; |
| 6236 | } |
| 6237 | |
| 6238 | /* Returns true if the HTTP message may still receive data. */ |
| 6239 | __LJMP static int hlua_http_msg_may_recv(lua_State *L) |
| 6240 | { |
| 6241 | struct http_msg *msg; |
| 6242 | struct htx *htx; |
| 6243 | |
| 6244 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 6245 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6246 | htx = htxbuf(&msg->chn->buf); |
| 6247 | lua_pushboolean(L, (htx_expect_more(htx) && !channel_input_closed(msg->chn) && channel_may_recv(msg->chn))); |
| 6248 | return 1; |
| 6249 | } |
| 6250 | |
| 6251 | /* Returns true if the HTTP message EOM was received */ |
| 6252 | __LJMP static int hlua_http_msg_is_eom(lua_State *L) |
| 6253 | { |
| 6254 | struct http_msg *msg; |
| 6255 | struct htx *htx; |
| 6256 | |
| 6257 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 6258 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6259 | htx = htxbuf(&msg->chn->buf); |
| 6260 | lua_pushboolean(L, !htx_expect_more(htx)); |
| 6261 | return 1; |
| 6262 | } |
| 6263 | |
| 6264 | /* Returns the number of bytes available in the input side of the HTTP |
| 6265 | * message. This function never fails. |
| 6266 | */ |
| 6267 | __LJMP static int hlua_http_msg_get_in_len(lua_State *L) |
| 6268 | { |
| 6269 | struct http_msg *msg; |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6270 | size_t output, input; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6271 | |
| 6272 | MAY_LJMP(check_args(L, 1, "input")); |
| 6273 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6274 | hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6275 | lua_pushinteger(L, input); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6276 | return 1; |
| 6277 | } |
| 6278 | |
| 6279 | /* Returns the number of bytes available in the output side of the HTTP |
| 6280 | * message. This function never fails. |
| 6281 | */ |
| 6282 | __LJMP static int hlua_http_msg_get_out_len(lua_State *L) |
| 6283 | { |
| 6284 | struct http_msg *msg; |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6285 | size_t output, input; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6286 | |
| 6287 | MAY_LJMP(check_args(L, 1, "output")); |
| 6288 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6289 | hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6290 | lua_pushinteger(L, output); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6291 | return 1; |
| 6292 | } |
| 6293 | |
| 6294 | /* Copies at most <len> bytes of DATA blocks from the HTTP message <msg> |
| 6295 | * starting at the offset <offset> and put it in a string LUA variables. It |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6296 | * returns the built string length. It stops on the first non-DATA HTX |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6297 | * block. This function is called during the payload filtering, so the headers |
| 6298 | * are already scheduled for output (from the filter point of view). |
| 6299 | */ |
| 6300 | static int _hlua_http_msg_dup(struct http_msg *msg, lua_State *L, size_t offset, size_t len) |
| 6301 | { |
| 6302 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 6303 | struct htx_blk *blk; |
| 6304 | struct htx_ret htxret; |
| 6305 | luaL_Buffer b; |
| 6306 | int ret = 0; |
| 6307 | |
| 6308 | luaL_buffinit(L, &b); |
| 6309 | htxret = htx_find_offset(htx, offset); |
| 6310 | for (blk = htxret.blk, offset = htxret.ret; blk && len; blk = htx_get_next_blk(htx, blk)) { |
| 6311 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 6312 | struct ist v; |
| 6313 | |
| 6314 | switch (type) { |
| 6315 | case HTX_BLK_UNUSED: |
| 6316 | break; |
| 6317 | |
| 6318 | case HTX_BLK_DATA: |
| 6319 | v = htx_get_blk_value(htx, blk); |
Tim Duesterhus | b113b5c | 2021-09-15 13:58:44 +0200 | [diff] [blame] | 6320 | v = istadv(v, offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6321 | if (v.len > len) |
| 6322 | v.len = len; |
| 6323 | |
| 6324 | luaL_addlstring(&b, v.ptr, v.len); |
| 6325 | ret += v.len; |
| 6326 | break; |
| 6327 | |
| 6328 | default: |
| 6329 | if (!ret) { |
| 6330 | /* Remove the empty string and push nil on the stack */ |
| 6331 | lua_pop(L, 1); |
| 6332 | lua_pushnil(L); |
| 6333 | } |
| 6334 | goto end; |
| 6335 | } |
| 6336 | offset = 0; |
| 6337 | } |
| 6338 | |
| 6339 | luaL_pushresult(&b); |
| 6340 | |
| 6341 | end: |
| 6342 | return ret; |
| 6343 | } |
| 6344 | |
| 6345 | /* Copies the string <str> to the HTTP message <msg> at the offset |
| 6346 | * <offset>. This function returns -1 if data cannot be copied. Otherwise, it |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6347 | * returns the amount of data written. This function is responsible to update |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6348 | * the filter context. |
| 6349 | */ |
| 6350 | static int _hlua_http_msg_insert(struct http_msg *msg, struct filter *filter, struct ist str, size_t offset) |
| 6351 | { |
| 6352 | struct htx *htx = htx_from_buf(&msg->chn->buf); |
| 6353 | struct htx_ret htxret; |
| 6354 | int /*max, */ret = 0; |
| 6355 | |
| 6356 | /* Nothing to do, just return */ |
| 6357 | if (unlikely(istlen(str) == 0)) |
| 6358 | goto end; |
| 6359 | |
| 6360 | if (istlen(str) > htx_free_data_space(htx)) { |
| 6361 | ret = -1; |
| 6362 | goto end; |
| 6363 | } |
| 6364 | |
| 6365 | htxret = htx_find_offset(htx, offset); |
| 6366 | if (!htxret.blk || htx_get_blk_type(htxret.blk) != HTX_BLK_DATA) { |
| 6367 | if (!htx_add_last_data(htx, str)) |
| 6368 | goto end; |
| 6369 | } |
| 6370 | else { |
| 6371 | struct ist v = htx_get_blk_value(htx, htxret.blk); |
| 6372 | v.ptr += htxret.ret; |
| 6373 | v.len = 0; |
| 6374 | if (!htx_replace_blk_value(htx, htxret.blk, v, str)) |
| 6375 | goto end; |
| 6376 | } |
| 6377 | ret = str.len; |
| 6378 | if (ret) { |
| 6379 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6380 | flt_update_offsets(filter, msg->chn, ret); |
| 6381 | flt_ctx->cur_len[CHN_IDX(msg->chn)] += ret; |
| 6382 | } |
| 6383 | |
| 6384 | end: |
| 6385 | htx_to_buf(htx, &msg->chn->buf); |
| 6386 | return ret; |
| 6387 | } |
| 6388 | |
| 6389 | /* Helper function removing at most <len> bytes of DATA blocks at the absolute |
| 6390 | * position <offset>. It stops on the first non-DATA HTX block. This function is |
| 6391 | * called during the payload filtering, so the headers are already scheduled for |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6392 | * output (from the filter point of view). This function is responsible to |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6393 | * update the filter context. |
| 6394 | */ |
| 6395 | static void _hlua_http_msg_delete(struct http_msg *msg, struct filter *filter, size_t offset, size_t len) |
| 6396 | { |
| 6397 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6398 | struct htx *htx = htx_from_buf(&msg->chn->buf); |
| 6399 | struct htx_blk *blk; |
| 6400 | struct htx_ret htxret; |
| 6401 | size_t ret = 0; |
| 6402 | |
| 6403 | /* Be sure <len> is always the amount of DATA to remove */ |
| 6404 | if (htx->data == offset+len && htx_get_tail_type(htx) == HTX_BLK_DATA) { |
| 6405 | htx_truncate(htx, offset); |
| 6406 | ret = len; |
| 6407 | goto end; |
| 6408 | } |
| 6409 | |
| 6410 | htxret = htx_find_offset(htx, offset); |
| 6411 | blk = htxret.blk; |
| 6412 | if (htxret.ret) { |
| 6413 | struct ist v; |
| 6414 | |
| 6415 | if (htx_get_blk_type(blk) != HTX_BLK_DATA) |
| 6416 | goto end; |
| 6417 | v = htx_get_blk_value(htx, blk); |
| 6418 | v.ptr += htxret.ret; |
| 6419 | if (v.len > len) |
| 6420 | v.len = len; |
Tim Duesterhus | b113b5c | 2021-09-15 13:58:44 +0200 | [diff] [blame] | 6421 | blk = htx_replace_blk_value(htx, blk, v, IST_NULL); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6422 | len -= v.len; |
| 6423 | ret += v.len; |
| 6424 | } |
| 6425 | |
| 6426 | |
| 6427 | while (blk && len) { |
| 6428 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 6429 | uint32_t sz = htx_get_blksz(blk); |
| 6430 | |
| 6431 | switch (type) { |
| 6432 | case HTX_BLK_UNUSED: |
| 6433 | break; |
| 6434 | |
| 6435 | case HTX_BLK_DATA: |
| 6436 | if (len < sz) { |
| 6437 | htx_cut_data_blk(htx, blk, len); |
| 6438 | ret += len; |
| 6439 | goto end; |
| 6440 | } |
| 6441 | break; |
| 6442 | |
| 6443 | default: |
| 6444 | goto end; |
| 6445 | } |
| 6446 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6447 | /* Remove all the data block */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6448 | len -= sz; |
| 6449 | ret += sz; |
| 6450 | blk = htx_remove_blk(htx, blk); |
| 6451 | } |
| 6452 | |
| 6453 | end: |
| 6454 | flt_update_offsets(filter, msg->chn, -ret); |
| 6455 | flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret; |
| 6456 | /* WARNING: we don't call htx_to_buf() on purpose, because we don't want |
| 6457 | * to loose the EOM flag if the message is empty. |
| 6458 | */ |
| 6459 | } |
| 6460 | |
| 6461 | /* Copies input data found in an HTTP message. Unlike the channel function used |
| 6462 | * to duplicate raw data, this one can only be called inside a filter, from |
| 6463 | * http_payload callback. So it cannot yield. An exception is returned if it is |
| 6464 | * called from another callback. If nothing was copied, a nil value is pushed on |
| 6465 | * the stack. |
| 6466 | */ |
| 6467 | __LJMP static int hlua_http_msg_get_body(lua_State *L) |
| 6468 | { |
| 6469 | struct http_msg *msg; |
| 6470 | struct filter *filter; |
| 6471 | size_t output, input; |
| 6472 | int offset, len; |
| 6473 | |
| 6474 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 6475 | WILL_LJMP(luaL_error(L, "'data' expects at most 2 arguments")); |
| 6476 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6477 | |
| 6478 | if (msg->msg_state < HTTP_MSG_DATA) |
| 6479 | WILL_LJMP(lua_error(L)); |
| 6480 | |
| 6481 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6482 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6483 | WILL_LJMP(lua_error(L)); |
| 6484 | |
| 6485 | if (!ci_data(msg->chn) && channel_input_closed(msg->chn)) { |
| 6486 | lua_pushnil(L); |
| 6487 | return 1; |
| 6488 | } |
| 6489 | |
| 6490 | offset = output; |
| 6491 | if (lua_gettop(L) > 1) { |
| 6492 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6493 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6494 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6495 | offset += output; |
| 6496 | if (offset < output || offset > input + output) { |
| 6497 | lua_pushfstring(L, "offset out of range."); |
| 6498 | WILL_LJMP(lua_error(L)); |
| 6499 | } |
| 6500 | } |
| 6501 | len = output + input - offset; |
| 6502 | if (lua_gettop(L) == 3) { |
| 6503 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6504 | if (!len) |
| 6505 | goto dup; |
| 6506 | if (len == -1) |
| 6507 | len = global.tune.bufsize; |
| 6508 | if (len < 0) { |
| 6509 | lua_pushfstring(L, "length out of range."); |
| 6510 | WILL_LJMP(lua_error(L)); |
| 6511 | } |
| 6512 | } |
| 6513 | |
| 6514 | dup: |
| 6515 | _hlua_http_msg_dup(msg, L, offset, len); |
| 6516 | return 1; |
| 6517 | } |
| 6518 | |
| 6519 | /* Appends a string to the HTTP message, after all existing DATA blocks but |
| 6520 | * before the trailers, if any. It returns the amount of data written or -1 if |
| 6521 | * nothing was copied. Unlike the channel function used to append data, this one |
| 6522 | * can only be called inside a filter, from http_payload callback. So it cannot |
| 6523 | * yield. An exception is returned if it is called from another callback. |
| 6524 | */ |
| 6525 | __LJMP static int hlua_http_msg_append(lua_State *L) |
| 6526 | { |
| 6527 | struct http_msg *msg; |
| 6528 | struct filter *filter; |
| 6529 | const char *str; |
| 6530 | size_t offset, len, sz; |
| 6531 | int ret; |
| 6532 | |
| 6533 | MAY_LJMP(check_args(L, 2, "append")); |
| 6534 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6535 | |
| 6536 | if (msg->msg_state < HTTP_MSG_DATA) |
| 6537 | WILL_LJMP(lua_error(L)); |
| 6538 | |
| 6539 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6540 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6541 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6542 | WILL_LJMP(lua_error(L)); |
| 6543 | |
| 6544 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset+len); |
| 6545 | lua_pushinteger(L, ret); |
| 6546 | return 1; |
| 6547 | } |
| 6548 | |
| 6549 | /* Prepends a string to the HTTP message, before all existing DATA blocks. It |
| 6550 | * returns the amount of data written or -1 if nothing was copied. Unlike the |
| 6551 | * channel function used to prepend data, this one can only be called inside a |
| 6552 | * filter, from http_payload callback. So it cannot yield. An exception is |
| 6553 | * returned if it is called from another callback. |
| 6554 | */ |
| 6555 | __LJMP static int hlua_http_msg_prepend(lua_State *L) |
| 6556 | { |
| 6557 | struct http_msg *msg; |
| 6558 | struct filter *filter; |
| 6559 | const char *str; |
| 6560 | size_t offset, len, sz; |
| 6561 | int ret; |
| 6562 | |
| 6563 | MAY_LJMP(check_args(L, 2, "prepend")); |
| 6564 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6565 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6566 | if (msg->msg_state < HTTP_MSG_DATA) |
| 6567 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6568 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6569 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6570 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6571 | if (!filter || !hlua_filter_from_payload(filter)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6572 | WILL_LJMP(lua_error(L)); |
| 6573 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6574 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6575 | lua_pushinteger(L, ret); |
| 6576 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6577 | } |
| 6578 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6579 | /* Inserts a string to the HTTP message at a given offset. By default the string |
| 6580 | * is appended at the end of DATA blocks. It returns the amount of data written |
| 6581 | * or -1 if nothing was copied. Unlike the channel function used to insert data, |
| 6582 | * this one can only be called inside a filter, from http_payload callback. So |
| 6583 | * it cannot yield. An exception is returned if it is called from another |
| 6584 | * callback. |
| 6585 | */ |
| 6586 | __LJMP static int hlua_http_msg_insert_data(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6587 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6588 | struct http_msg *msg; |
| 6589 | struct filter *filter; |
| 6590 | const char *str; |
| 6591 | size_t input, output, sz; |
| 6592 | int offset; |
| 6593 | int ret; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6594 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6595 | if (lua_gettop(L) < 2 || lua_gettop(L) > 3) |
| 6596 | WILL_LJMP(luaL_error(L, "'insert' expects at least 1 argument and at most 2 arguments")); |
| 6597 | MAY_LJMP(check_args(L, 2, "insert")); |
| 6598 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6599 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6600 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6601 | WILL_LJMP(lua_error(L)); |
| 6602 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6603 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6604 | filter = hlua_http_msg_filter(L, 1, msg, &input, &output); |
| 6605 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6606 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6607 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6608 | offset = input + output; |
| 6609 | if (lua_gettop(L) > 2) { |
| 6610 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6611 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6612 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6613 | offset += output; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6614 | if (offset < output || offset > output + input) { |
| 6615 | lua_pushfstring(L, "offset out of range."); |
| 6616 | WILL_LJMP(lua_error(L)); |
| 6617 | } |
| 6618 | } |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6619 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6620 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6621 | lua_pushinteger(L, ret); |
| 6622 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6623 | } |
| 6624 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6625 | /* Removes a given amount of data from the HTTP message at a given offset. By |
| 6626 | * default all DATA blocks are removed. It returns the amount of data |
| 6627 | * removed. Unlike the channel function used to remove data, this one can only |
| 6628 | * be called inside a filter, from http_payload callback. So it cannot yield. An |
| 6629 | * exception is returned if it is called from another callback. |
| 6630 | */ |
| 6631 | __LJMP static int hlua_http_msg_del_data(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6632 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6633 | struct http_msg *msg; |
| 6634 | struct filter *filter; |
| 6635 | size_t input, output; |
| 6636 | int offset, len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6637 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6638 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 6639 | WILL_LJMP(luaL_error(L, "'insert' expects at most 2 arguments")); |
| 6640 | MAY_LJMP(check_args(L, 2, "insert")); |
| 6641 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6642 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6643 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6644 | WILL_LJMP(lua_error(L)); |
| 6645 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6646 | filter = hlua_http_msg_filter(L, 1, msg, &input, &output); |
| 6647 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6648 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6649 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6650 | offset = input + output; |
| 6651 | if (lua_gettop(L) > 2) { |
| 6652 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6653 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6654 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6655 | offset += output; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6656 | if (offset < output || offset > output + input) { |
| 6657 | lua_pushfstring(L, "offset out of range."); |
| 6658 | WILL_LJMP(lua_error(L)); |
| 6659 | } |
| 6660 | } |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6661 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6662 | len = output + input - offset; |
| 6663 | if (lua_gettop(L) == 4) { |
| 6664 | len = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 6665 | if (!len) |
| 6666 | goto end; |
| 6667 | if (len == -1) |
| 6668 | len = output + input - offset; |
| 6669 | if (len < 0 || offset + len > output + input) { |
| 6670 | lua_pushfstring(L, "length out of range."); |
| 6671 | WILL_LJMP(lua_error(L)); |
| 6672 | } |
| 6673 | } |
| 6674 | |
| 6675 | _hlua_http_msg_delete(msg, filter, offset, len); |
| 6676 | |
| 6677 | end: |
| 6678 | lua_pushinteger(L, len); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6679 | return 1; |
| 6680 | } |
| 6681 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6682 | /* Replaces a given amount of data at the given offset by a string. By default, |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6683 | * all remaining data are removed, accordingly to the filter context. It returns |
| 6684 | * the amount of data written or -1 if nothing was copied. Unlike the channel |
| 6685 | * function used to replace data, this one can only be called inside a filter, |
| 6686 | * from http_payload callback. So it cannot yield. An exception is returned if |
| 6687 | * it is called from another callback. |
| 6688 | */ |
| 6689 | __LJMP static int hlua_http_msg_set_data(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6690 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6691 | struct http_msg *msg; |
| 6692 | struct filter *filter; |
| 6693 | struct htx *htx; |
| 6694 | const char *str; |
| 6695 | size_t input, output, sz; |
| 6696 | int offset, len; |
| 6697 | int ret; |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 6698 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6699 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 6700 | WILL_LJMP(luaL_error(L, "'set' expects at least 1 argument and at most 3 arguments")); |
| 6701 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6702 | |
| 6703 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6704 | WILL_LJMP(lua_error(L)); |
| 6705 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6706 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6707 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6708 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6709 | WILL_LJMP(lua_error(L)); |
| 6710 | |
| 6711 | offset = output; |
| 6712 | if (lua_gettop(L) > 2) { |
| 6713 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6714 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6715 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6716 | offset += output; |
| 6717 | if (offset < output || offset > input + output) { |
| 6718 | lua_pushfstring(L, "offset out of range."); |
| 6719 | WILL_LJMP(lua_error(L)); |
| 6720 | } |
| 6721 | } |
| 6722 | |
| 6723 | len = output + input - offset; |
| 6724 | if (lua_gettop(L) == 4) { |
| 6725 | len = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 6726 | if (!len) |
| 6727 | goto set; |
| 6728 | if (len == -1) |
| 6729 | len = output + input - offset; |
| 6730 | if (len < 0 || offset + len > output + input) { |
| 6731 | lua_pushfstring(L, "length out of range."); |
| 6732 | WILL_LJMP(lua_error(L)); |
| 6733 | } |
| 6734 | } |
| 6735 | |
| 6736 | set: |
| 6737 | /* Be sure we can copied the string once input data will be removed. */ |
| 6738 | htx = htx_from_buf(&msg->chn->buf); |
| 6739 | if (sz > htx_free_data_space(htx) + len) |
| 6740 | lua_pushinteger(L, -1); |
| 6741 | else { |
| 6742 | _hlua_http_msg_delete(msg, filter, offset, len); |
| 6743 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6744 | lua_pushinteger(L, ret); |
| 6745 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6746 | return 1; |
| 6747 | } |
| 6748 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6749 | /* Prepends data into an HTTP message and forward it, from the filter point of |
| 6750 | * view. It returns the amount of data written or -1 if nothing was sent. Unlike |
| 6751 | * the channel function used to send data, this one can only be called inside a |
| 6752 | * filter, from http_payload callback. So it cannot yield. An exception is |
| 6753 | * returned if it is called from another callback. |
| 6754 | */ |
| 6755 | __LJMP static int hlua_http_msg_send(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6756 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6757 | struct http_msg *msg; |
| 6758 | struct filter *filter; |
| 6759 | struct htx *htx; |
| 6760 | const char *str; |
| 6761 | size_t offset, len, sz; |
| 6762 | int ret; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6763 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6764 | MAY_LJMP(check_args(L, 2, "send")); |
| 6765 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6766 | |
| 6767 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6768 | WILL_LJMP(lua_error(L)); |
| 6769 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6770 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6771 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6772 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6773 | WILL_LJMP(lua_error(L)); |
| 6774 | |
| 6775 | /* Return an error if the channel's output is closed */ |
| 6776 | if (unlikely(channel_output_closed(msg->chn))) { |
| 6777 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6778 | return 1; |
| 6779 | } |
| 6780 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6781 | htx = htx_from_buf(&msg->chn->buf); |
| 6782 | if (sz > htx_free_data_space(htx)) { |
| 6783 | lua_pushinteger(L, -1); |
| 6784 | return 1; |
| 6785 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6786 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6787 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6788 | if (ret > 0) { |
| 6789 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6790 | |
| 6791 | FLT_OFF(filter, msg->chn) += ret; |
| 6792 | flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret; |
| 6793 | flt_ctx->cur_off[CHN_IDX(msg->chn)] += ret; |
| 6794 | } |
| 6795 | |
| 6796 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6797 | return 1; |
| 6798 | } |
| 6799 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6800 | /* Forwards a given amount of bytes. It return -1 if the channel's output is |
| 6801 | * closed. Otherwise, it returns the number of bytes forwarded. Unlike the |
| 6802 | * channel function used to forward data, this one can only be called inside a |
| 6803 | * filter, from http_payload callback. So it cannot yield. An exception is |
| 6804 | * returned if it is called from another callback. All other functions deal with |
| 6805 | * DATA block, this one not. |
| 6806 | */ |
| 6807 | __LJMP static int hlua_http_msg_forward(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6808 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6809 | struct http_msg *msg; |
| 6810 | struct filter *filter; |
| 6811 | size_t offset, len; |
| 6812 | int fwd, ret = 0; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6813 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6814 | MAY_LJMP(check_args(L, 2, "forward")); |
| 6815 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6816 | |
| 6817 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6818 | WILL_LJMP(lua_error(L)); |
| 6819 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6820 | fwd = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6821 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6822 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6823 | WILL_LJMP(lua_error(L)); |
| 6824 | |
| 6825 | /* Nothing to do, just return */ |
| 6826 | if (!fwd) |
| 6827 | goto end; |
| 6828 | |
| 6829 | /* Return an error if the channel's output is closed */ |
| 6830 | if (unlikely(channel_output_closed(msg->chn))) { |
| 6831 | ret = -1; |
| 6832 | goto end; |
| 6833 | } |
| 6834 | |
| 6835 | ret = fwd; |
| 6836 | if (ret > len) |
| 6837 | ret = len; |
| 6838 | |
| 6839 | if (ret) { |
| 6840 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6841 | |
| 6842 | FLT_OFF(filter, msg->chn) += ret; |
| 6843 | flt_ctx->cur_off[CHN_IDX(msg->chn)] += ret; |
| 6844 | flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret; |
| 6845 | } |
| 6846 | |
| 6847 | end: |
| 6848 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6849 | return 1; |
| 6850 | } |
| 6851 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6852 | /* Set EOM flag on the HTX message. |
| 6853 | * |
| 6854 | * NOTE: Not sure it is a good idea to manipulate this flag but for now I don't |
| 6855 | * really know how to do without this feature. |
| 6856 | */ |
| 6857 | __LJMP static int hlua_http_msg_set_eom(lua_State *L) |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 6858 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6859 | struct http_msg *msg; |
| 6860 | struct htx *htx; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 6861 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6862 | MAY_LJMP(check_args(L, 1, "set_eom")); |
| 6863 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6864 | htx = htxbuf(&msg->chn->buf); |
| 6865 | htx->flags |= HTX_FL_EOM; |
| 6866 | return 0; |
| 6867 | } |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6868 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6869 | /* Unset EOM flag on the HTX message. |
| 6870 | * |
| 6871 | * NOTE: Not sure it is a good idea to manipulate this flag but for now I don't |
| 6872 | * really know how to do without this feature. |
| 6873 | */ |
| 6874 | __LJMP static int hlua_http_msg_unset_eom(lua_State *L) |
| 6875 | { |
| 6876 | struct http_msg *msg; |
| 6877 | struct htx *htx; |
| 6878 | |
| 6879 | MAY_LJMP(check_args(L, 1, "set_eom")); |
| 6880 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6881 | htx = htxbuf(&msg->chn->buf); |
| 6882 | htx->flags &= ~HTX_FL_EOM; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 6883 | return 0; |
| 6884 | } |
| 6885 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6886 | /* |
| 6887 | * |
| 6888 | * |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 6889 | * Class HTTPClient |
| 6890 | * |
| 6891 | * |
| 6892 | */ |
| 6893 | __LJMP static struct hlua_httpclient *hlua_checkhttpclient(lua_State *L, int ud) |
| 6894 | { |
| 6895 | return MAY_LJMP(hlua_checkudata(L, ud, class_httpclient_ref)); |
| 6896 | } |
| 6897 | |
| 6898 | __LJMP static int hlua_httpclient_new(lua_State *L) |
| 6899 | { |
| 6900 | struct hlua_httpclient *hlua_hc; |
| 6901 | struct hlua *hlua; |
| 6902 | |
| 6903 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 6904 | hlua = hlua_gethlua(L); |
| 6905 | if (!hlua) |
| 6906 | return 0; |
| 6907 | |
| 6908 | /* Check stack size. */ |
| 6909 | if (!lua_checkstack(L, 3)) { |
| 6910 | hlua_pusherror(L, "httpclient: full stack"); |
| 6911 | goto err; |
| 6912 | } |
| 6913 | /* Create the object: obj[0] = userdata. */ |
| 6914 | lua_newtable(L); |
| 6915 | hlua_hc = MAY_LJMP(lua_newuserdata(L, sizeof(*hlua_hc))); |
| 6916 | lua_rawseti(L, -2, 0); |
| 6917 | memset(hlua_hc, 0, sizeof(*hlua_hc)); |
| 6918 | |
| 6919 | hlua_hc->hc = httpclient_new(hlua, 0, IST_NULL); |
| 6920 | if (!hlua_hc->hc) |
| 6921 | goto err; |
| 6922 | |
| 6923 | /* Pop a class stream metatable and affect it to the userdata. */ |
| 6924 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_httpclient_ref); |
| 6925 | lua_setmetatable(L, -2); |
| 6926 | |
| 6927 | return 1; |
| 6928 | |
| 6929 | err: |
| 6930 | WILL_LJMP(lua_error(L)); |
| 6931 | return 0; |
| 6932 | } |
| 6933 | |
| 6934 | |
| 6935 | /* |
| 6936 | * Callback of the httpclient, this callback wakes the lua task up, once the |
| 6937 | * httpclient receives some data |
| 6938 | * |
| 6939 | */ |
| 6940 | |
| 6941 | static void hlua_httpclient_res_cb(struct httpclient *hc) |
| 6942 | { |
| 6943 | struct hlua *hlua = hc->caller; |
| 6944 | |
| 6945 | if (!hlua || !hlua->task) |
| 6946 | return; |
| 6947 | |
| 6948 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 6949 | } |
| 6950 | |
| 6951 | /* |
William Lallemand | d7df73a | 2021-09-23 17:54:00 +0200 | [diff] [blame] | 6952 | * Fill the lua stack with headers from the httpclient response |
| 6953 | * This works the same way as the hlua_http_get_headers() function |
| 6954 | */ |
| 6955 | __LJMP static int hlua_httpclient_get_headers(lua_State *L, struct hlua_httpclient *hlua_hc) |
| 6956 | { |
| 6957 | struct http_hdr *hdr; |
| 6958 | |
| 6959 | lua_newtable(L); |
| 6960 | |
| 6961 | for (hdr = hlua_hc->hc->res.hdrs; isttest(hdr->n); hdr++) { |
| 6962 | struct ist n, v; |
| 6963 | int len; |
| 6964 | |
| 6965 | n = hdr->n; |
| 6966 | v = hdr->v; |
| 6967 | |
| 6968 | /* Check for existing entry: |
| 6969 | * assume that the table is on the top of the stack, and |
| 6970 | * push the key in the stack, the function lua_gettable() |
| 6971 | * perform the lookup. |
| 6972 | */ |
| 6973 | |
| 6974 | lua_pushlstring(L, n.ptr, n.len); |
| 6975 | lua_gettable(L, -2); |
| 6976 | |
| 6977 | switch (lua_type(L, -1)) { |
| 6978 | case LUA_TNIL: |
| 6979 | /* Table not found, create it. */ |
| 6980 | lua_pop(L, 1); /* remove the nil value. */ |
| 6981 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 6982 | lua_newtable(L); /* create and push empty table. */ |
| 6983 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 6984 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 6985 | lua_rawset(L, -3); /* index new table with header name (pop the values). */ |
| 6986 | break; |
| 6987 | |
| 6988 | case LUA_TTABLE: |
| 6989 | /* Entry found: push the value in the table. */ |
| 6990 | len = lua_rawlen(L, -1); |
| 6991 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 6992 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 6993 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 6994 | break; |
| 6995 | |
| 6996 | default: |
| 6997 | /* Other cases are errors. */ |
| 6998 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 6999 | WILL_LJMP(lua_error(L)); |
| 7000 | } |
| 7001 | } |
| 7002 | return 1; |
| 7003 | } |
| 7004 | |
| 7005 | /* |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7006 | * For each yield, checks if there is some data in the httpclient and push them |
| 7007 | * in the lua buffer, once the httpclient finished its job, push the result on |
| 7008 | * the stack |
| 7009 | */ |
William Lallemand | 7d21836 | 2021-09-24 16:51:55 +0200 | [diff] [blame] | 7010 | __LJMP static int hlua_httpclient_send_yield(lua_State *L, int status, lua_KContext ctx) |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7011 | { |
| 7012 | struct buffer *tr; |
| 7013 | int res; |
| 7014 | struct hlua *hlua = hlua_gethlua(L); |
| 7015 | struct hlua_httpclient *hlua_hc = hlua_checkhttpclient(L, 1); |
| 7016 | |
| 7017 | |
| 7018 | tr = get_trash_chunk(); |
| 7019 | |
| 7020 | res = httpclient_res_xfer(hlua_hc->hc, tr); |
| 7021 | luaL_addlstring(&hlua_hc->b, b_orig(tr), res); |
| 7022 | |
| 7023 | if (!httpclient_data(hlua_hc->hc) && httpclient_ended(hlua_hc->hc)) { |
| 7024 | |
| 7025 | luaL_pushresult(&hlua_hc->b); |
| 7026 | lua_settable(L, -3); |
| 7027 | |
| 7028 | lua_pushstring(L, "status"); |
| 7029 | lua_pushinteger(L, hlua_hc->hc->res.status); |
| 7030 | lua_settable(L, -3); |
| 7031 | |
| 7032 | |
| 7033 | lua_pushstring(L, "reason"); |
| 7034 | lua_pushlstring(L, hlua_hc->hc->res.reason.ptr, hlua_hc->hc->res.reason.len); |
| 7035 | lua_settable(L, -3); |
| 7036 | |
William Lallemand | d7df73a | 2021-09-23 17:54:00 +0200 | [diff] [blame] | 7037 | lua_pushstring(L, "headers"); |
| 7038 | hlua_httpclient_get_headers(L, hlua_hc); |
| 7039 | lua_settable(L, -3); |
| 7040 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7041 | return 1; |
| 7042 | } |
| 7043 | |
| 7044 | if (httpclient_data(hlua_hc->hc)) |
| 7045 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 7046 | |
| 7047 | |
William Lallemand | 7d21836 | 2021-09-24 16:51:55 +0200 | [diff] [blame] | 7048 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_send_yield, TICK_ETERNITY, 0)); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7049 | return 0; |
| 7050 | } |
| 7051 | |
| 7052 | /* |
William Lallemand | 7d21836 | 2021-09-24 16:51:55 +0200 | [diff] [blame] | 7053 | * Send an HTTP request and wait for a response |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7054 | */ |
| 7055 | |
William Lallemand | 7d21836 | 2021-09-24 16:51:55 +0200 | [diff] [blame] | 7056 | __LJMP static int hlua_httpclient_send(lua_State *L, enum http_meth_t meth) |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7057 | { |
| 7058 | struct hlua_httpclient *hlua_hc; |
| 7059 | struct hlua *hlua; |
| 7060 | const char *url_str; |
| 7061 | |
| 7062 | hlua = hlua_gethlua(L); |
| 7063 | |
| 7064 | if (!hlua || !hlua->task) |
| 7065 | WILL_LJMP(luaL_error(L, "The 'get' function is only allowed in " |
| 7066 | "'frontend', 'backend' or 'task'")); |
| 7067 | |
| 7068 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 7069 | WILL_LJMP(luaL_error(L, "'get' needs between 1 or 2 arguments")); |
| 7070 | |
William Lallemand | 79416cb | 2021-09-24 14:51:44 +0200 | [diff] [blame] | 7071 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7072 | WILL_LJMP(luaL_error(L, "'get' takes an URL as a string arugment")); |
| 7073 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7074 | /* arg 1: URL */ |
William Lallemand | 79416cb | 2021-09-24 14:51:44 +0200 | [diff] [blame] | 7075 | url_str = lua_tostring(L, -1); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7076 | |
| 7077 | hlua_hc = hlua_checkhttpclient(L, 1); |
| 7078 | |
| 7079 | hlua_hc->hc->req.url = istdup(ist(url_str)); |
| 7080 | hlua_hc->hc->req.meth = HTTP_METH_GET; |
| 7081 | |
| 7082 | /* update the httpclient callbacks */ |
| 7083 | hlua_hc->hc->ops.res_stline = hlua_httpclient_res_cb; |
| 7084 | hlua_hc->hc->ops.res_headers = hlua_httpclient_res_cb; |
| 7085 | hlua_hc->hc->ops.res_payload = hlua_httpclient_res_cb; |
| 7086 | hlua_hc->hc->ops.res_end = hlua_httpclient_res_cb; |
| 7087 | |
| 7088 | |
| 7089 | httpclient_req_gen(hlua_hc->hc, hlua_hc->hc->req.url, HTTP_METH_GET, NULL); |
| 7090 | httpclient_start(hlua_hc->hc); |
| 7091 | |
| 7092 | /* we return a "res" object */ |
| 7093 | lua_newtable(L); |
| 7094 | |
| 7095 | luaL_buffinit(L, &hlua_hc->b); |
| 7096 | lua_pushstring(L, "body"); |
| 7097 | |
| 7098 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_get_yield, TICK_ETERNITY, 0)); |
| 7099 | return 0; |
| 7100 | } |
| 7101 | |
| 7102 | /* |
William Lallemand | 7d21836 | 2021-09-24 16:51:55 +0200 | [diff] [blame] | 7103 | * Sends an HTTP HEAD request and wait for a response |
| 7104 | * |
| 7105 | * httpclient:head(url, headers, payload) |
| 7106 | */ |
| 7107 | __LJMP static int hlua_httpclient_head(lua_State *L) |
| 7108 | { |
| 7109 | return hlua_httpclient_send(L, HTTP_METH_HEAD); |
| 7110 | } |
| 7111 | |
| 7112 | /* |
| 7113 | * Send an HTTP GET request and wait for a response |
| 7114 | * |
| 7115 | * httpclient:get(url, headers, payload) |
| 7116 | */ |
| 7117 | __LJMP static int hlua_httpclient_get(lua_State *L) |
| 7118 | { |
| 7119 | return hlua_httpclient_send(L, HTTP_METH_GET); |
| 7120 | |
| 7121 | } |
| 7122 | |
| 7123 | /* |
| 7124 | * Sends an HTTP PUT request and wait for a response |
| 7125 | * |
| 7126 | * httpclient:put(url, headers, payload) |
| 7127 | */ |
| 7128 | __LJMP static int hlua_httpclient_put(lua_State *L) |
| 7129 | { |
| 7130 | return hlua_httpclient_send(L, HTTP_METH_PUT); |
| 7131 | } |
| 7132 | |
| 7133 | /* |
| 7134 | * Send an HTTP POST request and wait for a response |
| 7135 | * |
| 7136 | * httpclient:post(url, headers, payload) |
| 7137 | */ |
| 7138 | __LJMP static int hlua_httpclient_post(lua_State *L) |
| 7139 | { |
| 7140 | return hlua_httpclient_send(L, HTTP_METH_POST); |
| 7141 | } |
| 7142 | |
| 7143 | |
| 7144 | /* |
| 7145 | * Sends an HTTP HEAD request and wait for a response |
| 7146 | * |
| 7147 | * httpclient:delete(url, headers, payload) |
| 7148 | */ |
| 7149 | __LJMP static int hlua_httpclient_delete(lua_State *L) |
| 7150 | { |
| 7151 | return hlua_httpclient_send(L, HTTP_METH_DELETE); |
| 7152 | } |
| 7153 | |
| 7154 | /* |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7155 | * |
| 7156 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7157 | * Class TXN |
| 7158 | * |
| 7159 | * |
| 7160 | */ |
| 7161 | |
| 7162 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7163 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7164 | */ |
| 7165 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 7166 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7167 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7168 | } |
| 7169 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7170 | __LJMP static int hlua_set_var(lua_State *L) |
| 7171 | { |
| 7172 | struct hlua_txn *htxn; |
| 7173 | const char *name; |
| 7174 | size_t len; |
| 7175 | struct sample smp; |
| 7176 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 7177 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 7178 | 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] | 7179 | |
| 7180 | /* It is useles to retrieve the stream, but this function |
| 7181 | * runs only in a stream context. |
| 7182 | */ |
| 7183 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7184 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 7185 | |
| 7186 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 7187 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7188 | hlua_lua2smp(L, 3, &smp); |
| 7189 | |
| 7190 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 7191 | 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] | 7192 | |
| 7193 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 7194 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 7195 | else |
| 7196 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 7197 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 7198 | return 1; |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7199 | } |
| 7200 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 7201 | __LJMP static int hlua_unset_var(lua_State *L) |
| 7202 | { |
| 7203 | struct hlua_txn *htxn; |
| 7204 | const char *name; |
| 7205 | size_t len; |
| 7206 | struct sample smp; |
| 7207 | |
| 7208 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 7209 | |
| 7210 | /* It is useles to retrieve the stream, but this function |
| 7211 | * runs only in a stream context. |
| 7212 | */ |
| 7213 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7214 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 7215 | |
| 7216 | /* Unset the variable. */ |
| 7217 | 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] | 7218 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 7219 | return 1; |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 7220 | } |
| 7221 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7222 | __LJMP static int hlua_get_var(lua_State *L) |
| 7223 | { |
| 7224 | struct hlua_txn *htxn; |
| 7225 | const char *name; |
| 7226 | size_t len; |
| 7227 | struct sample smp; |
| 7228 | |
| 7229 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 7230 | |
| 7231 | /* It is useles to retrieve the stream, but this function |
| 7232 | * runs only in a stream context. |
| 7233 | */ |
| 7234 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7235 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 7236 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 7237 | smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR); |
Willy Tarreau | e352b9d | 2021-09-03 11:52:38 +0200 | [diff] [blame] | 7238 | if (!vars_get_by_name(name, len, &smp, NULL)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7239 | lua_pushnil(L); |
| 7240 | return 1; |
| 7241 | } |
| 7242 | |
| 7243 | return hlua_smp2lua(L, &smp); |
| 7244 | } |
| 7245 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7246 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7247 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7248 | struct hlua *hlua; |
| 7249 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7250 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 7251 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7252 | /* It is useles to retrieve the stream, but this function |
| 7253 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7254 | */ |
| 7255 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7256 | |
| 7257 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7258 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7259 | if (!hlua) |
| 7260 | return 0; |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7261 | |
| 7262 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 7263 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7264 | |
| 7265 | /* Get and store new value. */ |
| 7266 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 7267 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 7268 | |
| 7269 | return 0; |
| 7270 | } |
| 7271 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7272 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7273 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7274 | struct hlua *hlua; |
| 7275 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7276 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 7277 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7278 | /* It is useles to retrieve the stream, but this function |
| 7279 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7280 | */ |
| 7281 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7282 | |
| 7283 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7284 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7285 | if (!hlua) { |
| 7286 | lua_pushnil(L); |
| 7287 | return 1; |
| 7288 | } |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7289 | |
| 7290 | /* Push configuration index in the stack. */ |
| 7291 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 7292 | |
| 7293 | return 1; |
| 7294 | } |
| 7295 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7296 | /* Create stack entry containing a class TXN. This function |
| 7297 | * return 0 if the stack does not contains free slots, |
| 7298 | * otherwise it returns 1. |
| 7299 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 7300 | 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] | 7301 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7302 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7303 | |
| 7304 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 7305 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7306 | return 0; |
| 7307 | |
| 7308 | /* NOTE: The allocation never fails. The failure |
| 7309 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7310 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7311 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 7312 | /* Create the object: obj[0] = userdata. */ |
| 7313 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7314 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 7315 | lua_rawseti(L, -2, 0); |
| 7316 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7317 | htxn->s = s; |
| 7318 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 7319 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 7320 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7321 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7322 | /* Create the "f" field that contains a list of fetches. */ |
| 7323 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 7324 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7325 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7326 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7327 | |
| 7328 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 7329 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 7330 | 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] | 7331 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7332 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7333 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7334 | /* Create the "c" field that contains a list of converters. */ |
| 7335 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7336 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7337 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7338 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7339 | |
| 7340 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 7341 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 7342 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7343 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7344 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7345 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7346 | /* Create the "req" field that contains the request channel object. */ |
| 7347 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 7348 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7349 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7350 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7351 | |
| 7352 | /* Create the "res" field that contains the response channel object. */ |
| 7353 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 7354 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7355 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7356 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7357 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7358 | /* Creates the HTTP object is the current proxy allows http. */ |
| 7359 | lua_pushstring(L, "http"); |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 7360 | if (IS_HTX_STRM(s)) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7361 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7362 | return 0; |
| 7363 | } |
| 7364 | else |
| 7365 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7366 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7367 | |
Christopher Faulet | 78c3547 | 2020-02-26 17:14:08 +0100 | [diff] [blame] | 7368 | if ((htxn->flags & HLUA_TXN_CTX_MASK) == HLUA_TXN_FLT_CTX) { |
| 7369 | /* HTTPMessage object are created when a lua TXN is created from |
| 7370 | * a filter context only |
| 7371 | */ |
| 7372 | |
| 7373 | /* Creates the HTTP-Request object is the current proxy allows http. */ |
| 7374 | lua_pushstring(L, "http_req"); |
| 7375 | if (p->mode == PR_MODE_HTTP) { |
| 7376 | if (!hlua_http_msg_new(L, &s->txn->req)) |
| 7377 | return 0; |
| 7378 | } |
| 7379 | else |
| 7380 | lua_pushnil(L); |
| 7381 | lua_rawset(L, -3); |
| 7382 | |
| 7383 | /* Creates the HTTP-Response object is the current proxy allows http. */ |
| 7384 | lua_pushstring(L, "http_res"); |
| 7385 | if (p->mode == PR_MODE_HTTP) { |
| 7386 | if (!hlua_http_msg_new(L, &s->txn->rsp)) |
| 7387 | return 0; |
| 7388 | } |
| 7389 | else |
| 7390 | lua_pushnil(L); |
| 7391 | lua_rawset(L, -3); |
| 7392 | } |
| 7393 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7394 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 7395 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 7396 | lua_setmetatable(L, -2); |
| 7397 | |
| 7398 | return 1; |
| 7399 | } |
| 7400 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 7401 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 7402 | { |
| 7403 | const char *msg; |
| 7404 | struct hlua_txn *htxn; |
| 7405 | |
| 7406 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 7407 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7408 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7409 | |
| 7410 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 7411 | return 0; |
| 7412 | } |
| 7413 | |
| 7414 | __LJMP static int hlua_txn_log(lua_State *L) |
| 7415 | { |
| 7416 | int level; |
| 7417 | const char *msg; |
| 7418 | struct hlua_txn *htxn; |
| 7419 | |
| 7420 | MAY_LJMP(check_args(L, 3, "log")); |
| 7421 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7422 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7423 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 7424 | |
| 7425 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 7426 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 7427 | |
| 7428 | hlua_sendlog(htxn->s->be, level, msg); |
| 7429 | return 0; |
| 7430 | } |
| 7431 | |
| 7432 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 7433 | { |
| 7434 | const char *msg; |
| 7435 | struct hlua_txn *htxn; |
| 7436 | |
| 7437 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 7438 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7439 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7440 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 7441 | return 0; |
| 7442 | } |
| 7443 | |
| 7444 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 7445 | { |
| 7446 | const char *msg; |
| 7447 | struct hlua_txn *htxn; |
| 7448 | |
| 7449 | MAY_LJMP(check_args(L, 2, "Info")); |
| 7450 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7451 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7452 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 7453 | return 0; |
| 7454 | } |
| 7455 | |
| 7456 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 7457 | { |
| 7458 | const char *msg; |
| 7459 | struct hlua_txn *htxn; |
| 7460 | |
| 7461 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 7462 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7463 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7464 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 7465 | return 0; |
| 7466 | } |
| 7467 | |
| 7468 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 7469 | { |
| 7470 | const char *msg; |
| 7471 | struct hlua_txn *htxn; |
| 7472 | |
| 7473 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 7474 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7475 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7476 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 7477 | return 0; |
| 7478 | } |
| 7479 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7480 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 7481 | { |
| 7482 | struct hlua_txn *htxn; |
| 7483 | int ll; |
| 7484 | |
| 7485 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 7486 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7487 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7488 | |
| 7489 | if (ll < 0 || ll > 7) |
| 7490 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 7491 | |
| 7492 | htxn->s->logs.level = ll; |
| 7493 | return 0; |
| 7494 | } |
| 7495 | |
| 7496 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 7497 | { |
| 7498 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7499 | int tos; |
| 7500 | |
| 7501 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 7502 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7503 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7504 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 7505 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7506 | return 0; |
| 7507 | } |
| 7508 | |
| 7509 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 7510 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7511 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7512 | int mark; |
| 7513 | |
| 7514 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 7515 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7516 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7517 | |
Lukas Tribus | 579e3e3 | 2019-08-11 18:03:45 +0200 | [diff] [blame] | 7518 | conn_set_mark(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7519 | return 0; |
| 7520 | } |
| 7521 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 7522 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 7523 | { |
| 7524 | struct hlua_txn *htxn; |
| 7525 | |
| 7526 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 7527 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7528 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 7529 | return 0; |
| 7530 | } |
| 7531 | |
| 7532 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 7533 | { |
| 7534 | struct hlua_txn *htxn; |
| 7535 | |
| 7536 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 7537 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7538 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 7539 | return 0; |
| 7540 | } |
| 7541 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7542 | /* 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] | 7543 | * 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] | 7544 | * message and terminate the transaction. It returns 1 on success and 0 on |
| 7545 | * error. The Reply must be on top of the stack. |
| 7546 | */ |
| 7547 | __LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s) |
| 7548 | { |
| 7549 | struct htx *htx; |
| 7550 | struct htx_sl *sl; |
| 7551 | struct h1m h1m; |
| 7552 | const char *status, *reason, *body; |
| 7553 | size_t status_len, reason_len, body_len; |
| 7554 | int ret, code, flags; |
| 7555 | |
| 7556 | code = 200; |
| 7557 | status = "200"; |
| 7558 | status_len = 3; |
| 7559 | ret = lua_getfield(L, -1, "status"); |
| 7560 | if (ret == LUA_TNUMBER) { |
| 7561 | code = lua_tointeger(L, -1); |
| 7562 | status = lua_tolstring(L, -1, &status_len); |
| 7563 | } |
| 7564 | lua_pop(L, 1); |
| 7565 | |
| 7566 | reason = http_get_reason(code); |
| 7567 | reason_len = strlen(reason); |
| 7568 | ret = lua_getfield(L, -1, "reason"); |
| 7569 | if (ret == LUA_TSTRING) |
| 7570 | reason = lua_tolstring(L, -1, &reason_len); |
| 7571 | lua_pop(L, 1); |
| 7572 | |
| 7573 | body = NULL; |
| 7574 | body_len = 0; |
| 7575 | ret = lua_getfield(L, -1, "body"); |
| 7576 | if (ret == LUA_TSTRING) |
| 7577 | body = lua_tolstring(L, -1, &body_len); |
| 7578 | lua_pop(L, 1); |
| 7579 | |
| 7580 | /* Prepare the response before inserting the headers */ |
| 7581 | h1m_init_res(&h1m); |
| 7582 | htx = htx_from_buf(&s->res.buf); |
| 7583 | channel_htx_truncate(&s->res, htx); |
| 7584 | if (s->txn->req.flags & HTTP_MSGF_VER_11) { |
| 7585 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 7586 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), |
| 7587 | ist2(status, status_len), ist2(reason, reason_len)); |
| 7588 | } |
| 7589 | else { |
| 7590 | flags = HTX_SL_F_IS_RESP; |
| 7591 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), |
| 7592 | ist2(status, status_len), ist2(reason, reason_len)); |
| 7593 | } |
| 7594 | if (!sl) |
| 7595 | goto fail; |
| 7596 | sl->info.res.status = code; |
| 7597 | |
| 7598 | /* Push in the stack the "headers" entry. */ |
| 7599 | ret = lua_getfield(L, -1, "headers"); |
| 7600 | if (ret != LUA_TTABLE) |
| 7601 | goto skip_headers; |
| 7602 | |
| 7603 | lua_pushnil(L); |
| 7604 | while (lua_next(L, -2) != 0) { |
| 7605 | struct ist name, value; |
| 7606 | const char *n, *v; |
| 7607 | size_t nlen, vlen; |
| 7608 | |
| 7609 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 7610 | /* Skip element if the key is not a string or if the value is not a table */ |
| 7611 | goto next_hdr; |
| 7612 | } |
| 7613 | |
| 7614 | n = lua_tolstring(L, -2, &nlen); |
| 7615 | name = ist2(n, nlen); |
| 7616 | if (isteqi(name, ist("content-length"))) { |
| 7617 | /* Always skip content-length header. It will be added |
| 7618 | * later with the correct len |
| 7619 | */ |
| 7620 | goto next_hdr; |
| 7621 | } |
| 7622 | |
| 7623 | /* Loop on header's values */ |
| 7624 | lua_pushnil(L); |
| 7625 | while (lua_next(L, -2)) { |
| 7626 | if (!lua_isstring(L, -1)) { |
| 7627 | /* Skip the value if it is not a string */ |
| 7628 | goto next_value; |
| 7629 | } |
| 7630 | |
| 7631 | v = lua_tolstring(L, -1, &vlen); |
| 7632 | value = ist2(v, vlen); |
| 7633 | |
| 7634 | if (isteqi(name, ist("transfer-encoding"))) |
| 7635 | h1_parse_xfer_enc_header(&h1m, value); |
| 7636 | if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen))) |
| 7637 | goto fail; |
| 7638 | |
| 7639 | next_value: |
| 7640 | lua_pop(L, 1); |
| 7641 | } |
| 7642 | |
| 7643 | next_hdr: |
| 7644 | lua_pop(L, 1); |
| 7645 | } |
| 7646 | skip_headers: |
| 7647 | lua_pop(L, 1); |
| 7648 | |
| 7649 | /* Update h1m flags: CLEN is set if CHNK is not present */ |
| 7650 | if (!(h1m.flags & H1_MF_CHNK)) { |
| 7651 | const char *clen = ultoa(body_len); |
| 7652 | |
| 7653 | h1m.flags |= H1_MF_CLEN; |
| 7654 | if (!htx_add_header(htx, ist("content-length"), ist(clen))) |
| 7655 | goto fail; |
| 7656 | } |
| 7657 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 7658 | h1m.flags |= H1_MF_XFER_LEN; |
| 7659 | |
| 7660 | /* Update HTX start-line flags */ |
| 7661 | if (h1m.flags & H1_MF_XFER_ENC) |
| 7662 | flags |= HTX_SL_F_XFER_ENC; |
| 7663 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 7664 | flags |= HTX_SL_F_XFER_LEN; |
| 7665 | if (h1m.flags & H1_MF_CHNK) |
| 7666 | flags |= HTX_SL_F_CHNK; |
| 7667 | else if (h1m.flags & H1_MF_CLEN) |
| 7668 | flags |= HTX_SL_F_CLEN; |
| 7669 | if (h1m.body_len == 0) |
| 7670 | flags |= HTX_SL_F_BODYLESS; |
| 7671 | } |
| 7672 | sl->flags |= flags; |
| 7673 | |
| 7674 | |
| 7675 | if (!htx_add_endof(htx, HTX_BLK_EOH) || |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 7676 | (body_len && !htx_add_data_atonce(htx, ist2(body, body_len)))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7677 | goto fail; |
| 7678 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 7679 | htx->flags |= HTX_FL_EOM; |
| 7680 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7681 | /* Now, forward the response and terminate the transaction */ |
| 7682 | s->txn->status = code; |
| 7683 | htx_to_buf(htx, &s->res.buf); |
| 7684 | if (!http_forward_proxy_resp(s, 1)) |
| 7685 | goto fail; |
| 7686 | |
| 7687 | return 1; |
| 7688 | |
| 7689 | fail: |
| 7690 | channel_htx_truncate(&s->res, htx); |
| 7691 | return 0; |
| 7692 | } |
| 7693 | |
| 7694 | /* Terminate a transaction if called from a lua action. For TCP streams, |
| 7695 | * processing is just aborted. Nothing is returned to the client and all |
| 7696 | * arguments are ignored. For HTTP streams, if a reply is passed as argument, it |
| 7697 | * is forwarded to the client before terminating the transaction. On success, |
| 7698 | * the function exits with ACT_RET_DONE code. If an error occurred, it exits |
| 7699 | * with ACT_RET_ERR code. If this function is not called from a lua action, it |
| 7700 | * just exits without any processing. |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 7701 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 7702 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 7703 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 7704 | struct hlua_txn *htxn; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7705 | struct stream *s; |
| 7706 | int finst; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 7707 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 7708 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 7709 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7710 | /* If the flags NOTERM is set, we cannot terminate the session, so we |
| 7711 | * just end the execution of the current lua code. */ |
| 7712 | if (htxn->flags & HLUA_TXN_NOTERM) |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 7713 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 7714 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7715 | s = htxn->s; |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 7716 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7717 | struct channel *req = &s->req; |
| 7718 | struct channel *res = &s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 7719 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7720 | channel_auto_read(req); |
| 7721 | channel_abort(req); |
| 7722 | channel_auto_close(req); |
| 7723 | channel_erase(req); |
| 7724 | |
| 7725 | res->wex = tick_add_ifset(now_ms, res->wto); |
| 7726 | channel_auto_read(res); |
| 7727 | channel_auto_close(res); |
| 7728 | channel_shutr_now(res); |
| 7729 | |
| 7730 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D); |
| 7731 | goto done; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 7732 | } |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 7733 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7734 | if (lua_gettop(L) == 1 || !lua_istable(L, 2)) { |
| 7735 | /* No reply or invalid reply */ |
| 7736 | s->txn->status = 0; |
| 7737 | http_reply_and_close(s, 0, NULL); |
| 7738 | } |
| 7739 | else { |
| 7740 | /* Remove extra args to have the reply on top of the stack */ |
| 7741 | if (lua_gettop(L) > 2) |
| 7742 | lua_pop(L, lua_gettop(L) - 2); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 7743 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7744 | if (!hlua_txn_forward_reply(L, s)) { |
| 7745 | if (!(s->flags & SF_ERR_MASK)) |
| 7746 | s->flags |= SF_ERR_PRXCOND; |
| 7747 | lua_pushinteger(L, ACT_RET_ERR); |
| 7748 | WILL_LJMP(hlua_done(L)); |
| 7749 | return 0; /* Never reached */ |
| 7750 | } |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 7751 | } |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 7752 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7753 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H); |
| 7754 | if (htxn->dir == SMP_OPT_DIR_REQ) { |
| 7755 | /* let's log the request time */ |
| 7756 | s->logs.tv_request = now; |
| 7757 | 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] | 7758 | _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7759 | } |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 7760 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7761 | done: |
| 7762 | if (!(s->flags & SF_ERR_MASK)) |
| 7763 | s->flags |= SF_ERR_LOCAL; |
| 7764 | if (!(s->flags & SF_FINST_MASK)) |
| 7765 | s->flags |= finst; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 7766 | |
Christopher Faulet | e48d1dc | 2021-08-13 14:11:17 +0200 | [diff] [blame] | 7767 | if ((htxn->flags & HLUA_TXN_CTX_MASK) == HLUA_TXN_FLT_CTX) |
| 7768 | lua_pushinteger(L, -1); |
| 7769 | else |
| 7770 | lua_pushinteger(L, ACT_RET_ABRT); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 7771 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 7772 | return 0; |
| 7773 | } |
| 7774 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7775 | /* |
| 7776 | * |
| 7777 | * |
| 7778 | * Class REPLY |
| 7779 | * |
| 7780 | * |
| 7781 | */ |
| 7782 | |
| 7783 | /* Pushes the TXN reply onto the top of the stack. If the stask does not have a |
| 7784 | * free slots, the function fails and returns 0; |
| 7785 | */ |
| 7786 | static int hlua_txn_reply_new(lua_State *L) |
| 7787 | { |
| 7788 | struct hlua_txn *htxn; |
| 7789 | const char *reason, *body = NULL; |
| 7790 | int ret, status; |
| 7791 | |
| 7792 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 7793 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7794 | hlua_pusherror(L, "txn object is not an HTTP transaction."); |
| 7795 | WILL_LJMP(lua_error(L)); |
| 7796 | } |
| 7797 | |
| 7798 | /* Default value */ |
| 7799 | status = 200; |
| 7800 | reason = http_get_reason(status); |
| 7801 | |
| 7802 | if (lua_istable(L, 2)) { |
| 7803 | /* load status and reason from the table argument at index 2 */ |
| 7804 | ret = lua_getfield(L, 2, "status"); |
| 7805 | if (ret == LUA_TNIL) |
| 7806 | goto reason; |
| 7807 | else if (ret != LUA_TNUMBER) { |
| 7808 | /* invalid status: ignore the reason */ |
| 7809 | goto body; |
| 7810 | } |
| 7811 | status = lua_tointeger(L, -1); |
| 7812 | |
| 7813 | reason: |
| 7814 | lua_pop(L, 1); /* restore the stack: remove status */ |
| 7815 | ret = lua_getfield(L, 2, "reason"); |
| 7816 | if (ret == LUA_TSTRING) |
| 7817 | reason = lua_tostring(L, -1); |
| 7818 | |
| 7819 | body: |
| 7820 | lua_pop(L, 1); /* restore the stack: remove invalid status or reason */ |
| 7821 | ret = lua_getfield(L, 2, "body"); |
| 7822 | if (ret == LUA_TSTRING) |
| 7823 | body = lua_tostring(L, -1); |
| 7824 | lua_pop(L, 1); /* restore the stack: remove body */ |
| 7825 | } |
| 7826 | |
| 7827 | /* Create the Reply table */ |
| 7828 | lua_newtable(L); |
| 7829 | |
| 7830 | /* Add status element */ |
| 7831 | lua_pushstring(L, "status"); |
| 7832 | lua_pushinteger(L, status); |
| 7833 | lua_settable(L, -3); |
| 7834 | |
| 7835 | /* Add reason element */ |
| 7836 | reason = http_get_reason(status); |
| 7837 | lua_pushstring(L, "reason"); |
| 7838 | lua_pushstring(L, reason); |
| 7839 | lua_settable(L, -3); |
| 7840 | |
| 7841 | /* Add body element, nil if undefined */ |
| 7842 | lua_pushstring(L, "body"); |
| 7843 | if (body) |
| 7844 | lua_pushstring(L, body); |
| 7845 | else |
| 7846 | lua_pushnil(L); |
| 7847 | lua_settable(L, -3); |
| 7848 | |
| 7849 | /* Add headers element */ |
| 7850 | lua_pushstring(L, "headers"); |
| 7851 | lua_newtable(L); |
| 7852 | |
| 7853 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 7854 | if (lua_istable(L, 2)) { |
| 7855 | /* load headers from the table argument at index 2. If it is a table, copy it. */ |
| 7856 | ret = lua_getfield(L, 2, "headers"); |
| 7857 | if (ret == LUA_TTABLE) { |
| 7858 | /* stack: [ ... <headers:table>, <table> ] */ |
| 7859 | lua_pushnil(L); |
| 7860 | while (lua_next(L, -2) != 0) { |
| 7861 | /* stack: [ ... <headers:table>, <table>, k, v] */ |
| 7862 | if (!lua_isstring(L, -1) && !lua_istable(L, -1)) { |
| 7863 | /* invalid value type, skip it */ |
| 7864 | lua_pop(L, 1); |
| 7865 | continue; |
| 7866 | } |
| 7867 | |
| 7868 | |
| 7869 | /* Duplicate the key and swap it with the value. */ |
| 7870 | lua_pushvalue(L, -2); |
| 7871 | lua_insert(L, -2); |
| 7872 | /* stack: [ ... <headers:table>, <table>, k, k, v ] */ |
| 7873 | |
| 7874 | lua_newtable(L); |
| 7875 | lua_insert(L, -2); |
| 7876 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */ |
| 7877 | |
| 7878 | if (lua_isstring(L, -1)) { |
| 7879 | /* push the value in the inner table */ |
| 7880 | lua_rawseti(L, -2, 1); |
| 7881 | } |
| 7882 | else { /* table */ |
| 7883 | lua_pushnil(L); |
| 7884 | while (lua_next(L, -2) != 0) { |
| 7885 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */ |
| 7886 | if (!lua_isstring(L, -1)) { |
| 7887 | /* invalid value type, skip it*/ |
| 7888 | lua_pop(L, 1); |
| 7889 | continue; |
| 7890 | } |
| 7891 | /* push the value in the inner table */ |
| 7892 | lua_rawseti(L, -4, lua_rawlen(L, -4) + 1); |
| 7893 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */ |
| 7894 | } |
| 7895 | lua_pop(L, 1); |
| 7896 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */ |
| 7897 | } |
| 7898 | |
| 7899 | /* push (k,v) on the stack in the headers table: |
| 7900 | * stack: [ ... <headers:table>, <table>, k, k, v ] |
| 7901 | */ |
| 7902 | lua_settable(L, -5); |
| 7903 | /* stack: [ ... <headers:table>, <table>, k ] */ |
| 7904 | } |
| 7905 | } |
| 7906 | lua_pop(L, 1); |
| 7907 | } |
| 7908 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 7909 | lua_settable(L, -3); |
| 7910 | /* stack: [ txn, <Arg:table>, <Reply:table> ] */ |
| 7911 | |
| 7912 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 7913 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref); |
| 7914 | lua_setmetatable(L, -2); |
| 7915 | return 1; |
| 7916 | } |
| 7917 | |
| 7918 | /* Set the reply status code, and optionally the reason. If no reason is |
| 7919 | * provided, the default one corresponding to the status code is used. |
| 7920 | */ |
| 7921 | __LJMP static int hlua_txn_reply_set_status(lua_State *L) |
| 7922 | { |
| 7923 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7924 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 7925 | |
| 7926 | /* First argument (self) must be a table */ |
| 7927 | luaL_checktype(L, 1, LUA_TTABLE); |
| 7928 | |
| 7929 | if (status < 100 || status > 599) { |
| 7930 | lua_pushboolean(L, 0); |
| 7931 | return 1; |
| 7932 | } |
| 7933 | if (!reason) |
| 7934 | reason = http_get_reason(status); |
| 7935 | |
| 7936 | lua_pushinteger(L, status); |
| 7937 | lua_setfield(L, 1, "status"); |
| 7938 | |
| 7939 | lua_pushstring(L, reason); |
| 7940 | lua_setfield(L, 1, "reason"); |
| 7941 | |
| 7942 | lua_pushboolean(L, 1); |
| 7943 | return 1; |
| 7944 | } |
| 7945 | |
| 7946 | /* Add a header into the reply object. Each header name is associated to an |
| 7947 | * array of values in the "headers" table. If the header name is not found, a |
| 7948 | * new entry is created. |
| 7949 | */ |
| 7950 | __LJMP static int hlua_txn_reply_add_header(lua_State *L) |
| 7951 | { |
| 7952 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7953 | const char *value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 7954 | int ret; |
| 7955 | |
| 7956 | /* First argument (self) must be a table */ |
| 7957 | luaL_checktype(L, 1, LUA_TTABLE); |
| 7958 | |
| 7959 | /* Push in the stack the "headers" entry. */ |
| 7960 | ret = lua_getfield(L, 1, "headers"); |
| 7961 | if (ret != LUA_TTABLE) { |
| 7962 | hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret)); |
| 7963 | WILL_LJMP(lua_error(L)); |
| 7964 | } |
| 7965 | |
| 7966 | /* check if the header is already registered. If not, register it. */ |
| 7967 | ret = lua_getfield(L, -1, name); |
| 7968 | if (ret == LUA_TNIL) { |
| 7969 | /* Entry not found. */ |
| 7970 | lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */ |
| 7971 | |
| 7972 | /* Insert the new header name in the array in the top of the stack. |
| 7973 | * It left the new array in the top of the stack. |
| 7974 | */ |
| 7975 | lua_newtable(L); |
| 7976 | lua_pushstring(L, name); |
| 7977 | lua_pushvalue(L, -2); |
| 7978 | lua_settable(L, -4); |
| 7979 | } |
| 7980 | else if (ret != LUA_TTABLE) { |
| 7981 | hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret)); |
| 7982 | WILL_LJMP(lua_error(L)); |
| 7983 | } |
| 7984 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 7985 | /* Now the top of thestack is an array of values. We push |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7986 | * the header value as new entry. |
| 7987 | */ |
| 7988 | lua_pushstring(L, value); |
| 7989 | ret = lua_rawlen(L, -2); |
| 7990 | lua_rawseti(L, -2, ret + 1); |
| 7991 | |
| 7992 | lua_pushboolean(L, 1); |
| 7993 | return 1; |
| 7994 | } |
| 7995 | |
| 7996 | /* Remove all occurrences of a given header name. */ |
| 7997 | __LJMP static int hlua_txn_reply_del_header(lua_State *L) |
| 7998 | { |
| 7999 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8000 | int ret; |
| 8001 | |
| 8002 | /* First argument (self) must be a table */ |
| 8003 | luaL_checktype(L, 1, LUA_TTABLE); |
| 8004 | |
| 8005 | /* Push in the stack the "headers" entry. */ |
| 8006 | ret = lua_getfield(L, 1, "headers"); |
| 8007 | if (ret != LUA_TTABLE) { |
| 8008 | hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret)); |
| 8009 | WILL_LJMP(lua_error(L)); |
| 8010 | } |
| 8011 | |
| 8012 | lua_pushstring(L, name); |
| 8013 | lua_pushnil(L); |
| 8014 | lua_settable(L, -3); |
| 8015 | |
| 8016 | lua_pushboolean(L, 1); |
| 8017 | return 1; |
| 8018 | } |
| 8019 | |
| 8020 | /* Set the reply's body. Overwrite any existing entry. */ |
| 8021 | __LJMP static int hlua_txn_reply_set_body(lua_State *L) |
| 8022 | { |
| 8023 | const char *payload = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8024 | |
| 8025 | /* First argument (self) must be a table */ |
| 8026 | luaL_checktype(L, 1, LUA_TTABLE); |
| 8027 | |
| 8028 | lua_pushstring(L, payload); |
| 8029 | lua_setfield(L, 1, "body"); |
| 8030 | |
| 8031 | lua_pushboolean(L, 1); |
| 8032 | return 1; |
| 8033 | } |
| 8034 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 8035 | __LJMP static int hlua_log(lua_State *L) |
| 8036 | { |
| 8037 | int level; |
| 8038 | const char *msg; |
| 8039 | |
| 8040 | MAY_LJMP(check_args(L, 2, "log")); |
| 8041 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 8042 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8043 | |
| 8044 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 8045 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 8046 | |
| 8047 | hlua_sendlog(NULL, level, msg); |
| 8048 | return 0; |
| 8049 | } |
| 8050 | |
| 8051 | __LJMP static int hlua_log_debug(lua_State *L) |
| 8052 | { |
| 8053 | const char *msg; |
| 8054 | |
| 8055 | MAY_LJMP(check_args(L, 1, "debug")); |
| 8056 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8057 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 8058 | return 0; |
| 8059 | } |
| 8060 | |
| 8061 | __LJMP static int hlua_log_info(lua_State *L) |
| 8062 | { |
| 8063 | const char *msg; |
| 8064 | |
| 8065 | MAY_LJMP(check_args(L, 1, "info")); |
| 8066 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8067 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 8068 | return 0; |
| 8069 | } |
| 8070 | |
| 8071 | __LJMP static int hlua_log_warning(lua_State *L) |
| 8072 | { |
| 8073 | const char *msg; |
| 8074 | |
| 8075 | MAY_LJMP(check_args(L, 1, "warning")); |
| 8076 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8077 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 8078 | return 0; |
| 8079 | } |
| 8080 | |
| 8081 | __LJMP static int hlua_log_alert(lua_State *L) |
| 8082 | { |
| 8083 | const char *msg; |
| 8084 | |
| 8085 | MAY_LJMP(check_args(L, 1, "alert")); |
| 8086 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8087 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8088 | return 0; |
| 8089 | } |
| 8090 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8091 | __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] | 8092 | { |
| 8093 | int wakeup_ms = lua_tointeger(L, -1); |
| 8094 | if (now_ms < wakeup_ms) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8095 | 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] | 8096 | return 0; |
| 8097 | } |
| 8098 | |
| 8099 | __LJMP static int hlua_sleep(lua_State *L) |
| 8100 | { |
| 8101 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8102 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8103 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8104 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8105 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8106 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8107 | wakeup_ms = tick_add(now_ms, delay); |
| 8108 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8109 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8110 | 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] | 8111 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8112 | } |
| 8113 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8114 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8115 | { |
| 8116 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8117 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8118 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8119 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8120 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8121 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8122 | wakeup_ms = tick_add(now_ms, delay); |
| 8123 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8124 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8125 | 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] | 8126 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8127 | } |
| 8128 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8129 | /* This functionis an LUA binding. it permits to give back |
| 8130 | * the hand at the HAProxy scheduler. It is used when the |
| 8131 | * LUA processing consumes a lot of time. |
| 8132 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8133 | __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] | 8134 | { |
| 8135 | return 0; |
| 8136 | } |
| 8137 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8138 | __LJMP static int hlua_yield(lua_State *L) |
| 8139 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8140 | 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] | 8141 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8142 | } |
| 8143 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8144 | /* This function change the nice of the currently executed |
| 8145 | * task. It is used set low or high priority at the current |
| 8146 | * task. |
| 8147 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 8148 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8149 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8150 | struct hlua *hlua; |
| 8151 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8152 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8153 | MAY_LJMP(check_args(L, 1, "set_nice")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8154 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8155 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8156 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 8157 | hlua = hlua_gethlua(L); |
| 8158 | |
| 8159 | /* If the task is not set, I'm in a start mode. */ |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8160 | if (!hlua || !hlua->task) |
| 8161 | return 0; |
| 8162 | |
| 8163 | if (nice < -1024) |
| 8164 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8165 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8166 | nice = 1024; |
| 8167 | |
| 8168 | hlua->task->nice = nice; |
| 8169 | return 0; |
| 8170 | } |
| 8171 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8172 | /* 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] | 8173 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 8174 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 8175 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8176 | * |
| 8177 | * Task wrapper are longjmp safe because the only one Lua code |
| 8178 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8179 | */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 8180 | 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] | 8181 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 8182 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8183 | enum hlua_exec status; |
| 8184 | |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 8185 | if (task->thread_mask == MAX_THREADS_MASK) |
| 8186 | task_set_affinity(task, tid_bit); |
| 8187 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8188 | /* If it is the first call to the task, we must initialize the |
| 8189 | * execution timeouts. |
| 8190 | */ |
| 8191 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 8192 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8193 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8194 | /* Execute the Lua code. */ |
| 8195 | status = hlua_ctx_resume(hlua, 1); |
| 8196 | |
| 8197 | switch (status) { |
| 8198 | /* finished or yield */ |
| 8199 | case HLUA_E_OK: |
| 8200 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 8201 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 8202 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8203 | break; |
| 8204 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 8205 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 8206 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 8207 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8208 | break; |
| 8209 | |
| 8210 | /* finished with error. */ |
| 8211 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8212 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8213 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 8214 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 8215 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8216 | break; |
| 8217 | |
| 8218 | case HLUA_E_ERR: |
| 8219 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8220 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8221 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 8222 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 8223 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8224 | break; |
| 8225 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 8226 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8227 | } |
| 8228 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8229 | /* This function is an LUA binding that register LUA function to be |
| 8230 | * executed after the HAProxy configuration parsing and before the |
| 8231 | * HAProxy scheduler starts. This function expect only one LUA |
| 8232 | * argument that is a function. This function returns nothing, but |
| 8233 | * throws if an error is encountered. |
| 8234 | */ |
| 8235 | __LJMP static int hlua_register_init(lua_State *L) |
| 8236 | { |
| 8237 | struct hlua_init_function *init; |
| 8238 | int ref; |
| 8239 | |
| 8240 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 8241 | |
| 8242 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 8243 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8244 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8245 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8246 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8247 | |
| 8248 | init->function_ref = ref; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 8249 | LIST_APPEND(&hlua_init_functions[hlua_state_id], &init->l); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8250 | return 0; |
| 8251 | } |
| 8252 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8253 | /* This functio is an LUA binding. It permits to register a task |
| 8254 | * executed in parallel of the main HAroxy activity. The task is |
| 8255 | * created and it is set in the HAProxy scheduler. It can be called |
| 8256 | * from the "init" section, "post init" or during the runtime. |
| 8257 | * |
| 8258 | * Lua prototype: |
| 8259 | * |
| 8260 | * <none> core.register_task(<function>) |
| 8261 | */ |
| 8262 | static int hlua_register_task(lua_State *L) |
| 8263 | { |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8264 | struct hlua *hlua = NULL; |
| 8265 | struct task *task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8266 | int ref; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8267 | int state_id; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8268 | |
| 8269 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 8270 | |
| 8271 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 8272 | |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 8273 | /* Get the reference state. If the reference is NULL, L is the master |
| 8274 | * state, otherwise hlua->T is. |
| 8275 | */ |
| 8276 | hlua = hlua_gethlua(L); |
| 8277 | if (hlua) |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8278 | /* we are in runtime processing */ |
| 8279 | state_id = hlua->state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 8280 | else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8281 | /* we are in initialization mode */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8282 | state_id = hlua_state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 8283 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8284 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8285 | if (!hlua) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8286 | goto alloc_error; |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8287 | HLUA_INIT(hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8288 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8289 | /* We are in the common lua state, execute the task anywhere, |
| 8290 | * otherwise, inherit the current thread identifier |
| 8291 | */ |
| 8292 | if (state_id == 0) |
| 8293 | task = task_new(MAX_THREADS_MASK); |
| 8294 | else |
| 8295 | task = task_new(tid_bit); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 8296 | if (!task) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8297 | goto alloc_error; |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 8298 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8299 | task->context = hlua; |
| 8300 | task->process = hlua_process_task; |
| 8301 | |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8302 | if (!hlua_ctx_init(hlua, state_id, task, 1)) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8303 | goto alloc_error; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8304 | |
| 8305 | /* Restore the function in the stack. */ |
| 8306 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 8307 | hlua->nargs = 0; |
| 8308 | |
| 8309 | /* Schedule task. */ |
| 8310 | task_schedule(task, now_ms); |
| 8311 | |
| 8312 | return 0; |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8313 | |
| 8314 | alloc_error: |
| 8315 | task_destroy(task); |
| 8316 | hlua_ctx_destroy(hlua); |
| 8317 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 8318 | return 0; /* Never reached */ |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8319 | } |
| 8320 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8321 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 8322 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 8323 | * resume converters. |
| 8324 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8325 | 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] | 8326 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 8327 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8328 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8329 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8330 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8331 | if (!stream) |
| 8332 | return 0; |
| 8333 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8334 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8335 | * Lua context can be not initialized. This behavior |
| 8336 | * permits to save performances because a systematic |
| 8337 | * Lua initialization cause 5% performances loss. |
| 8338 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8339 | if (!stream->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8340 | struct hlua *hlua; |
| 8341 | |
| 8342 | hlua = pool_alloc(pool_head_hlua); |
| 8343 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8344 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 8345 | return 0; |
| 8346 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8347 | HLUA_INIT(hlua); |
| 8348 | stream->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8349 | 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] | 8350 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 8351 | return 0; |
| 8352 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8353 | } |
| 8354 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8355 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8356 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8357 | |
| 8358 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8359 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8360 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 8361 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8362 | else |
| 8363 | error = "critical error"; |
| 8364 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8365 | return 0; |
| 8366 | } |
| 8367 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8368 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8369 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8370 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8371 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8372 | return 0; |
| 8373 | } |
| 8374 | |
| 8375 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8376 | 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] | 8377 | |
| 8378 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8379 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8380 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8381 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8382 | return 0; |
| 8383 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8384 | hlua_smp2lua(stream->hlua->T, smp); |
| 8385 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8386 | |
| 8387 | /* push keywords in the stack. */ |
| 8388 | if (arg_p) { |
| 8389 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8390 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8391 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8392 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8393 | return 0; |
| 8394 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8395 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 8396 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8397 | } |
| 8398 | } |
| 8399 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8400 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8401 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8402 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8403 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8404 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8405 | } |
| 8406 | |
| 8407 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8408 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8409 | /* finished. */ |
| 8410 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 8411 | /* If the stack is empty, the function fails. */ |
| 8412 | if (lua_gettop(stream->hlua->T) <= 0) |
| 8413 | return 0; |
| 8414 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8415 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8416 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 8417 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8418 | return 1; |
| 8419 | |
| 8420 | /* yield. */ |
| 8421 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8422 | 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] | 8423 | return 0; |
| 8424 | |
| 8425 | /* finished with error. */ |
| 8426 | case HLUA_E_ERRMSG: |
| 8427 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8428 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8429 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 8430 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8431 | return 0; |
| 8432 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8433 | case HLUA_E_ETMOUT: |
| 8434 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 8435 | return 0; |
| 8436 | |
| 8437 | case HLUA_E_NOMEM: |
| 8438 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 8439 | return 0; |
| 8440 | |
| 8441 | case HLUA_E_YIELD: |
| 8442 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 8443 | return 0; |
| 8444 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8445 | case HLUA_E_ERR: |
| 8446 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8447 | 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] | 8448 | /* fall through */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8449 | |
| 8450 | default: |
| 8451 | return 0; |
| 8452 | } |
| 8453 | } |
| 8454 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8455 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 8456 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8457 | * resume sample-fetches. This function will be called by the sample |
| 8458 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8459 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8460 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 8461 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8462 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 8463 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8464 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8465 | const char *error; |
Christopher Faulet | 8c9e6bb | 2021-08-06 16:29:41 +0200 | [diff] [blame] | 8466 | unsigned int hflags = HLUA_TXN_NOTERM | HLUA_TXN_SMP_CTX; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8467 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8468 | if (!stream) |
| 8469 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 8470 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8471 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8472 | * Lua context can be not initialized. This behavior |
| 8473 | * permits to save performances because a systematic |
| 8474 | * Lua initialization cause 5% performances loss. |
| 8475 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8476 | if (!stream->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8477 | struct hlua *hlua; |
| 8478 | |
| 8479 | hlua = pool_alloc(pool_head_hlua); |
| 8480 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8481 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 8482 | return 0; |
| 8483 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8484 | hlua->T = NULL; |
| 8485 | stream->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8486 | 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] | 8487 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 8488 | return 0; |
| 8489 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8490 | } |
| 8491 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8492 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8493 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8494 | |
| 8495 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8496 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8497 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 8498 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8499 | else |
| 8500 | error = "critical error"; |
| 8501 | 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] | 8502 | return 0; |
| 8503 | } |
| 8504 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8505 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8506 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8507 | 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] | 8508 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8509 | return 0; |
| 8510 | } |
| 8511 | |
| 8512 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8513 | 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] | 8514 | |
| 8515 | /* push arguments in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 8516 | 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] | 8517 | 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] | 8518 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8519 | return 0; |
| 8520 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8521 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8522 | |
| 8523 | /* push keywords in the stack. */ |
| 8524 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 8525 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8526 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8527 | 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] | 8528 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8529 | return 0; |
| 8530 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8531 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 8532 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8533 | } |
| 8534 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8535 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8536 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8537 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8538 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8539 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8540 | } |
| 8541 | |
| 8542 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8543 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8544 | /* finished. */ |
| 8545 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 8546 | /* If the stack is empty, the function fails. */ |
| 8547 | if (lua_gettop(stream->hlua->T) <= 0) |
| 8548 | return 0; |
| 8549 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8550 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8551 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 8552 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8553 | |
| 8554 | /* Set the end of execution flag. */ |
| 8555 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 8556 | return 1; |
| 8557 | |
| 8558 | /* yield. */ |
| 8559 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8560 | 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] | 8561 | return 0; |
| 8562 | |
| 8563 | /* finished with error. */ |
| 8564 | case HLUA_E_ERRMSG: |
| 8565 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8566 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8567 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 8568 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8569 | return 0; |
| 8570 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8571 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8572 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 8573 | return 0; |
| 8574 | |
| 8575 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8576 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 8577 | return 0; |
| 8578 | |
| 8579 | case HLUA_E_YIELD: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8580 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 8581 | return 0; |
| 8582 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8583 | case HLUA_E_ERR: |
| 8584 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8585 | 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] | 8586 | /* fall through */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8587 | |
| 8588 | default: |
| 8589 | return 0; |
| 8590 | } |
| 8591 | } |
| 8592 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8593 | /* This function is an LUA binding used for registering |
| 8594 | * "sample-conv" functions. It expects a converter name used |
| 8595 | * in the haproxy configuration file, and an LUA function. |
| 8596 | */ |
| 8597 | __LJMP static int hlua_register_converters(lua_State *L) |
| 8598 | { |
| 8599 | struct sample_conv_kw_list *sck; |
| 8600 | const char *name; |
| 8601 | int ref; |
| 8602 | int len; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8603 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8604 | struct sample_conv *sc; |
| 8605 | struct buffer *trash; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8606 | |
| 8607 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 8608 | |
| 8609 | /* First argument : converter name. */ |
| 8610 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8611 | |
| 8612 | /* Second argument : lua function. */ |
| 8613 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 8614 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8615 | /* Check if the converter is already registered */ |
| 8616 | trash = get_trash_chunk(); |
| 8617 | chunk_printf(trash, "lua.%s", name); |
| 8618 | sc = find_sample_conv(trash->area, trash->data); |
| 8619 | if (sc != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8620 | fcn = sc->private; |
| 8621 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 8622 | ha_warning("Trying to register converter 'lua.%s' more than once. " |
| 8623 | "This will become a hard error in version 2.5.\n", name); |
| 8624 | } |
| 8625 | fcn->function_ref[hlua_state_id] = ref; |
| 8626 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8627 | } |
| 8628 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8629 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8630 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8631 | if (!sck) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8632 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 8633 | fcn = new_hlua_function(); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8634 | if (!fcn) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8635 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8636 | |
| 8637 | /* Fill fcn. */ |
| 8638 | fcn->name = strdup(name); |
| 8639 | if (!fcn->name) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8640 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8641 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8642 | |
| 8643 | /* List head */ |
| 8644 | sck->list.n = sck->list.p = NULL; |
| 8645 | |
| 8646 | /* converter keyword. */ |
| 8647 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8648 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8649 | if (!sck->kw[0].kw) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8650 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8651 | |
| 8652 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 8653 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 8654 | 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] | 8655 | sck->kw[0].val_args = NULL; |
| 8656 | sck->kw[0].in_type = SMP_T_STR; |
| 8657 | sck->kw[0].out_type = SMP_T_STR; |
| 8658 | sck->kw[0].private = fcn; |
| 8659 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8660 | /* Register this new converter */ |
| 8661 | sample_register_convs(sck); |
| 8662 | |
| 8663 | return 0; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8664 | |
| 8665 | alloc_error: |
| 8666 | release_hlua_function(fcn); |
| 8667 | ha_free(&sck); |
| 8668 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 8669 | return 0; /* Never reached */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8670 | } |
| 8671 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8672 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8673 | * "sample-fetch" functions. It expects a converter name used |
| 8674 | * in the haproxy configuration file, and an LUA function. |
| 8675 | */ |
| 8676 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 8677 | { |
| 8678 | const char *name; |
| 8679 | int ref; |
| 8680 | int len; |
| 8681 | struct sample_fetch_kw_list *sfk; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8682 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8683 | struct sample_fetch *sf; |
| 8684 | struct buffer *trash; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8685 | |
| 8686 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 8687 | |
| 8688 | /* First argument : sample-fetch name. */ |
| 8689 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8690 | |
| 8691 | /* Second argument : lua function. */ |
| 8692 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 8693 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8694 | /* Check if the sample-fetch is already registered */ |
| 8695 | trash = get_trash_chunk(); |
| 8696 | chunk_printf(trash, "lua.%s", name); |
| 8697 | sf = find_sample_fetch(trash->area, trash->data); |
| 8698 | if (sf != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8699 | fcn = sf->private; |
| 8700 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 8701 | ha_warning("Trying to register sample-fetch 'lua.%s' more than once. " |
| 8702 | "This will become a hard error in version 2.5.\n", name); |
| 8703 | } |
| 8704 | fcn->function_ref[hlua_state_id] = ref; |
| 8705 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8706 | } |
| 8707 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8708 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8709 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8710 | if (!sfk) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8711 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 8712 | fcn = new_hlua_function(); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8713 | if (!fcn) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8714 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8715 | |
| 8716 | /* Fill fcn. */ |
| 8717 | fcn->name = strdup(name); |
| 8718 | if (!fcn->name) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8719 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8720 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8721 | |
| 8722 | /* List head */ |
| 8723 | sfk->list.n = sfk->list.p = NULL; |
| 8724 | |
| 8725 | /* sample-fetch keyword. */ |
| 8726 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8727 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8728 | if (!sfk->kw[0].kw) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8729 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8730 | |
| 8731 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 8732 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 8733 | 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] | 8734 | sfk->kw[0].val_args = NULL; |
| 8735 | sfk->kw[0].out_type = SMP_T_STR; |
| 8736 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 8737 | sfk->kw[0].val = 0; |
| 8738 | sfk->kw[0].private = fcn; |
| 8739 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8740 | /* Register this new fetch. */ |
| 8741 | sample_register_fetches(sfk); |
| 8742 | |
| 8743 | return 0; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8744 | |
| 8745 | alloc_error: |
| 8746 | release_hlua_function(fcn); |
| 8747 | ha_free(&sfk); |
| 8748 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 8749 | return 0; /* Never reached */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8750 | } |
| 8751 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 8752 | /* This function is a lua binding to set the wake_time. |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8753 | */ |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 8754 | __LJMP static int hlua_set_wake_time(lua_State *L) |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8755 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8756 | struct hlua *hlua; |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8757 | unsigned int delay; |
| 8758 | unsigned int wakeup_ms; |
| 8759 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8760 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 8761 | hlua = hlua_gethlua(L); |
| 8762 | if (!hlua) { |
| 8763 | return 0; |
| 8764 | } |
| 8765 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8766 | MAY_LJMP(check_args(L, 1, "wake_time")); |
| 8767 | |
| 8768 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 8769 | wakeup_ms = tick_add(now_ms, delay); |
| 8770 | hlua->wake_time = wakeup_ms; |
| 8771 | return 0; |
| 8772 | } |
| 8773 | |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8774 | /* This function is a wrapper to execute each LUA function declared as an action |
| 8775 | * wrapper during the initialisation period. This function may return any |
| 8776 | * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is |
| 8777 | * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the |
| 8778 | * return value is the first element on the stack. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8779 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 8780 | 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] | 8781 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8782 | { |
| 8783 | char **arg; |
Christopher Faulet | 8c9e6bb | 2021-08-06 16:29:41 +0200 | [diff] [blame] | 8784 | unsigned int hflags = HLUA_TXN_ACT_CTX; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8785 | int dir, act_ret = ACT_RET_CONT; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8786 | const char *error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 8787 | |
| 8788 | switch (rule->from) { |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 8789 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 8790 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 8791 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 8792 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 8793 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8794 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8795 | goto end; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 8796 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8797 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8798 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8799 | * Lua context can be not initialized. This behavior |
| 8800 | * permits to save performances because a systematic |
| 8801 | * Lua initialization cause 5% performances loss. |
| 8802 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8803 | if (!s->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8804 | struct hlua *hlua; |
| 8805 | |
| 8806 | hlua = pool_alloc(pool_head_hlua); |
| 8807 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8808 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8809 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8810 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8811 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8812 | HLUA_INIT(hlua); |
| 8813 | s->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8814 | 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] | 8815 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8816 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8817 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8818 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8819 | } |
| 8820 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8821 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8822 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8823 | |
| 8824 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8825 | if (!SET_SAFE_LJMP(s->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8826 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 8827 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8828 | else |
| 8829 | error = "critical error"; |
| 8830 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8831 | rule->arg.hlua_rule->fcn->name, error); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8832 | goto end; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8833 | } |
| 8834 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8835 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8836 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8837 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8838 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8839 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8840 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8841 | } |
| 8842 | |
| 8843 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8844 | 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] | 8845 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8846 | /* Create and and push object stream in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 8847 | if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8848 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8849 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8850 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8851 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8852 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8853 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8854 | |
| 8855 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 8856 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8857 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8858 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8859 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8860 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8861 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8862 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8863 | lua_pushstring(s->hlua->T, *arg); |
| 8864 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8865 | } |
| 8866 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8867 | /* Now the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8868 | RESET_SAFE_LJMP(s->hlua); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8869 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8870 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8871 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8872 | } |
| 8873 | |
| 8874 | /* Execute the function. */ |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 8875 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8876 | /* finished. */ |
| 8877 | case HLUA_E_OK: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8878 | /* Catch the return value */ |
| 8879 | if (lua_gettop(s->hlua->T) > 0) |
| 8880 | act_ret = lua_tointeger(s->hlua->T, -1); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8881 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8882 | /* Set timeout in the required channel. */ |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 8883 | if (act_ret == ACT_RET_YIELD) { |
| 8884 | if (flags & ACT_OPT_FINAL) |
| 8885 | goto err_yield; |
| 8886 | |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 8887 | if (dir == SMP_OPT_DIR_REQ) |
| 8888 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 8889 | s->hlua->wake_time); |
| 8890 | else |
| 8891 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 8892 | s->hlua->wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8893 | } |
| 8894 | goto end; |
| 8895 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8896 | /* yield. */ |
| 8897 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 8898 | /* Set timeout in the required channel. */ |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 8899 | if (dir == SMP_OPT_DIR_REQ) |
| 8900 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 8901 | s->hlua->wake_time); |
| 8902 | else |
| 8903 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 8904 | s->hlua->wake_time); |
| 8905 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8906 | /* Some actions can be wake up when a "write" event |
| 8907 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8908 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8909 | */ |
Christopher Faulet | 51fa358 | 2019-07-26 14:54:52 +0200 | [diff] [blame] | 8910 | if (HLUA_IS_WAKERESWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8911 | s->res.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8912 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 8913 | s->req.flags |= CF_WAKE_WRITE; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8914 | act_ret = ACT_RET_YIELD; |
| 8915 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8916 | |
| 8917 | /* finished with error. */ |
| 8918 | case HLUA_E_ERRMSG: |
| 8919 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8920 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8921 | rule->arg.hlua_rule->fcn->name, lua_tostring(s->hlua->T, -1)); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8922 | lua_pop(s->hlua->T, 1); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8923 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8924 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8925 | case HLUA_E_ETMOUT: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8926 | 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] | 8927 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8928 | |
| 8929 | case HLUA_E_NOMEM: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8930 | 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] | 8931 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8932 | |
| 8933 | case HLUA_E_YIELD: |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 8934 | err_yield: |
| 8935 | act_ret = ACT_RET_CONT; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8936 | 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] | 8937 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8938 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8939 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8940 | case HLUA_E_ERR: |
| 8941 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8942 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8943 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8944 | |
| 8945 | default: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8946 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8947 | } |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8948 | |
| 8949 | end: |
Christopher Faulet | 2361fd9 | 2020-07-30 10:40:58 +0200 | [diff] [blame] | 8950 | if (act_ret != ACT_RET_YIELD && s->hlua) |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 8951 | s->hlua->wake_time = TICK_ETERNITY; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8952 | return act_ret; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8953 | } |
| 8954 | |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 8955 | 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] | 8956 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 8957 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8958 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8959 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 8960 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 8961 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8962 | } |
| 8963 | |
| 8964 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 8965 | { |
| 8966 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8967 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8968 | struct task *task; |
| 8969 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8970 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8971 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8972 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8973 | if (!hlua) { |
| 8974 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8975 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8976 | return 0; |
| 8977 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8978 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8979 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8980 | ctx->ctx.hlua_apptcp.flags = 0; |
| 8981 | |
| 8982 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 8983 | task = task_new(tid_bit); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8984 | if (!task) { |
| 8985 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8986 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8987 | return 0; |
| 8988 | } |
| 8989 | task->nice = 0; |
| 8990 | task->context = ctx; |
| 8991 | task->process = hlua_applet_wakeup; |
| 8992 | ctx->ctx.hlua_apptcp.task = task; |
| 8993 | |
| 8994 | /* In the execution wrappers linked with a stream, the |
| 8995 | * Lua context can be not initialized. This behavior |
| 8996 | * permits to save performances because a systematic |
| 8997 | * Lua initialization cause 5% performances loss. |
| 8998 | */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8999 | 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] | 9000 | 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] | 9001 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9002 | return 0; |
| 9003 | } |
| 9004 | |
| 9005 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 9006 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9007 | |
| 9008 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9009 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9010 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 9011 | error = lua_tostring(hlua->T, -1); |
| 9012 | else |
| 9013 | error = "critical error"; |
| 9014 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9015 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9016 | return 0; |
| 9017 | } |
| 9018 | |
| 9019 | /* Check stack available size. */ |
| 9020 | if (!lua_checkstack(hlua->T, 1)) { |
| 9021 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9022 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9023 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9024 | return 0; |
| 9025 | } |
| 9026 | |
| 9027 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9028 | 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] | 9029 | |
| 9030 | /* Create and and push object stream in the stack. */ |
| 9031 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 9032 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9033 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9034 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9035 | return 0; |
| 9036 | } |
| 9037 | hlua->nargs = 1; |
| 9038 | |
| 9039 | /* push keywords in the stack. */ |
| 9040 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 9041 | if (!lua_checkstack(hlua->T, 1)) { |
| 9042 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9043 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9044 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9045 | return 0; |
| 9046 | } |
| 9047 | lua_pushstring(hlua->T, *arg); |
| 9048 | hlua->nargs++; |
| 9049 | } |
| 9050 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9051 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9052 | |
| 9053 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 9054 | si_cant_get(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 9055 | si_rx_endp_more(si); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9056 | |
| 9057 | return 1; |
| 9058 | } |
| 9059 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 9060 | void hlua_applet_tcp_fct(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9061 | { |
| 9062 | struct stream_interface *si = ctx->owner; |
| 9063 | struct stream *strm = si_strm(si); |
| 9064 | struct channel *res = si_ic(si); |
| 9065 | struct act_rule *rule = ctx->rule; |
| 9066 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9067 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9068 | |
| 9069 | /* The applet execution is already done. */ |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 9070 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) { |
| 9071 | /* eat the whole request */ |
| 9072 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9073 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 9074 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9075 | |
| 9076 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 9077 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 9078 | return; |
| 9079 | |
| 9080 | /* Execute the function. */ |
| 9081 | switch (hlua_ctx_resume(hlua, 1)) { |
| 9082 | /* finished. */ |
| 9083 | case HLUA_E_OK: |
| 9084 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 9085 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9086 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 9087 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9088 | res->flags |= CF_READ_NULL; |
| 9089 | si_shutr(si); |
| 9090 | return; |
| 9091 | |
| 9092 | /* yield. */ |
| 9093 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 9094 | if (hlua->wake_time != TICK_ETERNITY) |
| 9095 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9096 | return; |
| 9097 | |
| 9098 | /* finished with error. */ |
| 9099 | case HLUA_E_ERRMSG: |
| 9100 | /* Display log. */ |
| 9101 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9102 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9103 | lua_pop(hlua->T, 1); |
| 9104 | goto error; |
| 9105 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9106 | case HLUA_E_ETMOUT: |
| 9107 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9108 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9109 | goto error; |
| 9110 | |
| 9111 | case HLUA_E_NOMEM: |
| 9112 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9113 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9114 | goto error; |
| 9115 | |
| 9116 | case HLUA_E_YIELD: /* unexpected */ |
| 9117 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9118 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9119 | goto error; |
| 9120 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9121 | case HLUA_E_ERR: |
| 9122 | /* Display log. */ |
| 9123 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9124 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9125 | goto error; |
| 9126 | |
| 9127 | default: |
| 9128 | goto error; |
| 9129 | } |
| 9130 | |
| 9131 | error: |
| 9132 | |
| 9133 | /* For all other cases, just close the stream. */ |
| 9134 | si_shutw(si); |
| 9135 | si_shutr(si); |
| 9136 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 9137 | } |
| 9138 | |
| 9139 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 9140 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 9141 | task_destroy(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9142 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9143 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9144 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9145 | } |
| 9146 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9147 | /* The function returns 1 if the initialisation is complete, 0 if |
| 9148 | * an errors occurs and -1 if more data are required for initializing |
| 9149 | * the applet. |
| 9150 | */ |
| 9151 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 9152 | { |
| 9153 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9154 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9155 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9156 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9157 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9158 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9159 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9160 | txn = strm->txn; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 9161 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9162 | if (!hlua) { |
| 9163 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9164 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9165 | return 0; |
| 9166 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9167 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9168 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9169 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 9170 | ctx->ctx.hlua_apphttp.flags = 0; |
| 9171 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 9172 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 9173 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 9174 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9175 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 9176 | task = task_new(tid_bit); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9177 | if (!task) { |
| 9178 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9179 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9180 | return 0; |
| 9181 | } |
| 9182 | task->nice = 0; |
| 9183 | task->context = ctx; |
| 9184 | task->process = hlua_applet_wakeup; |
| 9185 | ctx->ctx.hlua_apphttp.task = task; |
| 9186 | |
| 9187 | /* In the execution wrappers linked with a stream, the |
| 9188 | * Lua context can be not initialized. This behavior |
| 9189 | * permits to save performances because a systematic |
| 9190 | * Lua initialization cause 5% performances loss. |
| 9191 | */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9192 | 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] | 9193 | 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] | 9194 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9195 | return 0; |
| 9196 | } |
| 9197 | |
| 9198 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 9199 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9200 | |
| 9201 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9202 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9203 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 9204 | error = lua_tostring(hlua->T, -1); |
| 9205 | else |
| 9206 | error = "critical error"; |
| 9207 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9208 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9209 | return 0; |
| 9210 | } |
| 9211 | |
| 9212 | /* Check stack available size. */ |
| 9213 | if (!lua_checkstack(hlua->T, 1)) { |
| 9214 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9215 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9216 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9217 | return 0; |
| 9218 | } |
| 9219 | |
| 9220 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9221 | 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] | 9222 | |
| 9223 | /* Create and and push object stream in the stack. */ |
| 9224 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 9225 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9226 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9227 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9228 | return 0; |
| 9229 | } |
| 9230 | hlua->nargs = 1; |
| 9231 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9232 | /* push keywords in the stack. */ |
| 9233 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 9234 | if (!lua_checkstack(hlua->T, 1)) { |
| 9235 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9236 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9237 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9238 | return 0; |
| 9239 | } |
| 9240 | lua_pushstring(hlua->T, *arg); |
| 9241 | hlua->nargs++; |
| 9242 | } |
| 9243 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9244 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9245 | |
| 9246 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 9247 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9248 | |
| 9249 | return 1; |
| 9250 | } |
| 9251 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 9252 | void hlua_applet_http_fct(struct appctx *ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9253 | { |
| 9254 | struct stream_interface *si = ctx->owner; |
| 9255 | struct stream *strm = si_strm(si); |
| 9256 | struct channel *req = si_oc(si); |
| 9257 | struct channel *res = si_ic(si); |
| 9258 | struct act_rule *rule = ctx->rule; |
| 9259 | struct proxy *px = strm->be; |
| 9260 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
| 9261 | struct htx *req_htx, *res_htx; |
| 9262 | |
| 9263 | res_htx = htx_from_buf(&res->buf); |
| 9264 | |
| 9265 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 9266 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 9267 | goto out; |
| 9268 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9269 | /* Check if the input buffer is available. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9270 | if (!b_size(&res->buf)) { |
| 9271 | si_rx_room_blk(si); |
| 9272 | goto out; |
| 9273 | } |
| 9274 | /* check that the output is not closed */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9275 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9276 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 9277 | |
| 9278 | /* Set the currently running flag. */ |
| 9279 | if (!HLUA_IS_RUNNING(hlua) && |
| 9280 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
Christopher Faulet | bd878d2 | 2021-04-28 10:50:21 +0200 | [diff] [blame] | 9281 | if (!co_data(req)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9282 | si_cant_get(si); |
| 9283 | goto out; |
| 9284 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9285 | } |
| 9286 | |
| 9287 | /* Executes The applet if it is not done. */ |
| 9288 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 9289 | |
| 9290 | /* Execute the function. */ |
| 9291 | switch (hlua_ctx_resume(hlua, 1)) { |
| 9292 | /* finished. */ |
| 9293 | case HLUA_E_OK: |
| 9294 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 9295 | break; |
| 9296 | |
| 9297 | /* yield. */ |
| 9298 | case HLUA_E_AGAIN: |
| 9299 | if (hlua->wake_time != TICK_ETERNITY) |
| 9300 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9301 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9302 | |
| 9303 | /* finished with error. */ |
| 9304 | case HLUA_E_ERRMSG: |
| 9305 | /* Display log. */ |
| 9306 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9307 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9308 | lua_pop(hlua->T, 1); |
| 9309 | goto error; |
| 9310 | |
| 9311 | case HLUA_E_ETMOUT: |
| 9312 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9313 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9314 | goto error; |
| 9315 | |
| 9316 | case HLUA_E_NOMEM: |
| 9317 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9318 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9319 | goto error; |
| 9320 | |
| 9321 | case HLUA_E_YIELD: /* unexpected */ |
| 9322 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9323 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9324 | goto error; |
| 9325 | |
| 9326 | case HLUA_E_ERR: |
| 9327 | /* Display log. */ |
| 9328 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9329 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9330 | goto error; |
| 9331 | |
| 9332 | default: |
| 9333 | goto error; |
| 9334 | } |
| 9335 | } |
| 9336 | |
| 9337 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9338 | if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT) |
| 9339 | goto done; |
| 9340 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9341 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) |
| 9342 | goto error; |
| 9343 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 9344 | /* no more data are expected. Don't add TLR because mux-h1 will take care of it */ |
| 9345 | res_htx->flags |= HTX_FL_EOM; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9346 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 9347 | ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9348 | } |
| 9349 | |
| 9350 | done: |
| 9351 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9352 | if (!(res->flags & CF_SHUTR)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9353 | res->flags |= CF_READ_NULL; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9354 | si_shutr(si); |
| 9355 | } |
| 9356 | |
| 9357 | /* eat the whole request */ |
| 9358 | if (co_data(req)) { |
| 9359 | req_htx = htx_from_buf(&req->buf); |
| 9360 | co_htx_skip(req, req_htx, co_data(req)); |
| 9361 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9362 | } |
| 9363 | } |
| 9364 | |
| 9365 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9366 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9367 | return; |
| 9368 | |
| 9369 | error: |
| 9370 | |
| 9371 | /* If we are in HTTP mode, and we are not send any |
| 9372 | * data, return a 500 server error in best effort: |
| 9373 | * if there is no room available in the buffer, |
| 9374 | * just close the connection. |
| 9375 | */ |
| 9376 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 9377 | struct buffer *err = &http_err_chunks[HTTP_ERR_500]; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9378 | |
| 9379 | channel_erase(res); |
| 9380 | res->buf.data = b_data(err); |
| 9381 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 9382 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 9383 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9384 | } |
| 9385 | if (!(strm->flags & SF_ERR_MASK)) |
| 9386 | strm->flags |= SF_ERR_RESOURCE; |
| 9387 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 9388 | goto done; |
| 9389 | } |
| 9390 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9391 | static void hlua_applet_http_release(struct appctx *ctx) |
| 9392 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 9393 | task_destroy(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9394 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9395 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9396 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9397 | } |
| 9398 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9399 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9400 | * success case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9401 | * |
| 9402 | * This function can fail with an abort() due to an Lua critical error. |
| 9403 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 9404 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9405 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9406 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 9407 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9408 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9409 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9410 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9411 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9412 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9413 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9414 | if (!rule->arg.hlua_rule) { |
| 9415 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9416 | goto error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9417 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9418 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9419 | /* Memory for arguments. */ |
Tim Duesterhus | e52b6e5 | 2020-09-12 20:26:43 +0200 | [diff] [blame] | 9420 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, |
| 9421 | sizeof(*rule->arg.hlua_rule->args)); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9422 | if (!rule->arg.hlua_rule->args) { |
| 9423 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9424 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9425 | } |
| 9426 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9427 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9428 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9429 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9430 | /* Expect some arguments */ |
| 9431 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 9432 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9433 | memprintf(err, "expect %d arguments", fcn->nargs); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9434 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9435 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 9436 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9437 | if (!rule->arg.hlua_rule->args[i]) { |
| 9438 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9439 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9440 | } |
| 9441 | (*cur_arg)++; |
| 9442 | } |
| 9443 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9444 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 9445 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9446 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 9447 | return ACT_RET_PRS_OK; |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9448 | |
| 9449 | error: |
| 9450 | if (rule->arg.hlua_rule) { |
| 9451 | if (rule->arg.hlua_rule->args) { |
| 9452 | for (i = 0; i < fcn->nargs; i++) |
| 9453 | ha_free(&rule->arg.hlua_rule->args[i]); |
| 9454 | ha_free(&rule->arg.hlua_rule->args); |
| 9455 | } |
| 9456 | ha_free(&rule->arg.hlua_rule); |
| 9457 | } |
| 9458 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9459 | } |
| 9460 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9461 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 9462 | struct act_rule *rule, char **err) |
| 9463 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9464 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9465 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 9466 | /* HTTP applets are forbidden in tcp-request rules. |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9467 | * HTTP applet request requires everything initialized by |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 9468 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9469 | * The applet will be immediately initialized, but its before |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 9470 | * the call of this analyzer. |
| 9471 | */ |
| 9472 | if (rule->from != ACT_F_HTTP_REQ) { |
| 9473 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 9474 | return ACT_RET_PRS_ERR; |
| 9475 | } |
| 9476 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9477 | /* Memory for the rule. */ |
| 9478 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 9479 | if (!rule->arg.hlua_rule) { |
| 9480 | memprintf(err, "out of memory error"); |
| 9481 | return ACT_RET_PRS_ERR; |
| 9482 | } |
| 9483 | |
| 9484 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9485 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9486 | |
| 9487 | /* TODO: later accept arguments. */ |
| 9488 | rule->arg.hlua_rule->args = NULL; |
| 9489 | |
| 9490 | /* Add applet pointer in the rule. */ |
| 9491 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 9492 | rule->applet.name = fcn->name; |
| 9493 | rule->applet.init = hlua_applet_http_init; |
| 9494 | rule->applet.fct = hlua_applet_http_fct; |
| 9495 | rule->applet.release = hlua_applet_http_release; |
| 9496 | rule->applet.timeout = hlua_timeout_applet; |
| 9497 | |
| 9498 | return ACT_RET_PRS_OK; |
| 9499 | } |
| 9500 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9501 | /* This function is an LUA binding used for registering |
| 9502 | * "sample-conv" functions. It expects a converter name used |
| 9503 | * in the haproxy configuration file, and an LUA function. |
| 9504 | */ |
| 9505 | __LJMP static int hlua_register_action(lua_State *L) |
| 9506 | { |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9507 | struct action_kw_list *akl = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9508 | const char *name; |
| 9509 | int ref; |
| 9510 | int len; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9511 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9512 | int nargs; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9513 | struct buffer *trash; |
| 9514 | struct action_kw *akw; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9515 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9516 | /* Initialise the number of expected arguments at 0. */ |
| 9517 | nargs = 0; |
| 9518 | |
| 9519 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 9520 | 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] | 9521 | |
| 9522 | /* First argument : converter name. */ |
| 9523 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 9524 | |
| 9525 | /* Second argument : environment. */ |
| 9526 | if (lua_type(L, 2) != LUA_TTABLE) |
| 9527 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 9528 | |
| 9529 | /* Third argument : lua function. */ |
| 9530 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 9531 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9532 | /* 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] | 9533 | if (lua_gettop(L) >= 4) |
| 9534 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 9535 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9536 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9537 | lua_pushnil(L); |
| 9538 | while (lua_next(L, 2) != 0) { |
| 9539 | if (lua_type(L, -1) != LUA_TSTRING) |
| 9540 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 9541 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9542 | /* Check if action exists */ |
| 9543 | trash = get_trash_chunk(); |
| 9544 | chunk_printf(trash, "lua.%s", name); |
| 9545 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) { |
| 9546 | akw = tcp_req_cont_action(trash->area); |
| 9547 | } else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) { |
| 9548 | akw = tcp_res_cont_action(trash->area); |
| 9549 | } else if (strcmp(lua_tostring(L, -1), "http-req") == 0) { |
| 9550 | akw = action_http_req_custom(trash->area); |
| 9551 | } else if (strcmp(lua_tostring(L, -1), "http-res") == 0) { |
| 9552 | akw = action_http_res_custom(trash->area); |
| 9553 | } else { |
| 9554 | akw = NULL; |
| 9555 | } |
| 9556 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9557 | fcn = akw->private; |
| 9558 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 9559 | ha_warning("Trying to register action 'lua.%s' more than once. " |
| 9560 | "This will become a hard error in version 2.5.\n", name); |
| 9561 | } |
| 9562 | fcn->function_ref[hlua_state_id] = ref; |
| 9563 | |
| 9564 | /* pop the environment string. */ |
| 9565 | lua_pop(L, 1); |
| 9566 | continue; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9567 | } |
| 9568 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9569 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 9570 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9571 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9572 | if (!akl) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9573 | goto alloc_error;; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 9574 | fcn = new_hlua_function(); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9575 | if (!fcn) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9576 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9577 | |
| 9578 | /* Fill fcn. */ |
| 9579 | fcn->name = strdup(name); |
| 9580 | if (!fcn->name) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9581 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9582 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9583 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 9584 | /* Set the expected number of arguments. */ |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9585 | fcn->nargs = nargs; |
| 9586 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9587 | /* List head */ |
| 9588 | akl->list.n = akl->list.p = NULL; |
| 9589 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9590 | /* action keyword. */ |
| 9591 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9592 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9593 | if (!akl->kw[0].kw) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9594 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9595 | |
| 9596 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 9597 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 9598 | akl->kw[0].flags = 0; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9599 | akl->kw[0].private = fcn; |
| 9600 | akl->kw[0].parse = action_register_lua; |
| 9601 | |
| 9602 | /* select the action registering point. */ |
| 9603 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 9604 | tcp_req_cont_keywords_register(akl); |
| 9605 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 9606 | tcp_res_cont_keywords_register(akl); |
| 9607 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 9608 | http_req_keywords_register(akl); |
| 9609 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 9610 | http_res_keywords_register(akl); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9611 | else { |
| 9612 | release_hlua_function(fcn); |
| 9613 | if (akl) |
| 9614 | ha_free((char **)&(akl->kw[0].kw)); |
| 9615 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 9616 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9617 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 9618 | "are expected.", lua_tostring(L, -1))); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9619 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9620 | |
| 9621 | /* pop the environment string. */ |
| 9622 | lua_pop(L, 1); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9623 | |
| 9624 | /* reset for next loop */ |
| 9625 | akl = NULL; |
| 9626 | fcn = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9627 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9628 | return ACT_RET_PRS_OK; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9629 | |
| 9630 | alloc_error: |
| 9631 | release_hlua_function(fcn); |
| 9632 | ha_free(&akl); |
| 9633 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 9634 | return 0; /* Never reached */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9635 | } |
| 9636 | |
| 9637 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 9638 | struct act_rule *rule, char **err) |
| 9639 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9640 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9641 | |
Christopher Faulet | 280f85b | 2019-07-15 15:02:04 +0200 | [diff] [blame] | 9642 | if (px->mode == PR_MODE_HTTP) { |
| 9643 | memprintf(err, "Lua TCP services cannot be used on HTTP proxies"); |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 9644 | return ACT_RET_PRS_ERR; |
| 9645 | } |
| 9646 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9647 | /* Memory for the rule. */ |
| 9648 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 9649 | if (!rule->arg.hlua_rule) { |
| 9650 | memprintf(err, "out of memory error"); |
| 9651 | return ACT_RET_PRS_ERR; |
| 9652 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9653 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9654 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9655 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9656 | |
| 9657 | /* TODO: later accept arguments. */ |
| 9658 | rule->arg.hlua_rule->args = NULL; |
| 9659 | |
| 9660 | /* Add applet pointer in the rule. */ |
| 9661 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 9662 | rule->applet.name = fcn->name; |
| 9663 | rule->applet.init = hlua_applet_tcp_init; |
| 9664 | rule->applet.fct = hlua_applet_tcp_fct; |
| 9665 | rule->applet.release = hlua_applet_tcp_release; |
| 9666 | rule->applet.timeout = hlua_timeout_applet; |
| 9667 | |
| 9668 | return 0; |
| 9669 | } |
| 9670 | |
| 9671 | /* This function is an LUA binding used for registering |
| 9672 | * "sample-conv" functions. It expects a converter name used |
| 9673 | * in the haproxy configuration file, and an LUA function. |
| 9674 | */ |
| 9675 | __LJMP static int hlua_register_service(lua_State *L) |
| 9676 | { |
| 9677 | struct action_kw_list *akl; |
| 9678 | const char *name; |
| 9679 | const char *env; |
| 9680 | int ref; |
| 9681 | int len; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9682 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9683 | struct buffer *trash; |
| 9684 | struct action_kw *akw; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9685 | |
| 9686 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 9687 | |
| 9688 | /* First argument : converter name. */ |
| 9689 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 9690 | |
| 9691 | /* Second argument : environment. */ |
| 9692 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 9693 | |
| 9694 | /* Third argument : lua function. */ |
| 9695 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 9696 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9697 | /* Check for service already registered */ |
| 9698 | trash = get_trash_chunk(); |
| 9699 | chunk_printf(trash, "lua.%s", name); |
| 9700 | akw = service_find(trash->area); |
| 9701 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9702 | fcn = akw->private; |
| 9703 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 9704 | ha_warning("Trying to register service 'lua.%s' more than once. " |
| 9705 | "This will become a hard error in version 2.5.\n", name); |
| 9706 | } |
| 9707 | fcn->function_ref[hlua_state_id] = ref; |
| 9708 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9709 | } |
| 9710 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9711 | /* Allocate and fill the sample fetch keyword struct. */ |
| 9712 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 9713 | if (!akl) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9714 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 9715 | fcn = new_hlua_function(); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9716 | if (!fcn) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9717 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9718 | |
| 9719 | /* Fill fcn. */ |
| 9720 | len = strlen("<lua.>") + strlen(name) + 1; |
| 9721 | fcn->name = calloc(1, len); |
| 9722 | if (!fcn->name) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9723 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9724 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9725 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9726 | |
| 9727 | /* List head */ |
| 9728 | akl->list.n = akl->list.p = NULL; |
| 9729 | |
| 9730 | /* converter keyword. */ |
| 9731 | len = strlen("lua.") + strlen(name) + 1; |
| 9732 | akl->kw[0].kw = calloc(1, len); |
| 9733 | if (!akl->kw[0].kw) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9734 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9735 | |
| 9736 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 9737 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 9738 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9739 | if (strcmp(env, "tcp") == 0) |
| 9740 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9741 | else if (strcmp(env, "http") == 0) |
| 9742 | akl->kw[0].parse = action_register_service_http; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9743 | else { |
| 9744 | release_hlua_function(fcn); |
| 9745 | if (akl) |
| 9746 | ha_free((char **)&(akl->kw[0].kw)); |
| 9747 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 9748 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 9749 | "'tcp' or 'http' are expected.", env)); |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9750 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9751 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 9752 | akl->kw[0].flags = 0; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9753 | akl->kw[0].private = fcn; |
| 9754 | |
| 9755 | /* End of array. */ |
| 9756 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 9757 | |
| 9758 | /* Register this new converter */ |
| 9759 | service_keywords_register(akl); |
| 9760 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9761 | return 0; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9762 | |
| 9763 | alloc_error: |
| 9764 | release_hlua_function(fcn); |
| 9765 | ha_free(&akl); |
| 9766 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 9767 | return 0; /* Never reached */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9768 | } |
| 9769 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9770 | /* This function initialises Lua cli handler. It copies the |
| 9771 | * arguments in the Lua stack and create channel IO objects. |
| 9772 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 9773 | 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] | 9774 | { |
| 9775 | struct hlua *hlua; |
| 9776 | struct hlua_function *fcn; |
| 9777 | int i; |
| 9778 | const char *error; |
| 9779 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9780 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9781 | appctx->ctx.hlua_cli.fcn = private; |
| 9782 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 9783 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9784 | if (!hlua) { |
| 9785 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 9786 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9787 | } |
| 9788 | HLUA_INIT(hlua); |
| 9789 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9790 | |
| 9791 | /* Create task used by signal to wakeup applets. |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9792 | * 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] | 9793 | * applet_http. It is absolutely compatible. |
| 9794 | */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 9795 | appctx->ctx.hlua_cli.task = task_new(tid_bit); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9796 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 9797 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 9798 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9799 | } |
| 9800 | appctx->ctx.hlua_cli.task->nice = 0; |
| 9801 | appctx->ctx.hlua_cli.task->context = appctx; |
| 9802 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 9803 | |
| 9804 | /* Initialises the Lua context */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9805 | 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] | 9806 | 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] | 9807 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9808 | } |
| 9809 | |
| 9810 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9811 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9812 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 9813 | error = lua_tostring(hlua->T, -1); |
| 9814 | else |
| 9815 | error = "critical error"; |
| 9816 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 9817 | goto error; |
| 9818 | } |
| 9819 | |
| 9820 | /* Check stack available size. */ |
| 9821 | if (!lua_checkstack(hlua->T, 2)) { |
| 9822 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 9823 | goto error; |
| 9824 | } |
| 9825 | |
| 9826 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9827 | 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] | 9828 | |
| 9829 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 9830 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9831 | */ |
| 9832 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 9833 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 9834 | goto error; |
| 9835 | } |
| 9836 | hlua->nargs = 1; |
| 9837 | |
| 9838 | /* push keywords in the stack. */ |
| 9839 | for (i = 0; *args[i]; i++) { |
| 9840 | /* Check stack available size. */ |
| 9841 | if (!lua_checkstack(hlua->T, 1)) { |
| 9842 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 9843 | goto error; |
| 9844 | } |
| 9845 | lua_pushstring(hlua->T, args[i]); |
| 9846 | hlua->nargs++; |
| 9847 | } |
| 9848 | |
| 9849 | /* We must initialize the execution timeouts. */ |
| 9850 | hlua->max_time = hlua_timeout_session; |
| 9851 | |
| 9852 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9853 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9854 | |
| 9855 | /* It's ok */ |
| 9856 | return 0; |
| 9857 | |
| 9858 | /* It's not ok. */ |
| 9859 | error: |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9860 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9861 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 9862 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9863 | return 1; |
| 9864 | } |
| 9865 | |
| 9866 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 9867 | { |
| 9868 | struct hlua *hlua; |
| 9869 | struct stream_interface *si; |
| 9870 | struct hlua_function *fcn; |
| 9871 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9872 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9873 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 9874 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9875 | |
| 9876 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 9877 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 9878 | return 1; |
| 9879 | |
| 9880 | /* Execute the function. */ |
| 9881 | switch (hlua_ctx_resume(hlua, 1)) { |
| 9882 | |
| 9883 | /* finished. */ |
| 9884 | case HLUA_E_OK: |
| 9885 | return 1; |
| 9886 | |
| 9887 | /* yield. */ |
| 9888 | case HLUA_E_AGAIN: |
| 9889 | /* We want write. */ |
| 9890 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 9891 | si_rx_room_blk(si); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9892 | /* Set the timeout. */ |
| 9893 | if (hlua->wake_time != TICK_ETERNITY) |
| 9894 | task_schedule(hlua->task, hlua->wake_time); |
| 9895 | return 0; |
| 9896 | |
| 9897 | /* finished with error. */ |
| 9898 | case HLUA_E_ERRMSG: |
| 9899 | /* Display log. */ |
| 9900 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 9901 | fcn->name, lua_tostring(hlua->T, -1)); |
| 9902 | lua_pop(hlua->T, 1); |
| 9903 | return 1; |
| 9904 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9905 | case HLUA_E_ETMOUT: |
| 9906 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 9907 | fcn->name); |
| 9908 | return 1; |
| 9909 | |
| 9910 | case HLUA_E_NOMEM: |
| 9911 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 9912 | fcn->name); |
| 9913 | return 1; |
| 9914 | |
| 9915 | case HLUA_E_YIELD: /* unexpected */ |
| 9916 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 9917 | fcn->name); |
| 9918 | return 1; |
| 9919 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9920 | case HLUA_E_ERR: |
| 9921 | /* Display log. */ |
| 9922 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 9923 | fcn->name); |
| 9924 | return 1; |
| 9925 | |
| 9926 | default: |
| 9927 | return 1; |
| 9928 | } |
| 9929 | |
| 9930 | return 1; |
| 9931 | } |
| 9932 | |
| 9933 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 9934 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9935 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9936 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9937 | } |
| 9938 | |
| 9939 | /* This function is an LUA binding used for registering |
| 9940 | * new keywords in the cli. It expects a list of keywords |
| 9941 | * which are the "path". It is limited to 5 keywords. A |
| 9942 | * description of the command, a function to be executed |
| 9943 | * for the parsing and a function for io handlers. |
| 9944 | */ |
| 9945 | __LJMP static int hlua_register_cli(lua_State *L) |
| 9946 | { |
| 9947 | struct cli_kw_list *cli_kws; |
| 9948 | const char *message; |
| 9949 | int ref_io; |
| 9950 | int len; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 9951 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9952 | int index; |
| 9953 | int i; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9954 | struct buffer *trash; |
| 9955 | const char *kw[5]; |
| 9956 | struct cli_kw *cli_kw; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 9957 | const char *errmsg; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9958 | |
| 9959 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 9960 | |
| 9961 | /* First argument : an array of maximum 5 keywords. */ |
| 9962 | if (!lua_istable(L, 1)) |
| 9963 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 9964 | |
| 9965 | /* Second argument : string with contextual message. */ |
| 9966 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 9967 | |
| 9968 | /* Third and fourth argument : lua function. */ |
| 9969 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 9970 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9971 | /* Check for CLI service already registered */ |
| 9972 | trash = get_trash_chunk(); |
| 9973 | index = 0; |
| 9974 | lua_pushnil(L); |
| 9975 | memset(kw, 0, sizeof(kw)); |
| 9976 | while (lua_next(L, 1) != 0) { |
| 9977 | if (index >= CLI_PREFIX_KW_NB) |
| 9978 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 9979 | if (lua_type(L, -1) != LUA_TSTRING) |
| 9980 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 9981 | kw[index] = lua_tostring(L, -1); |
| 9982 | if (index == 0) |
| 9983 | chunk_printf(trash, "%s", kw[index]); |
| 9984 | else |
| 9985 | chunk_appendf(trash, " %s", kw[index]); |
| 9986 | index++; |
| 9987 | lua_pop(L, 1); |
| 9988 | } |
| 9989 | cli_kw = cli_find_kw_exact((char **)kw); |
| 9990 | if (cli_kw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9991 | fcn = cli_kw->private; |
| 9992 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 9993 | ha_warning("Trying to register CLI keyword 'lua.%s' more than once. " |
| 9994 | "This will become a hard error in version 2.5.\n", trash->area); |
| 9995 | } |
| 9996 | fcn->function_ref[hlua_state_id] = ref_io; |
| 9997 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9998 | } |
| 9999 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10000 | /* Allocate and fill the sample fetch keyword struct. */ |
| 10001 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10002 | if (!cli_kws) { |
| 10003 | errmsg = "Lua out of memory error."; |
| 10004 | goto error; |
| 10005 | } |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 10006 | fcn = new_hlua_function(); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10007 | if (!fcn) { |
| 10008 | errmsg = "Lua out of memory error."; |
| 10009 | goto error; |
| 10010 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10011 | |
| 10012 | /* Fill path. */ |
| 10013 | index = 0; |
| 10014 | lua_pushnil(L); |
| 10015 | while(lua_next(L, 1) != 0) { |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10016 | if (index >= 5) { |
| 10017 | errmsg = "1st argument must be a table with a maximum of 5 entries"; |
| 10018 | goto error; |
| 10019 | } |
| 10020 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 10021 | errmsg = "1st argument must be a table filled with strings"; |
| 10022 | goto error; |
| 10023 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10024 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10025 | if (!cli_kws->kw[0].str_kw[index]) { |
| 10026 | errmsg = "Lua out of memory error."; |
| 10027 | goto error; |
| 10028 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10029 | index++; |
| 10030 | lua_pop(L, 1); |
| 10031 | } |
| 10032 | |
| 10033 | /* Copy help message. */ |
| 10034 | cli_kws->kw[0].usage = strdup(message); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10035 | if (!cli_kws->kw[0].usage) { |
| 10036 | errmsg = "Lua out of memory error."; |
| 10037 | goto error; |
| 10038 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10039 | |
| 10040 | /* Fill fcn io handler. */ |
| 10041 | len = strlen("<lua.cli>") + 1; |
| 10042 | for (i = 0; i < index; i++) |
| 10043 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 10044 | fcn->name = calloc(1, len); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10045 | if (!fcn->name) { |
| 10046 | errmsg = "Lua out of memory error."; |
| 10047 | goto error; |
| 10048 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10049 | strncat((char *)fcn->name, "<lua.cli", len); |
| 10050 | for (i = 0; i < index; i++) { |
| 10051 | strncat((char *)fcn->name, ".", len); |
| 10052 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 10053 | } |
| 10054 | strncat((char *)fcn->name, ">", len); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 10055 | fcn->function_ref[hlua_state_id] = ref_io; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10056 | |
| 10057 | /* Fill last entries. */ |
| 10058 | cli_kws->kw[0].private = fcn; |
| 10059 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 10060 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 10061 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 10062 | |
| 10063 | /* Register this new converter */ |
| 10064 | cli_register_kw(cli_kws); |
| 10065 | |
| 10066 | return 0; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10067 | |
| 10068 | error: |
| 10069 | release_hlua_function(fcn); |
| 10070 | if (cli_kws) { |
| 10071 | for (i = 0; i < index; i++) |
| 10072 | ha_free((char **)&(cli_kws->kw[0].str_kw[i])); |
| 10073 | ha_free((char **)&(cli_kws->kw[0].usage)); |
| 10074 | } |
| 10075 | ha_free(&cli_kws); |
| 10076 | WILL_LJMP(luaL_error(L, errmsg)); |
| 10077 | return 0; /* Never reached */ |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10078 | } |
| 10079 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10080 | static int hlua_filter_init_per_thread(struct proxy *px, struct flt_conf *fconf) |
| 10081 | { |
| 10082 | struct hlua_flt_config *conf = fconf->conf; |
| 10083 | lua_State *L; |
| 10084 | int error, pos, state_id, flt_ref; |
| 10085 | |
| 10086 | state_id = reg_flt_to_stack_id(conf->reg); |
| 10087 | L = hlua_states[state_id]; |
| 10088 | pos = lua_gettop(L); |
| 10089 | |
| 10090 | /* The filter parsing function */ |
| 10091 | lua_rawgeti(L, LUA_REGISTRYINDEX, conf->reg->fun_ref[state_id]); |
| 10092 | |
| 10093 | /* Push the filter class on the stack and resolve all callbacks */ |
| 10094 | lua_rawgeti(L, LUA_REGISTRYINDEX, conf->reg->flt_ref[state_id]); |
| 10095 | |
| 10096 | /* Duplicate the filter class so each filter will have its own copy */ |
| 10097 | lua_newtable(L); |
| 10098 | lua_pushnil(L); |
| 10099 | |
| 10100 | while (lua_next(L, pos+2)) { |
| 10101 | lua_pushvalue(L, -2); |
| 10102 | lua_insert(L, -2); |
| 10103 | lua_settable(L, -4); |
| 10104 | } |
| 10105 | flt_ref = luaL_ref(L, LUA_REGISTRYINDEX); |
| 10106 | |
| 10107 | /* Remove the original lua filter class from the stack */ |
| 10108 | lua_pop(L, 1); |
| 10109 | |
| 10110 | /* Push the copy on the stack */ |
| 10111 | lua_rawgeti(L, LUA_REGISTRYINDEX, flt_ref); |
| 10112 | |
| 10113 | /* extra args are pushed in a table */ |
| 10114 | lua_newtable(L); |
| 10115 | for (pos = 0; conf->args[pos]; pos++) { |
| 10116 | /* Check stack available size. */ |
| 10117 | if (!lua_checkstack(L, 1)) { |
| 10118 | ha_alert("Lua filter '%s' : Lua error : full stack.", conf->reg->name); |
| 10119 | goto error; |
| 10120 | } |
| 10121 | lua_pushstring(L, conf->args[pos]); |
| 10122 | lua_rawseti(L, -2, lua_rawlen(L, -2) + 1); |
| 10123 | } |
| 10124 | |
| 10125 | error = lua_pcall(L, 2, LUA_MULTRET, 0); |
| 10126 | switch (error) { |
| 10127 | case LUA_OK: |
| 10128 | /* replace the filter ref */ |
| 10129 | conf->ref[state_id] = flt_ref; |
| 10130 | break; |
| 10131 | case LUA_ERRRUN: |
| 10132 | ha_alert("Lua filter '%s' : runtime error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 10133 | goto error; |
| 10134 | case LUA_ERRMEM: |
| 10135 | ha_alert("Lua filter '%s' : out of memory error", conf->reg->name); |
| 10136 | goto error; |
| 10137 | case LUA_ERRERR: |
| 10138 | ha_alert("Lua filter '%s' : message handler error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 10139 | goto error; |
| 10140 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
| 10141 | case LUA_ERRGCMM: |
| 10142 | ha_alert("Lua filter '%s' : garbage collector error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 10143 | goto error; |
| 10144 | #endif |
| 10145 | default: |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 10146 | ha_alert("Lua filter '%s' : unknown error : %s", conf->reg->name, lua_tostring(L, -1)); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10147 | goto error; |
| 10148 | } |
| 10149 | |
| 10150 | lua_settop(L, 0); |
| 10151 | return 0; |
| 10152 | |
| 10153 | error: |
| 10154 | lua_settop(L, 0); |
| 10155 | return -1; |
| 10156 | } |
| 10157 | |
| 10158 | static void hlua_filter_deinit_per_thread(struct proxy *px, struct flt_conf *fconf) |
| 10159 | { |
| 10160 | struct hlua_flt_config *conf = fconf->conf; |
| 10161 | lua_State *L; |
| 10162 | int state_id; |
| 10163 | |
| 10164 | if (!conf) |
| 10165 | return; |
| 10166 | |
| 10167 | state_id = reg_flt_to_stack_id(conf->reg); |
| 10168 | L = hlua_states[state_id]; |
| 10169 | luaL_unref(L, LUA_REGISTRYINDEX, conf->ref[state_id]); |
| 10170 | } |
| 10171 | |
| 10172 | static int hlua_filter_init(struct proxy *px, struct flt_conf *fconf) |
| 10173 | { |
| 10174 | struct hlua_flt_config *conf = fconf->conf; |
| 10175 | int state_id = reg_flt_to_stack_id(conf->reg); |
| 10176 | |
| 10177 | /* Rely on per-thread init for global scripts */ |
| 10178 | if (!state_id) |
| 10179 | return hlua_filter_init_per_thread(px, fconf); |
| 10180 | return 0; |
| 10181 | } |
| 10182 | |
| 10183 | static void hlua_filter_deinit(struct proxy *px, struct flt_conf *fconf) |
| 10184 | { |
| 10185 | |
| 10186 | if (fconf->conf) { |
| 10187 | struct hlua_flt_config *conf = fconf->conf; |
| 10188 | int state_id = reg_flt_to_stack_id(conf->reg); |
| 10189 | int pos; |
| 10190 | |
| 10191 | /* Rely on per-thread deinit for global scripts */ |
| 10192 | if (!state_id) |
| 10193 | hlua_filter_deinit_per_thread(px, fconf); |
| 10194 | |
| 10195 | for (pos = 0; conf->args[pos]; pos++) |
| 10196 | free(conf->args[pos]); |
| 10197 | free(conf->args); |
| 10198 | } |
| 10199 | ha_free(&fconf->conf); |
| 10200 | ha_free((char **)&fconf->id); |
| 10201 | ha_free(&fconf->ops); |
| 10202 | } |
| 10203 | |
| 10204 | static int hlua_filter_new(struct stream *s, struct filter *filter) |
| 10205 | { |
| 10206 | struct hlua_flt_config *conf = FLT_CONF(filter); |
| 10207 | struct hlua_flt_ctx *flt_ctx = NULL; |
| 10208 | int ret = 1; |
| 10209 | |
| 10210 | /* In the execution wrappers linked with a stream, the |
| 10211 | * Lua context can be not initialized. This behavior |
| 10212 | * permits to save performances because a systematic |
| 10213 | * Lua initialization cause 5% performances loss. |
| 10214 | */ |
| 10215 | if (!s->hlua) { |
| 10216 | struct hlua *hlua; |
| 10217 | |
| 10218 | hlua = pool_alloc(pool_head_hlua); |
| 10219 | if (!hlua) { |
| 10220 | SEND_ERR(s->be, "Lua filter '%s': can't initialize Lua context.\n", |
| 10221 | conf->reg->name); |
| 10222 | ret = 0; |
| 10223 | goto end; |
| 10224 | } |
| 10225 | HLUA_INIT(hlua); |
| 10226 | s->hlua = hlua; |
| 10227 | if (!hlua_ctx_init(s->hlua, reg_flt_to_stack_id(conf->reg), s->task, 0)) { |
| 10228 | SEND_ERR(s->be, "Lua filter '%s': can't initialize Lua context.\n", |
| 10229 | conf->reg->name); |
| 10230 | ret = 0; |
| 10231 | goto end; |
| 10232 | } |
| 10233 | } |
| 10234 | |
| 10235 | flt_ctx = pool_zalloc(pool_head_hlua_flt_ctx); |
| 10236 | if (!flt_ctx) { |
| 10237 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 10238 | conf->reg->name); |
| 10239 | ret = 0; |
| 10240 | goto end; |
| 10241 | } |
| 10242 | flt_ctx->hlua[0] = pool_alloc(pool_head_hlua); |
| 10243 | flt_ctx->hlua[1] = pool_alloc(pool_head_hlua); |
| 10244 | if (!flt_ctx->hlua[0] || !flt_ctx->hlua[1]) { |
| 10245 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 10246 | conf->reg->name); |
| 10247 | ret = 0; |
| 10248 | goto end; |
| 10249 | } |
| 10250 | HLUA_INIT(flt_ctx->hlua[0]); |
| 10251 | HLUA_INIT(flt_ctx->hlua[1]); |
| 10252 | if (!hlua_ctx_init(flt_ctx->hlua[0], reg_flt_to_stack_id(conf->reg), s->task, 0) || |
| 10253 | !hlua_ctx_init(flt_ctx->hlua[1], reg_flt_to_stack_id(conf->reg), s->task, 0)) { |
| 10254 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 10255 | conf->reg->name); |
| 10256 | ret = 0; |
| 10257 | goto end; |
| 10258 | } |
| 10259 | |
| 10260 | if (!HLUA_IS_RUNNING(s->hlua)) { |
| 10261 | /* The following Lua calls can fail. */ |
| 10262 | if (!SET_SAFE_LJMP(s->hlua)) { |
| 10263 | const char *error; |
| 10264 | |
| 10265 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 10266 | error = lua_tostring(s->hlua->T, -1); |
| 10267 | else |
| 10268 | error = "critical error"; |
| 10269 | SEND_ERR(s->be, "Lua filter '%s': %s.\n", conf->reg->name, error); |
| 10270 | ret = 0; |
| 10271 | goto end; |
| 10272 | } |
| 10273 | |
| 10274 | /* Check stack size. */ |
| 10275 | if (!lua_checkstack(s->hlua->T, 1)) { |
| 10276 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
Tim Duesterhus | 2281738 | 2021-09-11 23:17:25 +0200 | [diff] [blame] | 10277 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10278 | ret = 0; |
| 10279 | goto end; |
| 10280 | } |
| 10281 | |
| 10282 | lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, conf->ref[s->hlua->state_id]); |
| 10283 | if (lua_getfield(s->hlua->T, -1, "new") != LUA_TFUNCTION) { |
| 10284 | SEND_ERR(s->be, "Lua filter '%s': 'new' field is not a function.\n", |
| 10285 | conf->reg->name); |
| 10286 | RESET_SAFE_LJMP(s->hlua); |
| 10287 | ret = 0; |
| 10288 | goto end; |
| 10289 | } |
| 10290 | lua_insert(s->hlua->T, -2); |
| 10291 | |
| 10292 | /* Push the copy on the stack */ |
| 10293 | s->hlua->nargs = 1; |
| 10294 | |
| 10295 | /* We must initialize the execution timeouts. */ |
| 10296 | s->hlua->max_time = hlua_timeout_session; |
| 10297 | |
| 10298 | /* At this point the execution is safe. */ |
| 10299 | RESET_SAFE_LJMP(s->hlua); |
| 10300 | } |
| 10301 | |
| 10302 | switch (hlua_ctx_resume(s->hlua, 0)) { |
| 10303 | case HLUA_E_OK: |
| 10304 | /* Nothing returned or not a table, ignore the filter for current stream */ |
| 10305 | if (!lua_gettop(s->hlua->T) || !lua_istable(s->hlua->T, 1)) { |
| 10306 | ret = 0; |
| 10307 | goto end; |
| 10308 | } |
| 10309 | |
| 10310 | /* Attached the filter pointer to the ctx */ |
| 10311 | lua_pushstring(s->hlua->T, "__filter"); |
| 10312 | lua_pushlightuserdata(s->hlua->T, filter); |
| 10313 | lua_settable(s->hlua->T, -3); |
| 10314 | |
| 10315 | /* Save a ref on the filter ctx */ |
| 10316 | lua_pushvalue(s->hlua->T, 1); |
| 10317 | flt_ctx->ref = luaL_ref(s->hlua->T, LUA_REGISTRYINDEX); |
| 10318 | filter->ctx = flt_ctx; |
| 10319 | break; |
| 10320 | case HLUA_E_ERRMSG: |
| 10321 | SEND_ERR(s->be, "Lua filter '%s' : %s.\n", conf->reg->name, lua_tostring(s->hlua->T, -1)); |
| 10322 | ret = -1; |
| 10323 | goto end; |
| 10324 | case HLUA_E_ETMOUT: |
| 10325 | SEND_ERR(s->be, "Lua filter '%s' : 'new' execution timeout.\n", conf->reg->name); |
| 10326 | ret = 0; |
| 10327 | goto end; |
| 10328 | case HLUA_E_NOMEM: |
| 10329 | SEND_ERR(s->be, "Lua filter '%s' : out of memory error.\n", conf->reg->name); |
| 10330 | ret = 0; |
| 10331 | goto end; |
| 10332 | case HLUA_E_AGAIN: |
| 10333 | case HLUA_E_YIELD: |
| 10334 | SEND_ERR(s->be, "Lua filter '%s': yield functions like core.tcp() or core.sleep()" |
| 10335 | " are not allowed from 'new' function.\n", conf->reg->name); |
| 10336 | ret = 0; |
| 10337 | goto end; |
| 10338 | case HLUA_E_ERR: |
| 10339 | SEND_ERR(s->be, "Lua filter '%s': 'new' returns an unknown error.\n", conf->reg->name); |
| 10340 | ret = 0; |
| 10341 | goto end; |
| 10342 | default: |
| 10343 | ret = 0; |
| 10344 | goto end; |
| 10345 | } |
| 10346 | |
| 10347 | end: |
| 10348 | if (s->hlua) |
| 10349 | lua_settop(s->hlua->T, 0); |
| 10350 | if (ret <= 0) { |
| 10351 | if (flt_ctx) { |
| 10352 | hlua_ctx_destroy(flt_ctx->hlua[0]); |
| 10353 | hlua_ctx_destroy(flt_ctx->hlua[1]); |
| 10354 | pool_free(pool_head_hlua_flt_ctx, flt_ctx); |
| 10355 | } |
| 10356 | } |
| 10357 | return ret; |
| 10358 | } |
| 10359 | |
| 10360 | static void hlua_filter_delete(struct stream *s, struct filter *filter) |
| 10361 | { |
| 10362 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10363 | |
| 10364 | luaL_unref(s->hlua->T, LUA_REGISTRYINDEX, flt_ctx->ref); |
| 10365 | hlua_ctx_destroy(flt_ctx->hlua[0]); |
| 10366 | hlua_ctx_destroy(flt_ctx->hlua[1]); |
| 10367 | pool_free(pool_head_hlua_flt_ctx, flt_ctx); |
| 10368 | filter->ctx = NULL; |
| 10369 | } |
| 10370 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 10371 | static int hlua_filter_from_payload(struct filter *filter) |
| 10372 | { |
| 10373 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10374 | |
| 10375 | return (flt_ctx && !!(flt_ctx->flags & HLUA_FLT_CTX_FL_PAYLOAD)); |
| 10376 | } |
| 10377 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10378 | static int hlua_filter_callback(struct stream *s, struct filter *filter, const char *fun, |
| 10379 | int dir, unsigned int flags) |
| 10380 | { |
| 10381 | struct hlua *flt_hlua; |
| 10382 | struct hlua_flt_config *conf = FLT_CONF(filter); |
| 10383 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10384 | unsigned int hflags = HLUA_TXN_FLT_CTX; |
| 10385 | int ret = 1; |
| 10386 | |
| 10387 | flt_hlua = flt_ctx->hlua[(dir == SMP_OPT_DIR_REQ ? 0 : 1)]; |
| 10388 | if (!flt_hlua) |
| 10389 | goto end; |
| 10390 | |
| 10391 | if (!HLUA_IS_RUNNING(flt_hlua)) { |
| 10392 | int extra_idx = lua_gettop(flt_hlua->T); |
| 10393 | |
| 10394 | /* The following Lua calls can fail. */ |
| 10395 | if (!SET_SAFE_LJMP(flt_hlua)) { |
| 10396 | const char *error; |
| 10397 | |
| 10398 | if (lua_type(flt_hlua->T, -1) == LUA_TSTRING) |
| 10399 | error = lua_tostring(flt_hlua->T, -1); |
| 10400 | else |
| 10401 | error = "critical error"; |
| 10402 | SEND_ERR(s->be, "Lua filter '%s': %s.\n", conf->reg->name, error); |
| 10403 | goto end; |
| 10404 | } |
| 10405 | |
| 10406 | /* Check stack size. */ |
| 10407 | if (!lua_checkstack(flt_hlua->T, 3)) { |
| 10408 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 10409 | RESET_SAFE_LJMP(flt_hlua); |
| 10410 | goto end; |
| 10411 | } |
| 10412 | |
| 10413 | lua_rawgeti(flt_hlua->T, LUA_REGISTRYINDEX, flt_ctx->ref); |
| 10414 | if (lua_getfield(flt_hlua->T, -1, fun) != LUA_TFUNCTION) { |
| 10415 | RESET_SAFE_LJMP(flt_hlua); |
| 10416 | goto end; |
| 10417 | } |
| 10418 | lua_insert(flt_hlua->T, -2); |
| 10419 | |
| 10420 | if (!hlua_txn_new(flt_hlua->T, s, s->be, dir, hflags)) { |
| 10421 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 10422 | RESET_SAFE_LJMP(flt_hlua); |
| 10423 | goto end; |
| 10424 | } |
| 10425 | flt_hlua->nargs = 2; |
| 10426 | |
| 10427 | if (flags & HLUA_FLT_CB_ARG_CHN) { |
| 10428 | if (dir == SMP_OPT_DIR_REQ) |
| 10429 | lua_getfield(flt_hlua->T, -1, "req"); |
| 10430 | else |
| 10431 | lua_getfield(flt_hlua->T, -1, "res"); |
| 10432 | if (lua_type(flt_hlua->T, -1) == LUA_TTABLE) { |
| 10433 | lua_pushstring(flt_hlua->T, "__filter"); |
| 10434 | lua_pushlightuserdata(flt_hlua->T, filter); |
| 10435 | lua_settable(flt_hlua->T, -3); |
| 10436 | } |
| 10437 | flt_hlua->nargs++; |
| 10438 | } |
| 10439 | else if (flags & HLUA_FLT_CB_ARG_HTTP_MSG) { |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 10440 | if (dir == SMP_OPT_DIR_REQ) |
| 10441 | lua_getfield(flt_hlua->T, -1, "http_req"); |
| 10442 | else |
| 10443 | lua_getfield(flt_hlua->T, -1, "http_res"); |
| 10444 | if (lua_type(flt_hlua->T, -1) == LUA_TTABLE) { |
| 10445 | lua_pushstring(flt_hlua->T, "__filter"); |
| 10446 | lua_pushlightuserdata(flt_hlua->T, filter); |
| 10447 | lua_settable(flt_hlua->T, -3); |
| 10448 | } |
| 10449 | flt_hlua->nargs++; |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10450 | } |
| 10451 | |
| 10452 | /* Check stack size. */ |
| 10453 | if (!lua_checkstack(flt_hlua->T, 1)) { |
| 10454 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 10455 | RESET_SAFE_LJMP(flt_hlua); |
| 10456 | goto end; |
| 10457 | } |
| 10458 | |
| 10459 | while (extra_idx--) { |
| 10460 | lua_pushvalue(flt_hlua->T, 1); |
| 10461 | lua_remove(flt_hlua->T, 1); |
| 10462 | flt_hlua->nargs++; |
| 10463 | } |
| 10464 | |
| 10465 | /* We must initialize the execution timeouts. */ |
| 10466 | flt_hlua->max_time = hlua_timeout_session; |
| 10467 | |
| 10468 | /* At this point the execution is safe. */ |
| 10469 | RESET_SAFE_LJMP(flt_hlua); |
| 10470 | } |
| 10471 | |
| 10472 | switch (hlua_ctx_resume(flt_hlua, !(flags & HLUA_FLT_CB_FINAL))) { |
| 10473 | case HLUA_E_OK: |
| 10474 | /* Catch the return value if it required */ |
| 10475 | if ((flags & HLUA_FLT_CB_RETVAL) && lua_gettop(flt_hlua->T) > 0) { |
| 10476 | ret = lua_tointeger(flt_hlua->T, -1); |
| 10477 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 10478 | } |
| 10479 | |
| 10480 | /* Set timeout in the required channel. */ |
| 10481 | if (flt_hlua->wake_time != TICK_ETERNITY) { |
| 10482 | if (dir == SMP_OPT_DIR_REQ) |
| 10483 | s->req.analyse_exp = flt_hlua->wake_time; |
| 10484 | else |
| 10485 | s->res.analyse_exp = flt_hlua->wake_time; |
| 10486 | } |
| 10487 | break; |
| 10488 | case HLUA_E_AGAIN: |
| 10489 | /* Set timeout in the required channel. */ |
| 10490 | if (flt_hlua->wake_time != TICK_ETERNITY) { |
| 10491 | if (dir == SMP_OPT_DIR_REQ) |
| 10492 | s->req.analyse_exp = flt_hlua->wake_time; |
| 10493 | else |
| 10494 | s->res.analyse_exp = flt_hlua->wake_time; |
| 10495 | } |
| 10496 | /* Some actions can be wake up when a "write" event |
| 10497 | * is detected on a response channel. This is useful |
| 10498 | * only for actions targeted on the requests. |
| 10499 | */ |
| 10500 | if (HLUA_IS_WAKERESWR(flt_hlua)) |
| 10501 | s->res.flags |= CF_WAKE_WRITE; |
| 10502 | if (HLUA_IS_WAKEREQWR(flt_hlua)) |
| 10503 | s->req.flags |= CF_WAKE_WRITE; |
| 10504 | ret = 0; |
| 10505 | goto end; |
| 10506 | case HLUA_E_ERRMSG: |
| 10507 | SEND_ERR(s->be, "Lua filter '%s' : %s.\n", conf->reg->name, lua_tostring(flt_hlua->T, -1)); |
| 10508 | ret = -1; |
| 10509 | goto end; |
| 10510 | case HLUA_E_ETMOUT: |
| 10511 | SEND_ERR(s->be, "Lua filter '%s' : '%s' callback execution timeout.\n", conf->reg->name, fun); |
| 10512 | goto end; |
| 10513 | case HLUA_E_NOMEM: |
| 10514 | SEND_ERR(s->be, "Lua filter '%s' : out of memory error.\n", conf->reg->name); |
| 10515 | goto end; |
| 10516 | case HLUA_E_YIELD: |
| 10517 | SEND_ERR(s->be, "Lua filter '%s': yield functions like core.tcp() or core.sleep()" |
| 10518 | " are not allowed from '%s' callback.\n", conf->reg->name, fun); |
| 10519 | goto end; |
| 10520 | case HLUA_E_ERR: |
| 10521 | SEND_ERR(s->be, "Lua filter '%s': '%s' returns an unknown error.\n", conf->reg->name, fun); |
| 10522 | goto end; |
| 10523 | default: |
| 10524 | goto end; |
| 10525 | } |
| 10526 | |
| 10527 | |
| 10528 | end: |
| 10529 | return ret; |
| 10530 | } |
| 10531 | |
| 10532 | static int hlua_filter_start_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 10533 | { |
| 10534 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10535 | |
| 10536 | flt_ctx->flags = 0; |
| 10537 | return hlua_filter_callback(s, filter, "start_analyze", |
| 10538 | (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 10539 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_CHN)); |
| 10540 | } |
| 10541 | |
| 10542 | static int hlua_filter_end_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 10543 | { |
| 10544 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10545 | |
| 10546 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 10547 | return hlua_filter_callback(s, filter, "end_analyze", |
| 10548 | (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 10549 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_CHN)); |
| 10550 | } |
| 10551 | |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 10552 | static int hlua_filter_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg) |
| 10553 | { |
| 10554 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10555 | |
| 10556 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 10557 | return hlua_filter_callback(s, filter, "http_headers", |
| 10558 | (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 10559 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 10560 | } |
| 10561 | |
| 10562 | static int hlua_filter_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg, |
| 10563 | unsigned int offset, unsigned int len) |
| 10564 | { |
| 10565 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10566 | struct hlua *flt_hlua; |
| 10567 | int dir = (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES); |
| 10568 | int idx = (dir == SMP_OPT_DIR_REQ ? 0 : 1); |
| 10569 | int ret; |
| 10570 | |
| 10571 | flt_hlua = flt_ctx->hlua[idx]; |
| 10572 | flt_ctx->cur_off[idx] = offset; |
| 10573 | flt_ctx->cur_len[idx] = len; |
| 10574 | flt_ctx->flags |= HLUA_FLT_CTX_FL_PAYLOAD; |
| 10575 | ret = hlua_filter_callback(s, filter, "http_payload", dir, (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 10576 | if (ret != -1) { |
| 10577 | ret = flt_ctx->cur_len[idx]; |
| 10578 | if (lua_gettop(flt_hlua->T) > 0) { |
| 10579 | ret = lua_tointeger(flt_hlua->T, -1); |
| 10580 | if (ret > flt_ctx->cur_len[idx]) |
| 10581 | ret = flt_ctx->cur_len[idx]; |
| 10582 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 10583 | } |
| 10584 | } |
| 10585 | return ret; |
| 10586 | } |
| 10587 | |
| 10588 | static int hlua_filter_http_end(struct stream *s, struct filter *filter, struct http_msg *msg) |
| 10589 | { |
| 10590 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10591 | |
| 10592 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 10593 | return hlua_filter_callback(s, filter, "http_end", |
| 10594 | (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 10595 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 10596 | } |
| 10597 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10598 | static int hlua_filter_tcp_payload(struct stream *s, struct filter *filter, struct channel *chn, |
| 10599 | unsigned int offset, unsigned int len) |
| 10600 | { |
| 10601 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10602 | struct hlua *flt_hlua; |
| 10603 | int dir = (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES); |
| 10604 | int idx = (dir == SMP_OPT_DIR_REQ ? 0 : 1); |
| 10605 | int ret; |
| 10606 | |
| 10607 | flt_hlua = flt_ctx->hlua[idx]; |
| 10608 | flt_ctx->cur_off[idx] = offset; |
| 10609 | flt_ctx->cur_len[idx] = len; |
| 10610 | flt_ctx->flags |= HLUA_FLT_CTX_FL_PAYLOAD; |
| 10611 | ret = hlua_filter_callback(s, filter, "tcp_payload", dir, (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_ARG_CHN)); |
| 10612 | if (ret != -1) { |
| 10613 | ret = flt_ctx->cur_len[idx]; |
| 10614 | if (lua_gettop(flt_hlua->T) > 0) { |
| 10615 | ret = lua_tointeger(flt_hlua->T, -1); |
| 10616 | if (ret > flt_ctx->cur_len[idx]) |
| 10617 | ret = flt_ctx->cur_len[idx]; |
| 10618 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 10619 | } |
| 10620 | } |
| 10621 | return ret; |
| 10622 | } |
| 10623 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10624 | static int hlua_filter_parse_fct(char **args, int *cur_arg, struct proxy *px, |
| 10625 | struct flt_conf *fconf, char **err, void *private) |
| 10626 | { |
| 10627 | struct hlua_reg_filter *reg_flt = private; |
| 10628 | lua_State *L; |
| 10629 | struct hlua_flt_config *conf = NULL; |
| 10630 | const char *flt_id = NULL; |
| 10631 | int state_id, pos, flt_flags = 0; |
| 10632 | struct flt_ops *hlua_flt_ops = NULL; |
| 10633 | |
| 10634 | state_id = reg_flt_to_stack_id(reg_flt); |
| 10635 | L = hlua_states[state_id]; |
| 10636 | |
| 10637 | /* Initialize the filter ops with default callbacks */ |
| 10638 | hlua_flt_ops = calloc(1, sizeof(*hlua_flt_ops)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10639 | if (!hlua_flt_ops) |
| 10640 | goto error; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10641 | hlua_flt_ops->init = hlua_filter_init; |
| 10642 | hlua_flt_ops->deinit = hlua_filter_deinit; |
| 10643 | if (state_id) { |
| 10644 | /* Set per-thread callback if script is loaded per-thread */ |
| 10645 | hlua_flt_ops->init_per_thread = hlua_filter_init_per_thread; |
| 10646 | hlua_flt_ops->deinit_per_thread = hlua_filter_deinit_per_thread; |
| 10647 | } |
| 10648 | hlua_flt_ops->attach = hlua_filter_new; |
| 10649 | hlua_flt_ops->detach = hlua_filter_delete; |
| 10650 | |
| 10651 | /* Push the filter class on the stack and resolve all callbacks */ |
| 10652 | lua_rawgeti(L, LUA_REGISTRYINDEX, reg_flt->flt_ref[state_id]); |
| 10653 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10654 | if (lua_getfield(L, -1, "start_analyze") == LUA_TFUNCTION) |
| 10655 | hlua_flt_ops->channel_start_analyze = hlua_filter_start_analyze; |
| 10656 | lua_pop(L, 1); |
| 10657 | if (lua_getfield(L, -1, "end_analyze") == LUA_TFUNCTION) |
| 10658 | hlua_flt_ops->channel_end_analyze = hlua_filter_end_analyze; |
| 10659 | lua_pop(L, 1); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 10660 | if (lua_getfield(L, -1, "http_headers") == LUA_TFUNCTION) |
| 10661 | hlua_flt_ops->http_headers = hlua_filter_http_headers; |
| 10662 | lua_pop(L, 1); |
| 10663 | if (lua_getfield(L, -1, "http_payload") == LUA_TFUNCTION) |
| 10664 | hlua_flt_ops->http_payload = hlua_filter_http_payload; |
| 10665 | lua_pop(L, 1); |
| 10666 | if (lua_getfield(L, -1, "http_end") == LUA_TFUNCTION) |
| 10667 | hlua_flt_ops->http_end = hlua_filter_http_end; |
| 10668 | lua_pop(L, 1); |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10669 | if (lua_getfield(L, -1, "tcp_payload") == LUA_TFUNCTION) |
| 10670 | hlua_flt_ops->tcp_payload = hlua_filter_tcp_payload; |
| 10671 | lua_pop(L, 1); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10672 | |
| 10673 | /* Get id and flags of the filter class */ |
| 10674 | if (lua_getfield(L, -1, "id") == LUA_TSTRING) |
| 10675 | flt_id = lua_tostring(L, -1); |
| 10676 | lua_pop(L, 1); |
| 10677 | if (lua_getfield(L, -1, "flags") == LUA_TNUMBER) |
| 10678 | flt_flags = lua_tointeger(L, -1); |
| 10679 | lua_pop(L, 1); |
| 10680 | |
| 10681 | /* Create the filter config */ |
| 10682 | conf = calloc(1, sizeof(*conf)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10683 | if (!conf) |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10684 | goto error; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10685 | conf->reg = reg_flt; |
| 10686 | |
| 10687 | /* duplicate args */ |
| 10688 | for (pos = 0; *args[*cur_arg + 1 + pos]; pos++); |
| 10689 | conf->args = calloc(pos + 1, sizeof(*conf->args)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10690 | if (!conf->args) |
| 10691 | goto error; |
| 10692 | for (pos = 0; *args[*cur_arg + 1 + pos]; pos++) { |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10693 | conf->args[pos] = strdup(args[*cur_arg + 1 + pos]); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10694 | if (!conf->args[pos]) |
| 10695 | goto error; |
| 10696 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10697 | conf->args[pos] = NULL; |
| 10698 | *cur_arg += pos + 1; |
| 10699 | |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10700 | if (flt_id) { |
| 10701 | fconf->id = strdup(flt_id); |
| 10702 | if (!fconf->id) |
| 10703 | goto error; |
| 10704 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10705 | fconf->flags = flt_flags; |
| 10706 | fconf->conf = conf; |
| 10707 | fconf->ops = hlua_flt_ops; |
| 10708 | |
| 10709 | lua_settop(L, 0); |
| 10710 | return 0; |
| 10711 | |
| 10712 | error: |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10713 | memprintf(err, "Lua filter '%s' : Lua out of memory error", reg_flt->name); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10714 | free(hlua_flt_ops); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10715 | if (conf && conf->args) { |
| 10716 | for (pos = 0; conf->args[pos]; pos++) |
| 10717 | free(conf->args[pos]); |
| 10718 | free(conf->args); |
| 10719 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10720 | free(conf); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10721 | free((char *)fconf->id); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10722 | lua_settop(L, 0); |
| 10723 | return -1; |
| 10724 | } |
| 10725 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10726 | __LJMP static int hlua_register_data_filter(lua_State *L) |
| 10727 | { |
| 10728 | struct filter *filter; |
| 10729 | struct channel *chn; |
| 10730 | |
| 10731 | MAY_LJMP(check_args(L, 2, "register_data_filter")); |
| 10732 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
| 10733 | chn = MAY_LJMP(hlua_checkchannel(L, 2)); |
| 10734 | |
| 10735 | lua_getfield(L, 1, "__filter"); |
| 10736 | MAY_LJMP(luaL_checktype(L, -1, LUA_TLIGHTUSERDATA)); |
| 10737 | filter = lua_touserdata (L, -1); |
| 10738 | lua_pop(L, 1); |
| 10739 | |
| 10740 | register_data_filter(chn_strm(chn), chn, filter); |
| 10741 | return 1; |
| 10742 | } |
| 10743 | |
| 10744 | __LJMP static int hlua_unregister_data_filter(lua_State *L) |
| 10745 | { |
| 10746 | struct filter *filter; |
| 10747 | struct channel *chn; |
| 10748 | |
| 10749 | MAY_LJMP(check_args(L, 2, "unregister_data_filter")); |
| 10750 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
| 10751 | chn = MAY_LJMP(hlua_checkchannel(L, 2)); |
| 10752 | |
| 10753 | lua_getfield(L, 1, "__filter"); |
| 10754 | MAY_LJMP(luaL_checktype(L, -1, LUA_TLIGHTUSERDATA)); |
| 10755 | filter = lua_touserdata (L, -1); |
| 10756 | lua_pop(L, 1); |
| 10757 | |
| 10758 | unregister_data_filter(chn_strm(chn), chn, filter); |
| 10759 | return 1; |
| 10760 | } |
| 10761 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10762 | /* This function is an LUA binding used for registering a filter. It expects a |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 10763 | * filter name used in the haproxy configuration file and a LUA function to |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10764 | * parse configuration arguments. |
| 10765 | */ |
| 10766 | __LJMP static int hlua_register_filter(lua_State *L) |
| 10767 | { |
| 10768 | struct buffer *trash; |
| 10769 | struct flt_kw_list *fkl; |
| 10770 | struct flt_kw *fkw; |
| 10771 | const char *name; |
| 10772 | struct hlua_reg_filter *reg_flt= NULL; |
| 10773 | int flt_ref, fun_ref; |
| 10774 | int len; |
| 10775 | |
| 10776 | MAY_LJMP(check_args(L, 3, "register_filter")); |
| 10777 | |
| 10778 | /* First argument : filter name. */ |
| 10779 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 10780 | |
| 10781 | /* Second argument : The filter class */ |
| 10782 | flt_ref = MAY_LJMP(hlua_checktable(L, 2)); |
| 10783 | |
| 10784 | /* Third argument : lua function. */ |
| 10785 | fun_ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 10786 | |
| 10787 | trash = get_trash_chunk(); |
| 10788 | chunk_printf(trash, "lua.%s", name); |
| 10789 | fkw = flt_find_kw(trash->area); |
| 10790 | if (fkw != NULL) { |
| 10791 | reg_flt = fkw->private; |
| 10792 | if (reg_flt->flt_ref[hlua_state_id] != -1 || reg_flt->fun_ref[hlua_state_id] != -1) { |
| 10793 | ha_warning("Trying to register filter 'lua.%s' more than once. " |
| 10794 | "This will become a hard error in version 2.5.\n", name); |
| 10795 | } |
| 10796 | reg_flt->flt_ref[hlua_state_id] = flt_ref; |
| 10797 | reg_flt->fun_ref[hlua_state_id] = fun_ref; |
| 10798 | return 0; |
| 10799 | } |
| 10800 | |
| 10801 | fkl = calloc(1, sizeof(*fkl) + sizeof(struct flt_kw) * 2); |
| 10802 | if (!fkl) |
| 10803 | goto alloc_error; |
| 10804 | fkl->scope = "HLUA"; |
| 10805 | |
| 10806 | reg_flt = new_hlua_reg_filter(name); |
| 10807 | if (!reg_flt) |
| 10808 | goto alloc_error; |
| 10809 | |
| 10810 | reg_flt->flt_ref[hlua_state_id] = flt_ref; |
| 10811 | reg_flt->fun_ref[hlua_state_id] = fun_ref; |
| 10812 | |
| 10813 | /* The filter keyword */ |
| 10814 | len = strlen("lua.") + strlen(name) + 1; |
| 10815 | fkl->kw[0].kw = calloc(1, len); |
| 10816 | if (!fkl->kw[0].kw) |
| 10817 | goto alloc_error; |
| 10818 | |
| 10819 | snprintf((char *)fkl->kw[0].kw, len, "lua.%s", name); |
| 10820 | |
| 10821 | fkl->kw[0].parse = hlua_filter_parse_fct; |
| 10822 | fkl->kw[0].private = reg_flt; |
| 10823 | memset(&fkl->kw[1], 0, sizeof(*fkl->kw)); |
| 10824 | |
| 10825 | /* Register this new filter */ |
| 10826 | flt_register_keywords(fkl); |
| 10827 | |
| 10828 | return 0; |
| 10829 | |
| 10830 | alloc_error: |
| 10831 | release_hlua_reg_filter(reg_flt); |
| 10832 | ha_free(&fkl); |
| 10833 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 10834 | return 0; /* Never reached */ |
| 10835 | } |
| 10836 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 10837 | 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] | 10838 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 10839 | char **err, unsigned int *timeout) |
| 10840 | { |
| 10841 | const char *error; |
| 10842 | |
| 10843 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 10844 | if (error == PARSE_TIME_OVER) { |
| 10845 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 10846 | args[1], args[0]); |
| 10847 | return -1; |
| 10848 | } |
| 10849 | else if (error == PARSE_TIME_UNDER) { |
| 10850 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 10851 | args[1], args[0]); |
| 10852 | return -1; |
| 10853 | } |
| 10854 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 10855 | memprintf(err, "%s: invalid timeout", args[0]); |
| 10856 | return -1; |
| 10857 | } |
| 10858 | return 0; |
| 10859 | } |
| 10860 | |
| 10861 | 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] | 10862 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 10863 | char **err) |
| 10864 | { |
| 10865 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 10866 | file, line, err, &hlua_timeout_session); |
| 10867 | } |
| 10868 | |
| 10869 | 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] | 10870 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 10871 | char **err) |
| 10872 | { |
| 10873 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 10874 | file, line, err, &hlua_timeout_task); |
| 10875 | } |
| 10876 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10877 | 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] | 10878 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10879 | char **err) |
| 10880 | { |
| 10881 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 10882 | file, line, err, &hlua_timeout_applet); |
| 10883 | } |
| 10884 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 10885 | 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] | 10886 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 10887 | char **err) |
| 10888 | { |
| 10889 | char *error; |
| 10890 | |
| 10891 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 10892 | if (*error != '\0') { |
| 10893 | memprintf(err, "%s: invalid number", args[0]); |
| 10894 | return -1; |
| 10895 | } |
| 10896 | return 0; |
| 10897 | } |
| 10898 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 10899 | 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] | 10900 | const struct proxy *defpx, const char *file, int line, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 10901 | char **err) |
| 10902 | { |
| 10903 | char *error; |
| 10904 | |
| 10905 | if (*(args[1]) == 0) { |
| 10906 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 10907 | return -1; |
| 10908 | } |
| 10909 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 10910 | if (*error != '\0') { |
| 10911 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 10912 | return -1; |
| 10913 | } |
| 10914 | return 0; |
| 10915 | } |
| 10916 | |
| 10917 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 10918 | /* This function is called by the main configuration key "lua-load". It loads and |
| 10919 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 10920 | * the main lua entry point. |
| 10921 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 10922 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 10923 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 10924 | * |
| 10925 | * In some error case, LUA set an error message in top of the stack. This function |
| 10926 | * 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] | 10927 | * |
| 10928 | * This function can fail with an abort() due to an Lua critical error. |
| 10929 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 10930 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 10931 | */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 10932 | static int hlua_load_state(char *filename, lua_State *L, char **err) |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 10933 | { |
| 10934 | int error; |
| 10935 | |
| 10936 | /* Just load and compile the file. */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 10937 | error = luaL_loadfile(L, filename); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 10938 | if (error) { |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 10939 | memprintf(err, "error in Lua file '%s': %s", filename, lua_tostring(L, -1)); |
| 10940 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 10941 | return -1; |
| 10942 | } |
| 10943 | |
| 10944 | /* If no syntax error where detected, execute the code. */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 10945 | error = lua_pcall(L, 0, LUA_MULTRET, 0); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 10946 | switch (error) { |
| 10947 | case LUA_OK: |
| 10948 | break; |
| 10949 | case LUA_ERRRUN: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 10950 | memprintf(err, "Lua runtime error: %s\n", lua_tostring(L, -1)); |
| 10951 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 10952 | return -1; |
| 10953 | case LUA_ERRMEM: |
Thierry Fournier | de6145f | 2020-11-29 00:55:53 +0100 | [diff] [blame] | 10954 | memprintf(err, "Lua out of memory error\n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 10955 | return -1; |
| 10956 | case LUA_ERRERR: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 10957 | memprintf(err, "Lua message handler error: %s\n", lua_tostring(L, -1)); |
| 10958 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 10959 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 10960 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 10961 | case LUA_ERRGCMM: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 10962 | memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(L, -1)); |
| 10963 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 10964 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 10965 | #endif |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 10966 | default: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 10967 | memprintf(err, "Lua unknown error: %s\n", lua_tostring(L, -1)); |
| 10968 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 10969 | return -1; |
| 10970 | } |
| 10971 | |
| 10972 | return 0; |
| 10973 | } |
| 10974 | |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 10975 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 10976 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 10977 | char **err) |
| 10978 | { |
| 10979 | if (*(args[1]) == 0) { |
| 10980 | memprintf(err, "'%s' expects a file name as parameter.\n", args[0]); |
| 10981 | return -1; |
| 10982 | } |
| 10983 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10984 | /* loading for global state */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 10985 | hlua_state_id = 0; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10986 | ha_set_tid(0); |
Thierry Fournier | afc63e2 | 2020-11-28 17:06:51 +0100 | [diff] [blame] | 10987 | return hlua_load_state(args[1], hlua_states[0], err); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 10988 | } |
| 10989 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10990 | 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] | 10991 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10992 | char **err) |
| 10993 | { |
| 10994 | int len; |
| 10995 | |
| 10996 | if (*(args[1]) == 0) { |
| 10997 | memprintf(err, "'%s' expects a file as parameter.\n", args[0]); |
| 10998 | return -1; |
| 10999 | } |
| 11000 | |
| 11001 | if (per_thread_load == NULL) { |
| 11002 | /* allocate the first entry large enough to store the final NULL */ |
| 11003 | per_thread_load = calloc(1, sizeof(*per_thread_load)); |
| 11004 | if (per_thread_load == NULL) { |
| 11005 | memprintf(err, "out of memory error"); |
| 11006 | return -1; |
| 11007 | } |
| 11008 | } |
| 11009 | |
| 11010 | /* count used entries */ |
| 11011 | for (len = 0; per_thread_load[len] != NULL; len++) |
| 11012 | ; |
| 11013 | |
| 11014 | per_thread_load = realloc(per_thread_load, (len + 2) * sizeof(*per_thread_load)); |
| 11015 | if (per_thread_load == NULL) { |
| 11016 | memprintf(err, "out of memory error"); |
| 11017 | return -1; |
| 11018 | } |
| 11019 | |
| 11020 | per_thread_load[len] = strdup(args[1]); |
| 11021 | per_thread_load[len + 1] = NULL; |
| 11022 | |
| 11023 | if (per_thread_load[len] == NULL) { |
| 11024 | memprintf(err, "out of memory error"); |
| 11025 | return -1; |
| 11026 | } |
| 11027 | |
| 11028 | /* loading for thread 1 only */ |
| 11029 | hlua_state_id = 1; |
| 11030 | ha_set_tid(0); |
| 11031 | return hlua_load_state(args[1], hlua_states[1], err); |
| 11032 | } |
| 11033 | |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 11034 | /* Prepend the given <path> followed by a semicolon to the `package.<type>` variable |
| 11035 | * in the given <ctx>. |
| 11036 | */ |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 11037 | static int hlua_prepend_path(lua_State *L, char *type, char *path) |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 11038 | { |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 11039 | lua_getglobal(L, "package"); /* push package variable */ |
| 11040 | lua_pushstring(L, path); /* push given path */ |
| 11041 | lua_pushstring(L, ";"); /* push semicolon */ |
| 11042 | lua_getfield(L, -3, type); /* push old path */ |
| 11043 | lua_concat(L, 3); /* concatenate to new path */ |
| 11044 | lua_setfield(L, -2, type); /* store new path */ |
| 11045 | lua_pop(L, 1); /* pop package variable */ |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 11046 | |
| 11047 | return 0; |
| 11048 | } |
| 11049 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11050 | 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] | 11051 | const struct proxy *defpx, const char *file, int line, |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11052 | char **err) |
| 11053 | { |
| 11054 | char *path; |
| 11055 | char *type = "path"; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11056 | struct prepend_path *p = NULL; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11057 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11058 | if (too_many_args(2, args, err, NULL)) { |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11059 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11060 | } |
| 11061 | |
| 11062 | if (!(*args[1])) { |
| 11063 | memprintf(err, "'%s' expects to receive a <path> as argument", args[0]); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11064 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11065 | } |
| 11066 | path = args[1]; |
| 11067 | |
| 11068 | if (*args[2]) { |
| 11069 | if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) { |
| 11070 | 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] | 11071 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11072 | } |
| 11073 | type = args[2]; |
| 11074 | } |
| 11075 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11076 | p = calloc(1, sizeof(*p)); |
| 11077 | if (p == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 11078 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11079 | goto err; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11080 | } |
| 11081 | p->path = strdup(path); |
| 11082 | if (p->path == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 11083 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11084 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11085 | } |
| 11086 | p->type = strdup(type); |
| 11087 | if (p->type == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 11088 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11089 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11090 | } |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 11091 | LIST_APPEND(&prepend_path_list, &p->l); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11092 | |
| 11093 | hlua_prepend_path(hlua_states[0], type, path); |
| 11094 | hlua_prepend_path(hlua_states[1], type, path); |
| 11095 | return 0; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11096 | |
| 11097 | err2: |
| 11098 | free(p->type); |
| 11099 | free(p->path); |
| 11100 | err: |
| 11101 | free(p); |
| 11102 | return -1; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11103 | } |
| 11104 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11105 | /* configuration keywords declaration */ |
| 11106 | static struct cfg_kw_list cfg_kws = {{ },{ |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11107 | { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11108 | { CFG_GLOBAL, "lua-load", hlua_load }, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11109 | { CFG_GLOBAL, "lua-load-per-thread", hlua_load_per_thread }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11110 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 11111 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 11112 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 11113 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11114 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11115 | { 0, NULL, NULL }, |
| 11116 | }}; |
| 11117 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 11118 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 11119 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 11120 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 11121 | /* This function can fail with an abort() due to an Lua critical error. |
| 11122 | * We are in the initialisation process of HAProxy, this abort() is |
| 11123 | * tolerated. |
| 11124 | */ |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11125 | int hlua_post_init_state(lua_State *L) |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11126 | { |
| 11127 | struct hlua_init_function *init; |
| 11128 | const char *msg; |
| 11129 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 11130 | const char *error; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11131 | const char *kind; |
| 11132 | const char *trace; |
| 11133 | int return_status = 1; |
| 11134 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 11135 | int nres; |
| 11136 | #endif |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11137 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11138 | /* disable memory limit checks if limit is not set */ |
| 11139 | if (!hlua_global_allocator.limit) |
| 11140 | hlua_global_allocator.limit = ~hlua_global_allocator.limit; |
| 11141 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 11142 | /* Call post initialisation function in safe environment. */ |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11143 | if (setjmp(safe_ljmp_env) != 0) { |
| 11144 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11145 | if (lua_type(L, -1) == LUA_TSTRING) |
| 11146 | error = lua_tostring(L, -1); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 11147 | else |
| 11148 | error = "critical error"; |
| 11149 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 11150 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11151 | } else { |
| 11152 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 11153 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 11154 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11155 | hlua_fcn_post_init(L); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 11156 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 11157 | list_for_each_entry(init, &hlua_init_functions[hlua_state_id], l) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11158 | lua_rawgeti(L, LUA_REGISTRYINDEX, init->function_ref); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11159 | |
| 11160 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11161 | ret = lua_resume(L, L, 0, &nres); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11162 | #else |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11163 | ret = lua_resume(L, L, 0); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11164 | #endif |
| 11165 | kind = NULL; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11166 | switch (ret) { |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11167 | |
| 11168 | case LUA_OK: |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11169 | lua_pop(L, -1); |
Thierry Fournier | 13d08b7 | 2020-11-28 11:02:58 +0100 | [diff] [blame] | 11170 | break; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11171 | |
| 11172 | case LUA_ERRERR: |
| 11173 | kind = "message handler error"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 11174 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11175 | case LUA_ERRRUN: |
| 11176 | if (!kind) |
| 11177 | kind = "runtime error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11178 | msg = lua_tostring(L, -1); |
| 11179 | lua_settop(L, 0); /* Empty the stack. */ |
| 11180 | lua_pop(L, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 11181 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11182 | if (msg) |
| 11183 | ha_alert("Lua init: %s: '%s' from %s\n", kind, msg, trace); |
| 11184 | else |
| 11185 | ha_alert("Lua init: unknown %s from %s\n", kind, trace); |
| 11186 | return_status = 0; |
| 11187 | break; |
| 11188 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11189 | default: |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11190 | /* Unknown error */ |
| 11191 | kind = "Unknown error"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 11192 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11193 | case LUA_YIELD: |
| 11194 | /* yield is not configured at this step, this state doesn't happen */ |
| 11195 | if (!kind) |
| 11196 | kind = "yield not allowed"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 11197 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11198 | case LUA_ERRMEM: |
| 11199 | if (!kind) |
| 11200 | kind = "out of memory error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11201 | lua_settop(L, 0); |
| 11202 | lua_pop(L, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 11203 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11204 | ha_alert("Lua init: %s: %s\n", kind, trace); |
| 11205 | return_status = 0; |
| 11206 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11207 | } |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11208 | if (!return_status) |
| 11209 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11210 | } |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11211 | |
| 11212 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11213 | return return_status; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11214 | } |
| 11215 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11216 | int hlua_post_init() |
| 11217 | { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11218 | int ret; |
| 11219 | int i; |
| 11220 | int errors; |
| 11221 | char *err = NULL; |
| 11222 | struct hlua_function *fcn; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11223 | struct hlua_reg_filter *reg_flt; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11224 | |
Willy Tarreau | b131049 | 2021-08-30 09:35:18 +0200 | [diff] [blame] | 11225 | #if defined(USE_OPENSSL) |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11226 | /* Initialize SSL server. */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 11227 | if (socket_ssl->xprt->prepare_srv) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11228 | int saved_used_backed = global.ssl_used_backend; |
| 11229 | // don't affect maxconn automatic computation |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 11230 | socket_ssl->xprt->prepare_srv(socket_ssl); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11231 | global.ssl_used_backend = saved_used_backed; |
| 11232 | } |
| 11233 | #endif |
| 11234 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 11235 | /* Perform post init of common thread */ |
| 11236 | hlua_state_id = 0; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11237 | ha_set_tid(0); |
| 11238 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 11239 | if (ret == 0) |
| 11240 | return 0; |
| 11241 | |
| 11242 | /* init remaining lua states and load files */ |
| 11243 | for (hlua_state_id = 2; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 11244 | |
| 11245 | /* set thread context */ |
| 11246 | ha_set_tid(hlua_state_id - 1); |
| 11247 | |
| 11248 | /* Init lua state */ |
| 11249 | hlua_states[hlua_state_id] = hlua_init_state(hlua_state_id); |
| 11250 | |
| 11251 | /* Load lua files */ |
| 11252 | for (i = 0; per_thread_load && per_thread_load[i]; i++) { |
| 11253 | ret = hlua_load_state(per_thread_load[i], hlua_states[hlua_state_id], &err); |
| 11254 | if (ret != 0) { |
| 11255 | ha_alert("Lua init: %s\n", err); |
| 11256 | return 0; |
| 11257 | } |
| 11258 | } |
| 11259 | } |
| 11260 | |
| 11261 | /* Reset thread context */ |
| 11262 | ha_set_tid(0); |
| 11263 | |
| 11264 | /* Execute post init for all states */ |
| 11265 | for (hlua_state_id = 1; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 11266 | |
| 11267 | /* set thread context */ |
| 11268 | ha_set_tid(hlua_state_id - 1); |
| 11269 | |
| 11270 | /* run post init */ |
| 11271 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 11272 | if (ret == 0) |
| 11273 | return 0; |
| 11274 | } |
| 11275 | |
| 11276 | /* Reset thread context */ |
| 11277 | ha_set_tid(0); |
| 11278 | |
| 11279 | /* control functions registering. Each function must have: |
| 11280 | * - only the function_ref[0] set positive and all other to -1 |
| 11281 | * - only the function_ref[0] set to -1 and all other positive |
| 11282 | * This ensure a same reference is not used both in shared |
| 11283 | * lua state and thread dedicated lua state. Note: is the case |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 11284 | * reach, the shared state is priority, but the bug will be |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11285 | * complicated to found for the end user. |
| 11286 | */ |
| 11287 | errors = 0; |
| 11288 | list_for_each_entry(fcn, &referenced_functions, l) { |
| 11289 | ret = 0; |
| 11290 | for (i = 1; i < global.nbthread + 1; i++) { |
| 11291 | if (fcn->function_ref[i] == -1) |
| 11292 | ret--; |
| 11293 | else |
| 11294 | ret++; |
| 11295 | } |
| 11296 | if (abs(ret) != global.nbthread) { |
| 11297 | ha_alert("Lua function '%s' is not referenced in all thread. " |
| 11298 | "Expect function in all thread or in none thread.\n", fcn->name); |
| 11299 | errors++; |
| 11300 | continue; |
| 11301 | } |
| 11302 | |
| 11303 | if ((fcn->function_ref[0] == -1) == (ret < 0)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 11304 | ha_alert("Lua function '%s' is referenced both ins shared Lua context (through lua-load) " |
| 11305 | "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] | 11306 | "exclusive.\n", fcn->name); |
| 11307 | errors++; |
| 11308 | } |
| 11309 | } |
| 11310 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11311 | /* Do the same with registered filters */ |
| 11312 | list_for_each_entry(reg_flt, &referenced_filters, l) { |
| 11313 | ret = 0; |
| 11314 | for (i = 1; i < global.nbthread + 1; i++) { |
| 11315 | if (reg_flt->flt_ref[i] == -1) |
| 11316 | ret--; |
| 11317 | else |
| 11318 | ret++; |
| 11319 | } |
| 11320 | if (abs(ret) != global.nbthread) { |
| 11321 | ha_alert("Lua filter '%s' is not referenced in all thread. " |
| 11322 | "Expect function in all thread or in none thread.\n", reg_flt->name); |
| 11323 | errors++; |
| 11324 | continue; |
| 11325 | } |
| 11326 | |
| 11327 | if ((reg_flt->flt_ref[0] == -1) == (ret < 0)) { |
| 11328 | ha_alert("Lua filter '%s' is referenced both ins shared Lua context (through lua-load) " |
| 11329 | "and per-thread Lua context (through lua-load-per-thread). these two context " |
| 11330 | "exclusive.\n", fcn->name); |
| 11331 | errors++; |
| 11332 | } |
| 11333 | } |
| 11334 | |
| 11335 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11336 | if (errors > 0) |
| 11337 | return 0; |
| 11338 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 11339 | /* 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] | 11340 | * -1 in order to have probably a segfault if someone use it |
| 11341 | */ |
| 11342 | hlua_state_id = -1; |
| 11343 | |
| 11344 | return 1; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11345 | } |
| 11346 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11347 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 11348 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 11349 | * 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] | 11350 | * allocation. <nsize> is the requested new size. A new allocation is |
| 11351 | * indicated by <ptr> being NULL. A free is indicated by <nsize> being |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11352 | * zero. This one verifies that the limits are respected but is optimized |
| 11353 | * 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] | 11354 | */ |
| 11355 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 11356 | { |
| 11357 | struct hlua_mem_allocator *zone = ud; |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11358 | size_t limit, old, new; |
| 11359 | |
Tim Duesterhus | 2258652 | 2021-01-08 10:35:33 +0100 | [diff] [blame] | 11360 | if (unlikely(!ptr && !nsize)) |
| 11361 | return NULL; |
| 11362 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11363 | /* a limit of ~0 means unlimited and boot complete, so there's no need |
| 11364 | * for accounting anymore. |
| 11365 | */ |
Christopher Faulet | cc2c4f8 | 2021-03-24 14:52:24 +0100 | [diff] [blame] | 11366 | if (likely(~zone->limit == 0)) |
| 11367 | return realloc(ptr, nsize); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11368 | |
Willy Tarreau | d36c7fa | 2020-12-02 12:26:29 +0100 | [diff] [blame] | 11369 | if (!ptr) |
| 11370 | osize = 0; |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11371 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11372 | /* enforce strict limits across all threads */ |
| 11373 | limit = zone->limit; |
| 11374 | old = _HA_ATOMIC_LOAD(&zone->allocated); |
| 11375 | do { |
| 11376 | new = old + nsize - osize; |
| 11377 | if (unlikely(nsize && limit && new > limit)) |
| 11378 | return NULL; |
| 11379 | } while (!_HA_ATOMIC_CAS(&zone->allocated, &old, new)); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11380 | |
| 11381 | ptr = realloc(ptr, nsize); |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11382 | |
| 11383 | if (unlikely(!ptr && nsize)) // failed |
| 11384 | _HA_ATOMIC_SUB(&zone->allocated, nsize - osize); |
| 11385 | |
| 11386 | __ha_barrier_atomic_store(); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11387 | return ptr; |
| 11388 | } |
| 11389 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 11390 | /* 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] | 11391 | * We are in the initialisation process of HAProxy, this abort() is |
| 11392 | * tolerated. |
| 11393 | */ |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 11394 | lua_State *hlua_init_state(int thread_num) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 11395 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11396 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 11397 | int idx; |
| 11398 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 11399 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 11400 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 11401 | const char *error_msg; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 11402 | void **context; |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11403 | lua_State *L; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11404 | struct prepend_path *pp; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11405 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 11406 | /* Init main lua stack. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11407 | L = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 11408 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 11409 | /* Initialise Lua context to NULL */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11410 | context = lua_getextraspace(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 11411 | *context = NULL; |
| 11412 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 11413 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 11414 | * the Lua function can fail with an abort. We are in the initialisation |
| 11415 | * process of HAProxy, this abort() is tolerated. |
| 11416 | */ |
| 11417 | |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11418 | /* Call post initialisation function in safe environment. */ |
| 11419 | if (setjmp(safe_ljmp_env) != 0) { |
| 11420 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11421 | if (lua_type(L, -1) == LUA_TSTRING) |
| 11422 | error_msg = lua_tostring(L, -1); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 11423 | else |
| 11424 | error_msg = "critical error"; |
| 11425 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
| 11426 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11427 | } else { |
| 11428 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 11429 | } |
| 11430 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 11431 | /* Initialise lua. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11432 | luaL_openlibs(L); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 11433 | #define HLUA_PREPEND_PATH_TOSTRING1(x) #x |
| 11434 | #define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x) |
| 11435 | #ifdef HLUA_PREPEND_PATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11436 | hlua_prepend_path(L, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 11437 | #endif |
| 11438 | #ifdef HLUA_PREPEND_CPATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11439 | hlua_prepend_path(L, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 11440 | #endif |
| 11441 | #undef HLUA_PREPEND_PATH_TOSTRING |
| 11442 | #undef HLUA_PREPEND_PATH_TOSTRING1 |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11443 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11444 | /* Apply configured prepend path */ |
| 11445 | list_for_each_entry(pp, &prepend_path_list, l) |
| 11446 | hlua_prepend_path(L, pp->type, pp->path); |
| 11447 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11448 | /* |
| 11449 | * |
| 11450 | * Create "core" object. |
| 11451 | * |
| 11452 | */ |
| 11453 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 11454 | /* This table entry is the object "core" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11455 | lua_newtable(L); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11456 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 11457 | /* set the thread id */ |
| 11458 | hlua_class_const_int(L, "thread", thread_num); |
| 11459 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11460 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 11461 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11462 | hlua_class_const_int(L, log_levels[i], i); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11463 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11464 | /* Register special functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11465 | hlua_class_function(L, "register_init", hlua_register_init); |
| 11466 | hlua_class_function(L, "register_task", hlua_register_task); |
| 11467 | hlua_class_function(L, "register_fetches", hlua_register_fetches); |
| 11468 | hlua_class_function(L, "register_converters", hlua_register_converters); |
| 11469 | hlua_class_function(L, "register_action", hlua_register_action); |
| 11470 | hlua_class_function(L, "register_service", hlua_register_service); |
| 11471 | hlua_class_function(L, "register_cli", hlua_register_cli); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11472 | hlua_class_function(L, "register_filter", hlua_register_filter); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11473 | hlua_class_function(L, "yield", hlua_yield); |
| 11474 | hlua_class_function(L, "set_nice", hlua_set_nice); |
| 11475 | hlua_class_function(L, "sleep", hlua_sleep); |
| 11476 | hlua_class_function(L, "msleep", hlua_msleep); |
| 11477 | hlua_class_function(L, "add_acl", hlua_add_acl); |
| 11478 | hlua_class_function(L, "del_acl", hlua_del_acl); |
| 11479 | hlua_class_function(L, "set_map", hlua_set_map); |
| 11480 | hlua_class_function(L, "del_map", hlua_del_map); |
| 11481 | hlua_class_function(L, "tcp", hlua_socket_new); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 11482 | hlua_class_function(L, "httpclient", hlua_httpclient_new); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11483 | hlua_class_function(L, "log", hlua_log); |
| 11484 | hlua_class_function(L, "Debug", hlua_log_debug); |
| 11485 | hlua_class_function(L, "Info", hlua_log_info); |
| 11486 | hlua_class_function(L, "Warning", hlua_log_warning); |
| 11487 | hlua_class_function(L, "Alert", hlua_log_alert); |
| 11488 | hlua_class_function(L, "done", hlua_done); |
| 11489 | hlua_fcn_reg_core_fcn(L); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11490 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11491 | lua_setglobal(L, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 11492 | |
| 11493 | /* |
| 11494 | * |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 11495 | * Create "act" object. |
| 11496 | * |
| 11497 | */ |
| 11498 | |
| 11499 | /* This table entry is the object "act" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11500 | lua_newtable(L); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 11501 | |
| 11502 | /* push action return constants */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11503 | hlua_class_const_int(L, "CONTINUE", ACT_RET_CONT); |
| 11504 | hlua_class_const_int(L, "STOP", ACT_RET_STOP); |
| 11505 | hlua_class_const_int(L, "YIELD", ACT_RET_YIELD); |
| 11506 | hlua_class_const_int(L, "ERROR", ACT_RET_ERR); |
| 11507 | hlua_class_const_int(L, "DONE", ACT_RET_DONE); |
| 11508 | hlua_class_const_int(L, "DENY", ACT_RET_DENY); |
| 11509 | hlua_class_const_int(L, "ABORT", ACT_RET_ABRT); |
| 11510 | hlua_class_const_int(L, "INVALID", ACT_RET_INV); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 11511 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11512 | hlua_class_function(L, "wake_time", hlua_set_wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 11513 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11514 | lua_setglobal(L, "act"); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 11515 | |
| 11516 | /* |
| 11517 | * |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11518 | * Create "Filter" object. |
| 11519 | * |
| 11520 | */ |
| 11521 | |
| 11522 | /* This table entry is the object "filter" base. */ |
| 11523 | lua_newtable(L); |
| 11524 | |
| 11525 | /* push flags and constants */ |
| 11526 | hlua_class_const_int(L, "CONTINUE", 1); |
| 11527 | hlua_class_const_int(L, "WAIT", 0); |
| 11528 | hlua_class_const_int(L, "ERROR", -1); |
| 11529 | |
| 11530 | hlua_class_const_int(L, "FLT_CFG_FL_HTX", FLT_CFG_FL_HTX); |
| 11531 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 11532 | hlua_class_function(L, "wake_time", hlua_set_wake_time); |
| 11533 | hlua_class_function(L, "register_data_filter", hlua_register_data_filter); |
| 11534 | hlua_class_function(L, "unregister_data_filter", hlua_unregister_data_filter); |
| 11535 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11536 | lua_setglobal(L, "filter"); |
| 11537 | |
| 11538 | /* |
| 11539 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11540 | * Register class Map |
| 11541 | * |
| 11542 | */ |
| 11543 | |
| 11544 | /* This table entry is the object "Map" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11545 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11546 | |
| 11547 | /* register pattern types. */ |
| 11548 | for (i=0; i<PAT_MATCH_NUM; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11549 | hlua_class_const_int(L, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 11550 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 11551 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11552 | hlua_class_const_int(L, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 11553 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11554 | |
| 11555 | /* register constructor. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11556 | hlua_class_function(L, "new", hlua_map_new); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11557 | |
| 11558 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11559 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11560 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 11561 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11562 | lua_pushstring(L, "__index"); |
| 11563 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11564 | |
| 11565 | /* Register . */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11566 | hlua_class_function(L, "lookup", hlua_map_lookup); |
| 11567 | hlua_class_function(L, "slookup", hlua_map_slookup); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11568 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11569 | lua_rawset(L, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11570 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 11571 | /* Register previous table in the registry with reference and named entry. |
| 11572 | * The function hlua_register_metatable() pops the stack, so we |
| 11573 | * previously create a copy of the table. |
| 11574 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11575 | lua_pushvalue(L, -1); /* Copy the -1 entry and push it on the stack. */ |
| 11576 | class_map_ref = hlua_register_metatable(L, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11577 | |
| 11578 | /* Assign the metatable to the mai Map object. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11579 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11580 | |
| 11581 | /* Set a name to the table. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11582 | lua_setglobal(L, "Map"); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11583 | |
| 11584 | /* |
| 11585 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 11586 | * Register class Channel |
| 11587 | * |
| 11588 | */ |
| 11589 | |
| 11590 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11591 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 11592 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 11593 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11594 | lua_pushstring(L, "__index"); |
| 11595 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 11596 | |
| 11597 | /* Register . */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 11598 | hlua_class_function(L, "data", hlua_channel_get_data); |
| 11599 | hlua_class_function(L, "line", hlua_channel_get_line); |
| 11600 | hlua_class_function(L, "set", hlua_channel_set_data); |
| 11601 | hlua_class_function(L, "remove", hlua_channel_del_data); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11602 | hlua_class_function(L, "append", hlua_channel_append); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 11603 | hlua_class_function(L, "prepend", hlua_channel_prepend); |
| 11604 | hlua_class_function(L, "insert", hlua_channel_insert_data); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11605 | hlua_class_function(L, "send", hlua_channel_send); |
| 11606 | hlua_class_function(L, "forward", hlua_channel_forward); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 11607 | hlua_class_function(L, "input", hlua_channel_get_in_len); |
| 11608 | hlua_class_function(L, "output", hlua_channel_get_out_len); |
| 11609 | hlua_class_function(L, "may_recv", hlua_channel_may_recv); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11610 | hlua_class_function(L, "is_full", hlua_channel_is_full); |
| 11611 | hlua_class_function(L, "is_resp", hlua_channel_is_resp); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 11612 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 11613 | /* Deprecated API */ |
| 11614 | hlua_class_function(L, "get", hlua_channel_get); |
| 11615 | hlua_class_function(L, "dup", hlua_channel_dup); |
| 11616 | hlua_class_function(L, "getline", hlua_channel_getline); |
| 11617 | hlua_class_function(L, "get_in_len", hlua_channel_get_in_len); |
| 11618 | hlua_class_function(L, "get_out_len", hlua_channel_get_out_len); |
| 11619 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11620 | lua_rawset(L, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 11621 | |
| 11622 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11623 | class_channel_ref = hlua_register_metatable(L, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 11624 | |
| 11625 | /* |
| 11626 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 11627 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 11628 | * |
| 11629 | */ |
| 11630 | |
| 11631 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11632 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 11633 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 11634 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11635 | lua_pushstring(L, "__index"); |
| 11636 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 11637 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 11638 | /* Browse existing fetches and create the associated |
| 11639 | * object method. |
| 11640 | */ |
| 11641 | sf = NULL; |
| 11642 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 11643 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 11644 | * by an underscore. |
| 11645 | */ |
Willy Tarreau | 5ef9656 | 2021-08-26 16:48:53 +0200 | [diff] [blame] | 11646 | strlcpy2(trash.area, sf->kw, trash.size); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 11647 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 11648 | if (*p == '.' || *p == '-' || *p == '+') |
| 11649 | *p = '_'; |
| 11650 | |
| 11651 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11652 | lua_pushstring(L, trash.area); |
| 11653 | lua_pushlightuserdata(L, sf); |
| 11654 | lua_pushcclosure(L, hlua_run_sample_fetch, 1); |
| 11655 | lua_rawset(L, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 11656 | } |
| 11657 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11658 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 11659 | |
| 11660 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11661 | class_fetches_ref = hlua_register_metatable(L, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 11662 | |
| 11663 | /* |
| 11664 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 11665 | * Register class Converters |
| 11666 | * |
| 11667 | */ |
| 11668 | |
| 11669 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11670 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 11671 | |
| 11672 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11673 | lua_pushstring(L, "__index"); |
| 11674 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 11675 | |
| 11676 | /* Browse existing converters and create the associated |
| 11677 | * object method. |
| 11678 | */ |
| 11679 | sc = NULL; |
| 11680 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 11681 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 11682 | * by an underscore. |
| 11683 | */ |
Willy Tarreau | 5ef9656 | 2021-08-26 16:48:53 +0200 | [diff] [blame] | 11684 | strlcpy2(trash.area, sc->kw, trash.size); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 11685 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 11686 | if (*p == '.' || *p == '-' || *p == '+') |
| 11687 | *p = '_'; |
| 11688 | |
| 11689 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11690 | lua_pushstring(L, trash.area); |
| 11691 | lua_pushlightuserdata(L, sc); |
| 11692 | lua_pushcclosure(L, hlua_run_sample_conv, 1); |
| 11693 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 11694 | } |
| 11695 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11696 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 11697 | |
| 11698 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11699 | class_converters_ref = hlua_register_metatable(L, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 11700 | |
| 11701 | /* |
| 11702 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 11703 | * Register class HTTP |
| 11704 | * |
| 11705 | */ |
| 11706 | |
| 11707 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11708 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 11709 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 11710 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11711 | lua_pushstring(L, "__index"); |
| 11712 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 11713 | |
| 11714 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11715 | hlua_class_function(L, "req_get_headers",hlua_http_req_get_headers); |
| 11716 | hlua_class_function(L, "req_del_header", hlua_http_req_del_hdr); |
| 11717 | hlua_class_function(L, "req_rep_header", hlua_http_req_rep_hdr); |
| 11718 | hlua_class_function(L, "req_rep_value", hlua_http_req_rep_val); |
| 11719 | hlua_class_function(L, "req_add_header", hlua_http_req_add_hdr); |
| 11720 | hlua_class_function(L, "req_set_header", hlua_http_req_set_hdr); |
| 11721 | hlua_class_function(L, "req_set_method", hlua_http_req_set_meth); |
| 11722 | hlua_class_function(L, "req_set_path", hlua_http_req_set_path); |
| 11723 | hlua_class_function(L, "req_set_query", hlua_http_req_set_query); |
| 11724 | hlua_class_function(L, "req_set_uri", hlua_http_req_set_uri); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 11725 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11726 | hlua_class_function(L, "res_get_headers",hlua_http_res_get_headers); |
| 11727 | hlua_class_function(L, "res_del_header", hlua_http_res_del_hdr); |
| 11728 | hlua_class_function(L, "res_rep_header", hlua_http_res_rep_hdr); |
| 11729 | hlua_class_function(L, "res_rep_value", hlua_http_res_rep_val); |
| 11730 | hlua_class_function(L, "res_add_header", hlua_http_res_add_hdr); |
| 11731 | hlua_class_function(L, "res_set_header", hlua_http_res_set_hdr); |
| 11732 | hlua_class_function(L, "res_set_status", hlua_http_res_set_status); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 11733 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11734 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 11735 | |
| 11736 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11737 | class_http_ref = hlua_register_metatable(L, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 11738 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 11739 | /* |
| 11740 | * |
| 11741 | * Register class HTTPMessage |
| 11742 | * |
| 11743 | */ |
| 11744 | |
| 11745 | /* Create and fill the metatable. */ |
| 11746 | lua_newtable(L); |
| 11747 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 11748 | /* Create and fill the __index entry. */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 11749 | lua_pushstring(L, "__index"); |
| 11750 | lua_newtable(L); |
| 11751 | |
| 11752 | /* Register Lua functions. */ |
| 11753 | hlua_class_function(L, "is_resp", hlua_http_msg_is_resp); |
| 11754 | hlua_class_function(L, "get_stline", hlua_http_msg_get_stline); |
| 11755 | hlua_class_function(L, "get_headers", hlua_http_msg_get_headers); |
| 11756 | hlua_class_function(L, "del_header", hlua_http_msg_del_hdr); |
| 11757 | hlua_class_function(L, "rep_header", hlua_http_msg_rep_hdr); |
| 11758 | hlua_class_function(L, "rep_value", hlua_http_msg_rep_val); |
| 11759 | hlua_class_function(L, "add_header", hlua_http_msg_add_hdr); |
| 11760 | hlua_class_function(L, "set_header", hlua_http_msg_set_hdr); |
| 11761 | hlua_class_function(L, "set_method", hlua_http_msg_set_meth); |
| 11762 | hlua_class_function(L, "set_path", hlua_http_msg_set_path); |
| 11763 | hlua_class_function(L, "set_query", hlua_http_msg_set_query); |
| 11764 | hlua_class_function(L, "set_uri", hlua_http_msg_set_uri); |
| 11765 | hlua_class_function(L, "set_status", hlua_http_msg_set_status); |
| 11766 | hlua_class_function(L, "is_full", hlua_http_msg_is_full); |
| 11767 | hlua_class_function(L, "may_recv", hlua_http_msg_may_recv); |
| 11768 | hlua_class_function(L, "eom", hlua_http_msg_is_eom); |
| 11769 | hlua_class_function(L, "input", hlua_http_msg_get_in_len); |
| 11770 | hlua_class_function(L, "output", hlua_http_msg_get_out_len); |
| 11771 | |
| 11772 | hlua_class_function(L, "body", hlua_http_msg_get_body); |
| 11773 | hlua_class_function(L, "set", hlua_http_msg_set_data); |
| 11774 | hlua_class_function(L, "remove", hlua_http_msg_del_data); |
| 11775 | hlua_class_function(L, "append", hlua_http_msg_append); |
| 11776 | hlua_class_function(L, "prepend", hlua_http_msg_prepend); |
| 11777 | hlua_class_function(L, "insert", hlua_http_msg_insert_data); |
| 11778 | hlua_class_function(L, "set_eom", hlua_http_msg_set_eom); |
| 11779 | hlua_class_function(L, "unset_eom", hlua_http_msg_unset_eom); |
| 11780 | |
| 11781 | hlua_class_function(L, "send", hlua_http_msg_send); |
| 11782 | hlua_class_function(L, "forward", hlua_http_msg_forward); |
| 11783 | |
| 11784 | lua_rawset(L, -3); |
| 11785 | |
| 11786 | /* Register previous table in the registry with reference and named entry. */ |
| 11787 | class_http_msg_ref = hlua_register_metatable(L, CLASS_HTTP_MSG); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 11788 | |
| 11789 | /* |
| 11790 | * |
| 11791 | * Register class HTTPClient |
| 11792 | * |
| 11793 | */ |
| 11794 | |
| 11795 | /* Create and fill the metatable. */ |
| 11796 | lua_newtable(L); |
| 11797 | lua_pushstring(L, "__index"); |
| 11798 | lua_newtable(L); |
| 11799 | hlua_class_function(L, "get", hlua_httpclient_get); |
William Lallemand | 7d21836 | 2021-09-24 16:51:55 +0200 | [diff] [blame] | 11800 | hlua_class_function(L, "head", hlua_httpclient_head); |
| 11801 | hlua_class_function(L, "put", hlua_httpclient_put); |
| 11802 | hlua_class_function(L, "post", hlua_httpclient_post); |
| 11803 | hlua_class_function(L, "delete", hlua_httpclient_delete); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 11804 | lua_settable(L, -3); /* Sets the __index entry. */ |
| 11805 | |
| 11806 | |
| 11807 | class_httpclient_ref = hlua_register_metatable(L, CLASS_HTTPCLIENT); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 11808 | /* |
| 11809 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11810 | * Register class AppletTCP |
| 11811 | * |
| 11812 | */ |
| 11813 | |
| 11814 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11815 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11816 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 11817 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11818 | lua_pushstring(L, "__index"); |
| 11819 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11820 | |
| 11821 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11822 | hlua_class_function(L, "getline", hlua_applet_tcp_getline); |
| 11823 | hlua_class_function(L, "receive", hlua_applet_tcp_recv); |
| 11824 | hlua_class_function(L, "send", hlua_applet_tcp_send); |
| 11825 | hlua_class_function(L, "set_priv", hlua_applet_tcp_set_priv); |
| 11826 | hlua_class_function(L, "get_priv", hlua_applet_tcp_get_priv); |
| 11827 | hlua_class_function(L, "set_var", hlua_applet_tcp_set_var); |
| 11828 | hlua_class_function(L, "unset_var", hlua_applet_tcp_unset_var); |
| 11829 | hlua_class_function(L, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11830 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11831 | lua_settable(L, -3); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11832 | |
| 11833 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11834 | class_applet_tcp_ref = hlua_register_metatable(L, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11835 | |
| 11836 | /* |
| 11837 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 11838 | * Register class AppletHTTP |
| 11839 | * |
| 11840 | */ |
| 11841 | |
| 11842 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11843 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 11844 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 11845 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11846 | lua_pushstring(L, "__index"); |
| 11847 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 11848 | |
| 11849 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11850 | hlua_class_function(L, "set_priv", hlua_applet_http_set_priv); |
| 11851 | hlua_class_function(L, "get_priv", hlua_applet_http_get_priv); |
| 11852 | hlua_class_function(L, "set_var", hlua_applet_http_set_var); |
| 11853 | hlua_class_function(L, "unset_var", hlua_applet_http_unset_var); |
| 11854 | hlua_class_function(L, "get_var", hlua_applet_http_get_var); |
| 11855 | hlua_class_function(L, "getline", hlua_applet_http_getline); |
| 11856 | hlua_class_function(L, "receive", hlua_applet_http_recv); |
| 11857 | hlua_class_function(L, "send", hlua_applet_http_send); |
| 11858 | hlua_class_function(L, "add_header", hlua_applet_http_addheader); |
| 11859 | hlua_class_function(L, "set_status", hlua_applet_http_status); |
| 11860 | hlua_class_function(L, "start_response", hlua_applet_http_start_response); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 11861 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11862 | lua_settable(L, -3); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 11863 | |
| 11864 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11865 | class_applet_http_ref = hlua_register_metatable(L, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 11866 | |
| 11867 | /* |
| 11868 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 11869 | * Register class TXN |
| 11870 | * |
| 11871 | */ |
| 11872 | |
| 11873 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11874 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 11875 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 11876 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11877 | lua_pushstring(L, "__index"); |
| 11878 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 11879 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 11880 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11881 | hlua_class_function(L, "set_priv", hlua_set_priv); |
| 11882 | hlua_class_function(L, "get_priv", hlua_get_priv); |
| 11883 | hlua_class_function(L, "set_var", hlua_set_var); |
| 11884 | hlua_class_function(L, "unset_var", hlua_unset_var); |
| 11885 | hlua_class_function(L, "get_var", hlua_get_var); |
| 11886 | hlua_class_function(L, "done", hlua_txn_done); |
| 11887 | hlua_class_function(L, "reply", hlua_txn_reply_new); |
| 11888 | hlua_class_function(L, "set_loglevel", hlua_txn_set_loglevel); |
| 11889 | hlua_class_function(L, "set_tos", hlua_txn_set_tos); |
| 11890 | hlua_class_function(L, "set_mark", hlua_txn_set_mark); |
| 11891 | hlua_class_function(L, "set_priority_class", hlua_txn_set_priority_class); |
| 11892 | hlua_class_function(L, "set_priority_offset", hlua_txn_set_priority_offset); |
| 11893 | hlua_class_function(L, "deflog", hlua_txn_deflog); |
| 11894 | hlua_class_function(L, "log", hlua_txn_log); |
| 11895 | hlua_class_function(L, "Debug", hlua_txn_log_debug); |
| 11896 | hlua_class_function(L, "Info", hlua_txn_log_info); |
| 11897 | hlua_class_function(L, "Warning", hlua_txn_log_warning); |
| 11898 | hlua_class_function(L, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 11899 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11900 | lua_rawset(L, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 11901 | |
| 11902 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11903 | class_txn_ref = hlua_register_metatable(L, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 11904 | |
| 11905 | /* |
| 11906 | * |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 11907 | * Register class reply |
| 11908 | * |
| 11909 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11910 | lua_newtable(L); |
| 11911 | lua_pushstring(L, "__index"); |
| 11912 | lua_newtable(L); |
| 11913 | hlua_class_function(L, "set_status", hlua_txn_reply_set_status); |
| 11914 | hlua_class_function(L, "add_header", hlua_txn_reply_add_header); |
| 11915 | hlua_class_function(L, "del_header", hlua_txn_reply_del_header); |
| 11916 | hlua_class_function(L, "set_body", hlua_txn_reply_set_body); |
| 11917 | lua_settable(L, -3); /* Sets the __index entry. */ |
| 11918 | class_txn_reply_ref = luaL_ref(L, LUA_REGISTRYINDEX); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 11919 | |
| 11920 | |
| 11921 | /* |
| 11922 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 11923 | * Register class Socket |
| 11924 | * |
| 11925 | */ |
| 11926 | |
| 11927 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11928 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 11929 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 11930 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11931 | lua_pushstring(L, "__index"); |
| 11932 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 11933 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 11934 | #ifdef USE_OPENSSL |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11935 | hlua_class_function(L, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 11936 | #endif |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11937 | hlua_class_function(L, "connect", hlua_socket_connect); |
| 11938 | hlua_class_function(L, "send", hlua_socket_send); |
| 11939 | hlua_class_function(L, "receive", hlua_socket_receive); |
| 11940 | hlua_class_function(L, "close", hlua_socket_close); |
| 11941 | hlua_class_function(L, "getpeername", hlua_socket_getpeername); |
| 11942 | hlua_class_function(L, "getsockname", hlua_socket_getsockname); |
| 11943 | hlua_class_function(L, "setoption", hlua_socket_setoption); |
| 11944 | hlua_class_function(L, "settimeout", hlua_socket_settimeout); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 11945 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11946 | 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] | 11947 | |
| 11948 | /* Register the garbage collector entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11949 | lua_pushstring(L, "__gc"); |
| 11950 | lua_pushcclosure(L, hlua_socket_gc, 0); |
| 11951 | 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] | 11952 | |
| 11953 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11954 | class_socket_ref = hlua_register_metatable(L, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 11955 | |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 11956 | lua_atpanic(L, hlua_panic_safe); |
| 11957 | |
| 11958 | return L; |
| 11959 | } |
| 11960 | |
| 11961 | void hlua_init(void) { |
| 11962 | int i; |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 11963 | char *errmsg; |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 11964 | #ifdef USE_OPENSSL |
| 11965 | struct srv_kw *kw; |
| 11966 | int tmp_error; |
| 11967 | char *error; |
| 11968 | char *args[] = { /* SSL client configuration. */ |
| 11969 | "ssl", |
| 11970 | "verify", |
| 11971 | "none", |
| 11972 | NULL |
| 11973 | }; |
| 11974 | #endif |
| 11975 | |
| 11976 | /* Init post init function list head */ |
| 11977 | for (i = 0; i < MAX_THREADS + 1; i++) |
| 11978 | LIST_INIT(&hlua_init_functions[i]); |
| 11979 | |
| 11980 | /* Init state for common/shared lua parts */ |
| 11981 | hlua_state_id = 0; |
| 11982 | ha_set_tid(0); |
| 11983 | hlua_states[0] = hlua_init_state(0); |
| 11984 | |
| 11985 | /* Init state 1 for thread 0. We have at least one thread. */ |
| 11986 | hlua_state_id = 1; |
| 11987 | ha_set_tid(0); |
| 11988 | hlua_states[1] = hlua_init_state(1); |
| 11989 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 11990 | /* Proxy and server configuration initialisation. */ |
William Lallemand | 6bb77b9 | 2021-07-28 15:48:16 +0200 | [diff] [blame] | 11991 | socket_proxy = alloc_new_proxy("LUA-SOCKET", PR_CAP_FE|PR_CAP_BE|PR_CAP_INT, &errmsg); |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 11992 | if (!socket_proxy) { |
| 11993 | fprintf(stderr, "Lua init: %s\n", errmsg); |
| 11994 | exit(1); |
| 11995 | } |
| 11996 | proxy_preset_defaults(socket_proxy); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 11997 | |
| 11998 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 11999 | socket_tcp = new_server(socket_proxy); |
| 12000 | if (!socket_tcp) { |
| 12001 | fprintf(stderr, "Lua init: failed to allocate tcp server socket\n"); |
| 12002 | exit(1); |
| 12003 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12004 | |
| 12005 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12006 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12007 | socket_ssl = new_server(socket_proxy); |
| 12008 | if (!socket_ssl) { |
| 12009 | fprintf(stderr, "Lua init: failed to allocate ssl server socket\n"); |
| 12010 | exit(1); |
| 12011 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12012 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12013 | socket_ssl->use_ssl = 1; |
| 12014 | socket_ssl->xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12015 | |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 12016 | for (i = 0; args[i] != NULL; i++) { |
| 12017 | 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] | 12018 | /* |
| 12019 | * |
| 12020 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 12021 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12022 | * features like client certificates and ssl_verify. |
| 12023 | * |
| 12024 | */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12025 | tmp_error = kw->parse(args, &i, socket_proxy, socket_ssl, &error); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12026 | if (tmp_error != 0) { |
| 12027 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 12028 | abort(); /* This must be never arrives because the command line |
| 12029 | not editable by the user. */ |
| 12030 | } |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 12031 | i += kw->skip; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12032 | } |
| 12033 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12034 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 12035 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 12036 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 12037 | |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 12038 | static void hlua_deinit() |
| 12039 | { |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 12040 | int thr; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12041 | struct hlua_reg_filter *reg_flt, *reg_flt_bck; |
| 12042 | |
| 12043 | list_for_each_entry_safe(reg_flt, reg_flt_bck, &referenced_filters, l) |
| 12044 | release_hlua_reg_filter(reg_flt); |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 12045 | |
| 12046 | for (thr = 0; thr < MAX_THREADS+1; thr++) { |
| 12047 | if (hlua_states[thr]) |
| 12048 | lua_close(hlua_states[thr]); |
| 12049 | } |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 12050 | |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 12051 | srv_drop(socket_tcp); |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 12052 | |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 12053 | #ifdef USE_OPENSSL |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 12054 | srv_drop(socket_ssl); |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 12055 | #endif |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 12056 | |
| 12057 | free_proxy(socket_proxy); |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 12058 | } |
| 12059 | |
| 12060 | REGISTER_POST_DEINIT(hlua_deinit); |
| 12061 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 12062 | static void hlua_register_build_options(void) |
| 12063 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 12064 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 12065 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 12066 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 12067 | hap_register_build_opts(ptr, 1); |
| 12068 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 12069 | |
| 12070 | INITCALL0(STG_REGISTER, hlua_register_build_options); |