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 | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 35 | #include <haproxy/clock.h> |
Willy Tarreau | 7ea393d | 2020-06-04 18:02:10 +0200 | [diff] [blame] | 36 | #include <haproxy/connection.h> |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 37 | #include <haproxy/filters.h> |
Willy Tarreau | 5413a87 | 2020-06-02 19:33:08 +0200 | [diff] [blame] | 38 | #include <haproxy/h1.h> |
Willy Tarreau | 8641605 | 2020-06-04 09:20:54 +0200 | [diff] [blame] | 39 | #include <haproxy/hlua.h> |
Willy Tarreau | 8c79400 | 2020-06-04 10:05:25 +0200 | [diff] [blame] | 40 | #include <haproxy/hlua_fcn.h> |
Willy Tarreau | c2b1ff0 | 2020-06-04 21:21:03 +0200 | [diff] [blame] | 41 | #include <haproxy/http_ana.h> |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 42 | #include <haproxy/http_client.h> |
Willy Tarreau | 126ba3a | 2020-06-04 18:26:43 +0200 | [diff] [blame] | 43 | #include <haproxy/http_fetch.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 44 | #include <haproxy/http_htx.h> |
Willy Tarreau | c761f84 | 2020-06-04 11:40:28 +0200 | [diff] [blame] | 45 | #include <haproxy/http_rules.h> |
Willy Tarreau | 36979d9 | 2020-06-05 17:27:29 +0200 | [diff] [blame] | 46 | #include <haproxy/log.h> |
Willy Tarreau | 2cd5809 | 2020-06-04 15:10:43 +0200 | [diff] [blame] | 47 | #include <haproxy/map.h> |
Willy Tarreau | 8efbdfb | 2020-06-04 11:29:21 +0200 | [diff] [blame] | 48 | #include <haproxy/obj_type.h> |
Willy Tarreau | 225a90a | 2020-06-04 15:06:28 +0200 | [diff] [blame] | 49 | #include <haproxy/pattern.h> |
Willy Tarreau | 469509b | 2020-06-04 15:13:30 +0200 | [diff] [blame] | 50 | #include <haproxy/payload.h> |
Willy Tarreau | 3d6ee40 | 2021-05-08 20:28:07 +0200 | [diff] [blame] | 51 | #include <haproxy/proxy.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 52 | #include <haproxy/regex.h> |
Willy Tarreau | e6ce10b | 2020-06-04 15:33:47 +0200 | [diff] [blame] | 53 | #include <haproxy/sample.h> |
Willy Tarreau | 198e92a | 2021-03-05 10:23:32 +0100 | [diff] [blame] | 54 | #include <haproxy/server.h> |
Willy Tarreau | 48d25b3 | 2020-06-04 18:58:52 +0200 | [diff] [blame] | 55 | #include <haproxy/session.h> |
Willy Tarreau | 2eec9b5 | 2020-06-04 19:58:55 +0200 | [diff] [blame] | 56 | #include <haproxy/stats-t.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 57 | #include <haproxy/stream.h> |
Willy Tarreau | 5e539c9 | 2020-06-04 20:45:39 +0200 | [diff] [blame] | 58 | #include <haproxy/stream_interface.h> |
Willy Tarreau | cea0e1b | 2020-06-04 17:25:40 +0200 | [diff] [blame] | 59 | #include <haproxy/task.h> |
Willy Tarreau | 8b550af | 2020-06-04 17:42:48 +0200 | [diff] [blame] | 60 | #include <haproxy/tcp_rules.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 61 | #include <haproxy/thread.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 62 | #include <haproxy/tools.h> |
Willy Tarreau | a171892 | 2020-06-04 16:25:31 +0200 | [diff] [blame] | 63 | #include <haproxy/vars.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 64 | #include <haproxy/xref.h> |
| 65 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 66 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 67 | /* Lua uses longjmp to perform yield or throwing errors. This |
| 68 | * macro is used only for identifying the function that can |
| 69 | * not return because a longjmp is executed. |
| 70 | * __LJMP marks a prototype of hlua file that can use longjmp. |
| 71 | * WILL_LJMP() marks an lua function that will use longjmp. |
| 72 | * MAY_LJMP() marks an lua function that may use longjmp. |
| 73 | */ |
| 74 | #define __LJMP |
Willy Tarreau | 4e7cc33 | 2018-10-20 17:45:48 +0200 | [diff] [blame] | 75 | #define WILL_LJMP(func) do { func; my_unreachable(); } while(0) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 76 | #define MAY_LJMP(func) func |
| 77 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 78 | /* This couple of function executes securely some Lua calls outside of |
| 79 | * the lua runtime environment. Each Lua call can return a longjmp |
| 80 | * if it encounter a memory error. |
| 81 | * |
| 82 | * Lua documentation extract: |
| 83 | * |
| 84 | * If an error happens outside any protected environment, Lua calls |
| 85 | * a panic function (see lua_atpanic) and then calls abort, thus |
| 86 | * exiting the host application. Your panic function can avoid this |
| 87 | * exit by never returning (e.g., doing a long jump to your own |
| 88 | * recovery point outside Lua). |
| 89 | * |
| 90 | * The panic function runs as if it were a message handler (see |
| 91 | * §2.3); in particular, the error message is at the top of the |
| 92 | * stack. However, there is no guarantee about stack space. To push |
| 93 | * anything on the stack, the panic function must first check the |
| 94 | * available space (see §4.2). |
| 95 | * |
| 96 | * We must check all the Lua entry point. This includes: |
| 97 | * - The include/proto/hlua.h exported functions |
| 98 | * - the task wrapper function |
| 99 | * - The action wrapper function |
| 100 | * - The converters wrapper function |
| 101 | * - The sample-fetch wrapper functions |
| 102 | * |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 103 | * It is tolerated that the initialisation function returns an abort. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 104 | * Before each Lua abort, an error message is written on stderr. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 105 | * |
| 106 | * The macro SET_SAFE_LJMP initialise the longjmp. The Macro |
| 107 | * RESET_SAFE_LJMP reset the longjmp. These function must be macro |
| 108 | * because they must be exists in the program stack when the longjmp |
| 109 | * is called. |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 110 | * |
| 111 | * Note that the Lua processing is not really thread safe. It provides |
| 112 | * heavy system which consists to add our own lock function in the Lua |
| 113 | * code and recompile the library. This system will probably not accepted |
| 114 | * by maintainers of various distribs. |
| 115 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 116 | * 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] | 117 | * quick looking on the Lua sources displays a lua_lock() a the start |
| 118 | * of function and a lua_unlock() at the end of the function. So I |
| 119 | * conclude that the Lua thread safe mode just perform a mutex around |
| 120 | * all execution. So I prefer to do this in the HAProxy code, it will be |
| 121 | * easier for distro maintainers. |
| 122 | * |
| 123 | * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP |
| 124 | * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful |
| 125 | * to set mutex around these functions. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 126 | */ |
Willy Tarreau | 86abe44 | 2018-11-25 20:12:18 +0100 | [diff] [blame] | 127 | __decl_spinlock(hlua_global_lock); |
Thierry FOURNIER | ffbad79 | 2017-07-12 11:39:04 +0200 | [diff] [blame] | 128 | THREAD_LOCAL jmp_buf safe_ljmp_env; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 129 | static int hlua_panic_safe(lua_State *L) { return 0; } |
Willy Tarreau | 6a51090 | 2021-07-14 19:41:25 +0200 | [diff] [blame] | 130 | 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] | 131 | |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 132 | /* This is the chained list of struct hlua_function referenced |
| 133 | * for haproxy action, sample-fetches, converters, cli and |
| 134 | * applet bindings. It is used for a post-initialisation control. |
| 135 | */ |
| 136 | static struct list referenced_functions = LIST_HEAD_INIT(referenced_functions); |
| 137 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 138 | /* This variable is used only during initialization to identify the Lua state |
| 139 | * currently being initialized. 0 is the common lua state, 1 to n are the Lua |
| 140 | * states dedicated to each thread (in this case hlua_state_id==tid+1). |
| 141 | */ |
| 142 | static int hlua_state_id; |
| 143 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 144 | /* This is a NULL-terminated list of lua file which are referenced to load per thread */ |
| 145 | static char **per_thread_load = NULL; |
| 146 | |
| 147 | lua_State *hlua_init_state(int thread_id); |
| 148 | |
Willy Tarreau | 1e7bef1 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 149 | /* This function takes the Lua global lock. Keep this function's visibility |
| 150 | * global so that it can appear in stack dumps and performance profiles! |
| 151 | */ |
| 152 | void lua_take_global_lock() |
| 153 | { |
| 154 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); |
| 155 | } |
| 156 | |
| 157 | static inline void lua_drop_global_lock() |
| 158 | { |
| 159 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); |
| 160 | } |
| 161 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 162 | #define SET_SAFE_LJMP_L(__L, __HLUA) \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 163 | ({ \ |
| 164 | int ret; \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 165 | if ((__HLUA)->state_id == 0) \ |
Willy Tarreau | 1e7bef1 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 166 | lua_take_global_lock(); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 167 | if (setjmp(safe_ljmp_env) != 0) { \ |
| 168 | lua_atpanic(__L, hlua_panic_safe); \ |
| 169 | ret = 0; \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 170 | if ((__HLUA)->state_id == 0) \ |
Willy Tarreau | 1e7bef1 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 171 | lua_drop_global_lock(); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 172 | } else { \ |
| 173 | lua_atpanic(__L, hlua_panic_ljmp); \ |
| 174 | ret = 1; \ |
| 175 | } \ |
| 176 | ret; \ |
| 177 | }) |
| 178 | |
| 179 | /* If we are the last function catching Lua errors, we |
| 180 | * must reset the panic function. |
| 181 | */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 182 | #define RESET_SAFE_LJMP_L(__L, __HLUA) \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 183 | do { \ |
| 184 | lua_atpanic(__L, hlua_panic_safe); \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 185 | if ((__HLUA)->state_id == 0) \ |
Willy Tarreau | 1e7bef1 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 186 | lua_drop_global_lock(); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 187 | } while(0) |
| 188 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 189 | #define SET_SAFE_LJMP(__HLUA) \ |
| 190 | SET_SAFE_LJMP_L((__HLUA)->T, __HLUA) |
| 191 | |
| 192 | #define RESET_SAFE_LJMP(__HLUA) \ |
| 193 | RESET_SAFE_LJMP_L((__HLUA)->T, __HLUA) |
| 194 | |
| 195 | #define SET_SAFE_LJMP_PARENT(__HLUA) \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 196 | SET_SAFE_LJMP_L(hlua_states[(__HLUA)->state_id], __HLUA) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 197 | |
| 198 | #define RESET_SAFE_LJMP_PARENT(__HLUA) \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 199 | RESET_SAFE_LJMP_L(hlua_states[(__HLUA)->state_id], __HLUA) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 200 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 201 | /* Applet status flags */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 202 | #define APPLET_DONE 0x01 /* applet processing is done. */ |
Christopher Faulet | 18c2e8d | 2019-03-01 12:02:08 +0100 | [diff] [blame] | 203 | /* unused: 0x02 */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 204 | #define APPLET_HDR_SENT 0x04 /* Response header sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 205 | /* unused: 0x08, 0x10 */ |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 206 | #define APPLET_HTTP11 0x20 /* Last chunk sent. */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 207 | #define APPLET_RSP_SENT 0x40 /* The response was fully sent */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 208 | |
Thierry Fournier | afc63e2 | 2020-11-28 17:06:51 +0100 | [diff] [blame] | 209 | /* The main Lua execution context. The 0 index is the |
| 210 | * common state shared by all threads. |
| 211 | */ |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 212 | static lua_State *hlua_states[MAX_THREADS + 1]; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 213 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 214 | #define HLUA_FLT_CB_FINAL 0x00000001 |
| 215 | #define HLUA_FLT_CB_RETVAL 0x00000002 |
| 216 | #define HLUA_FLT_CB_ARG_CHN 0x00000004 |
| 217 | #define HLUA_FLT_CB_ARG_HTTP_MSG 0x00000008 |
| 218 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 219 | #define HLUA_FLT_CTX_FL_PAYLOAD 0x00000001 |
| 220 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 221 | struct hlua_reg_filter { |
| 222 | char *name; |
| 223 | int flt_ref[MAX_THREADS + 1]; |
| 224 | int fun_ref[MAX_THREADS + 1]; |
| 225 | struct list l; |
| 226 | }; |
| 227 | |
| 228 | struct hlua_flt_config { |
| 229 | struct hlua_reg_filter *reg; |
| 230 | int ref[MAX_THREADS + 1]; |
| 231 | char **args; |
| 232 | }; |
| 233 | |
| 234 | struct hlua_flt_ctx { |
| 235 | int ref; /* ref to the filter lua object */ |
| 236 | struct hlua *hlua[2]; /* lua runtime context (0: request, 1: response) */ |
| 237 | unsigned int cur_off[2]; /* current offset (0: request, 1: response) */ |
| 238 | unsigned int cur_len[2]; /* current forwardable length (0: request, 1: response) */ |
| 239 | unsigned int flags; /* HLUA_FLT_CTX_FL_* */ |
| 240 | }; |
| 241 | |
| 242 | DECLARE_STATIC_POOL(pool_head_hlua_flt_ctx, "hlua_flt_ctx", sizeof(struct hlua_flt_ctx)); |
| 243 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 244 | static int hlua_filter_from_payload(struct filter *filter); |
| 245 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 246 | /* This is the chained list of struct hlua_flt referenced |
| 247 | * for haproxy filters. It is used for a post-initialisation control. |
| 248 | */ |
| 249 | static struct list referenced_filters = LIST_HEAD_INIT(referenced_filters); |
| 250 | |
| 251 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 252 | /* This is the memory pool containing struct lua for applets |
| 253 | * (including cli). |
| 254 | */ |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 255 | DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua)); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 256 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 257 | /* Used for Socket connection. */ |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 258 | static struct proxy *socket_proxy; |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 259 | static struct server *socket_tcp; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 260 | #ifdef USE_OPENSSL |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 261 | static struct server *socket_ssl; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 262 | #endif |
| 263 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 264 | /* List head of the function called at the initialisation time. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 265 | struct list hlua_init_functions[MAX_THREADS + 1]; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 266 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 267 | /* The following variables contains the reference of the different |
| 268 | * Lua classes. These references are useful for identify metadata |
| 269 | * associated with an object. |
| 270 | */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 271 | static int class_txn_ref; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 272 | static int class_socket_ref; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 273 | static int class_channel_ref; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 274 | static int class_fetches_ref; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 275 | static int class_converters_ref; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 276 | static int class_http_ref; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 277 | static int class_http_msg_ref; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 278 | static int class_httpclient_ref; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 279 | static int class_map_ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 280 | static int class_applet_tcp_ref; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 281 | static int class_applet_http_ref; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 282 | static int class_txn_reply_ref; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 283 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 284 | /* Global Lua execution timeout. By default Lua, execution linked |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 285 | * with stream (actions, sample-fetches and converters) have a |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 286 | * short timeout. Lua linked with tasks doesn't have a timeout |
| 287 | * because a task may remain alive during all the haproxy execution. |
| 288 | */ |
| 289 | static unsigned int hlua_timeout_session = 4000; /* session timeout. */ |
| 290 | static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 291 | static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 292 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 293 | /* Interrupts the Lua processing each "hlua_nb_instruction" instructions. |
| 294 | * it is used for preventing infinite loops. |
| 295 | * |
| 296 | * I test the scheer with an infinite loop containing one incrementation |
| 297 | * and one test. I run this loop between 10 seconds, I raise a ceil of |
| 298 | * 710M loops from one interrupt each 9000 instructions, so I fix the value |
| 299 | * to one interrupt each 10 000 instructions. |
| 300 | * |
| 301 | * configured | Number of |
| 302 | * instructions | loops executed |
| 303 | * between two | in milions |
| 304 | * forced yields | |
| 305 | * ---------------+--------------- |
| 306 | * 10 | 160 |
| 307 | * 500 | 670 |
| 308 | * 1000 | 680 |
| 309 | * 5000 | 700 |
| 310 | * 7000 | 700 |
| 311 | * 8000 | 700 |
| 312 | * 9000 | 710 <- ceil |
| 313 | * 10000 | 710 |
| 314 | * 100000 | 710 |
| 315 | * 1000000 | 710 |
| 316 | * |
| 317 | */ |
| 318 | static unsigned int hlua_nb_instruction = 10000; |
| 319 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 320 | /* Descriptor for the memory allocation state. The limit is pre-initialised to |
| 321 | * 0 until it is replaced by "tune.lua.maxmem" during the config parsing, or it |
| 322 | * is replaced with ~0 during post_init after everything was loaded. This way |
| 323 | * it is guaranteed that if limit is ~0 the boot is complete and that if it's |
| 324 | * zero it's not yet limited and proper accounting is required. |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 325 | */ |
| 326 | struct hlua_mem_allocator { |
| 327 | size_t allocated; |
| 328 | size_t limit; |
| 329 | }; |
| 330 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 331 | static struct hlua_mem_allocator hlua_global_allocator THREAD_ALIGNED(64); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 332 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 333 | /* These functions converts types between HAProxy internal args or |
| 334 | * sample and LUA types. Another function permits to check if the |
| 335 | * LUA stack contains arguments according with an required ARG_T |
| 336 | * format. |
| 337 | */ |
| 338 | static int hlua_arg2lua(lua_State *L, const struct arg *arg); |
| 339 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 340 | __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] | 341 | uint64_t mask, struct proxy *p); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 342 | static int hlua_smp2lua(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 343 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 344 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp); |
| 345 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 346 | __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] | 347 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 348 | struct prepend_path { |
| 349 | struct list l; |
| 350 | char *type; |
| 351 | char *path; |
| 352 | }; |
| 353 | |
| 354 | static struct list prepend_path_list = LIST_HEAD_INIT(prepend_path_list); |
| 355 | |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 356 | #define SEND_ERR(__be, __fmt, __args...) \ |
| 357 | do { \ |
| 358 | send_log(__be, LOG_ERR, __fmt, ## __args); \ |
| 359 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 360 | ha_alert(__fmt, ## __args); \ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 361 | } while (0) |
| 362 | |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 363 | static inline struct hlua_function *new_hlua_function() |
| 364 | { |
| 365 | struct hlua_function *fcn; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 366 | int i; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 367 | |
| 368 | fcn = calloc(1, sizeof(*fcn)); |
| 369 | if (!fcn) |
| 370 | return NULL; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 371 | LIST_APPEND(&referenced_functions, &fcn->l); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 372 | for (i = 0; i < MAX_THREADS + 1; i++) |
| 373 | fcn->function_ref[i] = -1; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 374 | return fcn; |
| 375 | } |
| 376 | |
Christopher Faulet | dda4444 | 2021-04-12 14:05:43 +0200 | [diff] [blame] | 377 | static inline void release_hlua_function(struct hlua_function *fcn) |
| 378 | { |
| 379 | if (!fcn) |
| 380 | return; |
| 381 | if (fcn->name) |
| 382 | ha_free(&fcn->name); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 383 | LIST_DELETE(&fcn->l); |
Christopher Faulet | dda4444 | 2021-04-12 14:05:43 +0200 | [diff] [blame] | 384 | ha_free(&fcn); |
| 385 | } |
| 386 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 387 | /* If the common state is set, the stack id is 0, otherwise it is the tid + 1 */ |
| 388 | static inline int fcn_ref_to_stack_id(struct hlua_function *fcn) |
| 389 | { |
| 390 | if (fcn->function_ref[0] == -1) |
| 391 | return tid + 1; |
| 392 | return 0; |
| 393 | } |
| 394 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 395 | /* Create a new registered filter. Only its name is filled */ |
| 396 | static inline struct hlua_reg_filter *new_hlua_reg_filter(const char *name) |
| 397 | { |
| 398 | struct hlua_reg_filter *reg_flt; |
| 399 | int i; |
| 400 | |
| 401 | reg_flt = calloc(1, sizeof(*reg_flt)); |
| 402 | if (!reg_flt) |
| 403 | return NULL; |
| 404 | reg_flt->name = strdup(name); |
| 405 | if (!reg_flt->name) { |
| 406 | free(reg_flt); |
| 407 | return NULL; |
| 408 | } |
| 409 | LIST_APPEND(&referenced_filters, ®_flt->l); |
| 410 | for (i = 0; i < MAX_THREADS + 1; i++) { |
| 411 | reg_flt->flt_ref[i] = -1; |
| 412 | reg_flt->fun_ref[i] = -1; |
| 413 | } |
| 414 | return reg_flt; |
| 415 | } |
| 416 | |
| 417 | /* Release a registered filter */ |
| 418 | static inline void release_hlua_reg_filter(struct hlua_reg_filter *reg_flt) |
| 419 | { |
| 420 | if (!reg_flt) |
| 421 | return; |
| 422 | if (reg_flt->name) |
| 423 | ha_free(®_flt->name); |
| 424 | LIST_DELETE(®_flt->l); |
| 425 | ha_free(®_flt); |
| 426 | } |
| 427 | |
| 428 | /* If the common state is set, the stack id is 0, otherwise it is the tid + 1 */ |
| 429 | static inline int reg_flt_to_stack_id(struct hlua_reg_filter *reg_flt) |
| 430 | { |
| 431 | if (reg_flt->fun_ref[0] == -1) |
| 432 | return tid + 1; |
| 433 | return 0; |
| 434 | } |
| 435 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 436 | /* Used to check an Lua function type in the stack. It creates and |
| 437 | * returns a reference of the function. This function throws an |
| 438 | * error if the rgument is not a "function". |
| 439 | */ |
| 440 | __LJMP unsigned int hlua_checkfunction(lua_State *L, int argno) |
| 441 | { |
| 442 | if (!lua_isfunction(L, argno)) { |
Thierry FOURNIER | fd1e955 | 2018-02-23 18:41:18 +0100 | [diff] [blame] | 443 | 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] | 444 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 445 | } |
| 446 | lua_pushvalue(L, argno); |
| 447 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 448 | } |
| 449 | |
Christopher Faulet | ba9e21d | 2020-02-25 10:20:04 +0100 | [diff] [blame] | 450 | /* Used to check an Lua table type in the stack. It creates and |
| 451 | * returns a reference of the table. This function throws an |
| 452 | * error if the rgument is not a "table". |
| 453 | */ |
| 454 | __LJMP unsigned int hlua_checktable(lua_State *L, int argno) |
| 455 | { |
| 456 | if (!lua_istable(L, argno)) { |
| 457 | const char *msg = lua_pushfstring(L, "table expected, got %s", luaL_typename(L, argno)); |
| 458 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 459 | } |
| 460 | lua_pushvalue(L, argno); |
| 461 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 462 | } |
| 463 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 464 | /* Return the string that is of the top of the stack. */ |
| 465 | const char *hlua_get_top_error_string(lua_State *L) |
| 466 | { |
| 467 | if (lua_gettop(L) < 1) |
| 468 | return "unknown error"; |
| 469 | if (lua_type(L, -1) != LUA_TSTRING) |
| 470 | return "unknown error"; |
| 471 | return lua_tostring(L, -1); |
| 472 | } |
| 473 | |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 474 | __LJMP const char *hlua_traceback(lua_State *L, const char* sep) |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 475 | { |
| 476 | lua_Debug ar; |
| 477 | int level = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 478 | struct buffer *msg = get_trash_chunk(); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 479 | |
| 480 | while (lua_getstack(L, level++, &ar)) { |
| 481 | |
| 482 | /* Add separator */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 483 | if (b_data(msg)) |
| 484 | chunk_appendf(msg, "%s", sep); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 485 | |
| 486 | /* Fill fields: |
| 487 | * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what; |
| 488 | * 'l': fills in the field currentline; |
| 489 | * 'n': fills in the field name and namewhat; |
| 490 | * 't': fills in the field istailcall; |
| 491 | */ |
| 492 | lua_getinfo(L, "Slnt", &ar); |
| 493 | |
| 494 | /* Append code localisation */ |
| 495 | if (ar.currentline > 0) |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 496 | chunk_appendf(msg, "%s:%d: ", ar.short_src, ar.currentline); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 497 | else |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 498 | chunk_appendf(msg, "%s: ", ar.short_src); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 499 | |
| 500 | /* |
| 501 | * Get function name |
| 502 | * |
| 503 | * if namewhat is no empty, name is defined. |
| 504 | * what contains "Lua" for Lua function, "C" for C function, |
| 505 | * or "main" for main code. |
| 506 | */ |
| 507 | 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] | 508 | chunk_appendf(msg, "in %s '%s'", ar.namewhat, ar.name); /* use it */ |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 509 | |
| 510 | 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] | 511 | chunk_appendf(msg, "in main chunk"); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 512 | |
| 513 | else if (*ar.what != 'C') /* for Lua functions, use <file:line> */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 514 | chunk_appendf(msg, "in function line %d", ar.linedefined); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 515 | |
| 516 | else /* nothing left... */ |
| 517 | chunk_appendf(msg, "?"); |
| 518 | |
| 519 | |
| 520 | /* Display tailed call */ |
| 521 | if (ar.istailcall) |
| 522 | chunk_appendf(msg, " ..."); |
| 523 | } |
| 524 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 525 | return msg->area; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 529 | /* This function check the number of arguments available in the |
| 530 | * stack. If the number of arguments available is not the same |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 531 | * then <nb> an error is thrown. |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 532 | */ |
| 533 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 534 | { |
| 535 | if (lua_gettop(L) == nb) |
| 536 | return; |
| 537 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 538 | } |
| 539 | |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 540 | /* This function pushes an error string prefixed by the file name |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 541 | * and the line number where the error is encountered. |
| 542 | */ |
| 543 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 544 | { |
| 545 | va_list argp; |
| 546 | va_start(argp, fmt); |
| 547 | luaL_where(L, 1); |
| 548 | lua_pushvfstring(L, fmt, argp); |
| 549 | va_end(argp); |
| 550 | lua_concat(L, 2); |
| 551 | return 1; |
| 552 | } |
| 553 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 554 | /* This functions is used with sample fetch and converters. It |
| 555 | * converts the HAProxy configuration argument in a lua stack |
| 556 | * values. |
| 557 | * |
| 558 | * It takes an array of "arg", and each entry of the array is |
| 559 | * converted and pushed in the LUA stack. |
| 560 | */ |
| 561 | static int hlua_arg2lua(lua_State *L, const struct arg *arg) |
| 562 | { |
| 563 | switch (arg->type) { |
| 564 | case ARGT_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 565 | case ARGT_TIME: |
| 566 | case ARGT_SIZE: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 567 | lua_pushinteger(L, arg->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 568 | break; |
| 569 | |
| 570 | case ARGT_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 571 | lua_pushlstring(L, arg->data.str.area, arg->data.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 572 | break; |
| 573 | |
| 574 | case ARGT_IPV4: |
| 575 | case ARGT_IPV6: |
| 576 | case ARGT_MSK4: |
| 577 | case ARGT_MSK6: |
| 578 | case ARGT_FE: |
| 579 | case ARGT_BE: |
| 580 | case ARGT_TAB: |
| 581 | case ARGT_SRV: |
| 582 | case ARGT_USR: |
| 583 | case ARGT_MAP: |
| 584 | default: |
| 585 | lua_pushnil(L); |
| 586 | break; |
| 587 | } |
| 588 | return 1; |
| 589 | } |
| 590 | |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 591 | /* 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] | 592 | * 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] | 593 | * with sample fetch wrappers. The input arguments are given to the |
| 594 | * lua wrapper and converted as arg list by the function. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 595 | */ |
| 596 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg) |
| 597 | { |
| 598 | switch (lua_type(L, ud)) { |
| 599 | |
| 600 | case LUA_TNUMBER: |
| 601 | case LUA_TBOOLEAN: |
| 602 | arg->type = ARGT_SINT; |
| 603 | arg->data.sint = lua_tointeger(L, ud); |
| 604 | break; |
| 605 | |
| 606 | case LUA_TSTRING: |
| 607 | arg->type = ARGT_STR; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 608 | 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] | 609 | /* 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] | 610 | 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] | 611 | arg->data.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 612 | break; |
| 613 | |
| 614 | case LUA_TUSERDATA: |
| 615 | case LUA_TNIL: |
| 616 | case LUA_TTABLE: |
| 617 | case LUA_TFUNCTION: |
| 618 | case LUA_TTHREAD: |
| 619 | case LUA_TLIGHTUSERDATA: |
| 620 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 621 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 622 | break; |
| 623 | } |
| 624 | return 1; |
| 625 | } |
| 626 | |
| 627 | /* the following functions are used to convert a struct sample |
| 628 | * in Lua type. This useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 629 | * fetches or converters. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 630 | */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 631 | static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 632 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 633 | switch (smp->data.type) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 634 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 635 | case SMP_T_BOOL: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 636 | lua_pushinteger(L, smp->data.u.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 637 | break; |
| 638 | |
| 639 | case SMP_T_BIN: |
| 640 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 641 | 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] | 642 | break; |
| 643 | |
| 644 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 645 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 646 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 647 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 648 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 649 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 650 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 651 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 652 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 653 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 654 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 655 | 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] | 656 | break; |
| 657 | default: |
| 658 | lua_pushnil(L); |
| 659 | break; |
| 660 | } |
| 661 | break; |
| 662 | |
| 663 | case SMP_T_IPV4: |
| 664 | case SMP_T_IPV6: |
| 665 | 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] | 666 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 667 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 668 | 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] | 669 | else |
| 670 | lua_pushnil(L); |
| 671 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 672 | default: |
| 673 | lua_pushnil(L); |
| 674 | break; |
| 675 | } |
| 676 | return 1; |
| 677 | } |
| 678 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 679 | /* the following functions are used to convert a struct sample |
| 680 | * in Lua strings. This is useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 681 | * fetches or converters. |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 682 | */ |
| 683 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
| 684 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 685 | switch (smp->data.type) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 686 | |
| 687 | case SMP_T_BIN: |
| 688 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 689 | 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] | 690 | break; |
| 691 | |
| 692 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 693 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 694 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 695 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 696 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 697 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 698 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 699 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 700 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 701 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 702 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 703 | 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] | 704 | break; |
| 705 | default: |
| 706 | lua_pushstring(L, ""); |
| 707 | break; |
| 708 | } |
| 709 | break; |
| 710 | |
| 711 | case SMP_T_SINT: |
| 712 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 713 | case SMP_T_IPV4: |
| 714 | case SMP_T_IPV6: |
| 715 | 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] | 716 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 717 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 718 | 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] | 719 | else |
| 720 | lua_pushstring(L, ""); |
| 721 | break; |
| 722 | default: |
| 723 | lua_pushstring(L, ""); |
| 724 | break; |
| 725 | } |
| 726 | return 1; |
| 727 | } |
| 728 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 729 | /* the following functions are used to convert an Lua type in a |
| 730 | * struct sample. This is useful to provide data from a converter |
| 731 | * to the LUA code. |
| 732 | */ |
| 733 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 734 | { |
| 735 | switch (lua_type(L, ud)) { |
| 736 | |
| 737 | case LUA_TNUMBER: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 738 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 739 | smp->data.u.sint = lua_tointeger(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 740 | break; |
| 741 | |
| 742 | |
| 743 | case LUA_TBOOLEAN: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 744 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 745 | smp->data.u.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 746 | break; |
| 747 | |
| 748 | case LUA_TSTRING: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 749 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 750 | smp->flags |= SMP_F_CONST; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 751 | 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] | 752 | /* 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] | 753 | 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] | 754 | smp->data.u.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 755 | break; |
| 756 | |
| 757 | case LUA_TUSERDATA: |
| 758 | case LUA_TNIL: |
| 759 | case LUA_TTABLE: |
| 760 | case LUA_TFUNCTION: |
| 761 | case LUA_TTHREAD: |
| 762 | case LUA_TLIGHTUSERDATA: |
Thierry FOURNIER | 93405e1 | 2015-08-26 14:19:03 +0200 | [diff] [blame] | 763 | case LUA_TNONE: |
| 764 | default: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 765 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 766 | smp->data.u.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 767 | break; |
| 768 | } |
| 769 | return 1; |
| 770 | } |
| 771 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 772 | /* This function check the "argp" built by another conversion function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 773 | * 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] | 774 | * 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] | 775 | * |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 776 | * This function assumes that the argp argument contains ARGM_NBARGS + 1 |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 777 | * 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] | 778 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 779 | __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] | 780 | uint64_t mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 781 | { |
| 782 | int min_arg; |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 783 | int idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 784 | struct proxy *px; |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 785 | struct userlist *ul; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 786 | struct my_regex *reg; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 787 | const char *msg = NULL; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 788 | char *sname, *pname, *err = NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 789 | |
| 790 | idx = 0; |
| 791 | min_arg = ARGM(mask); |
| 792 | mask >>= ARGM_BITS; |
| 793 | |
| 794 | while (1) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 795 | struct buffer tmp = BUF_NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 796 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 797 | /* Check for mandatory arguments. */ |
| 798 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 799 | if (idx < min_arg) { |
| 800 | |
| 801 | /* If miss other argument than the first one, we return an error. */ |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 802 | if (idx > 0) { |
| 803 | msg = "Mandatory argument expected"; |
| 804 | goto error; |
| 805 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 806 | |
| 807 | /* If first argument have a certain type, some default values |
| 808 | * may be used. See the function smp_resolve_args(). |
| 809 | */ |
| 810 | switch (mask & ARGT_MASK) { |
| 811 | |
| 812 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 813 | if (!(p->cap & PR_CAP_FE)) { |
| 814 | msg = "Mandatory argument expected"; |
| 815 | goto error; |
| 816 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 817 | argp[idx].data.prx = p; |
| 818 | argp[idx].type = ARGT_FE; |
| 819 | argp[idx+1].type = ARGT_STOP; |
| 820 | break; |
| 821 | |
| 822 | case ARGT_BE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 823 | if (!(p->cap & PR_CAP_BE)) { |
| 824 | msg = "Mandatory argument expected"; |
| 825 | goto error; |
| 826 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 827 | argp[idx].data.prx = p; |
| 828 | argp[idx].type = ARGT_BE; |
| 829 | argp[idx+1].type = ARGT_STOP; |
| 830 | break; |
| 831 | |
| 832 | case ARGT_TAB: |
| 833 | argp[idx].data.prx = p; |
| 834 | argp[idx].type = ARGT_TAB; |
| 835 | argp[idx+1].type = ARGT_STOP; |
| 836 | break; |
| 837 | |
| 838 | default: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 839 | msg = "Mandatory argument expected"; |
| 840 | goto error; |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 841 | break; |
| 842 | } |
| 843 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 844 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 845 | } |
| 846 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 847 | /* Check for exceed the number of required argument. */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 848 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 849 | argp[idx].type != ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 850 | msg = "Last argument expected"; |
| 851 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 855 | argp[idx].type == ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 856 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 857 | } |
| 858 | |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 859 | /* Convert some argument types. All string in argp[] are for not |
| 860 | * duplicated yet. |
| 861 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 862 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 863 | case ARGT_SINT: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 864 | if (argp[idx].type != ARGT_SINT) { |
| 865 | msg = "integer expected"; |
| 866 | goto error; |
| 867 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 868 | argp[idx].type = ARGT_SINT; |
| 869 | break; |
| 870 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 871 | case ARGT_TIME: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 872 | if (argp[idx].type != ARGT_SINT) { |
| 873 | msg = "integer expected"; |
| 874 | goto error; |
| 875 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 876 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 877 | break; |
| 878 | |
| 879 | case ARGT_SIZE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 880 | if (argp[idx].type != ARGT_SINT) { |
| 881 | msg = "integer expected"; |
| 882 | goto error; |
| 883 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 884 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 885 | break; |
| 886 | |
| 887 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 888 | if (argp[idx].type != ARGT_STR) { |
| 889 | msg = "string expected"; |
| 890 | goto error; |
| 891 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 892 | 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] | 893 | if (!argp[idx].data.prx) { |
| 894 | msg = "frontend doesn't exist"; |
| 895 | goto error; |
| 896 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 897 | argp[idx].type = ARGT_FE; |
| 898 | break; |
| 899 | |
| 900 | case ARGT_BE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 901 | if (argp[idx].type != ARGT_STR) { |
| 902 | msg = "string expected"; |
| 903 | goto error; |
| 904 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 905 | 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] | 906 | if (!argp[idx].data.prx) { |
| 907 | msg = "backend doesn't exist"; |
| 908 | goto error; |
| 909 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 910 | argp[idx].type = ARGT_BE; |
| 911 | break; |
| 912 | |
| 913 | case ARGT_TAB: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 914 | if (argp[idx].type != ARGT_STR) { |
| 915 | msg = "string expected"; |
| 916 | goto error; |
| 917 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 918 | 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] | 919 | if (!argp[idx].data.t) { |
| 920 | msg = "table doesn't exist"; |
| 921 | goto error; |
| 922 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 923 | argp[idx].type = ARGT_TAB; |
| 924 | break; |
| 925 | |
| 926 | case ARGT_SRV: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 927 | if (argp[idx].type != ARGT_STR) { |
| 928 | msg = "string expected"; |
| 929 | goto error; |
| 930 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 931 | sname = strrchr(argp[idx].data.str.area, '/'); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 932 | if (sname) { |
| 933 | *sname++ = '\0'; |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 934 | pname = argp[idx].data.str.area; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 935 | px = proxy_be_by_name(pname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 936 | if (!px) { |
| 937 | msg = "backend doesn't exist"; |
| 938 | goto error; |
| 939 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 940 | } |
| 941 | else { |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 942 | sname = argp[idx].data.str.area; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 943 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 944 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 945 | argp[idx].data.srv = findserver(px, sname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 946 | if (!argp[idx].data.srv) { |
| 947 | msg = "server doesn't exist"; |
| 948 | goto error; |
| 949 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 950 | argp[idx].type = ARGT_SRV; |
| 951 | break; |
| 952 | |
| 953 | case ARGT_IPV4: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 954 | if (argp[idx].type != ARGT_STR) { |
| 955 | msg = "string expected"; |
| 956 | goto error; |
| 957 | } |
| 958 | if (inet_pton(AF_INET, argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 959 | msg = "invalid IPv4 address"; |
| 960 | goto error; |
| 961 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 962 | argp[idx].type = ARGT_IPV4; |
| 963 | break; |
| 964 | |
| 965 | case ARGT_MSK4: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 966 | if (argp[idx].type == ARGT_SINT) |
| 967 | len2mask4(argp[idx].data.sint, &argp[idx].data.ipv4); |
| 968 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 969 | if (!str2mask(argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 970 | msg = "invalid IPv4 mask"; |
| 971 | goto error; |
| 972 | } |
| 973 | } |
| 974 | else { |
| 975 | msg = "integer or string expected"; |
| 976 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 977 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 978 | argp[idx].type = ARGT_MSK4; |
| 979 | break; |
| 980 | |
| 981 | case ARGT_IPV6: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 982 | if (argp[idx].type != ARGT_STR) { |
| 983 | msg = "string expected"; |
| 984 | goto error; |
| 985 | } |
| 986 | if (inet_pton(AF_INET6, argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 987 | msg = "invalid IPv6 address"; |
| 988 | goto error; |
| 989 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 990 | argp[idx].type = ARGT_IPV6; |
| 991 | break; |
| 992 | |
| 993 | case ARGT_MSK6: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 994 | if (argp[idx].type == ARGT_SINT) |
| 995 | len2mask6(argp[idx].data.sint, &argp[idx].data.ipv6); |
| 996 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 997 | if (!str2mask6(argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 998 | msg = "invalid IPv6 mask"; |
| 999 | goto error; |
| 1000 | } |
| 1001 | } |
| 1002 | else { |
| 1003 | msg = "integer or string expected"; |
| 1004 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 1005 | } |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 1006 | argp[idx].type = ARGT_MSK6; |
| 1007 | break; |
| 1008 | |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 1009 | case ARGT_REG: |
| 1010 | if (argp[idx].type != ARGT_STR) { |
| 1011 | msg = "string expected"; |
| 1012 | goto error; |
| 1013 | } |
| 1014 | reg = regex_comp(argp[idx].data.str.area, !(argp[idx].type_flags & ARGF_REG_ICASE), 1, &err); |
| 1015 | if (!reg) { |
| 1016 | msg = lua_pushfstring(L, "error compiling regex '%s' : '%s'", |
| 1017 | argp[idx].data.str.area, err); |
| 1018 | free(err); |
| 1019 | goto error; |
| 1020 | } |
| 1021 | argp[idx].type = ARGT_REG; |
| 1022 | argp[idx].data.reg = reg; |
| 1023 | break; |
| 1024 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1025 | case ARGT_USR: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1026 | if (argp[idx].type != ARGT_STR) { |
| 1027 | msg = "string expected"; |
| 1028 | goto error; |
| 1029 | } |
| 1030 | if (p->uri_auth && p->uri_auth->userlist && |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1031 | strcmp(p->uri_auth->userlist->name, argp[idx].data.str.area) == 0) |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1032 | ul = p->uri_auth->userlist; |
| 1033 | else |
| 1034 | ul = auth_find_userlist(argp[idx].data.str.area); |
| 1035 | |
| 1036 | if (!ul) { |
| 1037 | msg = lua_pushfstring(L, "unable to find userlist '%s'", argp[idx].data.str.area); |
| 1038 | goto error; |
| 1039 | } |
| 1040 | argp[idx].type = ARGT_USR; |
| 1041 | argp[idx].data.usr = ul; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1042 | break; |
| 1043 | |
| 1044 | case ARGT_STR: |
| 1045 | if (!chunk_dup(&tmp, &argp[idx].data.str)) { |
| 1046 | msg = "unable to duplicate string arg"; |
| 1047 | goto error; |
| 1048 | } |
| 1049 | argp[idx].data.str = tmp; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1050 | break; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1051 | |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1052 | case ARGT_MAP: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1053 | msg = "type not yet supported"; |
| 1054 | goto error; |
| 1055 | break; |
| 1056 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1057 | } |
| 1058 | |
| 1059 | /* Check for type of argument. */ |
| 1060 | if ((mask & ARGT_MASK) != argp[idx].type) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1061 | msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 1062 | arg_type_names[(mask & ARGT_MASK)], |
| 1063 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 1064 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1065 | } |
| 1066 | |
| 1067 | /* Next argument. */ |
| 1068 | mask >>= ARGT_BITS; |
| 1069 | idx++; |
| 1070 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1071 | return 0; |
| 1072 | |
| 1073 | error: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 1074 | free_args(argp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1075 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 1076 | return 0; /* Never reached */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1077 | } |
| 1078 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1079 | /* |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 1080 | * The following functions are used to make correspondence between the the |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1081 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1082 | * |
| 1083 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1084 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 1085 | */ |
| 1086 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 1087 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 1088 | struct hlua **hlua = lua_getextraspace(L); |
| 1089 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1090 | } |
| 1091 | static inline void hlua_sethlua(struct hlua *hlua) |
| 1092 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 1093 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 1094 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1095 | } |
| 1096 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1097 | /* This function is used to send logs. It try to send on screen (stderr) |
| 1098 | * and on the default syslog server. |
| 1099 | */ |
| 1100 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 1101 | { |
| 1102 | struct tm tm; |
| 1103 | char *p; |
| 1104 | |
| 1105 | /* Cleanup the log message. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1106 | p = trash.area; |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1107 | for (; *msg != '\0'; msg++, p++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1108 | if (p >= trash.area + trash.size - 1) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 1109 | /* Break the message if exceed the buffer size. */ |
| 1110 | *(p-4) = ' '; |
| 1111 | *(p-3) = '.'; |
| 1112 | *(p-2) = '.'; |
| 1113 | *(p-1) = '.'; |
| 1114 | break; |
| 1115 | } |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 1116 | if (isprint((unsigned char)*msg)) |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1117 | *p = *msg; |
| 1118 | else |
| 1119 | *p = '.'; |
| 1120 | } |
| 1121 | *p = '\0'; |
| 1122 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1123 | send_log(px, level, "%s\n", trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1124 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Christopher Faulet | f98d821 | 2020-10-02 18:13:52 +0200 | [diff] [blame] | 1125 | if (level == LOG_DEBUG && !(global.mode & MODE_DEBUG)) |
| 1126 | return; |
| 1127 | |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 1128 | get_localtime(date.tv_sec, &tm); |
| 1129 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1130 | 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] | 1131 | (int)getpid(), trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1132 | fflush(stderr); |
| 1133 | } |
| 1134 | } |
| 1135 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1136 | /* This function just ensure that the yield will be always |
| 1137 | * returned with a timeout and permit to set some flags |
| 1138 | */ |
| 1139 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1140 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1141 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1142 | struct hlua *hlua; |
| 1143 | |
| 1144 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1145 | hlua = hlua_gethlua(L); |
| 1146 | if (!hlua) { |
| 1147 | return; |
| 1148 | } |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1149 | |
| 1150 | /* Set the wake timeout. If timeout is required, we set |
| 1151 | * the expiration time. |
| 1152 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1153 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1154 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 1155 | hlua->flags |= flags; |
| 1156 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1157 | /* Process the yield. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1158 | MAY_LJMP(lua_yieldk(L, nresults, ctx, k)); |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1159 | } |
| 1160 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1161 | /* This function initialises the Lua environment stored in the stream. |
| 1162 | * 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] | 1163 | * 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] | 1164 | * |
| 1165 | * This function is particular. it initialises a new Lua thread. If the |
| 1166 | * initialisation fails (example: out of memory error), the lua function |
| 1167 | * throws an error (longjmp). |
| 1168 | * |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1169 | * 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] | 1170 | * environment, so we must not initialise it. While the support of |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1171 | * threads appear, the safe environment set a lock to ensure only one |
| 1172 | * Lua execution at a time. If we initialize safe environment in another |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1173 | * safe environment, we have a dead lock. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1174 | * |
| 1175 | * set "already_safe" true if the context is initialized form safe |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1176 | * Lua function. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1177 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1178 | * This function manipulates two Lua stacks: the main and the thread. Only |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 1179 | * the main stack can fail. The thread is not manipulated. This function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1180 | * MUST NOT manipulate the created thread stack state, because it is not |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1181 | * protected against errors thrown by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1182 | */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1183 | 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] | 1184 | { |
| 1185 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1186 | lua->flags = 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1187 | lua->gc_count = 0; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1188 | lua->wake_time = TICK_ETERNITY; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1189 | lua->state_id = state_id; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1190 | LIST_INIT(&lua->com); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1191 | if (!already_safe) { |
| 1192 | if (!SET_SAFE_LJMP_PARENT(lua)) { |
| 1193 | lua->Tref = LUA_REFNIL; |
| 1194 | return 0; |
| 1195 | } |
| 1196 | } |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1197 | lua->T = lua_newthread(hlua_states[state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1198 | if (!lua->T) { |
| 1199 | lua->Tref = LUA_REFNIL; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1200 | if (!already_safe) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1201 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1202 | return 0; |
| 1203 | } |
| 1204 | hlua_sethlua(lua); |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1205 | lua->Tref = luaL_ref(hlua_states[state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1206 | lua->task = task; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1207 | if (!already_safe) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1208 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1209 | return 1; |
| 1210 | } |
| 1211 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1212 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1213 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 1214 | * is not freed. |
| 1215 | */ |
| 1216 | void hlua_ctx_destroy(struct hlua *lua) |
| 1217 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1218 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 1219 | return; |
| 1220 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1221 | if (!lua->T) |
| 1222 | goto end; |
| 1223 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1224 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1225 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1226 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1227 | if (!SET_SAFE_LJMP(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1228 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1229 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1230 | RESET_SAFE_LJMP(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1231 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1232 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1233 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1234 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1235 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1236 | /* Forces a garbage collecting process. If the Lua program is finished |
| 1237 | * without error, we run the GC on the thread pointer. Its freed all |
| 1238 | * the unused memory. |
| 1239 | * If the thread is finnish with an error or is currently yielded, |
| 1240 | * it seems that the GC applied on the thread doesn't clean anything, |
| 1241 | * so e run the GC on the main thread. |
| 1242 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 1243 | * the garbage collection. |
| 1244 | */ |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1245 | if (lua->gc_count) { |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1246 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1247 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1248 | lua_gc(hlua_states[lua->state_id], LUA_GCCOLLECT, 0); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1249 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1250 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1251 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 1252 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1253 | |
| 1254 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1255 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1256 | } |
| 1257 | |
| 1258 | /* This function is used to restore the Lua context when a coroutine |
| 1259 | * fails. This function copy the common memory between old coroutine |
| 1260 | * and the new coroutine. The old coroutine is destroyed, and its |
| 1261 | * replaced by the new coroutine. |
| 1262 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 1263 | * as string error message and it is copied in the new stack. |
| 1264 | */ |
| 1265 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 1266 | { |
| 1267 | lua_State *T; |
| 1268 | int new_ref; |
| 1269 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1270 | /* New Lua coroutine. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1271 | T = lua_newthread(hlua_states[lua->state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1272 | if (!T) |
| 1273 | return 0; |
| 1274 | |
| 1275 | /* Copy last error message. */ |
| 1276 | if (keep_msg) |
| 1277 | lua_xmove(lua->T, T, 1); |
| 1278 | |
| 1279 | /* Copy data between the coroutines. */ |
| 1280 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1281 | lua_xmove(lua->T, T, 1); |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 1282 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */ |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1283 | |
| 1284 | /* Destroy old data. */ |
| 1285 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1286 | |
| 1287 | /* The thread is garbage collected by Lua. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1288 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1289 | |
| 1290 | /* Fill the struct with the new coroutine values. */ |
| 1291 | lua->Mref = new_ref; |
| 1292 | lua->T = T; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1293 | lua->Tref = luaL_ref(hlua_states[lua->state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1294 | |
| 1295 | /* Set context. */ |
| 1296 | hlua_sethlua(lua); |
| 1297 | |
| 1298 | return 1; |
| 1299 | } |
| 1300 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1301 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1302 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1303 | struct hlua *hlua; |
| 1304 | |
| 1305 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1306 | hlua = hlua_gethlua(L); |
| 1307 | if (!hlua) |
| 1308 | return; |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1309 | |
| 1310 | /* Lua cannot yield when its returning from a function, |
| 1311 | * so, we can fix the interrupt hook to 1 instruction, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1312 | * expecting that the function is finished. |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1313 | */ |
| 1314 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1315 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1316 | return; |
| 1317 | } |
| 1318 | |
| 1319 | /* restore the interrupt condition. */ |
| 1320 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1321 | |
| 1322 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1323 | * If the state is not yieldable, trying yield causes an error. |
| 1324 | */ |
| 1325 | if (lua_isyieldable(L)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1326 | MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1327 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 1328 | /* If we cannot yield, update the clock and check the timeout. */ |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 1329 | clock_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1330 | hlua->run_time += now_ms - hlua->start_time; |
| 1331 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1332 | lua_pushfstring(L, "execution timeout"); |
| 1333 | WILL_LJMP(lua_error(L)); |
| 1334 | } |
| 1335 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1336 | /* Update the start time. */ |
| 1337 | hlua->start_time = now_ms; |
| 1338 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1339 | /* Try to interrupt the process at the end of the current |
| 1340 | * unyieldable function. |
| 1341 | */ |
| 1342 | 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] | 1343 | } |
| 1344 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1345 | /* This function start or resumes the Lua stack execution. If the flag |
| 1346 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1347 | * The function return an error. |
| 1348 | * |
| 1349 | * The function can returns 4 values: |
| 1350 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1351 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1352 | * task wakeup. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1353 | * - 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] | 1354 | * the top of the stack. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1355 | * - HLUA_E_ERR : An error has occurred without error message. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1356 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1357 | * 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] | 1358 | * LUA code. |
| 1359 | */ |
| 1360 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1361 | { |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1362 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 1363 | int nres; |
| 1364 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1365 | int ret; |
| 1366 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1367 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1368 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1369 | /* Initialise run time counter. */ |
| 1370 | if (!HLUA_IS_RUNNING(lua)) |
| 1371 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1372 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1373 | /* Lock the whole Lua execution. This lock must be before the |
| 1374 | * label "resume_execution". |
| 1375 | */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1376 | if (lua->state_id == 0) |
Willy Tarreau | 1e7bef1 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 1377 | lua_take_global_lock(); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1378 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1379 | resume_execution: |
| 1380 | |
| 1381 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1382 | * instructions. it is used for preventing infinite loops. |
| 1383 | */ |
| 1384 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1385 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1386 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1387 | HLUA_SET_RUN(lua); |
| 1388 | HLUA_CLR_CTRLYIELD(lua); |
| 1389 | HLUA_CLR_WAKERESWR(lua); |
| 1390 | HLUA_CLR_WAKEREQWR(lua); |
Christopher Faulet | 1f43a34 | 2021-08-04 17:58:21 +0200 | [diff] [blame] | 1391 | HLUA_CLR_NOYIELD(lua); |
| 1392 | if (!yield_allowed) |
| 1393 | HLUA_SET_NOYIELD(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1394 | |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1395 | /* Update the start time and reset wake_time. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1396 | lua->start_time = now_ms; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1397 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1398 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1399 | /* Call the function. */ |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1400 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1401 | 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] | 1402 | #else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1403 | ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs); |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1404 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1405 | switch (ret) { |
| 1406 | |
| 1407 | case LUA_OK: |
| 1408 | ret = HLUA_E_OK; |
| 1409 | break; |
| 1410 | |
| 1411 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1412 | /* Check if the execution timeout is expired. It it is the case, we |
| 1413 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1414 | */ |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 1415 | clock_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1416 | lua->run_time += now_ms - lua->start_time; |
| 1417 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1418 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1419 | ret = HLUA_E_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1420 | break; |
| 1421 | } |
| 1422 | /* Process the forced yield. if the general yield is not allowed or |
| 1423 | * if no task were associated this the current Lua execution |
| 1424 | * coroutine, we resume the execution. Else we want to return in the |
| 1425 | * scheduler and we want to be waked up again, to continue the |
| 1426 | * current Lua execution. So we schedule our own task. |
| 1427 | */ |
| 1428 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1429 | if (!yield_allowed || !lua->task) |
| 1430 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1431 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1432 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1433 | if (!yield_allowed) { |
| 1434 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1435 | ret = HLUA_E_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1436 | break; |
| 1437 | } |
| 1438 | ret = HLUA_E_AGAIN; |
| 1439 | break; |
| 1440 | |
| 1441 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1442 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1443 | /* Special exit case. The traditional exit is returned as an error |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1444 | * because the errors ares the only one mean to return immediately |
| 1445 | * from and lua execution. |
| 1446 | */ |
| 1447 | if (lua->flags & HLUA_EXIT) { |
| 1448 | ret = HLUA_E_OK; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 1449 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1450 | break; |
| 1451 | } |
| 1452 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1453 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1454 | if (!lua_checkstack(lua->T, 1)) { |
| 1455 | ret = HLUA_E_ERR; |
| 1456 | break; |
| 1457 | } |
| 1458 | msg = lua_tostring(lua->T, -1); |
| 1459 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1460 | lua_pop(lua->T, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 1461 | trace = hlua_traceback(lua->T, ", "); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1462 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1463 | 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] | 1464 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1465 | 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] | 1466 | ret = HLUA_E_ERRMSG; |
| 1467 | break; |
| 1468 | |
| 1469 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1470 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1471 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1472 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1473 | break; |
| 1474 | |
| 1475 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1476 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1477 | if (!lua_checkstack(lua->T, 1)) { |
| 1478 | ret = HLUA_E_ERR; |
| 1479 | break; |
| 1480 | } |
| 1481 | msg = lua_tostring(lua->T, -1); |
| 1482 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1483 | lua_pop(lua->T, 1); |
| 1484 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1485 | 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] | 1486 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1487 | 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] | 1488 | ret = HLUA_E_ERRMSG; |
| 1489 | break; |
| 1490 | |
| 1491 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1492 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1493 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1494 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1495 | break; |
| 1496 | } |
| 1497 | |
| 1498 | switch (ret) { |
| 1499 | case HLUA_E_AGAIN: |
| 1500 | break; |
| 1501 | |
| 1502 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1503 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1504 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1505 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1506 | break; |
| 1507 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1508 | case HLUA_E_ETMOUT: |
| 1509 | case HLUA_E_NOMEM: |
| 1510 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1511 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1512 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1513 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1514 | hlua_ctx_renew(lua, 0); |
| 1515 | break; |
| 1516 | |
| 1517 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1518 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1519 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1520 | break; |
| 1521 | } |
| 1522 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1523 | /* This is the main exit point, remove the Lua lock. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1524 | if (lua->state_id == 0) |
Willy Tarreau | 1e7bef1 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 1525 | lua_drop_global_lock(); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1526 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1527 | return ret; |
| 1528 | } |
| 1529 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1530 | /* This function exit the current code. */ |
| 1531 | __LJMP static int hlua_done(lua_State *L) |
| 1532 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1533 | struct hlua *hlua; |
| 1534 | |
| 1535 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1536 | hlua = hlua_gethlua(L); |
| 1537 | if (!hlua) |
| 1538 | return 0; |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1539 | |
| 1540 | hlua->flags |= HLUA_EXIT; |
| 1541 | WILL_LJMP(lua_error(L)); |
| 1542 | |
| 1543 | return 0; |
| 1544 | } |
| 1545 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1546 | /* This function is an LUA binding. It provides a function |
| 1547 | * for deleting ACL from a referenced ACL file. |
| 1548 | */ |
| 1549 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1550 | { |
| 1551 | const char *name; |
| 1552 | const char *key; |
| 1553 | struct pat_ref *ref; |
| 1554 | |
| 1555 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1556 | |
| 1557 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1558 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1559 | |
| 1560 | ref = pat_ref_lookup(name); |
| 1561 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1562 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1563 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1564 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1565 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1566 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1567 | return 0; |
| 1568 | } |
| 1569 | |
| 1570 | /* This function is an LUA binding. It provides a function |
| 1571 | * for deleting map entry from a referenced map file. |
| 1572 | */ |
| 1573 | static int hlua_del_map(lua_State *L) |
| 1574 | { |
| 1575 | const char *name; |
| 1576 | const char *key; |
| 1577 | struct pat_ref *ref; |
| 1578 | |
| 1579 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1580 | |
| 1581 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1582 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1583 | |
| 1584 | ref = pat_ref_lookup(name); |
| 1585 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1586 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1587 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1588 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1589 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1590 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1591 | return 0; |
| 1592 | } |
| 1593 | |
| 1594 | /* This function is an LUA binding. It provides a function |
| 1595 | * for adding ACL pattern from a referenced ACL file. |
| 1596 | */ |
| 1597 | static int hlua_add_acl(lua_State *L) |
| 1598 | { |
| 1599 | const char *name; |
| 1600 | const char *key; |
| 1601 | struct pat_ref *ref; |
| 1602 | |
| 1603 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1604 | |
| 1605 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1606 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1607 | |
| 1608 | ref = pat_ref_lookup(name); |
| 1609 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1610 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1611 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1612 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1613 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1614 | pat_ref_add(ref, key, NULL, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1615 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1616 | return 0; |
| 1617 | } |
| 1618 | |
| 1619 | /* This function is an LUA binding. It provides a function |
| 1620 | * for setting map pattern and sample from a referenced map |
| 1621 | * file. |
| 1622 | */ |
| 1623 | static int hlua_set_map(lua_State *L) |
| 1624 | { |
| 1625 | const char *name; |
| 1626 | const char *key; |
| 1627 | const char *value; |
| 1628 | struct pat_ref *ref; |
| 1629 | |
| 1630 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1631 | |
| 1632 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1633 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1634 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1635 | |
| 1636 | ref = pat_ref_lookup(name); |
| 1637 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1638 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1639 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1640 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1641 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1642 | pat_ref_set(ref, key, value, NULL); |
| 1643 | else |
| 1644 | pat_ref_add(ref, key, value, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1645 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1646 | return 0; |
| 1647 | } |
| 1648 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1649 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1650 | * 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] | 1651 | * metatable have an original version registered in the global context with |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1652 | * the name of the object (_G[<name>] = <metable> ). |
| 1653 | * |
| 1654 | * A metable is a table that modify the standard behavior of a standard |
| 1655 | * access to the associated data. The entries of this new metatable are |
| 1656 | * defined as is: |
| 1657 | * |
| 1658 | * http://lua-users.org/wiki/MetatableEvents |
| 1659 | * |
| 1660 | * __index |
| 1661 | * |
| 1662 | * we access an absent field in a table, the result is nil. This is |
| 1663 | * true, but it is not the whole truth. Actually, such access triggers |
| 1664 | * the interpreter to look for an __index metamethod: If there is no |
| 1665 | * such method, as usually happens, then the access results in nil; |
| 1666 | * otherwise, the metamethod will provide the result. |
| 1667 | * |
| 1668 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1669 | * the key does not appear in the table, but the metatable has an __index |
| 1670 | * property: |
| 1671 | * |
| 1672 | * - if the value is a function, the function is called, passing in the |
| 1673 | * table and the key; the return value of that function is returned as |
| 1674 | * the result. |
| 1675 | * |
| 1676 | * - if the value is another table, the value of the key in that table is |
| 1677 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1678 | * table's metatable has an __index property, then it continues on up) |
| 1679 | * |
| 1680 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1681 | * |
| 1682 | * http://www.lua.org/pil/13.4.1.html |
| 1683 | * |
| 1684 | * __newindex |
| 1685 | * |
| 1686 | * Like __index, but control property assignment. |
| 1687 | * |
| 1688 | * __mode - Control weak references. A string value with one or both |
| 1689 | * of the characters 'k' and 'v' which specifies that the the |
| 1690 | * keys and/or values in the table are weak references. |
| 1691 | * |
| 1692 | * __call - Treat a table like a function. When a table is followed by |
| 1693 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1694 | * a __call key pointing to a function, that function is invoked |
| 1695 | * (passing any specified arguments) and the return value is |
| 1696 | * returned. |
| 1697 | * |
| 1698 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1699 | * called, if the metatable for myTable has a __metatable |
| 1700 | * key, the value of that key is returned instead of the |
| 1701 | * actual metatable. |
| 1702 | * |
| 1703 | * __tostring - Control string representation. When the builtin |
| 1704 | * "tostring( myTable )" function is called, if the metatable |
| 1705 | * for myTable has a __tostring property set to a function, |
| 1706 | * that function is invoked (passing myTable to it) and the |
| 1707 | * return value is used as the string representation. |
| 1708 | * |
| 1709 | * __len - Control table length. When the table length is requested using |
| 1710 | * the length operator ( '#' ), if the metatable for myTable has |
| 1711 | * a __len key pointing to a function, that function is invoked |
| 1712 | * (passing myTable to it) and the return value used as the value |
| 1713 | * of "#myTable". |
| 1714 | * |
| 1715 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1716 | * collected, if the metatable has a __gc field pointing to a |
| 1717 | * function, that function is first invoked, passing the userdata |
| 1718 | * to it. The __gc metamethod is not called for tables. |
| 1719 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1720 | * |
| 1721 | * Special metamethods for redefining standard operators: |
| 1722 | * http://www.lua.org/pil/13.1.html |
| 1723 | * |
| 1724 | * __add "+" |
| 1725 | * __sub "-" |
| 1726 | * __mul "*" |
| 1727 | * __div "/" |
| 1728 | * __unm "!" |
| 1729 | * __pow "^" |
| 1730 | * __concat ".." |
| 1731 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1732 | * Special methods for redefining standard relations |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1733 | * http://www.lua.org/pil/13.2.html |
| 1734 | * |
| 1735 | * __eq "==" |
| 1736 | * __lt "<" |
| 1737 | * __le "<=" |
| 1738 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1739 | |
| 1740 | /* |
| 1741 | * |
| 1742 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1743 | * Class Map |
| 1744 | * |
| 1745 | * |
| 1746 | */ |
| 1747 | |
| 1748 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1749 | * a class session, otherwise it throws an error. |
| 1750 | */ |
| 1751 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1752 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1753 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1754 | } |
| 1755 | |
| 1756 | /* This function is the map constructor. It don't need |
| 1757 | * the class Map object. It creates and return a new Map |
| 1758 | * object. It must be called only during "body" or "init" |
| 1759 | * context because it process some filesystem accesses. |
| 1760 | */ |
| 1761 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1762 | { |
| 1763 | const char *fn; |
| 1764 | int match = PAT_MATCH_STR; |
| 1765 | struct sample_conv conv; |
| 1766 | const char *file = ""; |
| 1767 | int line = 0; |
| 1768 | lua_Debug ar; |
| 1769 | char *err = NULL; |
| 1770 | struct arg args[2]; |
| 1771 | |
| 1772 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1773 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1774 | |
| 1775 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1776 | |
| 1777 | if (lua_gettop(L) >= 2) { |
| 1778 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1779 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1780 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1781 | } |
| 1782 | |
| 1783 | /* Get Lua filename and line number. */ |
| 1784 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1785 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1786 | if (ar.currentline > 0) { /* is there info? */ |
| 1787 | file = ar.short_src; |
| 1788 | line = ar.currentline; |
| 1789 | } |
| 1790 | } |
| 1791 | |
| 1792 | /* fill fake sample_conv struct. */ |
| 1793 | conv.kw = ""; /* unused. */ |
| 1794 | conv.process = NULL; /* unused. */ |
| 1795 | conv.arg_mask = 0; /* unused. */ |
| 1796 | conv.val_args = NULL; /* unused. */ |
| 1797 | conv.out_type = SMP_T_STR; |
| 1798 | conv.private = (void *)(long)match; |
| 1799 | switch (match) { |
| 1800 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1801 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1802 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1803 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1804 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1805 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1806 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1807 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1808 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1809 | default: |
| 1810 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1811 | } |
| 1812 | |
| 1813 | /* fill fake args. */ |
| 1814 | args[0].type = ARGT_STR; |
Christopher Faulet | 73292e9 | 2020-08-06 08:40:09 +0200 | [diff] [blame] | 1815 | args[0].data.str.area = strdup(fn); |
| 1816 | args[0].data.str.data = strlen(fn); |
| 1817 | args[0].data.str.size = args[0].data.str.data+1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1818 | args[1].type = ARGT_STOP; |
| 1819 | |
| 1820 | /* load the map. */ |
| 1821 | if (!sample_load_map(args, &conv, file, line, &err)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1822 | /* error case: we can't use luaL_error because we must |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1823 | * free the err variable. |
| 1824 | */ |
| 1825 | luaL_where(L, 1); |
| 1826 | lua_pushfstring(L, "'new': %s.", err); |
| 1827 | lua_concat(L, 2); |
| 1828 | free(err); |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 1829 | chunk_destroy(&args[0].data.str); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1830 | WILL_LJMP(lua_error(L)); |
| 1831 | } |
| 1832 | |
| 1833 | /* create the lua object. */ |
| 1834 | lua_newtable(L); |
| 1835 | lua_pushlightuserdata(L, args[0].data.map); |
| 1836 | lua_rawseti(L, -2, 0); |
| 1837 | |
| 1838 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1839 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1840 | lua_setmetatable(L, -2); |
| 1841 | |
| 1842 | |
| 1843 | return 1; |
| 1844 | } |
| 1845 | |
| 1846 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1847 | { |
| 1848 | struct map_descriptor *desc; |
| 1849 | struct pattern *pat; |
| 1850 | struct sample smp; |
| 1851 | |
| 1852 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1853 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1854 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1855 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1856 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1857 | } |
| 1858 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1859 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1860 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1861 | 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] | 1862 | smp.data.u.str.size = smp.data.u.str.data + 1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1863 | } |
| 1864 | |
| 1865 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1866 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1867 | if (str) |
| 1868 | lua_pushstring(L, ""); |
| 1869 | else |
| 1870 | lua_pushnil(L); |
| 1871 | return 1; |
| 1872 | } |
| 1873 | |
| 1874 | /* 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] | 1875 | 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] | 1876 | return 1; |
| 1877 | } |
| 1878 | |
| 1879 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1880 | { |
| 1881 | return _hlua_map_lookup(L, 0); |
| 1882 | } |
| 1883 | |
| 1884 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1885 | { |
| 1886 | return _hlua_map_lookup(L, 1); |
| 1887 | } |
| 1888 | |
| 1889 | /* |
| 1890 | * |
| 1891 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1892 | * Class Socket |
| 1893 | * |
| 1894 | * |
| 1895 | */ |
| 1896 | |
| 1897 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1898 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1899 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1900 | } |
| 1901 | |
| 1902 | /* 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] | 1903 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1904 | * received. |
| 1905 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1906 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1907 | { |
Christopher Faulet | 86e1c33 | 2021-12-20 17:09:39 +0100 | [diff] [blame] | 1908 | struct stream_interface *si = cs_si(appctx->owner); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1909 | |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1910 | if (appctx->ctx.hlua_cosocket.die) { |
| 1911 | si_shutw(si); |
| 1912 | si_shutr(si); |
| 1913 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1914 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1915 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1916 | stream_shutdown(si_strm(si), SF_ERR_KILLED); |
| 1917 | } |
| 1918 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1919 | /* If we can't write, wakeup the pending write signals. */ |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1920 | if (channel_output_closed(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1921 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1922 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1923 | /* If we can't read, wakeup the pending read signals. */ |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1924 | if (channel_input_closed(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1925 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1926 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1927 | /* if the connection is not established, inform the stream that we want |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1928 | * to be notified whenever the connection completes. |
| 1929 | */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1930 | if (si_opposite(si)->state < SI_ST_EST) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 1931 | si_cant_get(si); |
Willy Tarreau | 12c2423 | 2018-12-06 15:29:50 +0100 | [diff] [blame] | 1932 | si_rx_conn_blk(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1933 | si_rx_endp_more(si); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1934 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1935 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1936 | |
| 1937 | /* This function is called after the connect. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1938 | appctx->ctx.hlua_cosocket.connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1939 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1940 | /* 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] | 1941 | if (channel_may_recv(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1942 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1943 | |
| 1944 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1945 | if (!channel_is_empty(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1946 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1947 | |
| 1948 | /* Some data were injected in the buffer, notify the stream |
| 1949 | * interface. |
| 1950 | */ |
| 1951 | if (!channel_is_empty(si_ic(si))) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 1952 | si_update(si); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1953 | |
| 1954 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1955 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1956 | */ |
| 1957 | if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write)) |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1958 | si_rx_endp_more(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1959 | } |
| 1960 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1961 | /* This function is called when the "struct stream" is destroyed. |
| 1962 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1963 | * Wake all the pending signals. |
| 1964 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1965 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1966 | { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1967 | struct xref *peer; |
| 1968 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1969 | /* Remove my link in the original object. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1970 | peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref); |
| 1971 | if (peer) |
| 1972 | xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1973 | |
| 1974 | /* Wake all the task waiting for me. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1975 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1976 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1977 | } |
| 1978 | |
| 1979 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1980 | * uses this object. If the stream does not exists, just quit. |
| 1981 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1982 | * pending signal can rest in the read and write lists. destroy |
| 1983 | * it. |
| 1984 | */ |
| 1985 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1986 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1987 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1988 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1989 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1990 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1991 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 1992 | |
| 1993 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1994 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1995 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1996 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1997 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1998 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1999 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2000 | appctx->ctx.hlua_cosocket.die = 1; |
| 2001 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2002 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2003 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2004 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2005 | return 0; |
| 2006 | } |
| 2007 | |
| 2008 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2009 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2010 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2011 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2012 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2013 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2014 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2015 | struct xref *peer; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2016 | struct hlua *hlua; |
| 2017 | |
| 2018 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2019 | hlua = hlua_gethlua(L); |
| 2020 | if (!hlua) |
| 2021 | return 0; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2022 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2023 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2024 | |
| 2025 | /* Check if we run on the same thread than the xreator thread. |
| 2026 | * We cannot access to the socket if the thread is different. |
| 2027 | */ |
| 2028 | if (socket->tid != tid) |
| 2029 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2030 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2031 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2032 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2033 | return 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2034 | |
| 2035 | hlua->gc_count--; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2036 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2037 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2038 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2039 | appctx->ctx.hlua_cosocket.die = 1; |
| 2040 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2041 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2042 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2043 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2044 | return 0; |
| 2045 | } |
| 2046 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2047 | /* The close function calls close_helper. |
| 2048 | */ |
| 2049 | __LJMP static int hlua_socket_close(lua_State *L) |
| 2050 | { |
| 2051 | MAY_LJMP(check_args(L, 1, "close")); |
| 2052 | return hlua_socket_close_helper(L); |
| 2053 | } |
| 2054 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2055 | /* This Lua function assumes that the stack contain three parameters. |
| 2056 | * 1 - USERDATA containing a struct socket |
| 2057 | * 2 - INTEGER with values of the macro defined below |
| 2058 | * If the integer is -1, we must read at most one line. |
| 2059 | * If the integer is -2, we ust read all the data until the |
| 2060 | * end of the stream. |
| 2061 | * If the integer is positive value, we must read a number of |
| 2062 | * bytes corresponding to this value. |
| 2063 | */ |
| 2064 | #define HLSR_READ_LINE (-1) |
| 2065 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2066 | __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] | 2067 | { |
| 2068 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2069 | int wanted = lua_tointeger(L, 2); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2070 | struct hlua *hlua; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2071 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2072 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2073 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 2074 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2075 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 2076 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2077 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 2078 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 2079 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2080 | struct stream_interface *si; |
| 2081 | struct stream *s; |
| 2082 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2083 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2084 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2085 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2086 | hlua = hlua_gethlua(L); |
| 2087 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2088 | /* Check if this lua stack is schedulable. */ |
| 2089 | if (!hlua || !hlua->task) |
| 2090 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 2091 | "'frontend', 'backend' or 'task'")); |
| 2092 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2093 | /* Check if we run on the same thread than the xreator thread. |
| 2094 | * We cannot access to the socket if the thread is different. |
| 2095 | */ |
| 2096 | if (socket->tid != tid) |
| 2097 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2098 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2099 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2100 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2101 | if (!peer) |
| 2102 | goto no_peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2103 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Christopher Faulet | 86e1c33 | 2021-12-20 17:09:39 +0100 | [diff] [blame] | 2104 | si = cs_si(appctx->owner); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2105 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2106 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2107 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2108 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2109 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2110 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2111 | if (nblk < 0) /* Connection close. */ |
| 2112 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2113 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2114 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 2115 | |
| 2116 | /* remove final \r\n. */ |
| 2117 | if (nblk == 1) { |
| 2118 | if (blk1[len1-1] == '\n') { |
| 2119 | len1--; |
| 2120 | skip_at_end++; |
| 2121 | if (blk1[len1-1] == '\r') { |
| 2122 | len1--; |
| 2123 | skip_at_end++; |
| 2124 | } |
| 2125 | } |
| 2126 | } |
| 2127 | else { |
| 2128 | if (blk2[len2-1] == '\n') { |
| 2129 | len2--; |
| 2130 | skip_at_end++; |
| 2131 | if (blk2[len2-1] == '\r') { |
| 2132 | len2--; |
| 2133 | skip_at_end++; |
| 2134 | } |
| 2135 | } |
| 2136 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2137 | } |
| 2138 | |
| 2139 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2140 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2141 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2142 | if (nblk < 0) /* Connection close. */ |
| 2143 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2144 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2145 | goto connection_empty; |
| 2146 | } |
| 2147 | |
| 2148 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2149 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2150 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2151 | if (nblk < 0) /* Connection close. */ |
| 2152 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2153 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2154 | goto connection_empty; |
| 2155 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2156 | missing_bytes = wanted - socket->b.n; |
| 2157 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2158 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2159 | len1 = missing_bytes; |
| 2160 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 2161 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2162 | } |
| 2163 | |
| 2164 | len = len1; |
| 2165 | |
| 2166 | luaL_addlstring(&socket->b, blk1, len1); |
| 2167 | if (nblk == 2) { |
| 2168 | len += len2; |
| 2169 | luaL_addlstring(&socket->b, blk2, len2); |
| 2170 | } |
| 2171 | |
| 2172 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2173 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2174 | |
| 2175 | /* Don't wait anything. */ |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 2176 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2177 | |
| 2178 | /* If the pattern reclaim to read all the data |
| 2179 | * in the connection, got out. |
| 2180 | */ |
| 2181 | if (wanted == HLSR_READ_ALL) |
| 2182 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2183 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2184 | goto connection_empty; |
| 2185 | |
| 2186 | /* Return result. */ |
| 2187 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2188 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2189 | return 1; |
| 2190 | |
| 2191 | connection_closed: |
| 2192 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2193 | xref_unlock(&socket->xref, peer); |
| 2194 | |
| 2195 | no_peer: |
| 2196 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2197 | /* If the buffer containds data. */ |
| 2198 | if (socket->b.n > 0) { |
| 2199 | luaL_pushresult(&socket->b); |
| 2200 | return 1; |
| 2201 | } |
| 2202 | lua_pushnil(L); |
| 2203 | lua_pushstring(L, "connection closed."); |
| 2204 | return 2; |
| 2205 | |
| 2206 | connection_empty: |
| 2207 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2208 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) { |
| 2209 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2210 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2211 | } |
| 2212 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2213 | 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] | 2214 | return 0; |
| 2215 | } |
| 2216 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2217 | /* This Lua function gets two parameters. The first one can be string |
| 2218 | * or a number. If the string is "*l", the user requires one line. If |
| 2219 | * 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] | 2220 | * If the value is a number, the user require a number of bytes equal |
| 2221 | * to the value. The default value is "*l" (a line). |
| 2222 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2223 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2224 | * integer takes this values: |
| 2225 | * -1 : read a line |
| 2226 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2227 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2228 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2229 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2230 | * concatenated with the read data. |
| 2231 | */ |
| 2232 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 2233 | { |
| 2234 | int wanted = HLSR_READ_LINE; |
| 2235 | const char *pattern; |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2236 | int lastarg, type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2237 | char *error; |
| 2238 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2239 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2240 | |
| 2241 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 2242 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 2243 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2244 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2245 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2246 | /* Check if we run on the same thread than the xreator thread. |
| 2247 | * We cannot access to the socket if the thread is different. |
| 2248 | */ |
| 2249 | if (socket->tid != tid) |
| 2250 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2251 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2252 | /* check for pattern. */ |
| 2253 | if (lua_gettop(L) >= 2) { |
| 2254 | type = lua_type(L, 2); |
| 2255 | if (type == LUA_TSTRING) { |
| 2256 | pattern = lua_tostring(L, 2); |
| 2257 | if (strcmp(pattern, "*a") == 0) |
| 2258 | wanted = HLSR_READ_ALL; |
| 2259 | else if (strcmp(pattern, "*l") == 0) |
| 2260 | wanted = HLSR_READ_LINE; |
| 2261 | else { |
| 2262 | wanted = strtoll(pattern, &error, 10); |
| 2263 | if (*error != '\0') |
| 2264 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 2265 | } |
| 2266 | } |
| 2267 | else if (type == LUA_TNUMBER) { |
| 2268 | wanted = lua_tointeger(L, 2); |
| 2269 | if (wanted < 0) |
| 2270 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 2271 | } |
| 2272 | } |
| 2273 | |
| 2274 | /* Set pattern. */ |
| 2275 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 2276 | |
| 2277 | /* Check if we would replace the top by itself. */ |
| 2278 | if (lua_gettop(L) != 2) |
| 2279 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2280 | |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2281 | /* Save index of the top of the stack because since buffers are used, it |
| 2282 | * may change |
| 2283 | */ |
| 2284 | lastarg = lua_gettop(L); |
| 2285 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2286 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2287 | luaL_buffinit(L, &socket->b); |
| 2288 | |
| 2289 | /* Check prefix. */ |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2290 | if (lastarg >= 3) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2291 | if (lua_type(L, 3) != LUA_TSTRING) |
| 2292 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 2293 | pattern = lua_tolstring(L, 3, &len); |
| 2294 | luaL_addlstring(&socket->b, pattern, len); |
| 2295 | } |
| 2296 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2297 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2298 | } |
| 2299 | |
| 2300 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 2301 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2302 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2303 | 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] | 2304 | { |
| 2305 | struct hlua_socket *socket; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2306 | struct hlua *hlua; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2307 | struct appctx *appctx; |
| 2308 | size_t buf_len; |
| 2309 | const char *buf; |
| 2310 | int len; |
| 2311 | int send_len; |
| 2312 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2313 | struct xref *peer; |
| 2314 | struct stream_interface *si; |
| 2315 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2316 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2317 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2318 | hlua = hlua_gethlua(L); |
| 2319 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2320 | /* Check if this lua stack is schedulable. */ |
| 2321 | if (!hlua || !hlua->task) |
| 2322 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 2323 | "'frontend', 'backend' or 'task'")); |
| 2324 | |
| 2325 | /* Get object */ |
| 2326 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2327 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2328 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2329 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2330 | /* Check if we run on the same thread than the xreator thread. |
| 2331 | * We cannot access to the socket if the thread is different. |
| 2332 | */ |
| 2333 | if (socket->tid != tid) |
| 2334 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2335 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2336 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2337 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2338 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2339 | lua_pushinteger(L, -1); |
| 2340 | return 1; |
| 2341 | } |
| 2342 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Christopher Faulet | 86e1c33 | 2021-12-20 17:09:39 +0100 | [diff] [blame] | 2343 | si = cs_si(appctx->owner); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2344 | s = si_strm(si); |
| 2345 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2346 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2347 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2348 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2349 | lua_pushinteger(L, -1); |
| 2350 | return 1; |
| 2351 | } |
| 2352 | |
| 2353 | /* Update the input buffer data. */ |
| 2354 | buf += sent; |
| 2355 | send_len = buf_len - sent; |
| 2356 | |
| 2357 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2358 | if (sent >= buf_len) { |
| 2359 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2360 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2361 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2362 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2363 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2364 | * the request buffer if its not required. |
| 2365 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2366 | if (s->req.buf.size == 0) { |
Willy Tarreau | 581abd3 | 2018-10-25 10:21:41 +0200 | [diff] [blame] | 2367 | if (!si_alloc_ibuf(si, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2368 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2369 | } |
| 2370 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2371 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2372 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2373 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2374 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2375 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2376 | |
| 2377 | /* send data */ |
| 2378 | if (len < send_len) |
| 2379 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2380 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2381 | |
| 2382 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2383 | * the data" (-2) are not expected because the available length |
| 2384 | * is tested. |
| 2385 | * Other unknown error are also not expected. |
| 2386 | */ |
| 2387 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2388 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2389 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2390 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2391 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2392 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2393 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2394 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2395 | return 1; |
| 2396 | } |
| 2397 | |
| 2398 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2399 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2400 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2401 | s->req.rex = TICK_ETERNITY; |
| 2402 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2403 | |
| 2404 | /* Update length sent. */ |
| 2405 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2406 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2407 | |
| 2408 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2409 | if (sent + len >= buf_len) { |
| 2410 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2411 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2412 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2413 | |
| 2414 | hlua_socket_write_yield_return: |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2415 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2416 | xref_unlock(&socket->xref, peer); |
| 2417 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2418 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2419 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2420 | 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] | 2421 | return 0; |
| 2422 | } |
| 2423 | |
| 2424 | /* This function initiate the send of data. It just check the input |
| 2425 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2426 | * 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] | 2427 | * "hlua_socket_write_yield" that can yield. |
| 2428 | * |
| 2429 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2430 | * the associated object. The second is a string buffer. The third is |
| 2431 | * a facultative integer that represents where is the buffer position |
| 2432 | * of the start of the data that can send. The first byte is the |
| 2433 | * position "1". The default value is "1". The fourth argument is a |
| 2434 | * facultative integer that represents where is the buffer position |
| 2435 | * of the end of the data that can send. The default is the last byte. |
| 2436 | */ |
| 2437 | static int hlua_socket_send(struct lua_State *L) |
| 2438 | { |
| 2439 | int i; |
| 2440 | int j; |
| 2441 | const char *buf; |
| 2442 | size_t buf_len; |
| 2443 | |
| 2444 | /* Check number of arguments. */ |
| 2445 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2446 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2447 | |
| 2448 | /* Get the string. */ |
| 2449 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2450 | |
| 2451 | /* Get and check j. */ |
| 2452 | if (lua_gettop(L) == 4) { |
| 2453 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2454 | if (j < 0) |
| 2455 | j = buf_len + j + 1; |
| 2456 | if (j > buf_len) |
| 2457 | j = buf_len + 1; |
| 2458 | lua_pop(L, 1); |
| 2459 | } |
| 2460 | else |
| 2461 | j = buf_len; |
| 2462 | |
| 2463 | /* Get and check i. */ |
| 2464 | if (lua_gettop(L) == 3) { |
| 2465 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2466 | if (i < 0) |
| 2467 | i = buf_len + i + 1; |
| 2468 | if (i > buf_len) |
| 2469 | i = buf_len + 1; |
| 2470 | lua_pop(L, 1); |
| 2471 | } else |
| 2472 | i = 1; |
| 2473 | |
| 2474 | /* Check bth i and j. */ |
| 2475 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2476 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2477 | return 1; |
| 2478 | } |
| 2479 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2480 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2481 | return 1; |
| 2482 | } |
| 2483 | if (i == 0) |
| 2484 | i = 1; |
| 2485 | if (j == 0) |
| 2486 | j = 1; |
| 2487 | |
| 2488 | /* Pop the string. */ |
| 2489 | lua_pop(L, 1); |
| 2490 | |
| 2491 | /* Update the buffer length. */ |
| 2492 | buf += i - 1; |
| 2493 | buf_len = j - i + 1; |
| 2494 | lua_pushlstring(L, buf, buf_len); |
| 2495 | |
| 2496 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2497 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2498 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2499 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2500 | } |
| 2501 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2502 | #define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345") |
Christopher Faulet | e6465b3 | 2021-10-22 15:36:08 +0200 | [diff] [blame] | 2503 | __LJMP static inline int hlua_socket_info(struct lua_State *L, const struct sockaddr_storage *addr) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2504 | { |
| 2505 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2506 | int ret; |
| 2507 | int len; |
| 2508 | char *p; |
| 2509 | |
| 2510 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2511 | if (ret <= 0) { |
| 2512 | lua_pushnil(L); |
| 2513 | return 1; |
| 2514 | } |
| 2515 | |
| 2516 | if (ret == AF_UNIX) { |
| 2517 | lua_pushstring(L, buffer+1); |
| 2518 | return 1; |
| 2519 | } |
| 2520 | else if (ret == AF_INET6) { |
| 2521 | buffer[0] = '['; |
| 2522 | len = strlen(buffer); |
| 2523 | buffer[len] = ']'; |
| 2524 | len++; |
| 2525 | buffer[len] = ':'; |
| 2526 | len++; |
| 2527 | p = buffer; |
| 2528 | } |
| 2529 | else if (ret == AF_INET) { |
| 2530 | p = buffer + 1; |
| 2531 | len = strlen(p); |
| 2532 | p[len] = ':'; |
| 2533 | len++; |
| 2534 | } |
| 2535 | else { |
| 2536 | lua_pushnil(L); |
| 2537 | return 1; |
| 2538 | } |
| 2539 | |
| 2540 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2541 | lua_pushnil(L); |
| 2542 | return 1; |
| 2543 | } |
| 2544 | |
| 2545 | lua_pushstring(L, p); |
| 2546 | return 1; |
| 2547 | } |
| 2548 | |
| 2549 | /* Returns information about the peer of the connection. */ |
| 2550 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2551 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2552 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2553 | struct xref *peer; |
| 2554 | struct appctx *appctx; |
| 2555 | struct stream_interface *si; |
Christopher Faulet | 16f16af | 2021-10-27 09:34:56 +0200 | [diff] [blame] | 2556 | const struct sockaddr_storage *dst; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2557 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2558 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2559 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2560 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2561 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2562 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2563 | /* Check if we run on the same thread than the xreator thread. |
| 2564 | * We cannot access to the socket if the thread is different. |
| 2565 | */ |
| 2566 | if (socket->tid != tid) |
| 2567 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2568 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2569 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2570 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2571 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2572 | lua_pushnil(L); |
| 2573 | return 1; |
| 2574 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2575 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Christopher Faulet | 86e1c33 | 2021-12-20 17:09:39 +0100 | [diff] [blame] | 2576 | si = cs_si(appctx->owner); |
Christopher Faulet | 16f16af | 2021-10-27 09:34:56 +0200 | [diff] [blame] | 2577 | dst = si_dst(si_opposite(si)); |
| 2578 | if (!dst) { |
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 | |
Christopher Faulet | 16f16af | 2021-10-27 09:34:56 +0200 | [diff] [blame] | 2584 | ret = MAY_LJMP(hlua_socket_info(L, dst)); |
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); |
Christopher Faulet | 86e1c33 | 2021-12-20 17:09:39 +0100 | [diff] [blame] | 2617 | si = cs_si(appctx->owner); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2618 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2619 | |
Christopher Faulet | 95a61e8 | 2021-12-22 14:22:03 +0100 | [diff] [blame] | 2620 | conn = cs_conn(s->csb); |
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); |
Christopher Faulet | 86e1c33 | 2021-12-20 17:09:39 +0100 | [diff] [blame] | 2668 | si = cs_si(appctx->owner); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 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 | |
Christopher Faulet | 95a61e8 | 2021-12-22 14:22:03 +0100 | [diff] [blame] | 2687 | appctx = cs_appctx(s->csf); |
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); |
Christopher Faulet | 86e1c33 | 2021-12-20 17:09:39 +0100 | [diff] [blame] | 2780 | si = cs_si(appctx->owner); |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2781 | s = si_strm(si); |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2782 | |
Christopher Faulet | 16f16af | 2021-10-27 09:34:56 +0200 | [diff] [blame] | 2783 | if (!sockaddr_alloc(&si_opposite(si)->dst, 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 | */ |
Christopher Faulet | 436811f | 2021-12-23 13:39:38 +0100 | [diff] [blame] | 2797 | si_cant_get(cs_si(s->csf)); |
| 2798 | si_rx_endp_more(cs_si(s->csf)); |
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); |
Christopher Faulet | 86e1c33 | 2021-12-20 17:09:39 +0100 | [diff] [blame] | 2836 | si = cs_si(appctx->owner); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 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); |
Christopher Faulet | 86e1c33 | 2021-12-20 17:09:39 +0100 | [diff] [blame] | 2892 | si = cs_si(appctx->owner); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 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; |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 2919 | struct conn_stream *cs; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2920 | struct stream *strm; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2921 | |
| 2922 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2923 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2924 | hlua_pusherror(L, "socket: full stack"); |
| 2925 | goto out_fail_conf; |
| 2926 | } |
| 2927 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2928 | /* Create the object: obj[0] = userdata. */ |
| 2929 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2930 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2931 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2932 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2933 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2934 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2935 | /* Check if the various memory pools are initialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2936 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2937 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2938 | goto out_fail_conf; |
| 2939 | } |
| 2940 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2941 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2942 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2943 | lua_setmetatable(L, -2); |
| 2944 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2945 | /* Create the applet context */ |
Willy Tarreau | e612446 | 2021-09-13 10:07:38 +0200 | [diff] [blame] | 2946 | appctx = appctx_new(&update_applet); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2947 | if (!appctx) { |
| 2948 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2949 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2950 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2951 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2952 | appctx->ctx.hlua_cosocket.connected = 0; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2953 | appctx->ctx.hlua_cosocket.die = 0; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2954 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 2955 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2956 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2957 | /* Now create a session, task and stream for this applet */ |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 2958 | sess = session_new(socket_proxy, NULL, &appctx->obj_type); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2959 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2960 | hlua_pusherror(L, "socket: out of memory"); |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 2961 | goto out_fail_appctx; |
| 2962 | } |
| 2963 | |
Christopher Faulet | cda94ac | 2021-12-23 17:28:17 +0100 | [diff] [blame^] | 2964 | cs = cs_new(); |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 2965 | if (!cs) { |
| 2966 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2967 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2968 | } |
Christopher Faulet | cda94ac | 2021-12-23 17:28:17 +0100 | [diff] [blame^] | 2969 | cs_attach_endp(cs, &appctx->obj_type, appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2970 | |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 2971 | strm = stream_new(sess, cs, &BUF_NULL); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2972 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2973 | hlua_pusherror(L, "socket: out of memory"); |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 2974 | goto out_fail_cs; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2975 | } |
| 2976 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2977 | /* Initialise cross reference between stream and Lua socket object. */ |
| 2978 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2979 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2980 | /* Configure "right" stream interface. this "si" is used to connect |
| 2981 | * and retrieve data from the server. The connection is initialized |
| 2982 | * with the "struct server". |
| 2983 | */ |
Christopher Faulet | cda94ac | 2021-12-23 17:28:17 +0100 | [diff] [blame^] | 2984 | si_set_state(strm->csb->si, SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2985 | |
| 2986 | /* Force destination server. */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2987 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED; |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 2988 | strm->target = &socket_tcp->obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2989 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2990 | return 1; |
| 2991 | |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 2992 | out_fail_cs: |
| 2993 | cs_free(cs); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2994 | out_fail_sess: |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 2995 | session_free(sess); |
| 2996 | out_fail_appctx: |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2997 | appctx_free(appctx); |
| 2998 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2999 | WILL_LJMP(lua_error(L)); |
| 3000 | return 0; |
| 3001 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 3002 | |
| 3003 | /* |
| 3004 | * |
| 3005 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3006 | * Class Channel |
| 3007 | * |
| 3008 | * |
| 3009 | */ |
| 3010 | |
| 3011 | /* Returns the struct hlua_channel join to the class channel in the |
| 3012 | * stack entry "ud" or throws an argument error. |
| 3013 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3014 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3015 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3016 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3017 | } |
| 3018 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3019 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 3020 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3021 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 3022 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3023 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3024 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3025 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3026 | return 0; |
| 3027 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3028 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3029 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3030 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3031 | |
| 3032 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3033 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 3034 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3035 | return 1; |
| 3036 | } |
| 3037 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 3038 | /* Helper function returning a filter attached to a channel at the position <ud> |
| 3039 | * 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] | 3040 | * filter is attached, NULL is returned and <offset> and <len> are not |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 3041 | * initialized. |
| 3042 | */ |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3043 | 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] | 3044 | { |
| 3045 | struct filter *filter = NULL; |
| 3046 | |
| 3047 | if (lua_getfield(L, ud, "__filter") == LUA_TLIGHTUSERDATA) { |
| 3048 | struct hlua_flt_ctx *flt_ctx; |
| 3049 | |
| 3050 | filter = lua_touserdata (L, -1); |
| 3051 | flt_ctx = filter->ctx; |
| 3052 | if (hlua_filter_from_payload(filter)) { |
| 3053 | *offset = flt_ctx->cur_off[CHN_IDX(chn)]; |
| 3054 | *len = flt_ctx->cur_len[CHN_IDX(chn)]; |
| 3055 | } |
| 3056 | } |
| 3057 | |
| 3058 | lua_pop(L, 1); |
| 3059 | return filter; |
| 3060 | } |
| 3061 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3062 | /* Copies <len> bytes of data present in the channel's buffer, starting at the |
| 3063 | * 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] | 3064 | * responsibility to ensure <len> and <offset> are valid. It always return the |
| 3065 | * length of the built string. <len> may be 0, in this case, an empty string is |
| 3066 | * created and 0 is returned. |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3067 | */ |
| 3068 | 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] | 3069 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3070 | size_t block1, block2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3071 | luaL_Buffer b; |
| 3072 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3073 | block1 = len; |
| 3074 | if (block1 > b_contig_data(&chn->buf, b_peek_ofs(&chn->buf, offset))) |
| 3075 | block1 = b_contig_data(&chn->buf, b_peek_ofs(&chn->buf, offset)); |
| 3076 | block2 = len - block1; |
| 3077 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3078 | luaL_buffinit(L, &b); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3079 | luaL_addlstring(&b, b_peek(&chn->buf, offset), block1); |
| 3080 | if (block2) |
| 3081 | luaL_addlstring(&b, b_orig(&chn->buf), block2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3082 | luaL_pushresult(&b); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3083 | return len; |
| 3084 | } |
| 3085 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3086 | /* Inserts the string <str> to the channel's buffer at the offset <offset>. This |
| 3087 | * function returns -1 if data cannot be copied. Otherwise, it returns the |
| 3088 | * number of bytes copied. |
| 3089 | */ |
| 3090 | static int _hlua_channel_insert(struct channel *chn, lua_State *L, struct ist str, size_t offset) |
| 3091 | { |
| 3092 | int ret = 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3093 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3094 | /* Nothing to do, just return */ |
| 3095 | if (unlikely(istlen(str) == 0)) |
| 3096 | goto end; |
| 3097 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3098 | if (istlen(str) > c_room(chn)) { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3099 | ret = -1; |
| 3100 | goto end; |
| 3101 | } |
| 3102 | ret = b_insert_blk(&chn->buf, offset, istptr(str), istlen(str)); |
| 3103 | |
| 3104 | end: |
| 3105 | return ret; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3106 | } |
| 3107 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3108 | /* Removes <len> bytes of data at the absolute position <offset>. |
| 3109 | */ |
| 3110 | static void _hlua_channel_delete(struct channel *chn, size_t offset, size_t len) |
| 3111 | { |
| 3112 | size_t end = offset + len; |
| 3113 | |
| 3114 | if (b_peek(&chn->buf, end) != b_tail(&chn->buf)) |
| 3115 | b_move(&chn->buf, b_peek_ofs(&chn->buf, end), |
| 3116 | b_data(&chn->buf) - end, -len); |
| 3117 | b_sub(&chn->buf, len); |
| 3118 | } |
| 3119 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3120 | /* Copies input data in the channel's buffer. It is possible to set a specific |
| 3121 | * offset (0 by default) and a length (all remaining input data starting for the |
| 3122 | * offset by default). If there is not enough input data and more data can be |
| 3123 | * received, this function yields. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3124 | * |
| 3125 | * From an action, All input data are considered. For a filter, the offset and |
| 3126 | * 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] | 3127 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3128 | __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] | 3129 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3130 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3131 | struct filter *filter; |
| 3132 | size_t input, output; |
| 3133 | int offset, len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3134 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3135 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3136 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3137 | output = co_data(chn); |
| 3138 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3139 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3140 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3141 | if (filter && !hlua_filter_from_payload(filter)) |
| 3142 | WILL_LJMP(lua_error(L)); |
| 3143 | |
| 3144 | offset = output; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3145 | if (lua_gettop(L) > 1) { |
| 3146 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3147 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3148 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3149 | offset += output; |
| 3150 | if (offset < output || offset > input + output) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3151 | lua_pushfstring(L, "offset out of range."); |
| 3152 | WILL_LJMP(lua_error(L)); |
| 3153 | } |
| 3154 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3155 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3156 | if (lua_gettop(L) == 3) { |
| 3157 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3158 | if (!len) |
| 3159 | goto dup; |
| 3160 | if (len == -1) |
| 3161 | len = global.tune.bufsize; |
| 3162 | if (len < 0) { |
| 3163 | lua_pushfstring(L, "length out of range."); |
| 3164 | WILL_LJMP(lua_error(L)); |
| 3165 | } |
| 3166 | } |
| 3167 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3168 | if (offset + len > output + input) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3169 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3170 | /* Yield waiting for more data, as requested */ |
| 3171 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_data_yield, TICK_ETERNITY, 0)); |
| 3172 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3173 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3174 | } |
| 3175 | |
| 3176 | dup: |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3177 | _hlua_channel_dup(chn, L, offset, len); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3178 | return 1; |
| 3179 | } |
| 3180 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3181 | /* Copies the first line (including the trailing LF) of input data in the |
| 3182 | * channel's buffer. It is possible to set a specific offset (0 by default) and |
| 3183 | * a length (all remaining input data starting for the offset by default). If |
| 3184 | * there is not enough input data and more data can be received, the function |
| 3185 | * yields. If a length is explicitly specified, no more data are |
| 3186 | * copied. Otherwise, if no LF is found and more data can be received, this |
| 3187 | * function yields. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3188 | * |
| 3189 | * From an action, All input data are considered. For a filter, the offset and |
| 3190 | * 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] | 3191 | */ |
| 3192 | __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] | 3193 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3194 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3195 | struct filter *filter; |
| 3196 | size_t l, input, output; |
| 3197 | int offset, len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3198 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3199 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3200 | output = co_data(chn); |
| 3201 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3202 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3203 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3204 | if (filter && !hlua_filter_from_payload(filter)) |
| 3205 | WILL_LJMP(lua_error(L)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3206 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3207 | offset = output; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3208 | if (lua_gettop(L) > 1) { |
| 3209 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3210 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3211 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3212 | offset += output; |
| 3213 | if (offset < output || offset > input + output) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3214 | lua_pushfstring(L, "offset out of range."); |
| 3215 | WILL_LJMP(lua_error(L)); |
| 3216 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3217 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3218 | |
| 3219 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3220 | if (lua_gettop(L) == 3) { |
| 3221 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3222 | if (!len) |
| 3223 | goto dup; |
| 3224 | if (len == -1) |
| 3225 | len = global.tune.bufsize; |
| 3226 | if (len < 0) { |
| 3227 | lua_pushfstring(L, "length out of range."); |
| 3228 | WILL_LJMP(lua_error(L)); |
| 3229 | } |
| 3230 | } |
| 3231 | |
| 3232 | for (l = 0; l < len; l++) { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3233 | if (l + offset >= output + input) |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3234 | break; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3235 | if (*(b_peek(&chn->buf, offset + l)) == '\n') { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3236 | len = l+1; |
| 3237 | goto dup; |
| 3238 | } |
| 3239 | } |
| 3240 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3241 | if (offset + len > output + input) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3242 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3243 | /* Yield waiting for more data */ |
| 3244 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_line_yield, TICK_ETERNITY, 0)); |
| 3245 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3246 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3247 | } |
| 3248 | |
| 3249 | dup: |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3250 | _hlua_channel_dup(chn, L, offset, len); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3251 | return 1; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3252 | } |
| 3253 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3254 | /* [ DEPRECATED ] |
| 3255 | * |
| 3256 | * Duplicate all input data foud in the channel's buffer. The data are not |
| 3257 | * removed from the buffer. This function relies on _hlua_channel_dup(). |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3258 | * |
| 3259 | * From an action, All input data are considered. For a filter, the offset and |
| 3260 | * 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] | 3261 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3262 | __LJMP static int hlua_channel_dup(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3263 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3264 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3265 | struct filter *filter; |
| 3266 | size_t offset, len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3267 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3268 | MAY_LJMP(check_args(L, 1, "dup")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3269 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3270 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3271 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3272 | WILL_LJMP(lua_error(L)); |
| 3273 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3274 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3275 | offset = co_data(chn); |
| 3276 | len = ci_data(chn); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3277 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3278 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3279 | if (filter && !hlua_filter_from_payload(filter)) |
| 3280 | WILL_LJMP(lua_error(L)); |
| 3281 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3282 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3283 | lua_pushnil(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3284 | return 1; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3285 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3286 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3287 | _hlua_channel_dup(chn, L, offset, len); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3288 | return 1; |
| 3289 | } |
| 3290 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3291 | /* [ DEPRECATED ] |
| 3292 | * |
| 3293 | * Get all input data foud in the channel's buffer. The data are removed from |
| 3294 | * the buffer after the copy. This function relies on _hlua_channel_dup() and |
| 3295 | * _hlua_channel_delete(). |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3296 | * |
| 3297 | * From an action, All input data are considered. For a filter, the offset and |
| 3298 | * 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] | 3299 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3300 | __LJMP static int hlua_channel_get(lua_State *L) |
| 3301 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3302 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3303 | struct filter *filter; |
| 3304 | size_t offset, len; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3305 | int ret; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3306 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3307 | MAY_LJMP(check_args(L, 1, "get")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3308 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3309 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3310 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3311 | WILL_LJMP(lua_error(L)); |
| 3312 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3313 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3314 | offset = co_data(chn); |
| 3315 | len = ci_data(chn); |
| 3316 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3317 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3318 | if (filter && !hlua_filter_from_payload(filter)) |
| 3319 | WILL_LJMP(lua_error(L)); |
| 3320 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3321 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3322 | lua_pushnil(L); |
| 3323 | return 1; |
| 3324 | } |
| 3325 | |
| 3326 | ret = _hlua_channel_dup(chn, L, offset, len); |
| 3327 | _hlua_channel_delete(chn, offset, ret); |
| 3328 | return 1; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3329 | } |
| 3330 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3331 | /* This functions consumes and returns one line. If the channel is closed, |
| 3332 | * 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] | 3333 | * 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] | 3334 | * value. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3335 | * |
| 3336 | * From an action, All input data are considered. For a filter, the offset and |
| 3337 | * 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] | 3338 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3339 | __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] | 3340 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3341 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3342 | struct filter *filter; |
| 3343 | size_t l, offset, len; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3344 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3345 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3346 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3347 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3348 | offset = co_data(chn); |
| 3349 | len = ci_data(chn); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3350 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3351 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3352 | if (filter && !hlua_filter_from_payload(filter)) |
| 3353 | WILL_LJMP(lua_error(L)); |
| 3354 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3355 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3356 | lua_pushnil(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3357 | return 1; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3358 | } |
| 3359 | |
| 3360 | for (l = 0; l < len; l++) { |
| 3361 | if (*(b_peek(&chn->buf, offset+l)) == '\n') { |
| 3362 | len = l+1; |
| 3363 | goto dup; |
| 3364 | } |
| 3365 | } |
| 3366 | |
| 3367 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3368 | /* Yield waiting for more data */ |
| 3369 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0)); |
| 3370 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3371 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3372 | dup: |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3373 | ret = _hlua_channel_dup(chn, L, offset, len); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3374 | _hlua_channel_delete(chn, offset, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3375 | return 1; |
| 3376 | } |
| 3377 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3378 | /* [ DEPRECATED ] |
| 3379 | * |
| 3380 | * Check arguments for the function "hlua_channel_getline_yield". |
| 3381 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3382 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 3383 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3384 | struct channel *chn; |
| 3385 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3386 | MAY_LJMP(check_args(L, 1, "getline")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3387 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3388 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3389 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3390 | WILL_LJMP(lua_error(L)); |
| 3391 | } |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3392 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 3393 | } |
| 3394 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3395 | /* Retrieves a given amount of input data at the given offset. By default all |
| 3396 | * available input data are returned. The offset may be negactive to start from |
| 3397 | * the end of input data. The length may be -1 to set it to the maximum buffer |
| 3398 | * size. |
| 3399 | */ |
| 3400 | __LJMP static int hlua_channel_get_data(lua_State *L) |
| 3401 | { |
| 3402 | struct channel *chn; |
| 3403 | |
| 3404 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3405 | WILL_LJMP(luaL_error(L, "'data' expects at most 2 arguments")); |
| 3406 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3407 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3408 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3409 | WILL_LJMP(lua_error(L)); |
| 3410 | } |
| 3411 | return MAY_LJMP(hlua_channel_get_data_yield(L, 0, 0)); |
| 3412 | } |
| 3413 | |
| 3414 | /* Retrieves a given amount of input data at the given offset. By default all |
| 3415 | * available input data are returned. The offset may be negactive to start from |
| 3416 | * the end of input data. The length may be -1 to set it to the maximum buffer |
| 3417 | * size. |
| 3418 | */ |
| 3419 | __LJMP static int hlua_channel_get_line(lua_State *L) |
| 3420 | { |
| 3421 | struct channel *chn; |
| 3422 | |
| 3423 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3424 | WILL_LJMP(luaL_error(L, "'line' expects at most 2 arguments")); |
| 3425 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3426 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3427 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3428 | WILL_LJMP(lua_error(L)); |
| 3429 | } |
| 3430 | return MAY_LJMP(hlua_channel_get_line_yield(L, 0, 0)); |
| 3431 | } |
| 3432 | |
| 3433 | /* Appends a string into the input side of channel. It returns the length of the |
| 3434 | * written string, or -1 if the channel is closed or if the buffer size is too |
| 3435 | * little for the data. 0 may be returned if nothing is copied. This function |
| 3436 | * does not yield. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3437 | * |
| 3438 | * For a filter, the context is updated on success. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3439 | */ |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3440 | __LJMP static int hlua_channel_append(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3441 | { |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3442 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3443 | struct filter *filter; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3444 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3445 | size_t sz, offset, len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3446 | int ret; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3447 | |
| 3448 | MAY_LJMP(check_args(L, 2, "append")); |
| 3449 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3450 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3451 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3452 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3453 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3454 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3455 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3456 | offset = co_data(chn); |
| 3457 | len = ci_data(chn); |
| 3458 | |
| 3459 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3460 | if (filter && !hlua_filter_from_payload(filter)) |
| 3461 | WILL_LJMP(lua_error(L)); |
| 3462 | |
| 3463 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3464 | if (ret > 0 && filter) { |
| 3465 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3466 | |
| 3467 | flt_update_offsets(filter, chn, ret); |
| 3468 | flt_ctx->cur_len[CHN_IDX(chn)] += ret; |
| 3469 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3470 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3471 | return 1; |
| 3472 | } |
| 3473 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3474 | /* Prepends a string into the input side of channel. It returns the length of the |
| 3475 | * written string, or -1 if the channel is closed or if the buffer size is too |
| 3476 | * little for the data. 0 may be returned if nothing is copied. This function |
| 3477 | * does not yield. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3478 | * |
| 3479 | * For a filter, the context is updated on success. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3480 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3481 | __LJMP static int hlua_channel_prepend(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3482 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3483 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3484 | struct filter *filter; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3485 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3486 | size_t sz, offset, len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3487 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3488 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3489 | MAY_LJMP(check_args(L, 2, "prepend")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3490 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3491 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 3492 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3493 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3494 | WILL_LJMP(lua_error(L)); |
| 3495 | } |
| 3496 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3497 | offset = co_data(chn); |
| 3498 | len = ci_data(chn); |
| 3499 | |
| 3500 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3501 | if (filter && !hlua_filter_from_payload(filter)) |
| 3502 | WILL_LJMP(lua_error(L)); |
| 3503 | |
| 3504 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3505 | if (ret > 0 && filter) { |
| 3506 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3507 | |
| 3508 | flt_update_offsets(filter, chn, ret); |
| 3509 | flt_ctx->cur_len[CHN_IDX(chn)] += ret; |
| 3510 | } |
| 3511 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3512 | lua_pushinteger(L, ret); |
| 3513 | return 1; |
| 3514 | } |
| 3515 | |
| 3516 | /* Inserts a given amount of input data at the given offset by a string |
| 3517 | * content. By default the string is appended at the end of input data. It |
| 3518 | * returns the length of the written string, or -1 if the channel is closed or |
| 3519 | * if the buffer size is too little for the data. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3520 | * |
| 3521 | * For a filter, the context is updated on success. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3522 | */ |
| 3523 | __LJMP static int hlua_channel_insert_data(lua_State *L) |
| 3524 | { |
| 3525 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3526 | struct filter *filter; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3527 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3528 | size_t sz, input, output; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3529 | int ret, offset; |
| 3530 | |
| 3531 | if (lua_gettop(L) < 2 || lua_gettop(L) > 3) |
| 3532 | WILL_LJMP(luaL_error(L, "'insert' expects at least 1 argument and at most 2 arguments")); |
| 3533 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3534 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3535 | |
| 3536 | output = co_data(chn); |
| 3537 | input = ci_data(chn); |
| 3538 | |
| 3539 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3540 | if (filter && !hlua_filter_from_payload(filter)) |
| 3541 | WILL_LJMP(lua_error(L)); |
| 3542 | |
| 3543 | offset = input + output; |
| 3544 | if (lua_gettop(L) > 2) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3545 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3546 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3547 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3548 | offset += output; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3549 | if (offset < output || offset > output + input) { |
| 3550 | lua_pushfstring(L, "offset out of range."); |
| 3551 | WILL_LJMP(lua_error(L)); |
| 3552 | } |
| 3553 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3554 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3555 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3556 | WILL_LJMP(lua_error(L)); |
| 3557 | } |
| 3558 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3559 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3560 | if (ret > 0 && filter) { |
| 3561 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3562 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3563 | flt_update_offsets(filter, chn, ret); |
| 3564 | flt_ctx->cur_len[CHN_IDX(chn)] += ret; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3565 | } |
| 3566 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3567 | lua_pushinteger(L, ret); |
| 3568 | return 1; |
| 3569 | } |
| 3570 | /* Replaces a given amount of input data at the given offset by a string |
| 3571 | * content. By default all remaining data are removed (offset = 0 and len = |
| 3572 | * -1). It returns the length of the written string, or -1 if the channel is |
| 3573 | * closed or if the buffer size is too little for the data. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3574 | * |
| 3575 | * For a filter, the context is updated on success. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3576 | */ |
| 3577 | __LJMP static int hlua_channel_set_data(lua_State *L) |
| 3578 | { |
| 3579 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3580 | struct filter *filter; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3581 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3582 | size_t sz, input, output; |
| 3583 | int ret, offset, len; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3584 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3585 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 3586 | WILL_LJMP(luaL_error(L, "'set' expects at least 1 argument and at most 3 arguments")); |
| 3587 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3588 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3589 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3590 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3591 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3592 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3593 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3594 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3595 | output = co_data(chn); |
| 3596 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3597 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3598 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3599 | if (filter && !hlua_filter_from_payload(filter)) |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3600 | WILL_LJMP(lua_error(L)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3601 | |
| 3602 | offset = output; |
| 3603 | if (lua_gettop(L) > 2) { |
| 3604 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3605 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3606 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3607 | offset += output; |
| 3608 | if (offset < output || offset > input + output) { |
| 3609 | lua_pushfstring(L, "offset out of range."); |
| 3610 | WILL_LJMP(lua_error(L)); |
| 3611 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3612 | } |
| 3613 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3614 | len = output + input - offset; |
| 3615 | if (lua_gettop(L) == 4) { |
| 3616 | len = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 3617 | if (!len) |
| 3618 | goto set; |
| 3619 | if (len == -1) |
| 3620 | len = output + input - offset; |
| 3621 | if (len < 0 || offset + len > output + input) { |
| 3622 | lua_pushfstring(L, "length out of range."); |
| 3623 | WILL_LJMP(lua_error(L)); |
| 3624 | } |
| 3625 | } |
| 3626 | |
| 3627 | set: |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3628 | /* 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] | 3629 | if (sz > c_room(chn) + len) |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3630 | lua_pushinteger(L, -1); |
| 3631 | else { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3632 | _hlua_channel_delete(chn, offset, len); |
| 3633 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3634 | if (filter) { |
| 3635 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3636 | |
| 3637 | len -= (ret > 0 ? ret : 0); |
| 3638 | flt_update_offsets(filter, chn, -len); |
| 3639 | flt_ctx->cur_len[CHN_IDX(chn)] -= len; |
| 3640 | } |
| 3641 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3642 | lua_pushinteger(L, ret); |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3643 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3644 | return 1; |
| 3645 | } |
| 3646 | |
| 3647 | /* 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] | 3648 | * 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] | 3649 | * the removed data. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3650 | * |
| 3651 | * For a filter, the context is updated on success. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3652 | */ |
| 3653 | __LJMP static int hlua_channel_del_data(lua_State *L) |
| 3654 | { |
| 3655 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3656 | struct filter *filter; |
| 3657 | size_t input, output; |
| 3658 | int offset, len; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3659 | |
| 3660 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3661 | WILL_LJMP(luaL_error(L, "'remove' expects at most 2 arguments")); |
| 3662 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3663 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3664 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3665 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3666 | WILL_LJMP(lua_error(L)); |
| 3667 | } |
| 3668 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3669 | output = co_data(chn); |
| 3670 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3671 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3672 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3673 | if (filter && !hlua_filter_from_payload(filter)) |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3674 | WILL_LJMP(lua_error(L)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3675 | |
| 3676 | offset = output; |
| 3677 | if (lua_gettop(L) > 2) { |
| 3678 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3679 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3680 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3681 | offset += output; |
| 3682 | if (offset < output || offset > input + output) { |
| 3683 | lua_pushfstring(L, "offset out of range."); |
| 3684 | WILL_LJMP(lua_error(L)); |
| 3685 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3686 | } |
| 3687 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3688 | len = output + input - offset; |
| 3689 | if (lua_gettop(L) == 4) { |
| 3690 | len = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 3691 | if (!len) |
| 3692 | goto end; |
| 3693 | if (len == -1) |
| 3694 | len = output + input - offset; |
| 3695 | if (len < 0 || offset + len > output + input) { |
| 3696 | lua_pushfstring(L, "length out of range."); |
| 3697 | WILL_LJMP(lua_error(L)); |
| 3698 | } |
| 3699 | } |
| 3700 | |
| 3701 | _hlua_channel_delete(chn, offset, len); |
| 3702 | if (filter) { |
| 3703 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3704 | |
| 3705 | flt_update_offsets(filter, chn, -len); |
| 3706 | flt_ctx->cur_len[CHN_IDX(chn)] -= len; |
| 3707 | } |
| 3708 | |
| 3709 | end: |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3710 | lua_pushinteger(L, len); |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3711 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3712 | } |
| 3713 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3714 | /* Append data in the output side of the buffer. This data is immediately |
| 3715 | * sent. The function returns the amount of data written. If the buffer |
| 3716 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3717 | * if the channel is closed. |
| 3718 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3719 | __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] | 3720 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3721 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3722 | struct filter *filter; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3723 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3724 | size_t offset, len, sz; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3725 | int l, ret; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3726 | struct hlua *hlua; |
| 3727 | |
| 3728 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3729 | hlua = hlua_gethlua(L); |
| 3730 | if (!hlua) { |
| 3731 | lua_pushnil(L); |
| 3732 | return 1; |
| 3733 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3734 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3735 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3736 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 3737 | l = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3738 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3739 | offset = co_data(chn); |
| 3740 | len = ci_data(chn); |
| 3741 | |
| 3742 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3743 | if (filter && !hlua_filter_from_payload(filter)) |
| 3744 | WILL_LJMP(lua_error(L)); |
| 3745 | |
| 3746 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3747 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3748 | lua_pushinteger(L, -1); |
| 3749 | return 1; |
| 3750 | } |
| 3751 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3752 | len = c_room(chn); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3753 | if (len > sz -l) { |
| 3754 | if (filter) { |
| 3755 | lua_pushinteger(L, -1); |
| 3756 | return 1; |
| 3757 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3758 | len = sz - l; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3759 | } |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3760 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3761 | ret = _hlua_channel_insert(chn, L, ist2(str, len), offset); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3762 | if (ret == -1) { |
| 3763 | lua_pop(L, 1); |
| 3764 | lua_pushinteger(L, -1); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3765 | return 1; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3766 | } |
| 3767 | if (ret) { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3768 | if (filter) { |
| 3769 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3770 | |
| 3771 | |
| 3772 | flt_update_offsets(filter, chn, ret); |
| 3773 | FLT_OFF(filter, chn) += ret; |
| 3774 | flt_ctx->cur_off[CHN_IDX(chn)] += ret; |
| 3775 | } |
| 3776 | else |
| 3777 | c_adv(chn, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3778 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3779 | l += ret; |
| 3780 | lua_pop(L, 1); |
| 3781 | lua_pushinteger(L, l); |
| 3782 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3783 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3784 | if (l < sz) { |
| 3785 | /* Yield only if the channel's output is not empty. |
| 3786 | * Otherwise it means we cannot add more data. */ |
| 3787 | if (co_data(chn) == 0 || HLUA_CANT_YIELD(hlua_gethlua(L))) |
Christopher Faulet | 2e60aa4 | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3788 | return 1; |
| 3789 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3790 | /* If we are waiting for space in the response buffer, we |
| 3791 | * must set the flag WAKERESWR. This flag required the task |
| 3792 | * wake up if any activity is detected on the response buffer. |
| 3793 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3794 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3795 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3796 | else |
| 3797 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3798 | 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] | 3799 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3800 | |
| 3801 | return 1; |
| 3802 | } |
| 3803 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3804 | /* Just a wrapper of "_hlua_channel_send". This wrapper permits |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3805 | * yield the LUA process, and resume it without checking the |
| 3806 | * input arguments. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3807 | * |
| 3808 | * This function cannot be called from a filter. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3809 | */ |
| 3810 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3811 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3812 | struct channel *chn; |
| 3813 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3814 | MAY_LJMP(check_args(L, 2, "send")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3815 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3816 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3817 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3818 | WILL_LJMP(lua_error(L)); |
| 3819 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3820 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3821 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3822 | } |
| 3823 | |
| 3824 | /* This function forward and amount of butes. The data pass from |
| 3825 | * the input side of the buffer to the output side, and can be |
| 3826 | * forwarded. This function never fails. |
| 3827 | * |
| 3828 | * The Lua function takes an amount of bytes to be forwarded in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3829 | * input. It returns the number of bytes forwarded. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3830 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3831 | __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] | 3832 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3833 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3834 | struct filter *filter; |
| 3835 | size_t offset, len, fwd; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3836 | int l, max; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3837 | struct hlua *hlua; |
| 3838 | |
| 3839 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3840 | hlua = hlua_gethlua(L); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3841 | if (!hlua) { |
| 3842 | lua_pushnil(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3843 | return 1; |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3844 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3845 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3846 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3847 | fwd = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3848 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3849 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3850 | offset = co_data(chn); |
| 3851 | len = ci_data(chn); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3852 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3853 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3854 | if (filter && !hlua_filter_from_payload(filter)) |
| 3855 | WILL_LJMP(lua_error(L)); |
| 3856 | |
| 3857 | max = fwd - l; |
| 3858 | if (max > len) |
| 3859 | max = len; |
| 3860 | |
| 3861 | if (filter) { |
| 3862 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3863 | |
| 3864 | FLT_OFF(filter, chn) += max; |
| 3865 | flt_ctx->cur_off[CHN_IDX(chn)] += max; |
| 3866 | flt_ctx->cur_len[CHN_IDX(chn)] -= max; |
| 3867 | } |
| 3868 | else |
| 3869 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3870 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3871 | l += max; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3872 | lua_pop(L, 1); |
| 3873 | lua_pushinteger(L, l); |
| 3874 | |
| 3875 | /* Check if it miss bytes to forward. */ |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3876 | if (l < fwd) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3877 | /* The the input channel or the output channel are closed, we |
| 3878 | * must return the amount of data forwarded. |
| 3879 | */ |
Christopher Faulet | 2e60aa4 | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3880 | 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] | 3881 | return 1; |
| 3882 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3883 | /* If we are waiting for space data in the response buffer, we |
| 3884 | * must set the flag WAKERESWR. This flag required the task |
| 3885 | * wake up if any activity is detected on the response buffer. |
| 3886 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3887 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3888 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3889 | else |
| 3890 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3891 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3892 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3893 | 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] | 3894 | } |
| 3895 | |
| 3896 | return 1; |
| 3897 | } |
| 3898 | |
| 3899 | /* Just check the input and prepare the stack for the previous |
| 3900 | * function "hlua_channel_forward_yield" |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3901 | * |
| 3902 | * This function cannot be called from a filter. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3903 | */ |
| 3904 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3905 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3906 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3907 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3908 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3909 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3910 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3911 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3912 | WILL_LJMP(lua_error(L)); |
| 3913 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3914 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3915 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3916 | } |
| 3917 | |
| 3918 | /* Just returns the number of bytes available in the input |
| 3919 | * side of the buffer. This function never fails. |
| 3920 | */ |
| 3921 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3922 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3923 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3924 | struct filter *filter; |
| 3925 | size_t output, input; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3926 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3927 | MAY_LJMP(check_args(L, 1, "input")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3928 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3929 | |
| 3930 | output = co_data(chn); |
| 3931 | input = ci_data(chn); |
| 3932 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3933 | if (filter || !IS_HTX_STRM(chn_strm(chn))) |
| 3934 | lua_pushinteger(L, input); |
| 3935 | else { |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3936 | struct htx *htx = htxbuf(&chn->buf); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3937 | |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3938 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 3939 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3940 | return 1; |
| 3941 | } |
| 3942 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3943 | /* Returns true if the channel is full. */ |
| 3944 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3945 | { |
| 3946 | struct channel *chn; |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3947 | |
| 3948 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3949 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 0ec740e | 2020-02-26 11:59:19 +0100 | [diff] [blame] | 3950 | /* ignore the reserve, we are not on a producer side (ie in an |
| 3951 | * applet). |
| 3952 | */ |
| 3953 | lua_pushboolean(L, channel_full(chn, 0)); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3954 | return 1; |
| 3955 | } |
| 3956 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3957 | /* Returns true if the channel may still receive data. */ |
| 3958 | __LJMP static int hlua_channel_may_recv(lua_State *L) |
| 3959 | { |
| 3960 | struct channel *chn; |
| 3961 | |
| 3962 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 3963 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3964 | lua_pushboolean(L, (!channel_input_closed(chn) && channel_may_recv(chn))); |
| 3965 | return 1; |
| 3966 | } |
| 3967 | |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 3968 | /* Returns true if the channel is the response channel. */ |
| 3969 | __LJMP static int hlua_channel_is_resp(lua_State *L) |
| 3970 | { |
| 3971 | struct channel *chn; |
| 3972 | |
| 3973 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 3974 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3975 | |
| 3976 | lua_pushboolean(L, !!(chn->flags & CF_ISRESP)); |
| 3977 | return 1; |
| 3978 | } |
| 3979 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3980 | /* Just returns the number of bytes available in the output |
| 3981 | * side of the buffer. This function never fails. |
| 3982 | */ |
| 3983 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 3984 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3985 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3986 | size_t output, input; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3987 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3988 | MAY_LJMP(check_args(L, 1, "output")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3989 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3990 | |
| 3991 | output = co_data(chn); |
| 3992 | input = ci_data(chn); |
| 3993 | hlua_channel_filter(L, 1, chn, &output, &input); |
| 3994 | |
| 3995 | lua_pushinteger(L, output); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3996 | return 1; |
| 3997 | } |
| 3998 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3999 | /* |
| 4000 | * |
| 4001 | * |
| 4002 | * Class Fetches |
| 4003 | * |
| 4004 | * |
| 4005 | */ |
| 4006 | |
| 4007 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4008 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4009 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4010 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4011 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4012 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4013 | } |
| 4014 | |
| 4015 | /* This function creates and push in the stack a fetch object according |
| 4016 | * with a current TXN. |
| 4017 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4018 | 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] | 4019 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4020 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4021 | |
| 4022 | /* Check stack size. */ |
| 4023 | if (!lua_checkstack(L, 3)) |
| 4024 | return 0; |
| 4025 | |
| 4026 | /* Create the object: obj[0] = userdata. |
| 4027 | * Note that the base of the Fetches object is the |
| 4028 | * transaction object. |
| 4029 | */ |
| 4030 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4031 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4032 | lua_rawseti(L, -2, 0); |
| 4033 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4034 | hsmp->s = txn->s; |
| 4035 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 4036 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4037 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4038 | |
| 4039 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 4040 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 4041 | lua_setmetatable(L, -2); |
| 4042 | |
| 4043 | return 1; |
| 4044 | } |
| 4045 | |
| 4046 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 4047 | * It uses closure argument to store the associated sample-fetch. It |
| 4048 | * returns only one argument or throws an error. An error is thrown |
| 4049 | * only if an error is encountered during the argument parsing. If |
| 4050 | * the "sample-fetch" function fails, nil is returned. |
| 4051 | */ |
| 4052 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 4053 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4054 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 4055 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 4056 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4057 | int i; |
| 4058 | struct sample smp; |
| 4059 | |
| 4060 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4061 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4062 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4063 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4064 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4065 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 4066 | /* Check execution authorization. */ |
| 4067 | if (f->use & SMP_USE_HTTP_ANY && |
| 4068 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 4069 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 4070 | "is not available in Lua services", f->kw); |
| 4071 | WILL_LJMP(lua_error(L)); |
| 4072 | } |
| 4073 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4074 | /* Get extra arguments. */ |
| 4075 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 4076 | if (i >= ARGM_NBARGS) |
| 4077 | break; |
| 4078 | hlua_lua2arg(L, i + 2, &args[i]); |
| 4079 | } |
| 4080 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4081 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4082 | |
| 4083 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4084 | 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] | 4085 | |
| 4086 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 4087 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4088 | lua_pushfstring(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4089 | goto error; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4090 | } |
| 4091 | |
| 4092 | /* Initialise the sample. */ |
| 4093 | memset(&smp, 0, sizeof(smp)); |
| 4094 | |
| 4095 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 4096 | 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] | 4097 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4098 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4099 | lua_pushstring(L, ""); |
| 4100 | else |
| 4101 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4102 | goto end; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4103 | } |
| 4104 | |
| 4105 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4106 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4107 | hlua_smp2lua_str(L, &smp); |
| 4108 | else |
| 4109 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4110 | |
| 4111 | end: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4112 | free_args(args); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4113 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4114 | |
| 4115 | error: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4116 | free_args(args); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4117 | WILL_LJMP(lua_error(L)); |
| 4118 | return 0; /* Never reached */ |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4119 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4120 | |
| 4121 | /* |
| 4122 | * |
| 4123 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4124 | * Class Converters |
| 4125 | * |
| 4126 | * |
| 4127 | */ |
| 4128 | |
| 4129 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4130 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4131 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4132 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4133 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4134 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4135 | } |
| 4136 | |
| 4137 | /* This function creates and push in the stack a Converters object |
| 4138 | * according with a current TXN. |
| 4139 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4140 | 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] | 4141 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4142 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4143 | |
| 4144 | /* Check stack size. */ |
| 4145 | if (!lua_checkstack(L, 3)) |
| 4146 | return 0; |
| 4147 | |
| 4148 | /* Create the object: obj[0] = userdata. |
| 4149 | * Note that the base of the Converters object is the |
| 4150 | * same than the TXN object. |
| 4151 | */ |
| 4152 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4153 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4154 | lua_rawseti(L, -2, 0); |
| 4155 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4156 | hsmp->s = txn->s; |
| 4157 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 4158 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4159 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4160 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4161 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4162 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 4163 | lua_setmetatable(L, -2); |
| 4164 | |
| 4165 | return 1; |
| 4166 | } |
| 4167 | |
| 4168 | /* This function is an LUA binding. It is called with each converter. |
| 4169 | * It uses closure argument to store the associated converter. It |
| 4170 | * returns only one argument or throws an error. An error is thrown |
| 4171 | * only if an error is encountered during the argument parsing. If |
| 4172 | * the converter function function fails, nil is returned. |
| 4173 | */ |
| 4174 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 4175 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 4176 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4177 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 4178 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4179 | int i; |
| 4180 | struct sample smp; |
| 4181 | |
| 4182 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4183 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4184 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4185 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 4186 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4187 | |
| 4188 | /* Get extra arguments. */ |
| 4189 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 4190 | if (i >= ARGM_NBARGS) |
| 4191 | break; |
| 4192 | hlua_lua2arg(L, i + 3, &args[i]); |
| 4193 | } |
| 4194 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4195 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4196 | |
| 4197 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 4198 | 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] | 4199 | |
| 4200 | /* Run the special args checker. */ |
| 4201 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 4202 | hlua_pusherror(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4203 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4204 | } |
| 4205 | |
| 4206 | /* Initialise the sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4207 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4208 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 4209 | hlua_pusherror(L, "error in the input argument"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4210 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4211 | } |
| 4212 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 4213 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 4214 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4215 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4216 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4217 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4218 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4219 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4220 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4221 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 4222 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4223 | hlua_pusherror(L, "error during the input argument casting"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4224 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4225 | } |
| 4226 | |
| 4227 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4228 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4229 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4230 | lua_pushstring(L, ""); |
| 4231 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 4232 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4233 | goto end; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4234 | } |
| 4235 | |
| 4236 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4237 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4238 | hlua_smp2lua_str(L, &smp); |
| 4239 | else |
| 4240 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4241 | end: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4242 | free_args(args); |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 4243 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4244 | |
| 4245 | error: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4246 | free_args(args); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4247 | WILL_LJMP(lua_error(L)); |
| 4248 | return 0; /* Never reached */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4249 | } |
| 4250 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4251 | /* |
| 4252 | * |
| 4253 | * |
| 4254 | * Class AppletTCP |
| 4255 | * |
| 4256 | * |
| 4257 | */ |
| 4258 | |
| 4259 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4260 | * a class stream, otherwise it throws an error. |
| 4261 | */ |
| 4262 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 4263 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4264 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4265 | } |
| 4266 | |
| 4267 | /* This function creates and push in the stack an Applet object |
| 4268 | * according with a current TXN. |
| 4269 | */ |
| 4270 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 4271 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4272 | struct hlua_appctx *luactx; |
Christopher Faulet | 86e1c33 | 2021-12-20 17:09:39 +0100 | [diff] [blame] | 4273 | struct stream_interface *si = cs_si(ctx->owner); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4274 | struct stream *s = si_strm(si); |
| 4275 | struct proxy *p = s->be; |
| 4276 | |
| 4277 | /* Check stack size. */ |
| 4278 | if (!lua_checkstack(L, 3)) |
| 4279 | return 0; |
| 4280 | |
| 4281 | /* Create the object: obj[0] = userdata. |
| 4282 | * Note that the base of the Converters object is the |
| 4283 | * same than the TXN object. |
| 4284 | */ |
| 4285 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4286 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4287 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4288 | luactx->appctx = ctx; |
| 4289 | luactx->htxn.s = s; |
| 4290 | luactx->htxn.p = p; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4291 | |
| 4292 | /* Create the "f" field that contains a list of fetches. */ |
| 4293 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4294 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4295 | return 0; |
| 4296 | lua_settable(L, -3); |
| 4297 | |
| 4298 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4299 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4300 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4301 | return 0; |
| 4302 | lua_settable(L, -3); |
| 4303 | |
| 4304 | /* Create the "c" field that contains a list of converters. */ |
| 4305 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4306 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4307 | return 0; |
| 4308 | lua_settable(L, -3); |
| 4309 | |
| 4310 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4311 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4312 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4313 | return 0; |
| 4314 | lua_settable(L, -3); |
| 4315 | |
| 4316 | /* Pop a class stream metatable and affect it to the table. */ |
| 4317 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 4318 | lua_setmetatable(L, -2); |
| 4319 | |
| 4320 | return 1; |
| 4321 | } |
| 4322 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4323 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 4324 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4325 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4326 | struct stream *s; |
| 4327 | const char *name; |
| 4328 | size_t len; |
| 4329 | struct sample smp; |
| 4330 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4331 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 4332 | 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] | 4333 | |
| 4334 | /* It is useles to retrieve the stream, but this function |
| 4335 | * runs only in a stream context. |
| 4336 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4337 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4338 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4339 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4340 | |
| 4341 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4342 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4343 | hlua_lua2smp(L, 3, &smp); |
| 4344 | |
| 4345 | /* Store the sample in a variable. */ |
| 4346 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4347 | |
| 4348 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 4349 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 4350 | else |
| 4351 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 4352 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4353 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4354 | } |
| 4355 | |
| 4356 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 4357 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4358 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4359 | struct stream *s; |
| 4360 | const char *name; |
| 4361 | size_t len; |
| 4362 | struct sample smp; |
| 4363 | |
| 4364 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4365 | |
| 4366 | /* It is useles to retrieve the stream, but this function |
| 4367 | * runs only in a stream context. |
| 4368 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4369 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4370 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4371 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4372 | |
| 4373 | /* Unset the variable. */ |
| 4374 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4375 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4376 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4377 | } |
| 4378 | |
| 4379 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 4380 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4381 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4382 | struct stream *s; |
| 4383 | const char *name; |
| 4384 | size_t len; |
| 4385 | struct sample smp; |
| 4386 | |
| 4387 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4388 | |
| 4389 | /* It is useles to retrieve the stream, but this function |
| 4390 | * runs only in a stream context. |
| 4391 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4392 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4393 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4394 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4395 | |
| 4396 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Willy Tarreau | e352b9d | 2021-09-03 11:52:38 +0200 | [diff] [blame] | 4397 | if (!vars_get_by_name(name, len, &smp, NULL)) { |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4398 | lua_pushnil(L); |
| 4399 | return 1; |
| 4400 | } |
| 4401 | |
| 4402 | return hlua_smp2lua(L, &smp); |
| 4403 | } |
| 4404 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4405 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 4406 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4407 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4408 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4409 | struct hlua *hlua; |
| 4410 | |
| 4411 | /* 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] | 4412 | if (!s->hlua) |
| 4413 | return 0; |
| 4414 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4415 | |
| 4416 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4417 | |
| 4418 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4419 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4420 | |
| 4421 | /* Get and store new value. */ |
| 4422 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4423 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4424 | |
| 4425 | return 0; |
| 4426 | } |
| 4427 | |
| 4428 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 4429 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4430 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4431 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4432 | struct hlua *hlua; |
| 4433 | |
| 4434 | /* 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] | 4435 | if (!s->hlua) { |
| 4436 | lua_pushnil(L); |
| 4437 | return 1; |
| 4438 | } |
| 4439 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4440 | |
| 4441 | /* Push configuration index in the stack. */ |
| 4442 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4443 | |
| 4444 | return 1; |
| 4445 | } |
| 4446 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4447 | /* If expected data not yet available, it returns a yield. This function |
| 4448 | * consumes the data in the buffer. It returns a string containing the |
| 4449 | * data. This string can be empty. |
| 4450 | */ |
| 4451 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 4452 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4453 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Christopher Faulet | 86e1c33 | 2021-12-20 17:09:39 +0100 | [diff] [blame] | 4454 | struct stream_interface *si = cs_si(luactx->appctx->owner); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4455 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4456 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4457 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4458 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4459 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4460 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4461 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4462 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4463 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4464 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4465 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4466 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4467 | 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] | 4468 | } |
| 4469 | |
| 4470 | /* End of data: commit the total strings and return. */ |
| 4471 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4472 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4473 | return 1; |
| 4474 | } |
| 4475 | |
| 4476 | /* Ensure that the block 2 length is usable. */ |
| 4477 | if (ret == 1) |
| 4478 | len2 = 0; |
| 4479 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 4480 | /* don't check the max length read and don't check. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4481 | luaL_addlstring(&luactx->b, blk1, len1); |
| 4482 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4483 | |
| 4484 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4485 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4486 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4487 | return 1; |
| 4488 | } |
| 4489 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4490 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4491 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 4492 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4493 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4494 | |
| 4495 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4496 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4497 | |
| 4498 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 4499 | } |
| 4500 | |
| 4501 | /* If expected data not yet available, it returns a yield. This function |
| 4502 | * consumes the data in the buffer. It returns a string containing the |
| 4503 | * data. This string can be empty. |
| 4504 | */ |
| 4505 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 4506 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4507 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Christopher Faulet | 86e1c33 | 2021-12-20 17:09:39 +0100 | [diff] [blame] | 4508 | struct stream_interface *si = cs_si(luactx->appctx->owner); |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4509 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4510 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4511 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4512 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4513 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4514 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4515 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4516 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4517 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4518 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4519 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4520 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4521 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4522 | 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] | 4523 | } |
| 4524 | |
| 4525 | /* End of data: commit the total strings and return. */ |
| 4526 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4527 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4528 | return 1; |
| 4529 | } |
| 4530 | |
| 4531 | /* Ensure that the block 2 length is usable. */ |
| 4532 | if (ret == 1) |
| 4533 | len2 = 0; |
| 4534 | |
| 4535 | if (len == -1) { |
| 4536 | |
| 4537 | /* If len == -1, catenate all the data avalaile and |
| 4538 | * yield because we want to get all the data until |
| 4539 | * the end of data stream. |
| 4540 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4541 | luaL_addlstring(&luactx->b, blk1, len1); |
| 4542 | luaL_addlstring(&luactx->b, blk2, len2); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4543 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4544 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4545 | 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] | 4546 | |
| 4547 | } else { |
| 4548 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4549 | /* Copy the first block caping to the length required. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4550 | if (len1 > len) |
| 4551 | len1 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4552 | luaL_addlstring(&luactx->b, blk1, len1); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4553 | len -= len1; |
| 4554 | |
| 4555 | /* Copy the second block. */ |
| 4556 | if (len2 > len) |
| 4557 | len2 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4558 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4559 | len -= len2; |
| 4560 | |
| 4561 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4562 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4563 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4564 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4565 | if (len > 0) { |
| 4566 | lua_pushinteger(L, len); |
| 4567 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4568 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4569 | 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] | 4570 | } |
| 4571 | |
| 4572 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4573 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4574 | return 1; |
| 4575 | } |
| 4576 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4577 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4578 | hlua_pusherror(L, "Lua: internal error"); |
| 4579 | WILL_LJMP(lua_error(L)); |
| 4580 | return 0; |
| 4581 | } |
| 4582 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4583 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4584 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 4585 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4586 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4587 | int len = -1; |
| 4588 | |
| 4589 | if (lua_gettop(L) > 2) |
| 4590 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4591 | if (lua_gettop(L) >= 2) { |
| 4592 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4593 | lua_pop(L, 1); |
| 4594 | } |
| 4595 | |
| 4596 | /* Confirm or set the required length */ |
| 4597 | lua_pushinteger(L, len); |
| 4598 | |
| 4599 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4600 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4601 | |
| 4602 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 4603 | } |
| 4604 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4605 | /* Append data in the output side of the buffer. This data is immediately |
| 4606 | * sent. The function returns the amount of data written. If the buffer |
| 4607 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4608 | * if the channel is closed. |
| 4609 | */ |
| 4610 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4611 | { |
| 4612 | size_t len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4613 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4614 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4615 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | 86e1c33 | 2021-12-20 17:09:39 +0100 | [diff] [blame] | 4616 | struct stream_interface *si = cs_si(luactx->appctx->owner); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4617 | struct channel *chn = si_ic(si); |
| 4618 | int max; |
| 4619 | |
| 4620 | /* Get the max amount of data which can write as input in the channel. */ |
| 4621 | max = channel_recv_max(chn); |
| 4622 | if (max > (len - l)) |
| 4623 | max = len - l; |
| 4624 | |
| 4625 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4626 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4627 | |
| 4628 | /* update counters. */ |
| 4629 | l += max; |
| 4630 | lua_pop(L, 1); |
| 4631 | lua_pushinteger(L, l); |
| 4632 | |
| 4633 | /* If some data is not send, declares the situation to the |
| 4634 | * applet, and returns a yield. |
| 4635 | */ |
| 4636 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 4637 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4638 | 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] | 4639 | } |
| 4640 | |
| 4641 | return 1; |
| 4642 | } |
| 4643 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4644 | /* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4645 | * yield the LUA process, and resume it without checking the |
| 4646 | * input arguments. |
| 4647 | */ |
| 4648 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 4649 | { |
| 4650 | MAY_LJMP(check_args(L, 2, "send")); |
| 4651 | lua_pushinteger(L, 0); |
| 4652 | |
| 4653 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 4654 | } |
| 4655 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4656 | /* |
| 4657 | * |
| 4658 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4659 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4660 | * |
| 4661 | * |
| 4662 | */ |
| 4663 | |
| 4664 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4665 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4666 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4667 | __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] | 4668 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4669 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4670 | } |
| 4671 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4672 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4673 | * according with a current TXN. |
| 4674 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4675 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4676 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4677 | struct hlua_appctx *luactx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 4678 | struct hlua_txn htxn; |
Christopher Faulet | 86e1c33 | 2021-12-20 17:09:39 +0100 | [diff] [blame] | 4679 | struct stream_interface *si = cs_si(ctx->owner); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4680 | struct stream *s = si_strm(si); |
| 4681 | struct proxy *px = s->be; |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4682 | struct htx *htx; |
| 4683 | struct htx_blk *blk; |
| 4684 | struct htx_sl *sl; |
| 4685 | struct ist path; |
| 4686 | unsigned long long len = 0; |
| 4687 | int32_t pos; |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 4688 | struct http_uri_parser parser; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4689 | |
| 4690 | /* Check stack size. */ |
| 4691 | if (!lua_checkstack(L, 3)) |
| 4692 | return 0; |
| 4693 | |
| 4694 | /* Create the object: obj[0] = userdata. |
| 4695 | * Note that the base of the Converters object is the |
| 4696 | * same than the TXN object. |
| 4697 | */ |
| 4698 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4699 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4700 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4701 | luactx->appctx = ctx; |
| 4702 | luactx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
| 4703 | luactx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */ |
| 4704 | luactx->htxn.s = s; |
| 4705 | luactx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4706 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4707 | /* Create the "f" field that contains a list of fetches. */ |
| 4708 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4709 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4710 | return 0; |
| 4711 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4712 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4713 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4714 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4715 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4716 | return 0; |
| 4717 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4718 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4719 | /* Create the "c" field that contains a list of converters. */ |
| 4720 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4721 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4722 | return 0; |
| 4723 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4724 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4725 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4726 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4727 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4728 | return 0; |
| 4729 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4730 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4731 | htx = htxbuf(&s->req.buf); |
| 4732 | blk = htx_get_first_blk(htx); |
Christopher Faulet | ea00973 | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 4733 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4734 | sl = htx_get_blk_ptr(htx, blk); |
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 | /* Stores the request method. */ |
| 4737 | lua_pushstring(L, "method"); |
| 4738 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 4739 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4740 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4741 | /* Stores the http version. */ |
| 4742 | lua_pushstring(L, "version"); |
| 4743 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 4744 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4745 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4746 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 4747 | * the array on the top of the stack. |
| 4748 | */ |
| 4749 | lua_pushstring(L, "headers"); |
| 4750 | htxn.s = s; |
| 4751 | htxn.p = px; |
| 4752 | htxn.dir = SMP_OPT_DIR_REQ; |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4753 | if (!hlua_http_get_headers(L, &htxn.s->txn->req)) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4754 | return 0; |
| 4755 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4756 | |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 4757 | parser = http_uri_parser_init(htx_sl_req_uri(sl)); |
| 4758 | path = http_parse_path(&parser); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 4759 | if (isttest(path)) { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4760 | char *p, *q, *end; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4761 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4762 | p = path.ptr; |
Tim Duesterhus | 4c8f75f | 2021-11-06 15:14:44 +0100 | [diff] [blame] | 4763 | end = istend(path); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4764 | q = p; |
| 4765 | while (q < end && *q != '?') |
| 4766 | q++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4767 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4768 | /* Stores the request path. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4769 | lua_pushstring(L, "path"); |
| 4770 | lua_pushlstring(L, p, q - p); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4771 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4772 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4773 | /* Stores the query string. */ |
| 4774 | lua_pushstring(L, "qs"); |
| 4775 | if (*q == '?') |
| 4776 | q++; |
| 4777 | lua_pushlstring(L, q, end - q); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4778 | lua_settable(L, -3); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4779 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4780 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4781 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4782 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4783 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4784 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4785 | if (type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4786 | break; |
| 4787 | if (type == HTX_BLK_DATA) |
| 4788 | len += htx_get_blksz(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4789 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4790 | if (htx->extra != ULLONG_MAX) |
| 4791 | len += htx->extra; |
| 4792 | |
| 4793 | /* Stores the request path. */ |
| 4794 | lua_pushstring(L, "length"); |
| 4795 | lua_pushinteger(L, len); |
| 4796 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4797 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4798 | /* Create an empty array of HTTP request headers. */ |
| 4799 | lua_pushstring(L, "response"); |
| 4800 | lua_newtable(L); |
| 4801 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4802 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4803 | /* Pop a class stream metatable and affect it to the table. */ |
| 4804 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 4805 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4806 | |
| 4807 | return 1; |
| 4808 | } |
| 4809 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4810 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 4811 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4812 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4813 | struct stream *s; |
| 4814 | const char *name; |
| 4815 | size_t len; |
| 4816 | struct sample smp; |
| 4817 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4818 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 4819 | 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] | 4820 | |
| 4821 | /* It is useles to retrieve the stream, but this function |
| 4822 | * runs only in a stream context. |
| 4823 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4824 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4825 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4826 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4827 | |
| 4828 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4829 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4830 | hlua_lua2smp(L, 3, &smp); |
| 4831 | |
| 4832 | /* Store the sample in a variable. */ |
| 4833 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4834 | |
| 4835 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 4836 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 4837 | else |
| 4838 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 4839 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4840 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4841 | } |
| 4842 | |
| 4843 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 4844 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4845 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4846 | struct stream *s; |
| 4847 | const char *name; |
| 4848 | size_t len; |
| 4849 | struct sample smp; |
| 4850 | |
| 4851 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4852 | |
| 4853 | /* It is useles to retrieve the stream, but this function |
| 4854 | * runs only in a stream context. |
| 4855 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4856 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4857 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4858 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4859 | |
| 4860 | /* Unset the variable. */ |
| 4861 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4862 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4863 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4864 | } |
| 4865 | |
| 4866 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4867 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4868 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4869 | struct stream *s; |
| 4870 | const char *name; |
| 4871 | size_t len; |
| 4872 | struct sample smp; |
| 4873 | |
| 4874 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4875 | |
| 4876 | /* It is useles to retrieve the stream, but this function |
| 4877 | * runs only in a stream context. |
| 4878 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4879 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4880 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4881 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4882 | |
| 4883 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Willy Tarreau | e352b9d | 2021-09-03 11:52:38 +0200 | [diff] [blame] | 4884 | if (!vars_get_by_name(name, len, &smp, NULL)) { |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4885 | lua_pushnil(L); |
| 4886 | return 1; |
| 4887 | } |
| 4888 | |
| 4889 | return hlua_smp2lua(L, &smp); |
| 4890 | } |
| 4891 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4892 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 4893 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4894 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4895 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4896 | struct hlua *hlua; |
| 4897 | |
| 4898 | /* 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] | 4899 | if (!s->hlua) |
| 4900 | return 0; |
| 4901 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4902 | |
| 4903 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4904 | |
| 4905 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4906 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4907 | |
| 4908 | /* Get and store new value. */ |
| 4909 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4910 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4911 | |
| 4912 | return 0; |
| 4913 | } |
| 4914 | |
| 4915 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4916 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4917 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4918 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4919 | struct hlua *hlua; |
| 4920 | |
| 4921 | /* 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] | 4922 | if (!s->hlua) { |
| 4923 | lua_pushnil(L); |
| 4924 | return 1; |
| 4925 | } |
| 4926 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4927 | |
| 4928 | /* Push configuration index in the stack. */ |
| 4929 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4930 | |
| 4931 | return 1; |
| 4932 | } |
| 4933 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4934 | /* If expected data not yet available, it returns a yield. This function |
| 4935 | * consumes the data in the buffer. It returns a string containing the |
| 4936 | * data. This string can be empty. |
| 4937 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4938 | __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] | 4939 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4940 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Christopher Faulet | 86e1c33 | 2021-12-20 17:09:39 +0100 | [diff] [blame] | 4941 | struct stream_interface *si = cs_si(luactx->appctx->owner); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4942 | struct channel *req = si_oc(si); |
| 4943 | struct htx *htx; |
| 4944 | struct htx_blk *blk; |
| 4945 | size_t count; |
| 4946 | int stop = 0; |
| 4947 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4948 | htx = htx_from_buf(&req->buf); |
| 4949 | count = co_data(req); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4950 | blk = htx_get_first_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 4951 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4952 | while (count && !stop && blk) { |
| 4953 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4954 | uint32_t sz = htx_get_blksz(blk); |
| 4955 | struct ist v; |
| 4956 | uint32_t vlen; |
| 4957 | char *nl; |
| 4958 | |
| 4959 | vlen = sz; |
| 4960 | if (vlen > count) { |
| 4961 | if (type != HTX_BLK_DATA) |
| 4962 | break; |
| 4963 | vlen = count; |
| 4964 | } |
| 4965 | |
| 4966 | switch (type) { |
| 4967 | case HTX_BLK_UNUSED: |
| 4968 | break; |
| 4969 | |
| 4970 | case HTX_BLK_DATA: |
| 4971 | v = htx_get_blk_value(htx, blk); |
| 4972 | v.len = vlen; |
| 4973 | nl = istchr(v, '\n'); |
| 4974 | if (nl != NULL) { |
| 4975 | stop = 1; |
| 4976 | vlen = nl - v.ptr + 1; |
| 4977 | } |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4978 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4979 | break; |
| 4980 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4981 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4982 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4983 | stop = 1; |
| 4984 | break; |
| 4985 | |
| 4986 | default: |
| 4987 | break; |
| 4988 | } |
| 4989 | |
| 4990 | co_set_data(req, co_data(req) - vlen); |
| 4991 | count -= vlen; |
| 4992 | if (sz == vlen) |
| 4993 | blk = htx_remove_blk(htx, blk); |
| 4994 | else { |
| 4995 | htx_cut_data_blk(htx, blk, vlen); |
| 4996 | break; |
| 4997 | } |
| 4998 | } |
| 4999 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 5000 | /* The message was fully consumed and no more data are expected |
| 5001 | * (EOM flag set). |
| 5002 | */ |
| 5003 | if (htx_is_empty(htx) && (htx->flags & HTX_FL_EOM)) |
| 5004 | stop = 1; |
| 5005 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 5006 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5007 | if (!stop) { |
| 5008 | si_cant_get(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5009 | 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] | 5010 | } |
| 5011 | |
| 5012 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5013 | luaL_pushresult(&luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5014 | return 1; |
| 5015 | } |
| 5016 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5017 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5018 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5019 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5020 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5021 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5022 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5023 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5024 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5025 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5026 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5027 | } |
| 5028 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5029 | /* If expected data not yet available, it returns a yield. This function |
| 5030 | * consumes the data in the buffer. It returns a string containing the |
| 5031 | * data. This string can be empty. |
| 5032 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5033 | __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] | 5034 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5035 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Christopher Faulet | 86e1c33 | 2021-12-20 17:09:39 +0100 | [diff] [blame] | 5036 | struct stream_interface *si = cs_si(luactx->appctx->owner); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5037 | struct channel *req = si_oc(si); |
| 5038 | struct htx *htx; |
| 5039 | struct htx_blk *blk; |
| 5040 | size_t count; |
| 5041 | int len; |
| 5042 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5043 | htx = htx_from_buf(&req->buf); |
| 5044 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5045 | count = co_data(req); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 5046 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5047 | while (count && len && blk) { |
| 5048 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5049 | uint32_t sz = htx_get_blksz(blk); |
| 5050 | struct ist v; |
| 5051 | uint32_t vlen; |
| 5052 | |
| 5053 | vlen = sz; |
| 5054 | if (len > 0 && vlen > len) |
| 5055 | vlen = len; |
| 5056 | if (vlen > count) { |
| 5057 | if (type != HTX_BLK_DATA) |
| 5058 | break; |
| 5059 | vlen = count; |
| 5060 | } |
| 5061 | |
| 5062 | switch (type) { |
| 5063 | case HTX_BLK_UNUSED: |
| 5064 | break; |
| 5065 | |
| 5066 | case HTX_BLK_DATA: |
| 5067 | v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5068 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5069 | break; |
| 5070 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5071 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5072 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5073 | len = 0; |
| 5074 | break; |
| 5075 | |
| 5076 | default: |
| 5077 | break; |
| 5078 | } |
| 5079 | |
| 5080 | co_set_data(req, co_data(req) - vlen); |
| 5081 | count -= vlen; |
| 5082 | if (len > 0) |
| 5083 | len -= vlen; |
| 5084 | if (sz == vlen) |
| 5085 | blk = htx_remove_blk(htx, blk); |
| 5086 | else { |
| 5087 | htx_cut_data_blk(htx, blk, vlen); |
| 5088 | break; |
| 5089 | } |
| 5090 | } |
| 5091 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 5092 | /* The message was fully consumed and no more data are expected |
| 5093 | * (EOM flag set). |
| 5094 | */ |
| 5095 | if (htx_is_empty(htx) && (htx->flags & HTX_FL_EOM)) |
| 5096 | len = 0; |
| 5097 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 5098 | htx_to_buf(htx, &req->buf); |
| 5099 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5100 | /* If we are no other data available, yield waiting for new data. */ |
| 5101 | if (len) { |
| 5102 | if (len > 0) { |
| 5103 | lua_pushinteger(L, len); |
| 5104 | lua_replace(L, 2); |
| 5105 | } |
| 5106 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5107 | 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] | 5108 | } |
| 5109 | |
| 5110 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5111 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5112 | return 1; |
| 5113 | } |
| 5114 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5115 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5116 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 5117 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5118 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5119 | int len = -1; |
| 5120 | |
| 5121 | /* Check arguments. */ |
| 5122 | if (lua_gettop(L) > 2) |
| 5123 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 5124 | if (lua_gettop(L) >= 2) { |
| 5125 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5126 | lua_pop(L, 1); |
| 5127 | } |
| 5128 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5129 | lua_pushinteger(L, len); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5130 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5131 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5132 | luaL_buffinit(L, &luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5133 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5134 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5135 | } |
| 5136 | |
| 5137 | /* Append data in the output side of the buffer. This data is immediately |
| 5138 | * sent. The function returns the amount of data written. If the buffer |
| 5139 | * cannot contain the data, the function yields. The function returns -1 |
| 5140 | * if the channel is closed. |
| 5141 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5142 | __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] | 5143 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5144 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Christopher Faulet | 86e1c33 | 2021-12-20 17:09:39 +0100 | [diff] [blame] | 5145 | struct stream_interface *si = cs_si(luactx->appctx->owner); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5146 | struct channel *res = si_ic(si); |
| 5147 | struct htx *htx = htx_from_buf(&res->buf); |
| 5148 | const char *data; |
| 5149 | size_t len; |
| 5150 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 5151 | int max; |
| 5152 | |
Christopher Faulet | 9060fc0 | 2019-07-03 11:39:30 +0200 | [diff] [blame] | 5153 | max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx)); |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 5154 | if (!max) |
| 5155 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5156 | |
| 5157 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5158 | |
| 5159 | /* Get the max amount of data which can write as input in the channel. */ |
| 5160 | if (max > (len - l)) |
| 5161 | max = len - l; |
| 5162 | |
| 5163 | /* Copy data. */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 5164 | max = htx_add_data(htx, ist2(data + l, max)); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 5165 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5166 | |
| 5167 | /* update counters. */ |
| 5168 | l += max; |
| 5169 | lua_pop(L, 1); |
| 5170 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5171 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5172 | /* If some data is not send, declares the situation to the |
| 5173 | * applet, and returns a yield. |
| 5174 | */ |
| 5175 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 5176 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 5177 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5178 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5179 | 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] | 5180 | } |
| 5181 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5182 | htx_to_buf(htx, &res->buf); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5183 | return 1; |
| 5184 | } |
| 5185 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5186 | /* Just a wrapper of "hlua_applet_send_yield". This wrapper permits |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5187 | * yield the LUA process, and resume it without checking the |
| 5188 | * input arguments. |
| 5189 | */ |
| 5190 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 5191 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5192 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5193 | |
| 5194 | /* We want to send some data. Headers must be sent. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5195 | if (!(luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5196 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 5197 | WILL_LJMP(lua_error(L)); |
| 5198 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5199 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5200 | /* This integer is used for followinf the amount of data sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5201 | lua_pushinteger(L, 0); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5202 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5203 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5204 | } |
| 5205 | |
| 5206 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 5207 | { |
| 5208 | const char *name; |
| 5209 | int ret; |
| 5210 | |
| 5211 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 5212 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5213 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 5214 | |
| 5215 | /* Push in the stack the "response" entry. */ |
| 5216 | ret = lua_getfield(L, 1, "response"); |
| 5217 | if (ret != LUA_TTABLE) { |
| 5218 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 5219 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 5220 | WILL_LJMP(lua_error(L)); |
| 5221 | } |
| 5222 | |
| 5223 | /* check if the header is already registered if it is not |
| 5224 | * the case, register it. |
| 5225 | */ |
| 5226 | ret = lua_getfield(L, -1, name); |
| 5227 | if (ret == LUA_TNIL) { |
| 5228 | |
| 5229 | /* Entry not found. */ |
| 5230 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 5231 | |
| 5232 | /* Insert the new header name in the array in the top of the stack. |
| 5233 | * It left the new array in the top of the stack. |
| 5234 | */ |
| 5235 | lua_newtable(L); |
| 5236 | lua_pushvalue(L, 2); |
| 5237 | lua_pushvalue(L, -2); |
| 5238 | lua_settable(L, -4); |
| 5239 | |
| 5240 | } else if (ret != LUA_TTABLE) { |
| 5241 | |
| 5242 | /* corruption error. */ |
| 5243 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 5244 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 5245 | WILL_LJMP(lua_error(L)); |
| 5246 | } |
| 5247 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 5248 | /* Now the top of thestack is an array of values. We push |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5249 | * the header value as new entry. |
| 5250 | */ |
| 5251 | lua_pushvalue(L, 3); |
| 5252 | ret = lua_rawlen(L, -2); |
| 5253 | lua_rawseti(L, -2, ret + 1); |
| 5254 | lua_pushboolean(L, 1); |
| 5255 | return 1; |
| 5256 | } |
| 5257 | |
| 5258 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 5259 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5260 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5261 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5262 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5263 | |
| 5264 | if (status < 100 || status > 599) { |
| 5265 | lua_pushboolean(L, 0); |
| 5266 | return 1; |
| 5267 | } |
| 5268 | |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5269 | luactx->appctx->ctx.hlua_apphttp.status = status; |
| 5270 | luactx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5271 | lua_pushboolean(L, 1); |
| 5272 | return 1; |
| 5273 | } |
| 5274 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5275 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5276 | __LJMP static int hlua_applet_http_send_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5277 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5278 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Christopher Faulet | 86e1c33 | 2021-12-20 17:09:39 +0100 | [diff] [blame] | 5279 | struct stream_interface *si = cs_si(luactx->appctx->owner); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5280 | struct channel *res = si_ic(si); |
| 5281 | struct htx *htx; |
| 5282 | struct htx_sl *sl; |
| 5283 | struct h1m h1m; |
| 5284 | const char *status, *reason; |
| 5285 | const char *name, *value; |
| 5286 | size_t nlen, vlen; |
| 5287 | unsigned int flags; |
| 5288 | |
| 5289 | /* Send the message at once. */ |
| 5290 | htx = htx_from_buf(&res->buf); |
| 5291 | h1m_init_res(&h1m); |
| 5292 | |
| 5293 | /* Use the same http version than the request. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5294 | status = ultoa_r(luactx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size); |
| 5295 | reason = luactx->appctx->ctx.hlua_apphttp.reason; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5296 | if (reason == NULL) |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5297 | reason = http_get_reason(luactx->appctx->ctx.hlua_apphttp.status); |
| 5298 | if (luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5299 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 5300 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 5301 | } |
| 5302 | else { |
| 5303 | flags = HTX_SL_F_IS_RESP; |
| 5304 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 5305 | } |
| 5306 | if (!sl) { |
| 5307 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5308 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5309 | WILL_LJMP(lua_error(L)); |
| 5310 | } |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5311 | sl->info.res.status = luactx->appctx->ctx.hlua_apphttp.status; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5312 | |
| 5313 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 5314 | lua_pushvalue(L, 0); |
| 5315 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 5316 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\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 | WILL_LJMP(lua_error(L)); |
| 5319 | } |
| 5320 | |
| 5321 | /* Browse the list of headers. */ |
| 5322 | lua_pushnil(L); |
| 5323 | while(lua_next(L, -2) != 0) { |
| 5324 | /* We expect a string as -2. */ |
| 5325 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 5326 | 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] | 5327 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5328 | lua_typename(L, lua_type(L, -2))); |
| 5329 | WILL_LJMP(lua_error(L)); |
| 5330 | } |
| 5331 | name = lua_tolstring(L, -2, &nlen); |
| 5332 | |
| 5333 | /* We expect an array as -1. */ |
| 5334 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 5335 | 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] | 5336 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5337 | name, |
| 5338 | lua_typename(L, lua_type(L, -1))); |
| 5339 | WILL_LJMP(lua_error(L)); |
| 5340 | } |
| 5341 | |
| 5342 | /* Browse the table who is on the top of the stack. */ |
| 5343 | lua_pushnil(L); |
| 5344 | while(lua_next(L, -2) != 0) { |
| 5345 | int id; |
| 5346 | |
| 5347 | /* We expect a number as -2. */ |
| 5348 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 5349 | 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] | 5350 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5351 | name, |
| 5352 | lua_typename(L, lua_type(L, -2))); |
| 5353 | WILL_LJMP(lua_error(L)); |
| 5354 | } |
| 5355 | id = lua_tointeger(L, -2); |
| 5356 | |
| 5357 | /* We expect a string as -2. */ |
| 5358 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 5359 | 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] | 5360 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5361 | name, id, |
| 5362 | lua_typename(L, lua_type(L, -1))); |
| 5363 | WILL_LJMP(lua_error(L)); |
| 5364 | } |
| 5365 | value = lua_tolstring(L, -1, &vlen); |
| 5366 | |
| 5367 | /* Simple Protocol checks. */ |
Christopher Faulet | 545fbba | 2021-09-28 09:36:25 +0200 | [diff] [blame] | 5368 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) { |
| 5369 | int ret; |
| 5370 | |
| 5371 | ret = h1_parse_xfer_enc_header(&h1m, ist2(value, vlen)); |
| 5372 | if (ret < 0) { |
| 5373 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
| 5374 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
| 5375 | name); |
| 5376 | WILL_LJMP(lua_error(L)); |
| 5377 | } |
| 5378 | else if (ret == 0) |
| 5379 | goto next; /* Skip it */ |
| 5380 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5381 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 5382 | struct ist v = ist2(value, vlen); |
| 5383 | int ret; |
| 5384 | |
| 5385 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 5386 | if (ret < 0) { |
| 5387 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5388 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5389 | name); |
| 5390 | WILL_LJMP(lua_error(L)); |
| 5391 | } |
| 5392 | else if (ret == 0) |
| 5393 | goto next; /* Skip it */ |
| 5394 | } |
| 5395 | |
| 5396 | /* Add a new header */ |
| 5397 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 5398 | 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] | 5399 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5400 | name); |
| 5401 | WILL_LJMP(lua_error(L)); |
| 5402 | } |
| 5403 | next: |
| 5404 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5405 | lua_pop(L, 1); |
| 5406 | } |
| 5407 | |
| 5408 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5409 | lua_pop(L, 1); |
| 5410 | } |
| 5411 | |
| 5412 | if (h1m.flags & H1_MF_CHNK) |
| 5413 | h1m.flags &= ~H1_MF_CLEN; |
| 5414 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 5415 | h1m.flags |= H1_MF_XFER_LEN; |
| 5416 | |
| 5417 | /* Uset HTX start-line flags */ |
| 5418 | if (h1m.flags & H1_MF_XFER_ENC) |
| 5419 | flags |= HTX_SL_F_XFER_ENC; |
| 5420 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 5421 | flags |= HTX_SL_F_XFER_LEN; |
| 5422 | if (h1m.flags & H1_MF_CHNK) |
| 5423 | flags |= HTX_SL_F_CHNK; |
| 5424 | else if (h1m.flags & H1_MF_CLEN) |
| 5425 | flags |= HTX_SL_F_CLEN; |
| 5426 | if (h1m.body_len == 0) |
| 5427 | flags |= HTX_SL_F_BODYLESS; |
| 5428 | } |
| 5429 | sl->flags |= flags; |
| 5430 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 5431 | /* 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] | 5432 | * and the status code implies the presence of a message body, we must |
| 5433 | * announce a transfer encoding chunked. This is required by haproxy |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5434 | * for the keepalive compliance. If the applet announces a transfer-encoding |
| 5435 | * chunked itself, don't do anything. |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5436 | */ |
| 5437 | 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] | 5438 | luactx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 5439 | luactx->appctx->ctx.hlua_apphttp.status != 204 && |
| 5440 | luactx->appctx->ctx.hlua_apphttp.status != 304) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5441 | /* Add a new header */ |
| 5442 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 5443 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 5444 | 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] | 5445 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5446 | WILL_LJMP(lua_error(L)); |
| 5447 | } |
| 5448 | } |
| 5449 | |
| 5450 | /* Finalize headers. */ |
| 5451 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 5452 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5453 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5454 | WILL_LJMP(lua_error(L)); |
| 5455 | } |
| 5456 | |
| 5457 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 5458 | b_reset(&res->buf); |
| 5459 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 5460 | WILL_LJMP(lua_error(L)); |
| 5461 | } |
| 5462 | |
| 5463 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 5464 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5465 | |
| 5466 | /* Headers sent, set the flag. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5467 | luactx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5468 | return 0; |
| 5469 | |
| 5470 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5471 | /* We will build the status line and the headers of the HTTP response. |
| 5472 | * We will try send at once if its not possible, we give back the hand |
| 5473 | * waiting for more room. |
| 5474 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5475 | __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] | 5476 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5477 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Christopher Faulet | 86e1c33 | 2021-12-20 17:09:39 +0100 | [diff] [blame] | 5478 | struct stream_interface *si = cs_si(luactx->appctx->owner); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5479 | struct channel *res = si_ic(si); |
| 5480 | |
| 5481 | if (co_data(res)) { |
| 5482 | si_rx_room_blk(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5483 | 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] | 5484 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5485 | return MAY_LJMP(hlua_applet_http_send_response(L)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5486 | } |
| 5487 | |
| 5488 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5489 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5490 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5491 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5492 | } |
| 5493 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5494 | /* |
| 5495 | * |
| 5496 | * |
| 5497 | * Class HTTP |
| 5498 | * |
| 5499 | * |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5500 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5501 | |
| 5502 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 5503 | * a class stream, otherwise it throws an error. |
| 5504 | */ |
| 5505 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5506 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5507 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
| 5508 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5509 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5510 | /* This function creates and push in the stack a HTTP object |
| 5511 | * according with a current TXN. |
| 5512 | */ |
| 5513 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 5514 | { |
| 5515 | struct hlua_txn *htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5516 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5517 | /* Check stack size. */ |
| 5518 | if (!lua_checkstack(L, 3)) |
| 5519 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5520 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5521 | /* Create the object: obj[0] = userdata. |
| 5522 | * Note that the base of the Converters object is the |
| 5523 | * same than the TXN object. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5524 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5525 | lua_newtable(L); |
| 5526 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 5527 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5528 | |
| 5529 | htxn->s = txn->s; |
| 5530 | htxn->p = txn->p; |
Christopher Faulet | 256b69a | 2019-05-23 11:14:21 +0200 | [diff] [blame] | 5531 | htxn->dir = txn->dir; |
| 5532 | htxn->flags = txn->flags; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5533 | |
| 5534 | /* Pop a class stream metatable and affect it to the table. */ |
| 5535 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 5536 | lua_setmetatable(L, -2); |
| 5537 | |
| 5538 | return 1; |
| 5539 | } |
| 5540 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 5541 | /* This function creates and returns an array containing the status-line |
| 5542 | * elements. This function does not fails. |
| 5543 | */ |
| 5544 | __LJMP static int hlua_http_get_stline(lua_State *L, struct htx_sl *sl) |
| 5545 | { |
| 5546 | /* Create the table. */ |
| 5547 | lua_newtable(L); |
| 5548 | |
| 5549 | if (sl->flags & HTX_SL_F_IS_RESP) { |
| 5550 | lua_pushstring(L, "version"); |
| 5551 | lua_pushlstring(L, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); |
| 5552 | lua_settable(L, -3); |
| 5553 | lua_pushstring(L, "code"); |
| 5554 | lua_pushlstring(L, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl)); |
| 5555 | lua_settable(L, -3); |
| 5556 | lua_pushstring(L, "reason"); |
| 5557 | lua_pushlstring(L, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl)); |
| 5558 | lua_settable(L, -3); |
| 5559 | } |
| 5560 | else { |
| 5561 | lua_pushstring(L, "method"); |
| 5562 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 5563 | lua_settable(L, -3); |
| 5564 | lua_pushstring(L, "uri"); |
| 5565 | lua_pushlstring(L, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl)); |
| 5566 | lua_settable(L, -3); |
| 5567 | lua_pushstring(L, "version"); |
| 5568 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 5569 | lua_settable(L, -3); |
| 5570 | } |
| 5571 | return 1; |
| 5572 | } |
| 5573 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5574 | /* This function creates ans returns an array of HTTP headers. |
| 5575 | * This function does not fails. It is used as wrapper with the |
| 5576 | * 2 following functions. |
| 5577 | */ |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5578 | __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] | 5579 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5580 | struct htx *htx; |
| 5581 | int32_t pos; |
| 5582 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5583 | /* Create the table. */ |
| 5584 | lua_newtable(L); |
| 5585 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5586 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5587 | htx = htxbuf(&msg->chn->buf); |
| 5588 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 5589 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 5590 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5591 | struct ist n, v; |
| 5592 | int len; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5593 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5594 | if (type == HTX_BLK_HDR) { |
| 5595 | n = htx_get_blk_name(htx,blk); |
| 5596 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5597 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5598 | else if (type == HTX_BLK_EOH) |
| 5599 | break; |
| 5600 | else |
| 5601 | continue; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5602 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5603 | /* Check for existing entry: |
| 5604 | * assume that the table is on the top of the stack, and |
| 5605 | * push the key in the stack, the function lua_gettable() |
| 5606 | * perform the lookup. |
| 5607 | */ |
| 5608 | lua_pushlstring(L, n.ptr, n.len); |
| 5609 | lua_gettable(L, -2); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5610 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5611 | switch (lua_type(L, -1)) { |
| 5612 | case LUA_TNIL: |
| 5613 | /* Table not found, create it. */ |
| 5614 | lua_pop(L, 1); /* remove the nil value. */ |
| 5615 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 5616 | lua_newtable(L); /* create and push empty table. */ |
| 5617 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5618 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 5619 | 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] | 5620 | break; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5621 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5622 | case LUA_TTABLE: |
| 5623 | /* Entry found: push the value in the table. */ |
| 5624 | len = lua_rawlen(L, -1); |
| 5625 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5626 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 5627 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 5628 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5629 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5630 | default: |
| 5631 | /* Other cases are errors. */ |
| 5632 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 5633 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5634 | } |
| 5635 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5636 | return 1; |
| 5637 | } |
| 5638 | |
| 5639 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 5640 | { |
| 5641 | struct hlua_txn *htxn; |
| 5642 | |
| 5643 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 5644 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5645 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5646 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5647 | WILL_LJMP(lua_error(L)); |
| 5648 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5649 | return hlua_http_get_headers(L, &htxn->s->txn->req); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5650 | } |
| 5651 | |
| 5652 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 5653 | { |
| 5654 | struct hlua_txn *htxn; |
| 5655 | |
| 5656 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 5657 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5658 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5659 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5660 | WILL_LJMP(lua_error(L)); |
| 5661 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5662 | return hlua_http_get_headers(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5663 | } |
| 5664 | |
| 5665 | /* This function replace full header, or just a value in |
| 5666 | * the request or in the response. It is a wrapper fir the |
| 5667 | * 4 following functions. |
| 5668 | */ |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5669 | __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] | 5670 | { |
| 5671 | size_t name_len; |
| 5672 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5673 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5674 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5675 | struct htx *htx; |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5676 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5677 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5678 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5679 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 5680 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5681 | htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5682 | 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] | 5683 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5684 | return 0; |
| 5685 | } |
| 5686 | |
| 5687 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 5688 | { |
| 5689 | struct hlua_txn *htxn; |
| 5690 | |
| 5691 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5692 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5693 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5694 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5695 | WILL_LJMP(lua_error(L)); |
| 5696 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5697 | 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] | 5698 | } |
| 5699 | |
| 5700 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 5701 | { |
| 5702 | struct hlua_txn *htxn; |
| 5703 | |
| 5704 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 5705 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5706 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5707 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5708 | WILL_LJMP(lua_error(L)); |
| 5709 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5710 | 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] | 5711 | } |
| 5712 | |
| 5713 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 5714 | { |
| 5715 | struct hlua_txn *htxn; |
| 5716 | |
| 5717 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5718 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5719 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5720 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5721 | WILL_LJMP(lua_error(L)); |
| 5722 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5723 | 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] | 5724 | } |
| 5725 | |
| 5726 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 5727 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5728 | struct hlua_txn *htxn; |
| 5729 | |
| 5730 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 5731 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5732 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5733 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5734 | WILL_LJMP(lua_error(L)); |
| 5735 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5736 | 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] | 5737 | } |
| 5738 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5739 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5740 | * It is a wrapper for the 2 following functions. |
| 5741 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5742 | __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] | 5743 | { |
| 5744 | size_t len; |
| 5745 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5746 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5747 | struct http_hdr_ctx ctx; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5748 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5749 | ctx.blk = NULL; |
| 5750 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 5751 | http_remove_header(htx, &ctx); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5752 | return 0; |
| 5753 | } |
| 5754 | |
| 5755 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 5756 | { |
| 5757 | struct hlua_txn *htxn; |
| 5758 | |
| 5759 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 5760 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5761 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5762 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5763 | WILL_LJMP(lua_error(L)); |
| 5764 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5765 | return hlua_http_del_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5766 | } |
| 5767 | |
| 5768 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 5769 | { |
| 5770 | struct hlua_txn *htxn; |
| 5771 | |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5772 | MAY_LJMP(check_args(L, 2, "res_del_hdr")); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5773 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5774 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5775 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5776 | WILL_LJMP(lua_error(L)); |
| 5777 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5778 | return hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5779 | } |
| 5780 | |
| 5781 | /* This function adds an header. It is a wrapper used by |
| 5782 | * the 2 following functions. |
| 5783 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5784 | __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] | 5785 | { |
| 5786 | size_t name_len; |
| 5787 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5788 | size_t value_len; |
| 5789 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5790 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5791 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5792 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 5793 | ist2(value, value_len))); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5794 | return 0; |
| 5795 | } |
| 5796 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 5797 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 5798 | { |
| 5799 | struct hlua_txn *htxn; |
| 5800 | |
| 5801 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 5802 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5803 | |
| 5804 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5805 | WILL_LJMP(lua_error(L)); |
| 5806 | |
| 5807 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
| 5808 | } |
| 5809 | |
| 5810 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 5811 | { |
| 5812 | struct hlua_txn *htxn; |
| 5813 | |
| 5814 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 5815 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5816 | |
| 5817 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
| 5818 | WILL_LJMP(lua_error(L)); |
| 5819 | |
| 5820 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
| 5821 | } |
| 5822 | |
| 5823 | static int hlua_http_req_set_hdr(lua_State *L) |
| 5824 | { |
| 5825 | struct hlua_txn *htxn; |
| 5826 | |
| 5827 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 5828 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5829 | |
| 5830 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5831 | WILL_LJMP(lua_error(L)); |
| 5832 | |
| 5833 | hlua_http_del_hdr(L, &htxn->s->txn->req); |
| 5834 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
| 5835 | } |
| 5836 | |
| 5837 | static int hlua_http_res_set_hdr(lua_State *L) |
| 5838 | { |
| 5839 | struct hlua_txn *htxn; |
| 5840 | |
| 5841 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 5842 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5843 | |
| 5844 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
| 5845 | WILL_LJMP(lua_error(L)); |
| 5846 | |
| 5847 | hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
| 5848 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
| 5849 | } |
| 5850 | |
| 5851 | /* This function set the method. */ |
| 5852 | static int hlua_http_req_set_meth(lua_State *L) |
| 5853 | { |
| 5854 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5855 | size_t name_len; |
| 5856 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5857 | |
| 5858 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5859 | WILL_LJMP(lua_error(L)); |
| 5860 | |
| 5861 | lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1); |
| 5862 | return 1; |
| 5863 | } |
| 5864 | |
| 5865 | /* This function set the method. */ |
| 5866 | static int hlua_http_req_set_path(lua_State *L) |
| 5867 | { |
| 5868 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5869 | size_t name_len; |
| 5870 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5871 | |
| 5872 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5873 | WILL_LJMP(lua_error(L)); |
| 5874 | |
| 5875 | lua_pushboolean(L, http_req_replace_stline(1, name, name_len, htxn->p, htxn->s) != -1); |
| 5876 | return 1; |
| 5877 | } |
| 5878 | |
| 5879 | /* This function set the query-string. */ |
| 5880 | static int hlua_http_req_set_query(lua_State *L) |
| 5881 | { |
| 5882 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5883 | size_t name_len; |
| 5884 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5885 | |
| 5886 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5887 | WILL_LJMP(lua_error(L)); |
| 5888 | |
| 5889 | /* Check length. */ |
| 5890 | if (name_len > trash.size - 1) { |
| 5891 | lua_pushboolean(L, 0); |
| 5892 | return 1; |
| 5893 | } |
| 5894 | |
| 5895 | /* Add the mark question as prefix. */ |
| 5896 | chunk_reset(&trash); |
| 5897 | trash.area[trash.data++] = '?'; |
| 5898 | memcpy(trash.area + trash.data, name, name_len); |
| 5899 | trash.data += name_len; |
| 5900 | |
| 5901 | lua_pushboolean(L, |
| 5902 | http_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1); |
| 5903 | return 1; |
| 5904 | } |
| 5905 | |
| 5906 | /* This function set the uri. */ |
| 5907 | static int hlua_http_req_set_uri(lua_State *L) |
| 5908 | { |
| 5909 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5910 | size_t name_len; |
| 5911 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5912 | |
| 5913 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5914 | WILL_LJMP(lua_error(L)); |
| 5915 | |
| 5916 | lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1); |
| 5917 | return 1; |
| 5918 | } |
| 5919 | |
| 5920 | /* This function set the response code & optionally reason. */ |
| 5921 | static int hlua_http_res_set_status(lua_State *L) |
| 5922 | { |
| 5923 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5924 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5925 | const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 5926 | const struct ist reason = ist2(str, (str ? strlen(str) : 0)); |
| 5927 | |
| 5928 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
| 5929 | WILL_LJMP(lua_error(L)); |
| 5930 | |
| 5931 | http_res_set_status(code, reason, htxn->s); |
| 5932 | return 0; |
| 5933 | } |
| 5934 | |
| 5935 | /* |
| 5936 | * |
| 5937 | * |
| 5938 | * Class HTTPMessage |
| 5939 | * |
| 5940 | * |
| 5941 | */ |
| 5942 | |
| 5943 | /* Returns a struct http_msg if the stack entry "ud" is a class HTTPMessage, |
| 5944 | * otherwise it throws an error. |
| 5945 | */ |
| 5946 | __LJMP static struct http_msg *hlua_checkhttpmsg(lua_State *L, int ud) |
| 5947 | { |
| 5948 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_msg_ref)); |
| 5949 | } |
| 5950 | |
| 5951 | /* Creates and pushes on the stack a HTTP object according with a current TXN. |
| 5952 | */ |
Christopher Faulet | 78c3547 | 2020-02-26 17:14:08 +0100 | [diff] [blame] | 5953 | 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] | 5954 | { |
| 5955 | /* Check stack size. */ |
| 5956 | if (!lua_checkstack(L, 3)) |
| 5957 | return 0; |
| 5958 | |
| 5959 | lua_newtable(L); |
| 5960 | lua_pushlightuserdata(L, msg); |
| 5961 | lua_rawseti(L, -2, 0); |
| 5962 | |
| 5963 | /* Create the "channel" field that contains the request channel object. */ |
| 5964 | lua_pushstring(L, "channel"); |
| 5965 | if (!hlua_channel_new(L, msg->chn)) |
| 5966 | return 0; |
| 5967 | lua_rawset(L, -3); |
| 5968 | |
| 5969 | /* Pop a class stream metatable and affect it to the table. */ |
| 5970 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_msg_ref); |
| 5971 | lua_setmetatable(L, -2); |
| 5972 | |
| 5973 | return 1; |
| 5974 | } |
| 5975 | |
| 5976 | /* Helper function returning a filter attached to the HTTP message at the |
| 5977 | * 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] | 5978 | * 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] | 5979 | * filled with output and input length respectively. |
| 5980 | */ |
| 5981 | static struct filter *hlua_http_msg_filter(lua_State *L, int ud, struct http_msg *msg, size_t *offset, size_t *len) |
| 5982 | { |
| 5983 | struct channel *chn = msg->chn; |
| 5984 | struct htx *htx = htxbuf(&chn->buf); |
| 5985 | struct filter *filter = NULL; |
| 5986 | |
| 5987 | *offset = co_data(msg->chn); |
| 5988 | *len = htx->data - co_data(msg->chn); |
| 5989 | |
| 5990 | if (lua_getfield(L, ud, "__filter") == LUA_TLIGHTUSERDATA) { |
| 5991 | filter = lua_touserdata (L, -1); |
| 5992 | if (msg->msg_state >= HTTP_MSG_DATA) { |
| 5993 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 5994 | |
| 5995 | *offset = flt_ctx->cur_off[CHN_IDX(chn)]; |
| 5996 | *len = flt_ctx->cur_len[CHN_IDX(chn)]; |
| 5997 | } |
| 5998 | } |
| 5999 | |
| 6000 | lua_pop(L, 1); |
| 6001 | return filter; |
| 6002 | } |
| 6003 | |
| 6004 | /* Returns true if the channel attached to the HTTP message is the response |
| 6005 | * channel. |
| 6006 | */ |
| 6007 | __LJMP static int hlua_http_msg_is_resp(lua_State *L) |
| 6008 | { |
| 6009 | struct http_msg *msg; |
| 6010 | |
| 6011 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 6012 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6013 | |
| 6014 | lua_pushboolean(L, !!(msg->chn->flags & CF_ISRESP)); |
| 6015 | return 1; |
| 6016 | } |
| 6017 | |
| 6018 | /* Returns an array containing the elements status-line of the HTTP message. It relies |
| 6019 | * on hlua_http_get_stline(). |
| 6020 | */ |
| 6021 | __LJMP static int hlua_http_msg_get_stline(lua_State *L) |
| 6022 | { |
| 6023 | struct http_msg *msg; |
| 6024 | struct htx *htx; |
| 6025 | struct htx_sl *sl; |
| 6026 | |
| 6027 | MAY_LJMP(check_args(L, 1, "get_stline")); |
| 6028 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6029 | |
| 6030 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6031 | WILL_LJMP(lua_error(L)); |
| 6032 | |
| 6033 | htx = htxbuf(&msg->chn->buf); |
| 6034 | sl = http_get_stline(htx); |
| 6035 | if (!sl) |
| 6036 | return 0; |
| 6037 | return hlua_http_get_stline(L, sl); |
| 6038 | } |
| 6039 | |
| 6040 | /* Returns an array containing all headers of the HTTP message. it relies on |
| 6041 | * hlua_http_get_headers(). |
| 6042 | */ |
| 6043 | __LJMP static int hlua_http_msg_get_headers(lua_State *L) |
| 6044 | { |
| 6045 | struct http_msg *msg; |
| 6046 | |
| 6047 | MAY_LJMP(check_args(L, 1, "get_headers")); |
| 6048 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6049 | |
| 6050 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6051 | WILL_LJMP(lua_error(L)); |
| 6052 | |
| 6053 | return hlua_http_get_headers(L, msg); |
| 6054 | } |
| 6055 | |
| 6056 | /* Deletes all occurrences of an header in the HTTP message matching on its |
| 6057 | * name. It relies on hlua_http_del_hdr(). |
| 6058 | */ |
| 6059 | __LJMP static int hlua_http_msg_del_hdr(lua_State *L) |
| 6060 | { |
| 6061 | struct http_msg *msg; |
| 6062 | |
| 6063 | MAY_LJMP(check_args(L, 2, "del_header")); |
| 6064 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6065 | |
| 6066 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6067 | WILL_LJMP(lua_error(L)); |
| 6068 | |
| 6069 | return hlua_http_del_hdr(L, msg); |
| 6070 | } |
| 6071 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6072 | /* 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] | 6073 | * message given its name against a regex and replaces it if it matches. It |
| 6074 | * relies on hlua_http_rep_hdr(). |
| 6075 | */ |
| 6076 | __LJMP static int hlua_http_msg_rep_hdr(lua_State *L) |
| 6077 | { |
| 6078 | struct http_msg *msg; |
| 6079 | |
| 6080 | MAY_LJMP(check_args(L, 4, "rep_header")); |
| 6081 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6082 | |
| 6083 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6084 | WILL_LJMP(lua_error(L)); |
| 6085 | |
| 6086 | return hlua_http_rep_hdr(L, msg, 1); |
| 6087 | } |
| 6088 | |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 6089 | /* Matches all comma-separated values of all occurrences of an header in the HTTP |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6090 | * message given its name against a regex and replaces it if it matches. It |
| 6091 | * relies on hlua_http_rep_hdr(). |
| 6092 | */ |
| 6093 | __LJMP static int hlua_http_msg_rep_val(lua_State *L) |
| 6094 | { |
| 6095 | struct http_msg *msg; |
| 6096 | |
| 6097 | MAY_LJMP(check_args(L, 4, "rep_value")); |
| 6098 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6099 | |
| 6100 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6101 | WILL_LJMP(lua_error(L)); |
| 6102 | |
| 6103 | return hlua_http_rep_hdr(L, msg, 0); |
| 6104 | } |
| 6105 | |
| 6106 | /* Add an header in the HTTP message. It relies on hlua_http_add_hdr() */ |
| 6107 | __LJMP static int hlua_http_msg_add_hdr(lua_State *L) |
| 6108 | { |
| 6109 | struct http_msg *msg; |
| 6110 | |
| 6111 | MAY_LJMP(check_args(L, 3, "add_header")); |
| 6112 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6113 | |
| 6114 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6115 | WILL_LJMP(lua_error(L)); |
| 6116 | |
| 6117 | return hlua_http_add_hdr(L, msg); |
| 6118 | } |
| 6119 | |
| 6120 | /* Add an header in the HTTP message removing existing headers with the same |
| 6121 | * name. It relies on hlua_http_del_hdr() and hlua_http_add_hdr(). |
| 6122 | */ |
| 6123 | __LJMP static int hlua_http_msg_set_hdr(lua_State *L) |
| 6124 | { |
| 6125 | struct http_msg *msg; |
| 6126 | |
| 6127 | MAY_LJMP(check_args(L, 3, "set_header")); |
| 6128 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6129 | |
| 6130 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6131 | WILL_LJMP(lua_error(L)); |
| 6132 | |
| 6133 | hlua_http_del_hdr(L, msg); |
| 6134 | return hlua_http_add_hdr(L, msg); |
| 6135 | } |
| 6136 | |
| 6137 | /* Rewrites the request method. It relies on http_req_replace_stline(). */ |
| 6138 | __LJMP static int hlua_http_msg_set_meth(lua_State *L) |
| 6139 | { |
| 6140 | struct stream *s; |
| 6141 | struct http_msg *msg; |
| 6142 | const char *name; |
| 6143 | size_t name_len; |
| 6144 | |
| 6145 | MAY_LJMP(check_args(L, 2, "set_method")); |
| 6146 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6147 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6148 | |
| 6149 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6150 | WILL_LJMP(lua_error(L)); |
| 6151 | |
| 6152 | s = chn_strm(msg->chn); |
| 6153 | lua_pushboolean(L, http_req_replace_stline(0, name, name_len, s->be, s) != -1); |
| 6154 | return 1; |
| 6155 | } |
| 6156 | |
| 6157 | /* Rewrites the request path. It relies on http_req_replace_stline(). */ |
| 6158 | __LJMP static int hlua_http_msg_set_path(lua_State *L) |
| 6159 | { |
| 6160 | struct stream *s; |
| 6161 | struct http_msg *msg; |
| 6162 | const char *name; |
| 6163 | size_t name_len; |
| 6164 | |
| 6165 | MAY_LJMP(check_args(L, 2, "set_path")); |
| 6166 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6167 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6168 | |
| 6169 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6170 | WILL_LJMP(lua_error(L)); |
| 6171 | |
| 6172 | s = chn_strm(msg->chn); |
| 6173 | lua_pushboolean(L, http_req_replace_stline(1, name, name_len, s->be, s) != -1); |
| 6174 | return 1; |
| 6175 | } |
| 6176 | |
| 6177 | /* Rewrites the request query-string. It relies on http_req_replace_stline(). */ |
| 6178 | __LJMP static int hlua_http_msg_set_query(lua_State *L) |
| 6179 | { |
| 6180 | struct stream *s; |
| 6181 | struct http_msg *msg; |
| 6182 | const char *name; |
| 6183 | size_t name_len; |
| 6184 | |
| 6185 | MAY_LJMP(check_args(L, 2, "set_query")); |
| 6186 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6187 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6188 | |
| 6189 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6190 | WILL_LJMP(lua_error(L)); |
| 6191 | |
| 6192 | /* Check length. */ |
| 6193 | if (name_len > trash.size - 1) { |
| 6194 | lua_pushboolean(L, 0); |
| 6195 | return 1; |
| 6196 | } |
| 6197 | |
| 6198 | /* Add the mark question as prefix. */ |
| 6199 | chunk_reset(&trash); |
| 6200 | trash.area[trash.data++] = '?'; |
| 6201 | memcpy(trash.area + trash.data, name, name_len); |
| 6202 | trash.data += name_len; |
| 6203 | |
| 6204 | s = chn_strm(msg->chn); |
| 6205 | lua_pushboolean(L, http_req_replace_stline(2, trash.area, trash.data, s->be, s) != -1); |
| 6206 | return 1; |
| 6207 | } |
| 6208 | |
| 6209 | /* Rewrites the request URI. It relies on http_req_replace_stline(). */ |
| 6210 | __LJMP static int hlua_http_msg_set_uri(lua_State *L) |
| 6211 | { |
| 6212 | struct stream *s; |
| 6213 | struct http_msg *msg; |
| 6214 | const char *name; |
| 6215 | size_t name_len; |
| 6216 | |
| 6217 | MAY_LJMP(check_args(L, 2, "set_uri")); |
| 6218 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6219 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6220 | |
| 6221 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6222 | WILL_LJMP(lua_error(L)); |
| 6223 | |
| 6224 | s = chn_strm(msg->chn); |
| 6225 | lua_pushboolean(L, http_req_replace_stline(3, name, name_len, s->be, s) != -1); |
| 6226 | return 1; |
| 6227 | } |
| 6228 | |
| 6229 | /* Rewrites the response status code. It relies on http_res_set_status(). */ |
| 6230 | __LJMP static int hlua_http_msg_set_status(lua_State *L) |
| 6231 | { |
| 6232 | struct http_msg *msg; |
| 6233 | unsigned int code; |
| 6234 | const char *reason; |
| 6235 | size_t reason_len; |
| 6236 | |
| 6237 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6238 | code = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6239 | reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, &reason_len)); |
| 6240 | |
| 6241 | if (!(msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6242 | WILL_LJMP(lua_error(L)); |
| 6243 | |
| 6244 | lua_pushboolean(L, http_res_set_status(code, ist2(reason, reason_len), chn_strm(msg->chn)) != -1); |
| 6245 | return 1; |
| 6246 | } |
| 6247 | |
| 6248 | /* Returns true if the HTTP message is full. */ |
| 6249 | __LJMP static int hlua_http_msg_is_full(lua_State *L) |
| 6250 | { |
| 6251 | struct http_msg *msg; |
| 6252 | |
| 6253 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 6254 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6255 | lua_pushboolean(L, channel_full(msg->chn, 0)); |
| 6256 | return 1; |
| 6257 | } |
| 6258 | |
| 6259 | /* Returns true if the HTTP message may still receive data. */ |
| 6260 | __LJMP static int hlua_http_msg_may_recv(lua_State *L) |
| 6261 | { |
| 6262 | struct http_msg *msg; |
| 6263 | struct htx *htx; |
| 6264 | |
| 6265 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 6266 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6267 | htx = htxbuf(&msg->chn->buf); |
| 6268 | lua_pushboolean(L, (htx_expect_more(htx) && !channel_input_closed(msg->chn) && channel_may_recv(msg->chn))); |
| 6269 | return 1; |
| 6270 | } |
| 6271 | |
| 6272 | /* Returns true if the HTTP message EOM was received */ |
| 6273 | __LJMP static int hlua_http_msg_is_eom(lua_State *L) |
| 6274 | { |
| 6275 | struct http_msg *msg; |
| 6276 | struct htx *htx; |
| 6277 | |
| 6278 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 6279 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6280 | htx = htxbuf(&msg->chn->buf); |
| 6281 | lua_pushboolean(L, !htx_expect_more(htx)); |
| 6282 | return 1; |
| 6283 | } |
| 6284 | |
| 6285 | /* Returns the number of bytes available in the input side of the HTTP |
| 6286 | * message. This function never fails. |
| 6287 | */ |
| 6288 | __LJMP static int hlua_http_msg_get_in_len(lua_State *L) |
| 6289 | { |
| 6290 | struct http_msg *msg; |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6291 | size_t output, input; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6292 | |
| 6293 | MAY_LJMP(check_args(L, 1, "input")); |
| 6294 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6295 | hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6296 | lua_pushinteger(L, input); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6297 | return 1; |
| 6298 | } |
| 6299 | |
| 6300 | /* Returns the number of bytes available in the output side of the HTTP |
| 6301 | * message. This function never fails. |
| 6302 | */ |
| 6303 | __LJMP static int hlua_http_msg_get_out_len(lua_State *L) |
| 6304 | { |
| 6305 | struct http_msg *msg; |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6306 | size_t output, input; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6307 | |
| 6308 | MAY_LJMP(check_args(L, 1, "output")); |
| 6309 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6310 | hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6311 | lua_pushinteger(L, output); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6312 | return 1; |
| 6313 | } |
| 6314 | |
| 6315 | /* Copies at most <len> bytes of DATA blocks from the HTTP message <msg> |
| 6316 | * 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] | 6317 | * 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] | 6318 | * block. This function is called during the payload filtering, so the headers |
| 6319 | * are already scheduled for output (from the filter point of view). |
| 6320 | */ |
| 6321 | static int _hlua_http_msg_dup(struct http_msg *msg, lua_State *L, size_t offset, size_t len) |
| 6322 | { |
| 6323 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 6324 | struct htx_blk *blk; |
| 6325 | struct htx_ret htxret; |
| 6326 | luaL_Buffer b; |
| 6327 | int ret = 0; |
| 6328 | |
| 6329 | luaL_buffinit(L, &b); |
| 6330 | htxret = htx_find_offset(htx, offset); |
| 6331 | for (blk = htxret.blk, offset = htxret.ret; blk && len; blk = htx_get_next_blk(htx, blk)) { |
| 6332 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 6333 | struct ist v; |
| 6334 | |
| 6335 | switch (type) { |
| 6336 | case HTX_BLK_UNUSED: |
| 6337 | break; |
| 6338 | |
| 6339 | case HTX_BLK_DATA: |
| 6340 | v = htx_get_blk_value(htx, blk); |
Tim Duesterhus | b113b5c | 2021-09-15 13:58:44 +0200 | [diff] [blame] | 6341 | v = istadv(v, offset); |
Tim Duesterhus | 2471f5c | 2021-11-08 09:05:01 +0100 | [diff] [blame] | 6342 | v = isttrim(v, len); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6343 | |
| 6344 | luaL_addlstring(&b, v.ptr, v.len); |
| 6345 | ret += v.len; |
| 6346 | break; |
| 6347 | |
| 6348 | default: |
vishnu | 0af4bd7 | 2021-10-24 06:46:24 +0530 | [diff] [blame] | 6349 | if (!ret) |
| 6350 | goto no_data; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6351 | goto end; |
| 6352 | } |
| 6353 | offset = 0; |
| 6354 | } |
| 6355 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6356 | end: |
vishnu | 0af4bd7 | 2021-10-24 06:46:24 +0530 | [diff] [blame] | 6357 | if (!ret && (htx->flags & HTX_FL_EOM)) |
| 6358 | goto no_data; |
| 6359 | luaL_pushresult(&b); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6360 | return ret; |
vishnu | 0af4bd7 | 2021-10-24 06:46:24 +0530 | [diff] [blame] | 6361 | |
| 6362 | no_data: |
| 6363 | /* Remove the empty string and push nil on the stack */ |
| 6364 | lua_pop(L, 1); |
| 6365 | lua_pushnil(L); |
| 6366 | return 0; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6367 | } |
| 6368 | |
| 6369 | /* Copies the string <str> to the HTTP message <msg> at the offset |
| 6370 | * <offset>. This function returns -1 if data cannot be copied. Otherwise, it |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6371 | * returns the amount of data written. This function is responsible to update |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6372 | * the filter context. |
| 6373 | */ |
| 6374 | static int _hlua_http_msg_insert(struct http_msg *msg, struct filter *filter, struct ist str, size_t offset) |
| 6375 | { |
| 6376 | struct htx *htx = htx_from_buf(&msg->chn->buf); |
| 6377 | struct htx_ret htxret; |
| 6378 | int /*max, */ret = 0; |
| 6379 | |
| 6380 | /* Nothing to do, just return */ |
| 6381 | if (unlikely(istlen(str) == 0)) |
| 6382 | goto end; |
| 6383 | |
| 6384 | if (istlen(str) > htx_free_data_space(htx)) { |
| 6385 | ret = -1; |
| 6386 | goto end; |
| 6387 | } |
| 6388 | |
| 6389 | htxret = htx_find_offset(htx, offset); |
| 6390 | if (!htxret.blk || htx_get_blk_type(htxret.blk) != HTX_BLK_DATA) { |
| 6391 | if (!htx_add_last_data(htx, str)) |
| 6392 | goto end; |
| 6393 | } |
| 6394 | else { |
| 6395 | struct ist v = htx_get_blk_value(htx, htxret.blk); |
| 6396 | v.ptr += htxret.ret; |
| 6397 | v.len = 0; |
| 6398 | if (!htx_replace_blk_value(htx, htxret.blk, v, str)) |
| 6399 | goto end; |
| 6400 | } |
| 6401 | ret = str.len; |
| 6402 | if (ret) { |
| 6403 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6404 | flt_update_offsets(filter, msg->chn, ret); |
| 6405 | flt_ctx->cur_len[CHN_IDX(msg->chn)] += ret; |
| 6406 | } |
| 6407 | |
| 6408 | end: |
| 6409 | htx_to_buf(htx, &msg->chn->buf); |
| 6410 | return ret; |
| 6411 | } |
| 6412 | |
| 6413 | /* Helper function removing at most <len> bytes of DATA blocks at the absolute |
| 6414 | * position <offset>. It stops on the first non-DATA HTX block. This function is |
| 6415 | * called during the payload filtering, so the headers are already scheduled for |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6416 | * output (from the filter point of view). This function is responsible to |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6417 | * update the filter context. |
| 6418 | */ |
| 6419 | static void _hlua_http_msg_delete(struct http_msg *msg, struct filter *filter, size_t offset, size_t len) |
| 6420 | { |
| 6421 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6422 | struct htx *htx = htx_from_buf(&msg->chn->buf); |
| 6423 | struct htx_blk *blk; |
| 6424 | struct htx_ret htxret; |
| 6425 | size_t ret = 0; |
| 6426 | |
| 6427 | /* Be sure <len> is always the amount of DATA to remove */ |
| 6428 | if (htx->data == offset+len && htx_get_tail_type(htx) == HTX_BLK_DATA) { |
| 6429 | htx_truncate(htx, offset); |
| 6430 | ret = len; |
| 6431 | goto end; |
| 6432 | } |
| 6433 | |
| 6434 | htxret = htx_find_offset(htx, offset); |
| 6435 | blk = htxret.blk; |
| 6436 | if (htxret.ret) { |
| 6437 | struct ist v; |
| 6438 | |
| 6439 | if (htx_get_blk_type(blk) != HTX_BLK_DATA) |
| 6440 | goto end; |
| 6441 | v = htx_get_blk_value(htx, blk); |
| 6442 | v.ptr += htxret.ret; |
Tim Duesterhus | 2471f5c | 2021-11-08 09:05:01 +0100 | [diff] [blame] | 6443 | v = isttrim(v, len); |
Tim Duesterhus | b113b5c | 2021-09-15 13:58:44 +0200 | [diff] [blame] | 6444 | blk = htx_replace_blk_value(htx, blk, v, IST_NULL); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6445 | len -= v.len; |
| 6446 | ret += v.len; |
| 6447 | } |
| 6448 | |
| 6449 | |
| 6450 | while (blk && len) { |
| 6451 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 6452 | uint32_t sz = htx_get_blksz(blk); |
| 6453 | |
| 6454 | switch (type) { |
| 6455 | case HTX_BLK_UNUSED: |
| 6456 | break; |
| 6457 | |
| 6458 | case HTX_BLK_DATA: |
| 6459 | if (len < sz) { |
| 6460 | htx_cut_data_blk(htx, blk, len); |
| 6461 | ret += len; |
| 6462 | goto end; |
| 6463 | } |
| 6464 | break; |
| 6465 | |
| 6466 | default: |
| 6467 | goto end; |
| 6468 | } |
| 6469 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6470 | /* Remove all the data block */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6471 | len -= sz; |
| 6472 | ret += sz; |
| 6473 | blk = htx_remove_blk(htx, blk); |
| 6474 | } |
| 6475 | |
| 6476 | end: |
| 6477 | flt_update_offsets(filter, msg->chn, -ret); |
| 6478 | flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret; |
| 6479 | /* WARNING: we don't call htx_to_buf() on purpose, because we don't want |
| 6480 | * to loose the EOM flag if the message is empty. |
| 6481 | */ |
| 6482 | } |
| 6483 | |
| 6484 | /* Copies input data found in an HTTP message. Unlike the channel function used |
| 6485 | * to duplicate raw data, this one can only be called inside a filter, from |
| 6486 | * http_payload callback. So it cannot yield. An exception is returned if it is |
| 6487 | * called from another callback. If nothing was copied, a nil value is pushed on |
| 6488 | * the stack. |
| 6489 | */ |
| 6490 | __LJMP static int hlua_http_msg_get_body(lua_State *L) |
| 6491 | { |
| 6492 | struct http_msg *msg; |
| 6493 | struct filter *filter; |
| 6494 | size_t output, input; |
| 6495 | int offset, len; |
| 6496 | |
| 6497 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 6498 | WILL_LJMP(luaL_error(L, "'data' expects at most 2 arguments")); |
| 6499 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6500 | |
| 6501 | if (msg->msg_state < HTTP_MSG_DATA) |
| 6502 | WILL_LJMP(lua_error(L)); |
| 6503 | |
| 6504 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6505 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6506 | WILL_LJMP(lua_error(L)); |
| 6507 | |
| 6508 | if (!ci_data(msg->chn) && channel_input_closed(msg->chn)) { |
| 6509 | lua_pushnil(L); |
| 6510 | return 1; |
| 6511 | } |
| 6512 | |
| 6513 | offset = output; |
| 6514 | if (lua_gettop(L) > 1) { |
| 6515 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6516 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6517 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6518 | offset += output; |
| 6519 | if (offset < output || offset > input + output) { |
| 6520 | lua_pushfstring(L, "offset out of range."); |
| 6521 | WILL_LJMP(lua_error(L)); |
| 6522 | } |
| 6523 | } |
| 6524 | len = output + input - offset; |
| 6525 | if (lua_gettop(L) == 3) { |
| 6526 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6527 | if (!len) |
| 6528 | goto dup; |
| 6529 | if (len == -1) |
| 6530 | len = global.tune.bufsize; |
| 6531 | if (len < 0) { |
| 6532 | lua_pushfstring(L, "length out of range."); |
| 6533 | WILL_LJMP(lua_error(L)); |
| 6534 | } |
| 6535 | } |
| 6536 | |
| 6537 | dup: |
| 6538 | _hlua_http_msg_dup(msg, L, offset, len); |
| 6539 | return 1; |
| 6540 | } |
| 6541 | |
| 6542 | /* Appends a string to the HTTP message, after all existing DATA blocks but |
| 6543 | * before the trailers, if any. It returns the amount of data written or -1 if |
| 6544 | * nothing was copied. Unlike the channel function used to append data, this one |
| 6545 | * can only be called inside a filter, from http_payload callback. So it cannot |
| 6546 | * yield. An exception is returned if it is called from another callback. |
| 6547 | */ |
| 6548 | __LJMP static int hlua_http_msg_append(lua_State *L) |
| 6549 | { |
| 6550 | struct http_msg *msg; |
| 6551 | struct filter *filter; |
| 6552 | const char *str; |
| 6553 | size_t offset, len, sz; |
| 6554 | int ret; |
| 6555 | |
| 6556 | MAY_LJMP(check_args(L, 2, "append")); |
| 6557 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6558 | |
| 6559 | if (msg->msg_state < HTTP_MSG_DATA) |
| 6560 | WILL_LJMP(lua_error(L)); |
| 6561 | |
| 6562 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6563 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6564 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6565 | WILL_LJMP(lua_error(L)); |
| 6566 | |
| 6567 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset+len); |
| 6568 | lua_pushinteger(L, ret); |
| 6569 | return 1; |
| 6570 | } |
| 6571 | |
| 6572 | /* Prepends a string to the HTTP message, before all existing DATA blocks. It |
| 6573 | * returns the amount of data written or -1 if nothing was copied. Unlike the |
| 6574 | * channel function used to prepend data, this one can only be called inside a |
| 6575 | * filter, from http_payload callback. So it cannot yield. An exception is |
| 6576 | * returned if it is called from another callback. |
| 6577 | */ |
| 6578 | __LJMP static int hlua_http_msg_prepend(lua_State *L) |
| 6579 | { |
| 6580 | struct http_msg *msg; |
| 6581 | struct filter *filter; |
| 6582 | const char *str; |
| 6583 | size_t offset, len, sz; |
| 6584 | int ret; |
| 6585 | |
| 6586 | MAY_LJMP(check_args(L, 2, "prepend")); |
| 6587 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6588 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6589 | if (msg->msg_state < HTTP_MSG_DATA) |
| 6590 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6591 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6592 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6593 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6594 | if (!filter || !hlua_filter_from_payload(filter)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6595 | WILL_LJMP(lua_error(L)); |
| 6596 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6597 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6598 | lua_pushinteger(L, ret); |
| 6599 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6600 | } |
| 6601 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6602 | /* Inserts a string to the HTTP message at a given offset. By default the string |
| 6603 | * is appended at the end of DATA blocks. It returns the amount of data written |
| 6604 | * or -1 if nothing was copied. Unlike the channel function used to insert data, |
| 6605 | * this one can only be called inside a filter, from http_payload callback. So |
| 6606 | * it cannot yield. An exception is returned if it is called from another |
| 6607 | * callback. |
| 6608 | */ |
| 6609 | __LJMP static int hlua_http_msg_insert_data(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6610 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6611 | struct http_msg *msg; |
| 6612 | struct filter *filter; |
| 6613 | const char *str; |
| 6614 | size_t input, output, sz; |
| 6615 | int offset; |
| 6616 | int ret; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6617 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6618 | if (lua_gettop(L) < 2 || lua_gettop(L) > 3) |
| 6619 | WILL_LJMP(luaL_error(L, "'insert' expects at least 1 argument and at most 2 arguments")); |
| 6620 | MAY_LJMP(check_args(L, 2, "insert")); |
| 6621 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6622 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6623 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6624 | WILL_LJMP(lua_error(L)); |
| 6625 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6626 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6627 | filter = hlua_http_msg_filter(L, 1, msg, &input, &output); |
| 6628 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6629 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6630 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6631 | offset = input + output; |
| 6632 | if (lua_gettop(L) > 2) { |
| 6633 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6634 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6635 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6636 | offset += output; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6637 | if (offset < output || offset > output + input) { |
| 6638 | lua_pushfstring(L, "offset out of range."); |
| 6639 | WILL_LJMP(lua_error(L)); |
| 6640 | } |
| 6641 | } |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6642 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6643 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6644 | lua_pushinteger(L, ret); |
| 6645 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6646 | } |
| 6647 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6648 | /* Removes a given amount of data from the HTTP message at a given offset. By |
| 6649 | * default all DATA blocks are removed. It returns the amount of data |
| 6650 | * removed. Unlike the channel function used to remove data, this one can only |
| 6651 | * be called inside a filter, from http_payload callback. So it cannot yield. An |
| 6652 | * exception is returned if it is called from another callback. |
| 6653 | */ |
| 6654 | __LJMP static int hlua_http_msg_del_data(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6655 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6656 | struct http_msg *msg; |
| 6657 | struct filter *filter; |
| 6658 | size_t input, output; |
| 6659 | int offset, len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6660 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6661 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 6662 | WILL_LJMP(luaL_error(L, "'insert' expects at most 2 arguments")); |
| 6663 | MAY_LJMP(check_args(L, 2, "insert")); |
| 6664 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6665 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6666 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6667 | WILL_LJMP(lua_error(L)); |
| 6668 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6669 | filter = hlua_http_msg_filter(L, 1, msg, &input, &output); |
| 6670 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6671 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6672 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6673 | offset = input + output; |
| 6674 | if (lua_gettop(L) > 2) { |
| 6675 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6676 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6677 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6678 | offset += output; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6679 | if (offset < output || offset > output + input) { |
| 6680 | lua_pushfstring(L, "offset out of range."); |
| 6681 | WILL_LJMP(lua_error(L)); |
| 6682 | } |
| 6683 | } |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6684 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6685 | len = output + input - offset; |
| 6686 | if (lua_gettop(L) == 4) { |
| 6687 | len = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 6688 | if (!len) |
| 6689 | goto end; |
| 6690 | if (len == -1) |
| 6691 | len = output + input - offset; |
| 6692 | if (len < 0 || offset + len > output + input) { |
| 6693 | lua_pushfstring(L, "length out of range."); |
| 6694 | WILL_LJMP(lua_error(L)); |
| 6695 | } |
| 6696 | } |
| 6697 | |
| 6698 | _hlua_http_msg_delete(msg, filter, offset, len); |
| 6699 | |
| 6700 | end: |
| 6701 | lua_pushinteger(L, len); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6702 | return 1; |
| 6703 | } |
| 6704 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6705 | /* 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] | 6706 | * all remaining data are removed, accordingly to the filter context. It returns |
| 6707 | * the amount of data written or -1 if nothing was copied. Unlike the channel |
| 6708 | * function used to replace data, this one can only be called inside a filter, |
| 6709 | * from http_payload callback. So it cannot yield. An exception is returned if |
| 6710 | * it is called from another callback. |
| 6711 | */ |
| 6712 | __LJMP static int hlua_http_msg_set_data(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6713 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6714 | struct http_msg *msg; |
| 6715 | struct filter *filter; |
| 6716 | struct htx *htx; |
| 6717 | const char *str; |
| 6718 | size_t input, output, sz; |
| 6719 | int offset, len; |
| 6720 | int ret; |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 6721 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6722 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 6723 | WILL_LJMP(luaL_error(L, "'set' expects at least 1 argument and at most 3 arguments")); |
| 6724 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6725 | |
| 6726 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6727 | WILL_LJMP(lua_error(L)); |
| 6728 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6729 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6730 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6731 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6732 | WILL_LJMP(lua_error(L)); |
| 6733 | |
| 6734 | offset = output; |
| 6735 | if (lua_gettop(L) > 2) { |
| 6736 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6737 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6738 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6739 | offset += output; |
| 6740 | if (offset < output || offset > input + output) { |
| 6741 | lua_pushfstring(L, "offset out of range."); |
| 6742 | WILL_LJMP(lua_error(L)); |
| 6743 | } |
| 6744 | } |
| 6745 | |
| 6746 | len = output + input - offset; |
| 6747 | if (lua_gettop(L) == 4) { |
| 6748 | len = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 6749 | if (!len) |
| 6750 | goto set; |
| 6751 | if (len == -1) |
| 6752 | len = output + input - offset; |
| 6753 | if (len < 0 || offset + len > output + input) { |
| 6754 | lua_pushfstring(L, "length out of range."); |
| 6755 | WILL_LJMP(lua_error(L)); |
| 6756 | } |
| 6757 | } |
| 6758 | |
| 6759 | set: |
| 6760 | /* Be sure we can copied the string once input data will be removed. */ |
| 6761 | htx = htx_from_buf(&msg->chn->buf); |
| 6762 | if (sz > htx_free_data_space(htx) + len) |
| 6763 | lua_pushinteger(L, -1); |
| 6764 | else { |
| 6765 | _hlua_http_msg_delete(msg, filter, offset, len); |
| 6766 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6767 | lua_pushinteger(L, ret); |
| 6768 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6769 | return 1; |
| 6770 | } |
| 6771 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6772 | /* Prepends data into an HTTP message and forward it, from the filter point of |
| 6773 | * view. It returns the amount of data written or -1 if nothing was sent. Unlike |
| 6774 | * the channel function used to send data, this one can only be called inside a |
| 6775 | * filter, from http_payload callback. So it cannot yield. An exception is |
| 6776 | * returned if it is called from another callback. |
| 6777 | */ |
| 6778 | __LJMP static int hlua_http_msg_send(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6779 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6780 | struct http_msg *msg; |
| 6781 | struct filter *filter; |
| 6782 | struct htx *htx; |
| 6783 | const char *str; |
| 6784 | size_t offset, len, sz; |
| 6785 | int ret; |
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 | MAY_LJMP(check_args(L, 2, "send")); |
| 6788 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6789 | |
| 6790 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6791 | WILL_LJMP(lua_error(L)); |
| 6792 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6793 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6794 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6795 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6796 | WILL_LJMP(lua_error(L)); |
| 6797 | |
| 6798 | /* Return an error if the channel's output is closed */ |
| 6799 | if (unlikely(channel_output_closed(msg->chn))) { |
| 6800 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6801 | return 1; |
| 6802 | } |
| 6803 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6804 | htx = htx_from_buf(&msg->chn->buf); |
| 6805 | if (sz > htx_free_data_space(htx)) { |
| 6806 | lua_pushinteger(L, -1); |
| 6807 | return 1; |
| 6808 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6809 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6810 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6811 | if (ret > 0) { |
| 6812 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6813 | |
| 6814 | FLT_OFF(filter, msg->chn) += ret; |
| 6815 | flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret; |
| 6816 | flt_ctx->cur_off[CHN_IDX(msg->chn)] += ret; |
| 6817 | } |
| 6818 | |
| 6819 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6820 | return 1; |
| 6821 | } |
| 6822 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6823 | /* Forwards a given amount of bytes. It return -1 if the channel's output is |
| 6824 | * closed. Otherwise, it returns the number of bytes forwarded. Unlike the |
| 6825 | * channel function used to forward data, this one can only be called inside a |
| 6826 | * filter, from http_payload callback. So it cannot yield. An exception is |
| 6827 | * returned if it is called from another callback. All other functions deal with |
| 6828 | * DATA block, this one not. |
| 6829 | */ |
| 6830 | __LJMP static int hlua_http_msg_forward(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6831 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6832 | struct http_msg *msg; |
| 6833 | struct filter *filter; |
| 6834 | size_t offset, len; |
| 6835 | int fwd, ret = 0; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6836 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6837 | MAY_LJMP(check_args(L, 2, "forward")); |
| 6838 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6839 | |
| 6840 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6841 | WILL_LJMP(lua_error(L)); |
| 6842 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6843 | fwd = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6844 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6845 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6846 | WILL_LJMP(lua_error(L)); |
| 6847 | |
| 6848 | /* Nothing to do, just return */ |
| 6849 | if (!fwd) |
| 6850 | goto end; |
| 6851 | |
| 6852 | /* Return an error if the channel's output is closed */ |
| 6853 | if (unlikely(channel_output_closed(msg->chn))) { |
| 6854 | ret = -1; |
| 6855 | goto end; |
| 6856 | } |
| 6857 | |
| 6858 | ret = fwd; |
| 6859 | if (ret > len) |
| 6860 | ret = len; |
| 6861 | |
| 6862 | if (ret) { |
| 6863 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6864 | |
| 6865 | FLT_OFF(filter, msg->chn) += ret; |
| 6866 | flt_ctx->cur_off[CHN_IDX(msg->chn)] += ret; |
| 6867 | flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret; |
| 6868 | } |
| 6869 | |
| 6870 | end: |
| 6871 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6872 | return 1; |
| 6873 | } |
| 6874 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6875 | /* Set EOM flag on the HTX message. |
| 6876 | * |
| 6877 | * NOTE: Not sure it is a good idea to manipulate this flag but for now I don't |
| 6878 | * really know how to do without this feature. |
| 6879 | */ |
| 6880 | __LJMP static int hlua_http_msg_set_eom(lua_State *L) |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 6881 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6882 | struct http_msg *msg; |
| 6883 | struct htx *htx; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 6884 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6885 | MAY_LJMP(check_args(L, 1, "set_eom")); |
| 6886 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6887 | htx = htxbuf(&msg->chn->buf); |
| 6888 | htx->flags |= HTX_FL_EOM; |
| 6889 | return 0; |
| 6890 | } |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6891 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6892 | /* Unset EOM flag on the HTX message. |
| 6893 | * |
| 6894 | * NOTE: Not sure it is a good idea to manipulate this flag but for now I don't |
| 6895 | * really know how to do without this feature. |
| 6896 | */ |
| 6897 | __LJMP static int hlua_http_msg_unset_eom(lua_State *L) |
| 6898 | { |
| 6899 | struct http_msg *msg; |
| 6900 | struct htx *htx; |
| 6901 | |
| 6902 | MAY_LJMP(check_args(L, 1, "set_eom")); |
| 6903 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6904 | htx = htxbuf(&msg->chn->buf); |
| 6905 | htx->flags &= ~HTX_FL_EOM; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 6906 | return 0; |
| 6907 | } |
| 6908 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6909 | /* |
| 6910 | * |
| 6911 | * |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 6912 | * Class HTTPClient |
| 6913 | * |
| 6914 | * |
| 6915 | */ |
| 6916 | __LJMP static struct hlua_httpclient *hlua_checkhttpclient(lua_State *L, int ud) |
| 6917 | { |
| 6918 | return MAY_LJMP(hlua_checkudata(L, ud, class_httpclient_ref)); |
| 6919 | } |
| 6920 | |
William Lallemand | f77f1de | 2021-09-28 19:10:38 +0200 | [diff] [blame] | 6921 | |
| 6922 | /* stops the httpclient and ask it to kill itself */ |
| 6923 | __LJMP static int hlua_httpclient_gc(lua_State *L) |
| 6924 | { |
| 6925 | struct hlua_httpclient *hlua_hc; |
| 6926 | |
| 6927 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 6928 | |
| 6929 | hlua_hc = MAY_LJMP(hlua_checkhttpclient(L, 1)); |
| 6930 | |
| 6931 | httpclient_stop_and_destroy(hlua_hc->hc); |
| 6932 | |
| 6933 | hlua_hc->hc = NULL; |
| 6934 | |
| 6935 | |
| 6936 | return 0; |
| 6937 | } |
| 6938 | |
| 6939 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 6940 | __LJMP static int hlua_httpclient_new(lua_State *L) |
| 6941 | { |
| 6942 | struct hlua_httpclient *hlua_hc; |
| 6943 | struct hlua *hlua; |
| 6944 | |
| 6945 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 6946 | hlua = hlua_gethlua(L); |
| 6947 | if (!hlua) |
| 6948 | return 0; |
| 6949 | |
| 6950 | /* Check stack size. */ |
| 6951 | if (!lua_checkstack(L, 3)) { |
| 6952 | hlua_pusherror(L, "httpclient: full stack"); |
| 6953 | goto err; |
| 6954 | } |
| 6955 | /* Create the object: obj[0] = userdata. */ |
| 6956 | lua_newtable(L); |
| 6957 | hlua_hc = MAY_LJMP(lua_newuserdata(L, sizeof(*hlua_hc))); |
| 6958 | lua_rawseti(L, -2, 0); |
| 6959 | memset(hlua_hc, 0, sizeof(*hlua_hc)); |
| 6960 | |
| 6961 | hlua_hc->hc = httpclient_new(hlua, 0, IST_NULL); |
| 6962 | if (!hlua_hc->hc) |
| 6963 | goto err; |
| 6964 | |
| 6965 | /* Pop a class stream metatable and affect it to the userdata. */ |
| 6966 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_httpclient_ref); |
| 6967 | lua_setmetatable(L, -2); |
| 6968 | |
| 6969 | return 1; |
| 6970 | |
| 6971 | err: |
| 6972 | WILL_LJMP(lua_error(L)); |
| 6973 | return 0; |
| 6974 | } |
| 6975 | |
| 6976 | |
| 6977 | /* |
| 6978 | * Callback of the httpclient, this callback wakes the lua task up, once the |
| 6979 | * httpclient receives some data |
| 6980 | * |
| 6981 | */ |
| 6982 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 6983 | static void hlua_httpclient_cb(struct httpclient *hc) |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 6984 | { |
| 6985 | struct hlua *hlua = hc->caller; |
| 6986 | |
| 6987 | if (!hlua || !hlua->task) |
| 6988 | return; |
| 6989 | |
| 6990 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 6991 | } |
| 6992 | |
| 6993 | /* |
William Lallemand | d7df73a | 2021-09-23 17:54:00 +0200 | [diff] [blame] | 6994 | * Fill the lua stack with headers from the httpclient response |
| 6995 | * This works the same way as the hlua_http_get_headers() function |
| 6996 | */ |
| 6997 | __LJMP static int hlua_httpclient_get_headers(lua_State *L, struct hlua_httpclient *hlua_hc) |
| 6998 | { |
| 6999 | struct http_hdr *hdr; |
| 7000 | |
| 7001 | lua_newtable(L); |
| 7002 | |
William Lallemand | ef574b2 | 2021-10-05 16:19:31 +0200 | [diff] [blame] | 7003 | for (hdr = hlua_hc->hc->res.hdrs; hdr && isttest(hdr->n); hdr++) { |
William Lallemand | d7df73a | 2021-09-23 17:54:00 +0200 | [diff] [blame] | 7004 | struct ist n, v; |
| 7005 | int len; |
| 7006 | |
| 7007 | n = hdr->n; |
| 7008 | v = hdr->v; |
| 7009 | |
| 7010 | /* Check for existing entry: |
| 7011 | * assume that the table is on the top of the stack, and |
| 7012 | * push the key in the stack, the function lua_gettable() |
| 7013 | * perform the lookup. |
| 7014 | */ |
| 7015 | |
| 7016 | lua_pushlstring(L, n.ptr, n.len); |
| 7017 | lua_gettable(L, -2); |
| 7018 | |
| 7019 | switch (lua_type(L, -1)) { |
| 7020 | case LUA_TNIL: |
| 7021 | /* Table not found, create it. */ |
| 7022 | lua_pop(L, 1); /* remove the nil value. */ |
| 7023 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 7024 | lua_newtable(L); /* create and push empty table. */ |
| 7025 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 7026 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 7027 | lua_rawset(L, -3); /* index new table with header name (pop the values). */ |
| 7028 | break; |
| 7029 | |
| 7030 | case LUA_TTABLE: |
| 7031 | /* Entry found: push the value in the table. */ |
| 7032 | len = lua_rawlen(L, -1); |
| 7033 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 7034 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 7035 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 7036 | break; |
| 7037 | |
| 7038 | default: |
| 7039 | /* Other cases are errors. */ |
| 7040 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 7041 | WILL_LJMP(lua_error(L)); |
| 7042 | } |
| 7043 | } |
| 7044 | return 1; |
| 7045 | } |
| 7046 | |
| 7047 | /* |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7048 | * Allocate and return an array of http_hdr ist extracted from the <headers> lua table |
| 7049 | * |
| 7050 | * Caller must free the result |
| 7051 | */ |
| 7052 | struct http_hdr *hlua_httpclient_table_to_hdrs(lua_State *L) |
| 7053 | { |
| 7054 | struct http_hdr hdrs[global.tune.max_http_hdr]; |
| 7055 | struct http_hdr *result = NULL; |
| 7056 | uint32_t hdr_num = 0; |
| 7057 | |
| 7058 | lua_pushnil(L); |
| 7059 | while (lua_next(L, -2) != 0) { |
| 7060 | struct ist name, value; |
| 7061 | const char *n, *v; |
| 7062 | size_t nlen, vlen; |
| 7063 | |
| 7064 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 7065 | /* Skip element if the key is not a string or if the value is not a table */ |
| 7066 | goto next_hdr; |
| 7067 | } |
| 7068 | |
| 7069 | n = lua_tolstring(L, -2, &nlen); |
| 7070 | name = ist2(n, nlen); |
| 7071 | |
| 7072 | /* Loop on header's values */ |
| 7073 | lua_pushnil(L); |
| 7074 | while (lua_next(L, -2)) { |
| 7075 | if (!lua_isstring(L, -1)) { |
| 7076 | /* Skip the value if it is not a string */ |
| 7077 | goto next_value; |
| 7078 | } |
| 7079 | |
| 7080 | v = lua_tolstring(L, -1, &vlen); |
| 7081 | value = ist2(v, vlen); |
| 7082 | name = ist2(n, nlen); |
| 7083 | |
| 7084 | hdrs[hdr_num].n = istdup(name); |
| 7085 | hdrs[hdr_num].v = istdup(value); |
| 7086 | |
| 7087 | hdr_num++; |
| 7088 | |
| 7089 | next_value: |
| 7090 | lua_pop(L, 1); |
| 7091 | } |
| 7092 | |
| 7093 | next_hdr: |
| 7094 | lua_pop(L, 1); |
| 7095 | |
| 7096 | } |
| 7097 | |
| 7098 | if (hdr_num) { |
| 7099 | /* alloc and copy the headers in the httpclient struct */ |
Tim Duesterhus | 16cc16d | 2021-11-06 15:14:45 +0100 | [diff] [blame] | 7100 | result = calloc((hdr_num + 1), sizeof(*result)); |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7101 | if (!result) |
| 7102 | goto skip_headers; |
| 7103 | memcpy(result, hdrs, sizeof(struct http_hdr) * (hdr_num + 1)); |
| 7104 | |
| 7105 | result[hdr_num].n = IST_NULL; |
| 7106 | result[hdr_num].v = IST_NULL; |
| 7107 | } |
| 7108 | |
| 7109 | skip_headers: |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7110 | |
| 7111 | return result; |
| 7112 | } |
| 7113 | |
| 7114 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7115 | /* |
| 7116 | * For each yield, checks if there is some data in the httpclient and push them |
| 7117 | * in the lua buffer, once the httpclient finished its job, push the result on |
| 7118 | * the stack |
| 7119 | */ |
| 7120 | __LJMP static int hlua_httpclient_rcv_yield(lua_State *L, int status, lua_KContext ctx) |
| 7121 | { |
| 7122 | struct buffer *tr; |
| 7123 | int res; |
| 7124 | struct hlua *hlua = hlua_gethlua(L); |
| 7125 | struct hlua_httpclient *hlua_hc = hlua_checkhttpclient(L, 1); |
| 7126 | |
| 7127 | |
| 7128 | tr = get_trash_chunk(); |
| 7129 | |
| 7130 | res = httpclient_res_xfer(hlua_hc->hc, tr); |
| 7131 | luaL_addlstring(&hlua_hc->b, b_orig(tr), res); |
| 7132 | |
| 7133 | if (!httpclient_data(hlua_hc->hc) && httpclient_ended(hlua_hc->hc)) { |
| 7134 | |
| 7135 | luaL_pushresult(&hlua_hc->b); |
| 7136 | lua_settable(L, -3); |
| 7137 | |
| 7138 | lua_pushstring(L, "status"); |
| 7139 | lua_pushinteger(L, hlua_hc->hc->res.status); |
| 7140 | lua_settable(L, -3); |
| 7141 | |
| 7142 | |
| 7143 | lua_pushstring(L, "reason"); |
| 7144 | lua_pushlstring(L, hlua_hc->hc->res.reason.ptr, hlua_hc->hc->res.reason.len); |
| 7145 | lua_settable(L, -3); |
| 7146 | |
| 7147 | lua_pushstring(L, "headers"); |
| 7148 | hlua_httpclient_get_headers(L, hlua_hc); |
| 7149 | lua_settable(L, -3); |
| 7150 | |
| 7151 | return 1; |
| 7152 | } |
| 7153 | |
| 7154 | if (httpclient_data(hlua_hc->hc)) |
| 7155 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 7156 | |
| 7157 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_rcv_yield, TICK_ETERNITY, 0)); |
| 7158 | |
| 7159 | return 0; |
| 7160 | } |
| 7161 | |
| 7162 | /* |
| 7163 | * Call this when trying to stream a body during a request |
| 7164 | */ |
| 7165 | __LJMP static int hlua_httpclient_snd_yield(lua_State *L, int status, lua_KContext ctx) |
| 7166 | { |
| 7167 | struct hlua *hlua; |
| 7168 | struct hlua_httpclient *hlua_hc = hlua_checkhttpclient(L, 1); |
| 7169 | const char *body_str = NULL; |
| 7170 | int ret; |
| 7171 | int end = 0; |
| 7172 | size_t buf_len; |
| 7173 | size_t to_send = 0; |
| 7174 | |
| 7175 | hlua = hlua_gethlua(L); |
| 7176 | |
| 7177 | if (!hlua || !hlua->task) |
| 7178 | WILL_LJMP(luaL_error(L, "The 'get' function is only allowed in " |
| 7179 | "'frontend', 'backend' or 'task'")); |
| 7180 | |
| 7181 | ret = lua_getfield(L, -1, "body"); |
| 7182 | if (ret != LUA_TSTRING) |
| 7183 | goto rcv; |
| 7184 | |
| 7185 | body_str = lua_tolstring(L, -1, &buf_len); |
| 7186 | lua_pop(L, 1); |
| 7187 | |
Christopher Faulet | fc59129 | 2022-01-12 14:46:03 +0100 | [diff] [blame] | 7188 | to_send = buf_len - hlua_hc->sent; |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7189 | |
| 7190 | if ((hlua_hc->sent + to_send) >= buf_len) |
| 7191 | end = 1; |
| 7192 | |
| 7193 | /* the end flag is always set since we are using the whole remaining size */ |
| 7194 | hlua_hc->sent += httpclient_req_xfer(hlua_hc->hc, ist2(body_str + hlua_hc->sent, to_send), end); |
| 7195 | |
| 7196 | if (buf_len > hlua_hc->sent) { |
| 7197 | /* still need to process the buffer */ |
| 7198 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_snd_yield, TICK_ETERNITY, 0)); |
| 7199 | } else { |
| 7200 | goto rcv; |
| 7201 | /* we sent the whole request buffer we can recv */ |
| 7202 | } |
| 7203 | return 0; |
| 7204 | |
| 7205 | rcv: |
| 7206 | |
| 7207 | /* we return a "res" object */ |
| 7208 | lua_newtable(L); |
| 7209 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7210 | lua_pushstring(L, "body"); |
William Lallemand | d1187eb | 2021-11-02 10:40:06 +0100 | [diff] [blame] | 7211 | luaL_buffinit(L, &hlua_hc->b); |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7212 | |
William Lallemand | 933fe39 | 2021-11-04 09:45:58 +0100 | [diff] [blame] | 7213 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7214 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_rcv_yield, TICK_ETERNITY, 0)); |
| 7215 | |
| 7216 | return 1; |
| 7217 | } |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7218 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7219 | /* |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7220 | * Send an HTTP request and wait for a response |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7221 | */ |
| 7222 | |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7223 | __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] | 7224 | { |
| 7225 | struct hlua_httpclient *hlua_hc; |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7226 | struct http_hdr *hdrs = NULL; |
| 7227 | struct http_hdr *hdrs_i = NULL; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7228 | struct hlua *hlua; |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7229 | const char *url_str = NULL; |
William Lallemand | dec25c3 | 2021-10-25 19:48:37 +0200 | [diff] [blame] | 7230 | const char *body_str = NULL; |
William Lallemand | b4a4ef6 | 2022-02-23 14:18:16 +0100 | [diff] [blame] | 7231 | int timeout; |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7232 | size_t buf_len; |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7233 | int ret; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7234 | |
| 7235 | hlua = hlua_gethlua(L); |
| 7236 | |
| 7237 | if (!hlua || !hlua->task) |
| 7238 | WILL_LJMP(luaL_error(L, "The 'get' function is only allowed in " |
| 7239 | "'frontend', 'backend' or 'task'")); |
| 7240 | |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7241 | if (lua_gettop(L) != 2 || lua_type(L, -1) != LUA_TTABLE) |
| 7242 | WILL_LJMP(luaL_error(L, "'get' needs a table as argument")); |
| 7243 | |
William Lallemand | 4f4f2b7 | 2022-02-17 20:00:23 +0100 | [diff] [blame] | 7244 | hlua_hc = hlua_checkhttpclient(L, 1); |
| 7245 | |
| 7246 | ret = lua_getfield(L, -1, "dst"); |
| 7247 | if (ret == LUA_TSTRING) { |
| 7248 | if (httpclient_set_dst(hlua_hc->hc, lua_tostring(L, -1)) < 0) |
| 7249 | WILL_LJMP(luaL_error(L, "Can't use the 'dst' argument")); |
| 7250 | } |
| 7251 | lua_pop(L, 1); |
| 7252 | |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7253 | ret = lua_getfield(L, -1, "url"); |
| 7254 | if (ret == LUA_TSTRING) { |
| 7255 | url_str = lua_tostring(L, -1); |
| 7256 | } |
| 7257 | lua_pop(L, 1); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7258 | |
William Lallemand | b4a4ef6 | 2022-02-23 14:18:16 +0100 | [diff] [blame] | 7259 | ret = lua_getfield(L, -1, "timeout"); |
| 7260 | if (ret == LUA_TNUMBER) { |
| 7261 | timeout = lua_tointeger(L, -1); |
| 7262 | httpclient_set_timeout(hlua_hc->hc, timeout); |
| 7263 | } |
William Lallemand | ab90ee8 | 2022-02-23 15:16:08 +0100 | [diff] [blame] | 7264 | lua_pop(L, 1); |
William Lallemand | b4a4ef6 | 2022-02-23 14:18:16 +0100 | [diff] [blame] | 7265 | |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7266 | ret = lua_getfield(L, -1, "headers"); |
| 7267 | if (ret == LUA_TTABLE) { |
| 7268 | hdrs = hlua_httpclient_table_to_hdrs(L); |
| 7269 | } |
| 7270 | lua_pop(L, 1); |
William Lallemand | 79416cb | 2021-09-24 14:51:44 +0200 | [diff] [blame] | 7271 | |
William Lallemand | dec25c3 | 2021-10-25 19:48:37 +0200 | [diff] [blame] | 7272 | ret = lua_getfield(L, -1, "body"); |
| 7273 | if (ret == LUA_TSTRING) { |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7274 | body_str = lua_tolstring(L, -1, &buf_len); |
William Lallemand | dec25c3 | 2021-10-25 19:48:37 +0200 | [diff] [blame] | 7275 | } |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7276 | |
William Lallemand | dec25c3 | 2021-10-25 19:48:37 +0200 | [diff] [blame] | 7277 | lua_pop(L, 1); |
| 7278 | |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7279 | if (!url_str) { |
| 7280 | WILL_LJMP(luaL_error(L, "'get' need a 'url' argument")); |
| 7281 | return 0; |
| 7282 | } |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7283 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7284 | |
| 7285 | hlua_hc->hc->req.url = istdup(ist(url_str)); |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7286 | hlua_hc->hc->req.meth = meth; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7287 | |
| 7288 | /* update the httpclient callbacks */ |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7289 | hlua_hc->hc->ops.res_stline = hlua_httpclient_cb; |
| 7290 | hlua_hc->hc->ops.res_headers = hlua_httpclient_cb; |
| 7291 | hlua_hc->hc->ops.res_payload = hlua_httpclient_cb; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7292 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7293 | /* a body is available, it will use the request callback */ |
| 7294 | if (body_str) { |
| 7295 | hlua_hc->hc->ops.req_payload = hlua_httpclient_cb; |
| 7296 | } |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7297 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7298 | ret = httpclient_req_gen(hlua_hc->hc, hlua_hc->hc->req.url, meth, hdrs, IST_NULL); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7299 | |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7300 | /* free the temporary headers array */ |
| 7301 | hdrs_i = hdrs; |
| 7302 | while (hdrs_i && isttest(hdrs_i->n)) { |
| 7303 | istfree(&hdrs_i->n); |
| 7304 | istfree(&hdrs_i->v); |
| 7305 | hdrs_i++; |
| 7306 | } |
| 7307 | ha_free(&hdrs); |
| 7308 | |
| 7309 | |
William Lallemand | 6137a9e | 2021-10-26 15:01:53 +0200 | [diff] [blame] | 7310 | if (ret != ERR_NONE) { |
| 7311 | WILL_LJMP(luaL_error(L, "Can't generate the HTTP request")); |
| 7312 | return 0; |
| 7313 | } |
| 7314 | |
William Lallemand | 6137a9e | 2021-10-26 15:01:53 +0200 | [diff] [blame] | 7315 | httpclient_start(hlua_hc->hc); |
| 7316 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7317 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_snd_yield, TICK_ETERNITY, 0)); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7318 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7319 | return 0; |
| 7320 | } |
| 7321 | |
| 7322 | /* |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7323 | * Sends an HTTP HEAD request and wait for a response |
| 7324 | * |
| 7325 | * httpclient:head(url, headers, payload) |
| 7326 | */ |
| 7327 | __LJMP static int hlua_httpclient_head(lua_State *L) |
| 7328 | { |
| 7329 | return hlua_httpclient_send(L, HTTP_METH_HEAD); |
| 7330 | } |
| 7331 | |
| 7332 | /* |
| 7333 | * Send an HTTP GET request and wait for a response |
| 7334 | * |
| 7335 | * httpclient:get(url, headers, payload) |
| 7336 | */ |
| 7337 | __LJMP static int hlua_httpclient_get(lua_State *L) |
| 7338 | { |
| 7339 | return hlua_httpclient_send(L, HTTP_METH_GET); |
| 7340 | |
| 7341 | } |
| 7342 | |
| 7343 | /* |
| 7344 | * Sends an HTTP PUT request and wait for a response |
| 7345 | * |
| 7346 | * httpclient:put(url, headers, payload) |
| 7347 | */ |
| 7348 | __LJMP static int hlua_httpclient_put(lua_State *L) |
| 7349 | { |
| 7350 | return hlua_httpclient_send(L, HTTP_METH_PUT); |
| 7351 | } |
| 7352 | |
| 7353 | /* |
| 7354 | * Send an HTTP POST request and wait for a response |
| 7355 | * |
| 7356 | * httpclient:post(url, headers, payload) |
| 7357 | */ |
| 7358 | __LJMP static int hlua_httpclient_post(lua_State *L) |
| 7359 | { |
| 7360 | return hlua_httpclient_send(L, HTTP_METH_POST); |
| 7361 | } |
| 7362 | |
| 7363 | |
| 7364 | /* |
| 7365 | * Sends an HTTP DELETE request and wait for a response |
| 7366 | * |
| 7367 | * httpclient:delete(url, headers, payload) |
| 7368 | */ |
| 7369 | __LJMP static int hlua_httpclient_delete(lua_State *L) |
| 7370 | { |
| 7371 | return hlua_httpclient_send(L, HTTP_METH_DELETE); |
| 7372 | } |
| 7373 | |
| 7374 | /* |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7375 | * |
| 7376 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7377 | * Class TXN |
| 7378 | * |
| 7379 | * |
| 7380 | */ |
| 7381 | |
| 7382 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7383 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7384 | */ |
| 7385 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 7386 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7387 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7388 | } |
| 7389 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7390 | __LJMP static int hlua_set_var(lua_State *L) |
| 7391 | { |
| 7392 | struct hlua_txn *htxn; |
| 7393 | const char *name; |
| 7394 | size_t len; |
| 7395 | struct sample smp; |
| 7396 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 7397 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 7398 | 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] | 7399 | |
| 7400 | /* It is useles to retrieve the stream, but this function |
| 7401 | * runs only in a stream context. |
| 7402 | */ |
| 7403 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7404 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 7405 | |
| 7406 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 7407 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7408 | hlua_lua2smp(L, 3, &smp); |
| 7409 | |
| 7410 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 7411 | 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] | 7412 | |
| 7413 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 7414 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 7415 | else |
| 7416 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 7417 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 7418 | return 1; |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7419 | } |
| 7420 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 7421 | __LJMP static int hlua_unset_var(lua_State *L) |
| 7422 | { |
| 7423 | struct hlua_txn *htxn; |
| 7424 | const char *name; |
| 7425 | size_t len; |
| 7426 | struct sample smp; |
| 7427 | |
| 7428 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 7429 | |
| 7430 | /* It is useles to retrieve the stream, but this function |
| 7431 | * runs only in a stream context. |
| 7432 | */ |
| 7433 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7434 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 7435 | |
| 7436 | /* Unset the variable. */ |
| 7437 | 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] | 7438 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 7439 | return 1; |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 7440 | } |
| 7441 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7442 | __LJMP static int hlua_get_var(lua_State *L) |
| 7443 | { |
| 7444 | struct hlua_txn *htxn; |
| 7445 | const char *name; |
| 7446 | size_t len; |
| 7447 | struct sample smp; |
| 7448 | |
| 7449 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 7450 | |
| 7451 | /* It is useles to retrieve the stream, but this function |
| 7452 | * runs only in a stream context. |
| 7453 | */ |
| 7454 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7455 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 7456 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 7457 | 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] | 7458 | if (!vars_get_by_name(name, len, &smp, NULL)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7459 | lua_pushnil(L); |
| 7460 | return 1; |
| 7461 | } |
| 7462 | |
| 7463 | return hlua_smp2lua(L, &smp); |
| 7464 | } |
| 7465 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7466 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7467 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7468 | struct hlua *hlua; |
| 7469 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7470 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 7471 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7472 | /* It is useles to retrieve the stream, but this function |
| 7473 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7474 | */ |
| 7475 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7476 | |
| 7477 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7478 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7479 | if (!hlua) |
| 7480 | return 0; |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7481 | |
| 7482 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 7483 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7484 | |
| 7485 | /* Get and store new value. */ |
| 7486 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 7487 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 7488 | |
| 7489 | return 0; |
| 7490 | } |
| 7491 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7492 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7493 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7494 | struct hlua *hlua; |
| 7495 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7496 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 7497 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7498 | /* It is useles to retrieve the stream, but this function |
| 7499 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7500 | */ |
| 7501 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7502 | |
| 7503 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7504 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7505 | if (!hlua) { |
| 7506 | lua_pushnil(L); |
| 7507 | return 1; |
| 7508 | } |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7509 | |
| 7510 | /* Push configuration index in the stack. */ |
| 7511 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 7512 | |
| 7513 | return 1; |
| 7514 | } |
| 7515 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7516 | /* Create stack entry containing a class TXN. This function |
| 7517 | * return 0 if the stack does not contains free slots, |
| 7518 | * otherwise it returns 1. |
| 7519 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 7520 | 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] | 7521 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7522 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7523 | |
| 7524 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 7525 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7526 | return 0; |
| 7527 | |
| 7528 | /* NOTE: The allocation never fails. The failure |
| 7529 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7530 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7531 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 7532 | /* Create the object: obj[0] = userdata. */ |
| 7533 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7534 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 7535 | lua_rawseti(L, -2, 0); |
| 7536 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7537 | htxn->s = s; |
| 7538 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 7539 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 7540 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7541 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7542 | /* Create the "f" field that contains a list of fetches. */ |
| 7543 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 7544 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7545 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7546 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7547 | |
| 7548 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 7549 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 7550 | 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] | 7551 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7552 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7553 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7554 | /* Create the "c" field that contains a list of converters. */ |
| 7555 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7556 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7557 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7558 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7559 | |
| 7560 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 7561 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 7562 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7563 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7564 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7565 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7566 | /* Create the "req" field that contains the request channel object. */ |
| 7567 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 7568 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7569 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7570 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7571 | |
| 7572 | /* Create the "res" field that contains the response channel object. */ |
| 7573 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 7574 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7575 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7576 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7577 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7578 | /* Creates the HTTP object is the current proxy allows http. */ |
| 7579 | lua_pushstring(L, "http"); |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 7580 | if (IS_HTX_STRM(s)) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7581 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7582 | return 0; |
| 7583 | } |
| 7584 | else |
| 7585 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7586 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7587 | |
Christopher Faulet | 78c3547 | 2020-02-26 17:14:08 +0100 | [diff] [blame] | 7588 | if ((htxn->flags & HLUA_TXN_CTX_MASK) == HLUA_TXN_FLT_CTX) { |
| 7589 | /* HTTPMessage object are created when a lua TXN is created from |
| 7590 | * a filter context only |
| 7591 | */ |
| 7592 | |
| 7593 | /* Creates the HTTP-Request object is the current proxy allows http. */ |
| 7594 | lua_pushstring(L, "http_req"); |
| 7595 | if (p->mode == PR_MODE_HTTP) { |
| 7596 | if (!hlua_http_msg_new(L, &s->txn->req)) |
| 7597 | return 0; |
| 7598 | } |
| 7599 | else |
| 7600 | lua_pushnil(L); |
| 7601 | lua_rawset(L, -3); |
| 7602 | |
| 7603 | /* Creates the HTTP-Response object is the current proxy allows http. */ |
| 7604 | lua_pushstring(L, "http_res"); |
| 7605 | if (p->mode == PR_MODE_HTTP) { |
| 7606 | if (!hlua_http_msg_new(L, &s->txn->rsp)) |
| 7607 | return 0; |
| 7608 | } |
| 7609 | else |
| 7610 | lua_pushnil(L); |
| 7611 | lua_rawset(L, -3); |
| 7612 | } |
| 7613 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7614 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 7615 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 7616 | lua_setmetatable(L, -2); |
| 7617 | |
| 7618 | return 1; |
| 7619 | } |
| 7620 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 7621 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 7622 | { |
| 7623 | const char *msg; |
| 7624 | struct hlua_txn *htxn; |
| 7625 | |
| 7626 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 7627 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7628 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7629 | |
| 7630 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 7631 | return 0; |
| 7632 | } |
| 7633 | |
| 7634 | __LJMP static int hlua_txn_log(lua_State *L) |
| 7635 | { |
| 7636 | int level; |
| 7637 | const char *msg; |
| 7638 | struct hlua_txn *htxn; |
| 7639 | |
| 7640 | MAY_LJMP(check_args(L, 3, "log")); |
| 7641 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7642 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7643 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 7644 | |
| 7645 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 7646 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 7647 | |
| 7648 | hlua_sendlog(htxn->s->be, level, msg); |
| 7649 | return 0; |
| 7650 | } |
| 7651 | |
| 7652 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 7653 | { |
| 7654 | const char *msg; |
| 7655 | struct hlua_txn *htxn; |
| 7656 | |
| 7657 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 7658 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7659 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7660 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 7661 | return 0; |
| 7662 | } |
| 7663 | |
| 7664 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 7665 | { |
| 7666 | const char *msg; |
| 7667 | struct hlua_txn *htxn; |
| 7668 | |
| 7669 | MAY_LJMP(check_args(L, 2, "Info")); |
| 7670 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7671 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7672 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 7673 | return 0; |
| 7674 | } |
| 7675 | |
| 7676 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 7677 | { |
| 7678 | const char *msg; |
| 7679 | struct hlua_txn *htxn; |
| 7680 | |
| 7681 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 7682 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7683 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7684 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 7685 | return 0; |
| 7686 | } |
| 7687 | |
| 7688 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 7689 | { |
| 7690 | const char *msg; |
| 7691 | struct hlua_txn *htxn; |
| 7692 | |
| 7693 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 7694 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7695 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7696 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 7697 | return 0; |
| 7698 | } |
| 7699 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7700 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 7701 | { |
| 7702 | struct hlua_txn *htxn; |
| 7703 | int ll; |
| 7704 | |
| 7705 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 7706 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7707 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7708 | |
| 7709 | if (ll < 0 || ll > 7) |
| 7710 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 7711 | |
| 7712 | htxn->s->logs.level = ll; |
| 7713 | return 0; |
| 7714 | } |
| 7715 | |
| 7716 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 7717 | { |
| 7718 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7719 | int tos; |
| 7720 | |
| 7721 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 7722 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7723 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7724 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 7725 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7726 | return 0; |
| 7727 | } |
| 7728 | |
| 7729 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 7730 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7731 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7732 | int mark; |
| 7733 | |
| 7734 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 7735 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7736 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7737 | |
Lukas Tribus | 579e3e3 | 2019-08-11 18:03:45 +0200 | [diff] [blame] | 7738 | conn_set_mark(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7739 | return 0; |
| 7740 | } |
| 7741 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 7742 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 7743 | { |
| 7744 | struct hlua_txn *htxn; |
| 7745 | |
| 7746 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 7747 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7748 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 7749 | return 0; |
| 7750 | } |
| 7751 | |
| 7752 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 7753 | { |
| 7754 | struct hlua_txn *htxn; |
| 7755 | |
| 7756 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 7757 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7758 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 7759 | return 0; |
| 7760 | } |
| 7761 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7762 | /* 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] | 7763 | * 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] | 7764 | * message and terminate the transaction. It returns 1 on success and 0 on |
| 7765 | * error. The Reply must be on top of the stack. |
| 7766 | */ |
| 7767 | __LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s) |
| 7768 | { |
| 7769 | struct htx *htx; |
| 7770 | struct htx_sl *sl; |
| 7771 | struct h1m h1m; |
| 7772 | const char *status, *reason, *body; |
| 7773 | size_t status_len, reason_len, body_len; |
| 7774 | int ret, code, flags; |
| 7775 | |
| 7776 | code = 200; |
| 7777 | status = "200"; |
| 7778 | status_len = 3; |
| 7779 | ret = lua_getfield(L, -1, "status"); |
| 7780 | if (ret == LUA_TNUMBER) { |
| 7781 | code = lua_tointeger(L, -1); |
| 7782 | status = lua_tolstring(L, -1, &status_len); |
| 7783 | } |
| 7784 | lua_pop(L, 1); |
| 7785 | |
| 7786 | reason = http_get_reason(code); |
| 7787 | reason_len = strlen(reason); |
| 7788 | ret = lua_getfield(L, -1, "reason"); |
| 7789 | if (ret == LUA_TSTRING) |
| 7790 | reason = lua_tolstring(L, -1, &reason_len); |
| 7791 | lua_pop(L, 1); |
| 7792 | |
| 7793 | body = NULL; |
| 7794 | body_len = 0; |
| 7795 | ret = lua_getfield(L, -1, "body"); |
| 7796 | if (ret == LUA_TSTRING) |
| 7797 | body = lua_tolstring(L, -1, &body_len); |
| 7798 | lua_pop(L, 1); |
| 7799 | |
| 7800 | /* Prepare the response before inserting the headers */ |
| 7801 | h1m_init_res(&h1m); |
| 7802 | htx = htx_from_buf(&s->res.buf); |
| 7803 | channel_htx_truncate(&s->res, htx); |
| 7804 | if (s->txn->req.flags & HTTP_MSGF_VER_11) { |
| 7805 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 7806 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), |
| 7807 | ist2(status, status_len), ist2(reason, reason_len)); |
| 7808 | } |
| 7809 | else { |
| 7810 | flags = HTX_SL_F_IS_RESP; |
| 7811 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), |
| 7812 | ist2(status, status_len), ist2(reason, reason_len)); |
| 7813 | } |
| 7814 | if (!sl) |
| 7815 | goto fail; |
| 7816 | sl->info.res.status = code; |
| 7817 | |
| 7818 | /* Push in the stack the "headers" entry. */ |
| 7819 | ret = lua_getfield(L, -1, "headers"); |
| 7820 | if (ret != LUA_TTABLE) |
| 7821 | goto skip_headers; |
| 7822 | |
| 7823 | lua_pushnil(L); |
| 7824 | while (lua_next(L, -2) != 0) { |
| 7825 | struct ist name, value; |
| 7826 | const char *n, *v; |
| 7827 | size_t nlen, vlen; |
| 7828 | |
| 7829 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 7830 | /* Skip element if the key is not a string or if the value is not a table */ |
| 7831 | goto next_hdr; |
| 7832 | } |
| 7833 | |
| 7834 | n = lua_tolstring(L, -2, &nlen); |
| 7835 | name = ist2(n, nlen); |
| 7836 | if (isteqi(name, ist("content-length"))) { |
| 7837 | /* Always skip content-length header. It will be added |
| 7838 | * later with the correct len |
| 7839 | */ |
| 7840 | goto next_hdr; |
| 7841 | } |
| 7842 | |
| 7843 | /* Loop on header's values */ |
| 7844 | lua_pushnil(L); |
| 7845 | while (lua_next(L, -2)) { |
| 7846 | if (!lua_isstring(L, -1)) { |
| 7847 | /* Skip the value if it is not a string */ |
| 7848 | goto next_value; |
| 7849 | } |
| 7850 | |
| 7851 | v = lua_tolstring(L, -1, &vlen); |
| 7852 | value = ist2(v, vlen); |
| 7853 | |
| 7854 | if (isteqi(name, ist("transfer-encoding"))) |
| 7855 | h1_parse_xfer_enc_header(&h1m, value); |
| 7856 | if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen))) |
| 7857 | goto fail; |
| 7858 | |
| 7859 | next_value: |
| 7860 | lua_pop(L, 1); |
| 7861 | } |
| 7862 | |
| 7863 | next_hdr: |
| 7864 | lua_pop(L, 1); |
| 7865 | } |
| 7866 | skip_headers: |
| 7867 | lua_pop(L, 1); |
| 7868 | |
| 7869 | /* Update h1m flags: CLEN is set if CHNK is not present */ |
| 7870 | if (!(h1m.flags & H1_MF_CHNK)) { |
| 7871 | const char *clen = ultoa(body_len); |
| 7872 | |
| 7873 | h1m.flags |= H1_MF_CLEN; |
| 7874 | if (!htx_add_header(htx, ist("content-length"), ist(clen))) |
| 7875 | goto fail; |
| 7876 | } |
| 7877 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 7878 | h1m.flags |= H1_MF_XFER_LEN; |
| 7879 | |
| 7880 | /* Update HTX start-line flags */ |
| 7881 | if (h1m.flags & H1_MF_XFER_ENC) |
| 7882 | flags |= HTX_SL_F_XFER_ENC; |
| 7883 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 7884 | flags |= HTX_SL_F_XFER_LEN; |
| 7885 | if (h1m.flags & H1_MF_CHNK) |
| 7886 | flags |= HTX_SL_F_CHNK; |
| 7887 | else if (h1m.flags & H1_MF_CLEN) |
| 7888 | flags |= HTX_SL_F_CLEN; |
| 7889 | if (h1m.body_len == 0) |
| 7890 | flags |= HTX_SL_F_BODYLESS; |
| 7891 | } |
| 7892 | sl->flags |= flags; |
| 7893 | |
| 7894 | |
| 7895 | if (!htx_add_endof(htx, HTX_BLK_EOH) || |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 7896 | (body_len && !htx_add_data_atonce(htx, ist2(body, body_len)))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7897 | goto fail; |
| 7898 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 7899 | htx->flags |= HTX_FL_EOM; |
| 7900 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7901 | /* Now, forward the response and terminate the transaction */ |
| 7902 | s->txn->status = code; |
| 7903 | htx_to_buf(htx, &s->res.buf); |
| 7904 | if (!http_forward_proxy_resp(s, 1)) |
| 7905 | goto fail; |
| 7906 | |
| 7907 | return 1; |
| 7908 | |
| 7909 | fail: |
| 7910 | channel_htx_truncate(&s->res, htx); |
| 7911 | return 0; |
| 7912 | } |
| 7913 | |
| 7914 | /* Terminate a transaction if called from a lua action. For TCP streams, |
| 7915 | * processing is just aborted. Nothing is returned to the client and all |
| 7916 | * arguments are ignored. For HTTP streams, if a reply is passed as argument, it |
| 7917 | * is forwarded to the client before terminating the transaction. On success, |
| 7918 | * the function exits with ACT_RET_DONE code. If an error occurred, it exits |
| 7919 | * with ACT_RET_ERR code. If this function is not called from a lua action, it |
| 7920 | * just exits without any processing. |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 7921 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 7922 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 7923 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 7924 | struct hlua_txn *htxn; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7925 | struct stream *s; |
| 7926 | int finst; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 7927 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 7928 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 7929 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7930 | /* If the flags NOTERM is set, we cannot terminate the session, so we |
| 7931 | * just end the execution of the current lua code. */ |
| 7932 | if (htxn->flags & HLUA_TXN_NOTERM) |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 7933 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 7934 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7935 | s = htxn->s; |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 7936 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7937 | struct channel *req = &s->req; |
| 7938 | struct channel *res = &s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 7939 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7940 | channel_auto_read(req); |
| 7941 | channel_abort(req); |
| 7942 | channel_auto_close(req); |
| 7943 | channel_erase(req); |
| 7944 | |
| 7945 | res->wex = tick_add_ifset(now_ms, res->wto); |
| 7946 | channel_auto_read(res); |
| 7947 | channel_auto_close(res); |
| 7948 | channel_shutr_now(res); |
| 7949 | |
| 7950 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D); |
| 7951 | goto done; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 7952 | } |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 7953 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7954 | if (lua_gettop(L) == 1 || !lua_istable(L, 2)) { |
| 7955 | /* No reply or invalid reply */ |
| 7956 | s->txn->status = 0; |
| 7957 | http_reply_and_close(s, 0, NULL); |
| 7958 | } |
| 7959 | else { |
| 7960 | /* Remove extra args to have the reply on top of the stack */ |
| 7961 | if (lua_gettop(L) > 2) |
| 7962 | lua_pop(L, lua_gettop(L) - 2); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 7963 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7964 | if (!hlua_txn_forward_reply(L, s)) { |
| 7965 | if (!(s->flags & SF_ERR_MASK)) |
| 7966 | s->flags |= SF_ERR_PRXCOND; |
| 7967 | lua_pushinteger(L, ACT_RET_ERR); |
| 7968 | WILL_LJMP(hlua_done(L)); |
| 7969 | return 0; /* Never reached */ |
| 7970 | } |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 7971 | } |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 7972 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7973 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H); |
| 7974 | if (htxn->dir == SMP_OPT_DIR_REQ) { |
| 7975 | /* let's log the request time */ |
| 7976 | s->logs.tv_request = now; |
| 7977 | 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] | 7978 | _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7979 | } |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 7980 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7981 | done: |
| 7982 | if (!(s->flags & SF_ERR_MASK)) |
| 7983 | s->flags |= SF_ERR_LOCAL; |
| 7984 | if (!(s->flags & SF_FINST_MASK)) |
| 7985 | s->flags |= finst; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 7986 | |
Christopher Faulet | e48d1dc | 2021-08-13 14:11:17 +0200 | [diff] [blame] | 7987 | if ((htxn->flags & HLUA_TXN_CTX_MASK) == HLUA_TXN_FLT_CTX) |
| 7988 | lua_pushinteger(L, -1); |
| 7989 | else |
| 7990 | lua_pushinteger(L, ACT_RET_ABRT); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 7991 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 7992 | return 0; |
| 7993 | } |
| 7994 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7995 | /* |
| 7996 | * |
| 7997 | * |
| 7998 | * Class REPLY |
| 7999 | * |
| 8000 | * |
| 8001 | */ |
| 8002 | |
| 8003 | /* Pushes the TXN reply onto the top of the stack. If the stask does not have a |
| 8004 | * free slots, the function fails and returns 0; |
| 8005 | */ |
| 8006 | static int hlua_txn_reply_new(lua_State *L) |
| 8007 | { |
| 8008 | struct hlua_txn *htxn; |
| 8009 | const char *reason, *body = NULL; |
| 8010 | int ret, status; |
| 8011 | |
| 8012 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 8013 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8014 | hlua_pusherror(L, "txn object is not an HTTP transaction."); |
| 8015 | WILL_LJMP(lua_error(L)); |
| 8016 | } |
| 8017 | |
| 8018 | /* Default value */ |
| 8019 | status = 200; |
| 8020 | reason = http_get_reason(status); |
| 8021 | |
| 8022 | if (lua_istable(L, 2)) { |
| 8023 | /* load status and reason from the table argument at index 2 */ |
| 8024 | ret = lua_getfield(L, 2, "status"); |
| 8025 | if (ret == LUA_TNIL) |
| 8026 | goto reason; |
| 8027 | else if (ret != LUA_TNUMBER) { |
| 8028 | /* invalid status: ignore the reason */ |
| 8029 | goto body; |
| 8030 | } |
| 8031 | status = lua_tointeger(L, -1); |
| 8032 | |
| 8033 | reason: |
| 8034 | lua_pop(L, 1); /* restore the stack: remove status */ |
| 8035 | ret = lua_getfield(L, 2, "reason"); |
| 8036 | if (ret == LUA_TSTRING) |
| 8037 | reason = lua_tostring(L, -1); |
| 8038 | |
| 8039 | body: |
| 8040 | lua_pop(L, 1); /* restore the stack: remove invalid status or reason */ |
| 8041 | ret = lua_getfield(L, 2, "body"); |
| 8042 | if (ret == LUA_TSTRING) |
| 8043 | body = lua_tostring(L, -1); |
| 8044 | lua_pop(L, 1); /* restore the stack: remove body */ |
| 8045 | } |
| 8046 | |
| 8047 | /* Create the Reply table */ |
| 8048 | lua_newtable(L); |
| 8049 | |
| 8050 | /* Add status element */ |
| 8051 | lua_pushstring(L, "status"); |
| 8052 | lua_pushinteger(L, status); |
| 8053 | lua_settable(L, -3); |
| 8054 | |
| 8055 | /* Add reason element */ |
| 8056 | reason = http_get_reason(status); |
| 8057 | lua_pushstring(L, "reason"); |
| 8058 | lua_pushstring(L, reason); |
| 8059 | lua_settable(L, -3); |
| 8060 | |
| 8061 | /* Add body element, nil if undefined */ |
| 8062 | lua_pushstring(L, "body"); |
| 8063 | if (body) |
| 8064 | lua_pushstring(L, body); |
| 8065 | else |
| 8066 | lua_pushnil(L); |
| 8067 | lua_settable(L, -3); |
| 8068 | |
| 8069 | /* Add headers element */ |
| 8070 | lua_pushstring(L, "headers"); |
| 8071 | lua_newtable(L); |
| 8072 | |
| 8073 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 8074 | if (lua_istable(L, 2)) { |
| 8075 | /* load headers from the table argument at index 2. If it is a table, copy it. */ |
| 8076 | ret = lua_getfield(L, 2, "headers"); |
| 8077 | if (ret == LUA_TTABLE) { |
| 8078 | /* stack: [ ... <headers:table>, <table> ] */ |
| 8079 | lua_pushnil(L); |
| 8080 | while (lua_next(L, -2) != 0) { |
| 8081 | /* stack: [ ... <headers:table>, <table>, k, v] */ |
| 8082 | if (!lua_isstring(L, -1) && !lua_istable(L, -1)) { |
| 8083 | /* invalid value type, skip it */ |
| 8084 | lua_pop(L, 1); |
| 8085 | continue; |
| 8086 | } |
| 8087 | |
| 8088 | |
| 8089 | /* Duplicate the key and swap it with the value. */ |
| 8090 | lua_pushvalue(L, -2); |
| 8091 | lua_insert(L, -2); |
| 8092 | /* stack: [ ... <headers:table>, <table>, k, k, v ] */ |
| 8093 | |
| 8094 | lua_newtable(L); |
| 8095 | lua_insert(L, -2); |
| 8096 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */ |
| 8097 | |
| 8098 | if (lua_isstring(L, -1)) { |
| 8099 | /* push the value in the inner table */ |
| 8100 | lua_rawseti(L, -2, 1); |
| 8101 | } |
| 8102 | else { /* table */ |
| 8103 | lua_pushnil(L); |
| 8104 | while (lua_next(L, -2) != 0) { |
| 8105 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */ |
| 8106 | if (!lua_isstring(L, -1)) { |
| 8107 | /* invalid value type, skip it*/ |
| 8108 | lua_pop(L, 1); |
| 8109 | continue; |
| 8110 | } |
| 8111 | /* push the value in the inner table */ |
| 8112 | lua_rawseti(L, -4, lua_rawlen(L, -4) + 1); |
| 8113 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */ |
| 8114 | } |
| 8115 | lua_pop(L, 1); |
| 8116 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */ |
| 8117 | } |
| 8118 | |
| 8119 | /* push (k,v) on the stack in the headers table: |
| 8120 | * stack: [ ... <headers:table>, <table>, k, k, v ] |
| 8121 | */ |
| 8122 | lua_settable(L, -5); |
| 8123 | /* stack: [ ... <headers:table>, <table>, k ] */ |
| 8124 | } |
| 8125 | } |
| 8126 | lua_pop(L, 1); |
| 8127 | } |
| 8128 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 8129 | lua_settable(L, -3); |
| 8130 | /* stack: [ txn, <Arg:table>, <Reply:table> ] */ |
| 8131 | |
| 8132 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 8133 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref); |
| 8134 | lua_setmetatable(L, -2); |
| 8135 | return 1; |
| 8136 | } |
| 8137 | |
| 8138 | /* Set the reply status code, and optionally the reason. If no reason is |
| 8139 | * provided, the default one corresponding to the status code is used. |
| 8140 | */ |
| 8141 | __LJMP static int hlua_txn_reply_set_status(lua_State *L) |
| 8142 | { |
| 8143 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 8144 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 8145 | |
| 8146 | /* First argument (self) must be a table */ |
| 8147 | luaL_checktype(L, 1, LUA_TTABLE); |
| 8148 | |
| 8149 | if (status < 100 || status > 599) { |
| 8150 | lua_pushboolean(L, 0); |
| 8151 | return 1; |
| 8152 | } |
| 8153 | if (!reason) |
| 8154 | reason = http_get_reason(status); |
| 8155 | |
| 8156 | lua_pushinteger(L, status); |
| 8157 | lua_setfield(L, 1, "status"); |
| 8158 | |
| 8159 | lua_pushstring(L, reason); |
| 8160 | lua_setfield(L, 1, "reason"); |
| 8161 | |
| 8162 | lua_pushboolean(L, 1); |
| 8163 | return 1; |
| 8164 | } |
| 8165 | |
| 8166 | /* Add a header into the reply object. Each header name is associated to an |
| 8167 | * array of values in the "headers" table. If the header name is not found, a |
| 8168 | * new entry is created. |
| 8169 | */ |
| 8170 | __LJMP static int hlua_txn_reply_add_header(lua_State *L) |
| 8171 | { |
| 8172 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8173 | const char *value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 8174 | int ret; |
| 8175 | |
| 8176 | /* First argument (self) must be a table */ |
| 8177 | luaL_checktype(L, 1, LUA_TTABLE); |
| 8178 | |
| 8179 | /* Push in the stack the "headers" entry. */ |
| 8180 | ret = lua_getfield(L, 1, "headers"); |
| 8181 | if (ret != LUA_TTABLE) { |
| 8182 | hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret)); |
| 8183 | WILL_LJMP(lua_error(L)); |
| 8184 | } |
| 8185 | |
| 8186 | /* check if the header is already registered. If not, register it. */ |
| 8187 | ret = lua_getfield(L, -1, name); |
| 8188 | if (ret == LUA_TNIL) { |
| 8189 | /* Entry not found. */ |
| 8190 | lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */ |
| 8191 | |
| 8192 | /* Insert the new header name in the array in the top of the stack. |
| 8193 | * It left the new array in the top of the stack. |
| 8194 | */ |
| 8195 | lua_newtable(L); |
| 8196 | lua_pushstring(L, name); |
| 8197 | lua_pushvalue(L, -2); |
| 8198 | lua_settable(L, -4); |
| 8199 | } |
| 8200 | else if (ret != LUA_TTABLE) { |
| 8201 | hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret)); |
| 8202 | WILL_LJMP(lua_error(L)); |
| 8203 | } |
| 8204 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8205 | /* Now the top of thestack is an array of values. We push |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8206 | * the header value as new entry. |
| 8207 | */ |
| 8208 | lua_pushstring(L, value); |
| 8209 | ret = lua_rawlen(L, -2); |
| 8210 | lua_rawseti(L, -2, ret + 1); |
| 8211 | |
| 8212 | lua_pushboolean(L, 1); |
| 8213 | return 1; |
| 8214 | } |
| 8215 | |
| 8216 | /* Remove all occurrences of a given header name. */ |
| 8217 | __LJMP static int hlua_txn_reply_del_header(lua_State *L) |
| 8218 | { |
| 8219 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8220 | int ret; |
| 8221 | |
| 8222 | /* First argument (self) must be a table */ |
| 8223 | luaL_checktype(L, 1, LUA_TTABLE); |
| 8224 | |
| 8225 | /* Push in the stack the "headers" entry. */ |
| 8226 | ret = lua_getfield(L, 1, "headers"); |
| 8227 | if (ret != LUA_TTABLE) { |
| 8228 | hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret)); |
| 8229 | WILL_LJMP(lua_error(L)); |
| 8230 | } |
| 8231 | |
| 8232 | lua_pushstring(L, name); |
| 8233 | lua_pushnil(L); |
| 8234 | lua_settable(L, -3); |
| 8235 | |
| 8236 | lua_pushboolean(L, 1); |
| 8237 | return 1; |
| 8238 | } |
| 8239 | |
| 8240 | /* Set the reply's body. Overwrite any existing entry. */ |
| 8241 | __LJMP static int hlua_txn_reply_set_body(lua_State *L) |
| 8242 | { |
| 8243 | const char *payload = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8244 | |
| 8245 | /* First argument (self) must be a table */ |
| 8246 | luaL_checktype(L, 1, LUA_TTABLE); |
| 8247 | |
| 8248 | lua_pushstring(L, payload); |
| 8249 | lua_setfield(L, 1, "body"); |
| 8250 | |
| 8251 | lua_pushboolean(L, 1); |
| 8252 | return 1; |
| 8253 | } |
| 8254 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 8255 | __LJMP static int hlua_log(lua_State *L) |
| 8256 | { |
| 8257 | int level; |
| 8258 | const char *msg; |
| 8259 | |
| 8260 | MAY_LJMP(check_args(L, 2, "log")); |
| 8261 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 8262 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8263 | |
| 8264 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 8265 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 8266 | |
| 8267 | hlua_sendlog(NULL, level, msg); |
| 8268 | return 0; |
| 8269 | } |
| 8270 | |
| 8271 | __LJMP static int hlua_log_debug(lua_State *L) |
| 8272 | { |
| 8273 | const char *msg; |
| 8274 | |
| 8275 | MAY_LJMP(check_args(L, 1, "debug")); |
| 8276 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8277 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 8278 | return 0; |
| 8279 | } |
| 8280 | |
| 8281 | __LJMP static int hlua_log_info(lua_State *L) |
| 8282 | { |
| 8283 | const char *msg; |
| 8284 | |
| 8285 | MAY_LJMP(check_args(L, 1, "info")); |
| 8286 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8287 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 8288 | return 0; |
| 8289 | } |
| 8290 | |
| 8291 | __LJMP static int hlua_log_warning(lua_State *L) |
| 8292 | { |
| 8293 | const char *msg; |
| 8294 | |
| 8295 | MAY_LJMP(check_args(L, 1, "warning")); |
| 8296 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8297 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 8298 | return 0; |
| 8299 | } |
| 8300 | |
| 8301 | __LJMP static int hlua_log_alert(lua_State *L) |
| 8302 | { |
| 8303 | const char *msg; |
| 8304 | |
| 8305 | MAY_LJMP(check_args(L, 1, "alert")); |
| 8306 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8307 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8308 | return 0; |
| 8309 | } |
| 8310 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8311 | __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] | 8312 | { |
| 8313 | int wakeup_ms = lua_tointeger(L, -1); |
Willy Tarreau | 12c0270 | 2021-09-30 16:12:31 +0200 | [diff] [blame] | 8314 | if (!tick_is_expired(wakeup_ms, now_ms)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8315 | 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] | 8316 | return 0; |
| 8317 | } |
| 8318 | |
| 8319 | __LJMP static int hlua_sleep(lua_State *L) |
| 8320 | { |
| 8321 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8322 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8323 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8324 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8325 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8326 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8327 | wakeup_ms = tick_add(now_ms, delay); |
| 8328 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8329 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8330 | 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] | 8331 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8332 | } |
| 8333 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8334 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8335 | { |
| 8336 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8337 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8338 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8339 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8340 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8341 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8342 | wakeup_ms = tick_add(now_ms, delay); |
| 8343 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8344 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8345 | 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] | 8346 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8347 | } |
| 8348 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8349 | /* This functionis an LUA binding. it permits to give back |
| 8350 | * the hand at the HAProxy scheduler. It is used when the |
| 8351 | * LUA processing consumes a lot of time. |
| 8352 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8353 | __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] | 8354 | { |
| 8355 | return 0; |
| 8356 | } |
| 8357 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8358 | __LJMP static int hlua_yield(lua_State *L) |
| 8359 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8360 | 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] | 8361 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8362 | } |
| 8363 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8364 | /* This function change the nice of the currently executed |
| 8365 | * task. It is used set low or high priority at the current |
| 8366 | * task. |
| 8367 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 8368 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8369 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8370 | struct hlua *hlua; |
| 8371 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8372 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8373 | MAY_LJMP(check_args(L, 1, "set_nice")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8374 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8375 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8376 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 8377 | hlua = hlua_gethlua(L); |
| 8378 | |
| 8379 | /* If the task is not set, I'm in a start mode. */ |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8380 | if (!hlua || !hlua->task) |
| 8381 | return 0; |
| 8382 | |
| 8383 | if (nice < -1024) |
| 8384 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8385 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8386 | nice = 1024; |
| 8387 | |
| 8388 | hlua->task->nice = nice; |
| 8389 | return 0; |
| 8390 | } |
| 8391 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8392 | /* 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] | 8393 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 8394 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 8395 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8396 | * |
| 8397 | * Task wrapper are longjmp safe because the only one Lua code |
| 8398 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8399 | */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 8400 | 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] | 8401 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 8402 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8403 | enum hlua_exec status; |
| 8404 | |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 8405 | if (task->thread_mask == MAX_THREADS_MASK) |
| 8406 | task_set_affinity(task, tid_bit); |
| 8407 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8408 | /* If it is the first call to the task, we must initialize the |
| 8409 | * execution timeouts. |
| 8410 | */ |
| 8411 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 8412 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8413 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8414 | /* Execute the Lua code. */ |
| 8415 | status = hlua_ctx_resume(hlua, 1); |
| 8416 | |
| 8417 | switch (status) { |
| 8418 | /* finished or yield */ |
| 8419 | case HLUA_E_OK: |
| 8420 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 8421 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 8422 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8423 | break; |
| 8424 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 8425 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 8426 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 8427 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8428 | break; |
| 8429 | |
| 8430 | /* finished with error. */ |
| 8431 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8432 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8433 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 8434 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 8435 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8436 | break; |
| 8437 | |
| 8438 | case HLUA_E_ERR: |
| 8439 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8440 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8441 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 8442 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 8443 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8444 | break; |
| 8445 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 8446 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8447 | } |
| 8448 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8449 | /* This function is an LUA binding that register LUA function to be |
| 8450 | * executed after the HAProxy configuration parsing and before the |
| 8451 | * HAProxy scheduler starts. This function expect only one LUA |
| 8452 | * argument that is a function. This function returns nothing, but |
| 8453 | * throws if an error is encountered. |
| 8454 | */ |
| 8455 | __LJMP static int hlua_register_init(lua_State *L) |
| 8456 | { |
| 8457 | struct hlua_init_function *init; |
| 8458 | int ref; |
| 8459 | |
| 8460 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 8461 | |
| 8462 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 8463 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8464 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8465 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8466 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8467 | |
| 8468 | init->function_ref = ref; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 8469 | LIST_APPEND(&hlua_init_functions[hlua_state_id], &init->l); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8470 | return 0; |
| 8471 | } |
| 8472 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8473 | /* This functio is an LUA binding. It permits to register a task |
| 8474 | * executed in parallel of the main HAroxy activity. The task is |
| 8475 | * created and it is set in the HAProxy scheduler. It can be called |
| 8476 | * from the "init" section, "post init" or during the runtime. |
| 8477 | * |
| 8478 | * Lua prototype: |
| 8479 | * |
| 8480 | * <none> core.register_task(<function>) |
| 8481 | */ |
| 8482 | static int hlua_register_task(lua_State *L) |
| 8483 | { |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8484 | struct hlua *hlua = NULL; |
| 8485 | struct task *task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8486 | int ref; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8487 | int state_id; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8488 | |
| 8489 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 8490 | |
| 8491 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 8492 | |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 8493 | /* Get the reference state. If the reference is NULL, L is the master |
| 8494 | * state, otherwise hlua->T is. |
| 8495 | */ |
| 8496 | hlua = hlua_gethlua(L); |
| 8497 | if (hlua) |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8498 | /* we are in runtime processing */ |
| 8499 | state_id = hlua->state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 8500 | else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8501 | /* we are in initialization mode */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8502 | state_id = hlua_state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 8503 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8504 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8505 | if (!hlua) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8506 | goto alloc_error; |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8507 | HLUA_INIT(hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8508 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8509 | /* We are in the common lua state, execute the task anywhere, |
| 8510 | * otherwise, inherit the current thread identifier |
| 8511 | */ |
| 8512 | if (state_id == 0) |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 8513 | task = task_new_anywhere(); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8514 | else |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 8515 | task = task_new_here(); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 8516 | if (!task) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8517 | goto alloc_error; |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 8518 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8519 | task->context = hlua; |
| 8520 | task->process = hlua_process_task; |
| 8521 | |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8522 | if (!hlua_ctx_init(hlua, state_id, task, 1)) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8523 | goto alloc_error; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8524 | |
| 8525 | /* Restore the function in the stack. */ |
| 8526 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 8527 | hlua->nargs = 0; |
| 8528 | |
| 8529 | /* Schedule task. */ |
Willy Tarreau | e3957f8 | 2021-09-30 16:17:37 +0200 | [diff] [blame] | 8530 | task_wakeup(task, TASK_WOKEN_INIT); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8531 | |
| 8532 | return 0; |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8533 | |
| 8534 | alloc_error: |
| 8535 | task_destroy(task); |
| 8536 | hlua_ctx_destroy(hlua); |
| 8537 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 8538 | return 0; /* Never reached */ |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8539 | } |
| 8540 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8541 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 8542 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 8543 | * resume converters. |
| 8544 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8545 | 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] | 8546 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 8547 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8548 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8549 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8550 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8551 | if (!stream) |
| 8552 | return 0; |
| 8553 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8554 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8555 | * Lua context can be not initialized. This behavior |
| 8556 | * permits to save performances because a systematic |
| 8557 | * Lua initialization cause 5% performances loss. |
| 8558 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8559 | if (!stream->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8560 | struct hlua *hlua; |
| 8561 | |
| 8562 | hlua = pool_alloc(pool_head_hlua); |
| 8563 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8564 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 8565 | return 0; |
| 8566 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8567 | HLUA_INIT(hlua); |
| 8568 | stream->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8569 | 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] | 8570 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 8571 | return 0; |
| 8572 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8573 | } |
| 8574 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8575 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8576 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8577 | |
| 8578 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8579 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8580 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 8581 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8582 | else |
| 8583 | error = "critical error"; |
| 8584 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8585 | return 0; |
| 8586 | } |
| 8587 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8588 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8589 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8590 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8591 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8592 | return 0; |
| 8593 | } |
| 8594 | |
| 8595 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8596 | 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] | 8597 | |
| 8598 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8599 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8600 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8601 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8602 | return 0; |
| 8603 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8604 | hlua_smp2lua(stream->hlua->T, smp); |
| 8605 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8606 | |
| 8607 | /* push keywords in the stack. */ |
| 8608 | if (arg_p) { |
| 8609 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8610 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8611 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8612 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8613 | return 0; |
| 8614 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8615 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 8616 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8617 | } |
| 8618 | } |
| 8619 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8620 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8621 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8622 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8623 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8624 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8625 | } |
| 8626 | |
| 8627 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8628 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8629 | /* finished. */ |
| 8630 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 8631 | /* If the stack is empty, the function fails. */ |
| 8632 | if (lua_gettop(stream->hlua->T) <= 0) |
| 8633 | return 0; |
| 8634 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8635 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8636 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 8637 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8638 | return 1; |
| 8639 | |
| 8640 | /* yield. */ |
| 8641 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8642 | 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] | 8643 | return 0; |
| 8644 | |
| 8645 | /* finished with error. */ |
| 8646 | case HLUA_E_ERRMSG: |
| 8647 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8648 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8649 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 8650 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8651 | return 0; |
| 8652 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8653 | case HLUA_E_ETMOUT: |
| 8654 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 8655 | return 0; |
| 8656 | |
| 8657 | case HLUA_E_NOMEM: |
| 8658 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 8659 | return 0; |
| 8660 | |
| 8661 | case HLUA_E_YIELD: |
| 8662 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 8663 | return 0; |
| 8664 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8665 | case HLUA_E_ERR: |
| 8666 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8667 | 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] | 8668 | /* fall through */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8669 | |
| 8670 | default: |
| 8671 | return 0; |
| 8672 | } |
| 8673 | } |
| 8674 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8675 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 8676 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8677 | * resume sample-fetches. This function will be called by the sample |
| 8678 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8679 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8680 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 8681 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8682 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 8683 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8684 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8685 | const char *error; |
Christopher Faulet | 8c9e6bb | 2021-08-06 16:29:41 +0200 | [diff] [blame] | 8686 | unsigned int hflags = HLUA_TXN_NOTERM | HLUA_TXN_SMP_CTX; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8687 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8688 | if (!stream) |
| 8689 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 8690 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8691 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8692 | * Lua context can be not initialized. This behavior |
| 8693 | * permits to save performances because a systematic |
| 8694 | * Lua initialization cause 5% performances loss. |
| 8695 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8696 | if (!stream->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8697 | struct hlua *hlua; |
| 8698 | |
| 8699 | hlua = pool_alloc(pool_head_hlua); |
| 8700 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8701 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 8702 | return 0; |
| 8703 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8704 | hlua->T = NULL; |
| 8705 | stream->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8706 | 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] | 8707 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 8708 | return 0; |
| 8709 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8710 | } |
| 8711 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8712 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8713 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8714 | |
| 8715 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8716 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8717 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 8718 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8719 | else |
| 8720 | error = "critical error"; |
| 8721 | 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] | 8722 | return 0; |
| 8723 | } |
| 8724 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8725 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8726 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8727 | 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] | 8728 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8729 | return 0; |
| 8730 | } |
| 8731 | |
| 8732 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8733 | 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] | 8734 | |
| 8735 | /* push arguments in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 8736 | 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] | 8737 | 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] | 8738 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8739 | return 0; |
| 8740 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8741 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8742 | |
| 8743 | /* push keywords in the stack. */ |
| 8744 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 8745 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8746 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8747 | 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] | 8748 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8749 | return 0; |
| 8750 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8751 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 8752 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8753 | } |
| 8754 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8755 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8756 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8757 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8758 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8759 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8760 | } |
| 8761 | |
| 8762 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8763 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8764 | /* finished. */ |
| 8765 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 8766 | /* If the stack is empty, the function fails. */ |
| 8767 | if (lua_gettop(stream->hlua->T) <= 0) |
| 8768 | return 0; |
| 8769 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8770 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8771 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 8772 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8773 | |
| 8774 | /* Set the end of execution flag. */ |
| 8775 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 8776 | return 1; |
| 8777 | |
| 8778 | /* yield. */ |
| 8779 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8780 | 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] | 8781 | return 0; |
| 8782 | |
| 8783 | /* finished with error. */ |
| 8784 | case HLUA_E_ERRMSG: |
| 8785 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8786 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8787 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 8788 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8789 | return 0; |
| 8790 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8791 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8792 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 8793 | return 0; |
| 8794 | |
| 8795 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8796 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 8797 | return 0; |
| 8798 | |
| 8799 | case HLUA_E_YIELD: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8800 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 8801 | return 0; |
| 8802 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8803 | case HLUA_E_ERR: |
| 8804 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8805 | 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] | 8806 | /* fall through */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8807 | |
| 8808 | default: |
| 8809 | return 0; |
| 8810 | } |
| 8811 | } |
| 8812 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8813 | /* This function is an LUA binding used for registering |
| 8814 | * "sample-conv" functions. It expects a converter name used |
| 8815 | * in the haproxy configuration file, and an LUA function. |
| 8816 | */ |
| 8817 | __LJMP static int hlua_register_converters(lua_State *L) |
| 8818 | { |
| 8819 | struct sample_conv_kw_list *sck; |
| 8820 | const char *name; |
| 8821 | int ref; |
| 8822 | int len; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8823 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8824 | struct sample_conv *sc; |
| 8825 | struct buffer *trash; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8826 | |
| 8827 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 8828 | |
| 8829 | /* First argument : converter name. */ |
| 8830 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8831 | |
| 8832 | /* Second argument : lua function. */ |
| 8833 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 8834 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8835 | /* Check if the converter is already registered */ |
| 8836 | trash = get_trash_chunk(); |
| 8837 | chunk_printf(trash, "lua.%s", name); |
| 8838 | sc = find_sample_conv(trash->area, trash->data); |
| 8839 | if (sc != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8840 | fcn = sc->private; |
| 8841 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 8842 | ha_warning("Trying to register converter 'lua.%s' more than once. " |
| 8843 | "This will become a hard error in version 2.5.\n", name); |
| 8844 | } |
| 8845 | fcn->function_ref[hlua_state_id] = ref; |
| 8846 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8847 | } |
| 8848 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8849 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8850 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8851 | if (!sck) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8852 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 8853 | fcn = new_hlua_function(); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8854 | if (!fcn) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8855 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8856 | |
| 8857 | /* Fill fcn. */ |
| 8858 | fcn->name = strdup(name); |
| 8859 | if (!fcn->name) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8860 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8861 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8862 | |
| 8863 | /* List head */ |
| 8864 | sck->list.n = sck->list.p = NULL; |
| 8865 | |
| 8866 | /* converter keyword. */ |
| 8867 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8868 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8869 | if (!sck->kw[0].kw) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8870 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8871 | |
| 8872 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 8873 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 8874 | 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] | 8875 | sck->kw[0].val_args = NULL; |
| 8876 | sck->kw[0].in_type = SMP_T_STR; |
| 8877 | sck->kw[0].out_type = SMP_T_STR; |
| 8878 | sck->kw[0].private = fcn; |
| 8879 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8880 | /* Register this new converter */ |
| 8881 | sample_register_convs(sck); |
| 8882 | |
| 8883 | return 0; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8884 | |
| 8885 | alloc_error: |
| 8886 | release_hlua_function(fcn); |
| 8887 | ha_free(&sck); |
| 8888 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 8889 | return 0; /* Never reached */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8890 | } |
| 8891 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8892 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8893 | * "sample-fetch" functions. It expects a converter name used |
| 8894 | * in the haproxy configuration file, and an LUA function. |
| 8895 | */ |
| 8896 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 8897 | { |
| 8898 | const char *name; |
| 8899 | int ref; |
| 8900 | int len; |
| 8901 | struct sample_fetch_kw_list *sfk; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8902 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8903 | struct sample_fetch *sf; |
| 8904 | struct buffer *trash; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8905 | |
| 8906 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 8907 | |
| 8908 | /* First argument : sample-fetch name. */ |
| 8909 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8910 | |
| 8911 | /* Second argument : lua function. */ |
| 8912 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 8913 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8914 | /* Check if the sample-fetch is already registered */ |
| 8915 | trash = get_trash_chunk(); |
| 8916 | chunk_printf(trash, "lua.%s", name); |
| 8917 | sf = find_sample_fetch(trash->area, trash->data); |
| 8918 | if (sf != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8919 | fcn = sf->private; |
| 8920 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 8921 | ha_warning("Trying to register sample-fetch 'lua.%s' more than once. " |
| 8922 | "This will become a hard error in version 2.5.\n", name); |
| 8923 | } |
| 8924 | fcn->function_ref[hlua_state_id] = ref; |
| 8925 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8926 | } |
| 8927 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8928 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8929 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8930 | if (!sfk) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8931 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 8932 | fcn = new_hlua_function(); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8933 | if (!fcn) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8934 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8935 | |
| 8936 | /* Fill fcn. */ |
| 8937 | fcn->name = strdup(name); |
| 8938 | if (!fcn->name) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8939 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8940 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8941 | |
| 8942 | /* List head */ |
| 8943 | sfk->list.n = sfk->list.p = NULL; |
| 8944 | |
| 8945 | /* sample-fetch keyword. */ |
| 8946 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8947 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8948 | if (!sfk->kw[0].kw) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8949 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8950 | |
| 8951 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 8952 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 8953 | 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] | 8954 | sfk->kw[0].val_args = NULL; |
| 8955 | sfk->kw[0].out_type = SMP_T_STR; |
| 8956 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 8957 | sfk->kw[0].val = 0; |
| 8958 | sfk->kw[0].private = fcn; |
| 8959 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8960 | /* Register this new fetch. */ |
| 8961 | sample_register_fetches(sfk); |
| 8962 | |
| 8963 | return 0; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8964 | |
| 8965 | alloc_error: |
| 8966 | release_hlua_function(fcn); |
| 8967 | ha_free(&sfk); |
| 8968 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 8969 | return 0; /* Never reached */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8970 | } |
| 8971 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 8972 | /* This function is a lua binding to set the wake_time. |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8973 | */ |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 8974 | __LJMP static int hlua_set_wake_time(lua_State *L) |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8975 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8976 | struct hlua *hlua; |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8977 | unsigned int delay; |
| 8978 | unsigned int wakeup_ms; |
| 8979 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8980 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 8981 | hlua = hlua_gethlua(L); |
| 8982 | if (!hlua) { |
| 8983 | return 0; |
| 8984 | } |
| 8985 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8986 | MAY_LJMP(check_args(L, 1, "wake_time")); |
| 8987 | |
| 8988 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 8989 | wakeup_ms = tick_add(now_ms, delay); |
| 8990 | hlua->wake_time = wakeup_ms; |
| 8991 | return 0; |
| 8992 | } |
| 8993 | |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8994 | /* This function is a wrapper to execute each LUA function declared as an action |
| 8995 | * wrapper during the initialisation period. This function may return any |
| 8996 | * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is |
| 8997 | * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the |
| 8998 | * return value is the first element on the stack. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8999 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9000 | 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] | 9001 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9002 | { |
| 9003 | char **arg; |
Christopher Faulet | 8c9e6bb | 2021-08-06 16:29:41 +0200 | [diff] [blame] | 9004 | unsigned int hflags = HLUA_TXN_ACT_CTX; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9005 | int dir, act_ret = ACT_RET_CONT; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9006 | const char *error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9007 | |
| 9008 | switch (rule->from) { |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 9009 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 9010 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 9011 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 9012 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9013 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9014 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9015 | goto end; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9016 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9017 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 9018 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 9019 | * Lua context can be not initialized. This behavior |
| 9020 | * permits to save performances because a systematic |
| 9021 | * Lua initialization cause 5% performances loss. |
| 9022 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9023 | if (!s->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 9024 | struct hlua *hlua; |
| 9025 | |
| 9026 | hlua = pool_alloc(pool_head_hlua); |
| 9027 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9028 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9029 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9030 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9031 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 9032 | HLUA_INIT(hlua); |
| 9033 | s->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9034 | 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] | 9035 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9036 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9037 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9038 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 9039 | } |
| 9040 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9041 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9042 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9043 | |
| 9044 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9045 | if (!SET_SAFE_LJMP(s->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9046 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 9047 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9048 | else |
| 9049 | error = "critical error"; |
| 9050 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9051 | rule->arg.hlua_rule->fcn->name, error); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9052 | goto end; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9053 | } |
| 9054 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9055 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9056 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9057 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9058 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9059 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9060 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9061 | } |
| 9062 | |
| 9063 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9064 | 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] | 9065 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 9066 | /* Create and and push object stream in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 9067 | if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9068 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9069 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9070 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9071 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9072 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9073 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9074 | |
| 9075 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9076 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9077 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9078 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9079 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9080 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9081 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9082 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9083 | lua_pushstring(s->hlua->T, *arg); |
| 9084 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9085 | } |
| 9086 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9087 | /* Now the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9088 | RESET_SAFE_LJMP(s->hlua); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9089 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 9090 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9091 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9092 | } |
| 9093 | |
| 9094 | /* Execute the function. */ |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 9095 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9096 | /* finished. */ |
| 9097 | case HLUA_E_OK: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9098 | /* Catch the return value */ |
| 9099 | if (lua_gettop(s->hlua->T) > 0) |
| 9100 | act_ret = lua_tointeger(s->hlua->T, -1); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9101 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9102 | /* Set timeout in the required channel. */ |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 9103 | if (act_ret == ACT_RET_YIELD) { |
| 9104 | if (flags & ACT_OPT_FINAL) |
| 9105 | goto err_yield; |
| 9106 | |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 9107 | if (dir == SMP_OPT_DIR_REQ) |
| 9108 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 9109 | s->hlua->wake_time); |
| 9110 | else |
| 9111 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 9112 | s->hlua->wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9113 | } |
| 9114 | goto end; |
| 9115 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9116 | /* yield. */ |
| 9117 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 9118 | /* Set timeout in the required channel. */ |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 9119 | if (dir == SMP_OPT_DIR_REQ) |
| 9120 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 9121 | s->hlua->wake_time); |
| 9122 | else |
| 9123 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 9124 | s->hlua->wake_time); |
| 9125 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9126 | /* Some actions can be wake up when a "write" event |
| 9127 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9128 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9129 | */ |
Christopher Faulet | 51fa358 | 2019-07-26 14:54:52 +0200 | [diff] [blame] | 9130 | if (HLUA_IS_WAKERESWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9131 | s->res.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9132 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9133 | s->req.flags |= CF_WAKE_WRITE; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9134 | act_ret = ACT_RET_YIELD; |
| 9135 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9136 | |
| 9137 | /* finished with error. */ |
| 9138 | case HLUA_E_ERRMSG: |
| 9139 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9140 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9141 | rule->arg.hlua_rule->fcn->name, lua_tostring(s->hlua->T, -1)); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9142 | lua_pop(s->hlua->T, 1); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9143 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9144 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9145 | case HLUA_E_ETMOUT: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9146 | 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] | 9147 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9148 | |
| 9149 | case HLUA_E_NOMEM: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9150 | 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] | 9151 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9152 | |
| 9153 | case HLUA_E_YIELD: |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 9154 | err_yield: |
| 9155 | act_ret = ACT_RET_CONT; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9156 | 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] | 9157 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9158 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9159 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9160 | case HLUA_E_ERR: |
| 9161 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9162 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9163 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9164 | |
| 9165 | default: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9166 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9167 | } |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9168 | |
| 9169 | end: |
Christopher Faulet | 2361fd9 | 2020-07-30 10:40:58 +0200 | [diff] [blame] | 9170 | if (act_ret != ACT_RET_YIELD && s->hlua) |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 9171 | s->hlua->wake_time = TICK_ETERNITY; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9172 | return act_ret; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9173 | } |
| 9174 | |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 9175 | 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] | 9176 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 9177 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9178 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9179 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 9180 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 9181 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9182 | } |
| 9183 | |
| 9184 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 9185 | { |
Christopher Faulet | 86e1c33 | 2021-12-20 17:09:39 +0100 | [diff] [blame] | 9186 | struct stream_interface *si = cs_si(ctx->owner); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9187 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9188 | struct task *task; |
| 9189 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9190 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9191 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 9192 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9193 | if (!hlua) { |
| 9194 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9195 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9196 | return 0; |
| 9197 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9198 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9199 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9200 | ctx->ctx.hlua_apptcp.flags = 0; |
| 9201 | |
| 9202 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 9203 | task = task_new_here(); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9204 | if (!task) { |
| 9205 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9206 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9207 | return 0; |
| 9208 | } |
| 9209 | task->nice = 0; |
| 9210 | task->context = ctx; |
| 9211 | task->process = hlua_applet_wakeup; |
| 9212 | ctx->ctx.hlua_apptcp.task = task; |
| 9213 | |
| 9214 | /* In the execution wrappers linked with a stream, the |
| 9215 | * Lua context can be not initialized. This behavior |
| 9216 | * permits to save performances because a systematic |
| 9217 | * Lua initialization cause 5% performances loss. |
| 9218 | */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9219 | 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] | 9220 | 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] | 9221 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9222 | return 0; |
| 9223 | } |
| 9224 | |
| 9225 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 9226 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9227 | |
| 9228 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9229 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9230 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 9231 | error = lua_tostring(hlua->T, -1); |
| 9232 | else |
| 9233 | error = "critical error"; |
| 9234 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9235 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9236 | return 0; |
| 9237 | } |
| 9238 | |
| 9239 | /* Check stack available size. */ |
| 9240 | if (!lua_checkstack(hlua->T, 1)) { |
| 9241 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9242 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9243 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9244 | return 0; |
| 9245 | } |
| 9246 | |
| 9247 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9248 | 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] | 9249 | |
| 9250 | /* Create and and push object stream in the stack. */ |
| 9251 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 9252 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9253 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9254 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9255 | return 0; |
| 9256 | } |
| 9257 | hlua->nargs = 1; |
| 9258 | |
| 9259 | /* push keywords in the stack. */ |
| 9260 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 9261 | if (!lua_checkstack(hlua->T, 1)) { |
| 9262 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9263 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9264 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9265 | return 0; |
| 9266 | } |
| 9267 | lua_pushstring(hlua->T, *arg); |
| 9268 | hlua->nargs++; |
| 9269 | } |
| 9270 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9271 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9272 | |
| 9273 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 9274 | si_cant_get(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 9275 | si_rx_endp_more(si); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9276 | |
| 9277 | return 1; |
| 9278 | } |
| 9279 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 9280 | void hlua_applet_tcp_fct(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9281 | { |
Christopher Faulet | 86e1c33 | 2021-12-20 17:09:39 +0100 | [diff] [blame] | 9282 | struct stream_interface *si = cs_si(ctx->owner); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9283 | struct stream *strm = si_strm(si); |
| 9284 | struct channel *res = si_ic(si); |
| 9285 | struct act_rule *rule = ctx->rule; |
| 9286 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9287 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9288 | |
| 9289 | /* The applet execution is already done. */ |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 9290 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) { |
| 9291 | /* eat the whole request */ |
| 9292 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9293 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 9294 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9295 | |
| 9296 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 9297 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 9298 | return; |
| 9299 | |
| 9300 | /* Execute the function. */ |
| 9301 | switch (hlua_ctx_resume(hlua, 1)) { |
| 9302 | /* finished. */ |
| 9303 | case HLUA_E_OK: |
| 9304 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 9305 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9306 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 9307 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9308 | res->flags |= CF_READ_NULL; |
| 9309 | si_shutr(si); |
| 9310 | return; |
| 9311 | |
| 9312 | /* yield. */ |
| 9313 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 9314 | if (hlua->wake_time != TICK_ETERNITY) |
| 9315 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9316 | return; |
| 9317 | |
| 9318 | /* finished with error. */ |
| 9319 | case HLUA_E_ERRMSG: |
| 9320 | /* Display log. */ |
| 9321 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9322 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9323 | lua_pop(hlua->T, 1); |
| 9324 | goto error; |
| 9325 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9326 | case HLUA_E_ETMOUT: |
| 9327 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9328 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9329 | goto error; |
| 9330 | |
| 9331 | case HLUA_E_NOMEM: |
| 9332 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9333 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9334 | goto error; |
| 9335 | |
| 9336 | case HLUA_E_YIELD: /* unexpected */ |
| 9337 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9338 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9339 | goto error; |
| 9340 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9341 | case HLUA_E_ERR: |
| 9342 | /* Display log. */ |
| 9343 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9344 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9345 | goto error; |
| 9346 | |
| 9347 | default: |
| 9348 | goto error; |
| 9349 | } |
| 9350 | |
| 9351 | error: |
| 9352 | |
| 9353 | /* For all other cases, just close the stream. */ |
| 9354 | si_shutw(si); |
| 9355 | si_shutr(si); |
| 9356 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 9357 | } |
| 9358 | |
| 9359 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 9360 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 9361 | task_destroy(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9362 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9363 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9364 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9365 | } |
| 9366 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9367 | /* The function returns 1 if the initialisation is complete, 0 if |
| 9368 | * an errors occurs and -1 if more data are required for initializing |
| 9369 | * the applet. |
| 9370 | */ |
| 9371 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 9372 | { |
Christopher Faulet | 86e1c33 | 2021-12-20 17:09:39 +0100 | [diff] [blame] | 9373 | struct stream_interface *si = cs_si(ctx->owner); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9374 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9375 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9376 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9377 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9378 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9379 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9380 | txn = strm->txn; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 9381 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9382 | if (!hlua) { |
| 9383 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9384 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9385 | return 0; |
| 9386 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9387 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9388 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9389 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 9390 | ctx->ctx.hlua_apphttp.flags = 0; |
| 9391 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 9392 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 9393 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 9394 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9395 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 9396 | task = task_new_here(); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9397 | if (!task) { |
| 9398 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9399 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9400 | return 0; |
| 9401 | } |
| 9402 | task->nice = 0; |
| 9403 | task->context = ctx; |
| 9404 | task->process = hlua_applet_wakeup; |
| 9405 | ctx->ctx.hlua_apphttp.task = task; |
| 9406 | |
| 9407 | /* In the execution wrappers linked with a stream, the |
| 9408 | * Lua context can be not initialized. This behavior |
| 9409 | * permits to save performances because a systematic |
| 9410 | * Lua initialization cause 5% performances loss. |
| 9411 | */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9412 | 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] | 9413 | 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] | 9414 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9415 | return 0; |
| 9416 | } |
| 9417 | |
| 9418 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 9419 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9420 | |
| 9421 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9422 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9423 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 9424 | error = lua_tostring(hlua->T, -1); |
| 9425 | else |
| 9426 | error = "critical error"; |
| 9427 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9428 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9429 | return 0; |
| 9430 | } |
| 9431 | |
| 9432 | /* Check stack available size. */ |
| 9433 | if (!lua_checkstack(hlua->T, 1)) { |
| 9434 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9435 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9436 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9437 | return 0; |
| 9438 | } |
| 9439 | |
| 9440 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9441 | 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] | 9442 | |
| 9443 | /* Create and and push object stream in the stack. */ |
| 9444 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 9445 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9446 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9447 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9448 | return 0; |
| 9449 | } |
| 9450 | hlua->nargs = 1; |
| 9451 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9452 | /* push keywords in the stack. */ |
| 9453 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 9454 | if (!lua_checkstack(hlua->T, 1)) { |
| 9455 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9456 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9457 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9458 | return 0; |
| 9459 | } |
| 9460 | lua_pushstring(hlua->T, *arg); |
| 9461 | hlua->nargs++; |
| 9462 | } |
| 9463 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9464 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9465 | |
| 9466 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 9467 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9468 | |
| 9469 | return 1; |
| 9470 | } |
| 9471 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 9472 | void hlua_applet_http_fct(struct appctx *ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9473 | { |
Christopher Faulet | 86e1c33 | 2021-12-20 17:09:39 +0100 | [diff] [blame] | 9474 | struct stream_interface *si = cs_si(ctx->owner); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9475 | struct stream *strm = si_strm(si); |
| 9476 | struct channel *req = si_oc(si); |
| 9477 | struct channel *res = si_ic(si); |
| 9478 | struct act_rule *rule = ctx->rule; |
| 9479 | struct proxy *px = strm->be; |
| 9480 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
| 9481 | struct htx *req_htx, *res_htx; |
| 9482 | |
| 9483 | res_htx = htx_from_buf(&res->buf); |
| 9484 | |
| 9485 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 9486 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 9487 | goto out; |
| 9488 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9489 | /* Check if the input buffer is available. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9490 | if (!b_size(&res->buf)) { |
| 9491 | si_rx_room_blk(si); |
| 9492 | goto out; |
| 9493 | } |
| 9494 | /* check that the output is not closed */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9495 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9496 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 9497 | |
| 9498 | /* Set the currently running flag. */ |
| 9499 | if (!HLUA_IS_RUNNING(hlua) && |
| 9500 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
Christopher Faulet | bd878d2 | 2021-04-28 10:50:21 +0200 | [diff] [blame] | 9501 | if (!co_data(req)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9502 | si_cant_get(si); |
| 9503 | goto out; |
| 9504 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9505 | } |
| 9506 | |
| 9507 | /* Executes The applet if it is not done. */ |
| 9508 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 9509 | |
| 9510 | /* Execute the function. */ |
| 9511 | switch (hlua_ctx_resume(hlua, 1)) { |
| 9512 | /* finished. */ |
| 9513 | case HLUA_E_OK: |
| 9514 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 9515 | break; |
| 9516 | |
| 9517 | /* yield. */ |
| 9518 | case HLUA_E_AGAIN: |
| 9519 | if (hlua->wake_time != TICK_ETERNITY) |
| 9520 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9521 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9522 | |
| 9523 | /* finished with error. */ |
| 9524 | case HLUA_E_ERRMSG: |
| 9525 | /* Display log. */ |
| 9526 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9527 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9528 | lua_pop(hlua->T, 1); |
| 9529 | goto error; |
| 9530 | |
| 9531 | case HLUA_E_ETMOUT: |
| 9532 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9533 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9534 | goto error; |
| 9535 | |
| 9536 | case HLUA_E_NOMEM: |
| 9537 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9538 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9539 | goto error; |
| 9540 | |
| 9541 | case HLUA_E_YIELD: /* unexpected */ |
| 9542 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9543 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9544 | goto error; |
| 9545 | |
| 9546 | case HLUA_E_ERR: |
| 9547 | /* Display log. */ |
| 9548 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9549 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9550 | goto error; |
| 9551 | |
| 9552 | default: |
| 9553 | goto error; |
| 9554 | } |
| 9555 | } |
| 9556 | |
| 9557 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9558 | if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT) |
| 9559 | goto done; |
| 9560 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9561 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) |
| 9562 | goto error; |
| 9563 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 9564 | /* no more data are expected. Don't add TLR because mux-h1 will take care of it */ |
| 9565 | res_htx->flags |= HTX_FL_EOM; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9566 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 9567 | ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9568 | } |
| 9569 | |
| 9570 | done: |
| 9571 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9572 | if (!(res->flags & CF_SHUTR)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9573 | res->flags |= CF_READ_NULL; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9574 | si_shutr(si); |
| 9575 | } |
| 9576 | |
| 9577 | /* eat the whole request */ |
| 9578 | if (co_data(req)) { |
| 9579 | req_htx = htx_from_buf(&req->buf); |
| 9580 | co_htx_skip(req, req_htx, co_data(req)); |
| 9581 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9582 | } |
| 9583 | } |
| 9584 | |
| 9585 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9586 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9587 | return; |
| 9588 | |
| 9589 | error: |
| 9590 | |
| 9591 | /* If we are in HTTP mode, and we are not send any |
| 9592 | * data, return a 500 server error in best effort: |
| 9593 | * if there is no room available in the buffer, |
| 9594 | * just close the connection. |
| 9595 | */ |
| 9596 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 9597 | struct buffer *err = &http_err_chunks[HTTP_ERR_500]; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9598 | |
| 9599 | channel_erase(res); |
| 9600 | res->buf.data = b_data(err); |
| 9601 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 9602 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 9603 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9604 | } |
| 9605 | if (!(strm->flags & SF_ERR_MASK)) |
| 9606 | strm->flags |= SF_ERR_RESOURCE; |
| 9607 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 9608 | goto done; |
| 9609 | } |
| 9610 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9611 | static void hlua_applet_http_release(struct appctx *ctx) |
| 9612 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 9613 | task_destroy(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9614 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9615 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9616 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9617 | } |
| 9618 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9619 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9620 | * success case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9621 | * |
| 9622 | * This function can fail with an abort() due to an Lua critical error. |
| 9623 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 9624 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9625 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9626 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 9627 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9628 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9629 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9630 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9631 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9632 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9633 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9634 | if (!rule->arg.hlua_rule) { |
| 9635 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9636 | goto error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9637 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9638 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9639 | /* Memory for arguments. */ |
Tim Duesterhus | e52b6e5 | 2020-09-12 20:26:43 +0200 | [diff] [blame] | 9640 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, |
| 9641 | sizeof(*rule->arg.hlua_rule->args)); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9642 | if (!rule->arg.hlua_rule->args) { |
| 9643 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9644 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9645 | } |
| 9646 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9647 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9648 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9649 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9650 | /* Expect some arguments */ |
| 9651 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 9652 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9653 | memprintf(err, "expect %d arguments", fcn->nargs); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9654 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9655 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 9656 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9657 | if (!rule->arg.hlua_rule->args[i]) { |
| 9658 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9659 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9660 | } |
| 9661 | (*cur_arg)++; |
| 9662 | } |
| 9663 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9664 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 9665 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9666 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 9667 | return ACT_RET_PRS_OK; |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9668 | |
| 9669 | error: |
| 9670 | if (rule->arg.hlua_rule) { |
| 9671 | if (rule->arg.hlua_rule->args) { |
| 9672 | for (i = 0; i < fcn->nargs; i++) |
| 9673 | ha_free(&rule->arg.hlua_rule->args[i]); |
| 9674 | ha_free(&rule->arg.hlua_rule->args); |
| 9675 | } |
| 9676 | ha_free(&rule->arg.hlua_rule); |
| 9677 | } |
| 9678 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9679 | } |
| 9680 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9681 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 9682 | struct act_rule *rule, char **err) |
| 9683 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9684 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9685 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 9686 | /* HTTP applets are forbidden in tcp-request rules. |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9687 | * HTTP applet request requires everything initialized by |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 9688 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9689 | * The applet will be immediately initialized, but its before |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 9690 | * the call of this analyzer. |
| 9691 | */ |
| 9692 | if (rule->from != ACT_F_HTTP_REQ) { |
| 9693 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 9694 | return ACT_RET_PRS_ERR; |
| 9695 | } |
| 9696 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9697 | /* Memory for the rule. */ |
| 9698 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 9699 | if (!rule->arg.hlua_rule) { |
| 9700 | memprintf(err, "out of memory error"); |
| 9701 | return ACT_RET_PRS_ERR; |
| 9702 | } |
| 9703 | |
| 9704 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9705 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9706 | |
| 9707 | /* TODO: later accept arguments. */ |
| 9708 | rule->arg.hlua_rule->args = NULL; |
| 9709 | |
| 9710 | /* Add applet pointer in the rule. */ |
| 9711 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 9712 | rule->applet.name = fcn->name; |
| 9713 | rule->applet.init = hlua_applet_http_init; |
| 9714 | rule->applet.fct = hlua_applet_http_fct; |
| 9715 | rule->applet.release = hlua_applet_http_release; |
| 9716 | rule->applet.timeout = hlua_timeout_applet; |
| 9717 | |
| 9718 | return ACT_RET_PRS_OK; |
| 9719 | } |
| 9720 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9721 | /* This function is an LUA binding used for registering |
| 9722 | * "sample-conv" functions. It expects a converter name used |
| 9723 | * in the haproxy configuration file, and an LUA function. |
| 9724 | */ |
| 9725 | __LJMP static int hlua_register_action(lua_State *L) |
| 9726 | { |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9727 | struct action_kw_list *akl = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9728 | const char *name; |
| 9729 | int ref; |
| 9730 | int len; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9731 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9732 | int nargs; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9733 | struct buffer *trash; |
| 9734 | struct action_kw *akw; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9735 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9736 | /* Initialise the number of expected arguments at 0. */ |
| 9737 | nargs = 0; |
| 9738 | |
| 9739 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 9740 | 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] | 9741 | |
| 9742 | /* First argument : converter name. */ |
| 9743 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 9744 | |
| 9745 | /* Second argument : environment. */ |
| 9746 | if (lua_type(L, 2) != LUA_TTABLE) |
| 9747 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 9748 | |
| 9749 | /* Third argument : lua function. */ |
| 9750 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 9751 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9752 | /* 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] | 9753 | if (lua_gettop(L) >= 4) |
| 9754 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 9755 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9756 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9757 | lua_pushnil(L); |
| 9758 | while (lua_next(L, 2) != 0) { |
| 9759 | if (lua_type(L, -1) != LUA_TSTRING) |
| 9760 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 9761 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9762 | /* Check if action exists */ |
| 9763 | trash = get_trash_chunk(); |
| 9764 | chunk_printf(trash, "lua.%s", name); |
| 9765 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) { |
| 9766 | akw = tcp_req_cont_action(trash->area); |
| 9767 | } else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) { |
| 9768 | akw = tcp_res_cont_action(trash->area); |
| 9769 | } else if (strcmp(lua_tostring(L, -1), "http-req") == 0) { |
| 9770 | akw = action_http_req_custom(trash->area); |
| 9771 | } else if (strcmp(lua_tostring(L, -1), "http-res") == 0) { |
| 9772 | akw = action_http_res_custom(trash->area); |
| 9773 | } else { |
| 9774 | akw = NULL; |
| 9775 | } |
| 9776 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9777 | fcn = akw->private; |
| 9778 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 9779 | ha_warning("Trying to register action 'lua.%s' more than once. " |
| 9780 | "This will become a hard error in version 2.5.\n", name); |
| 9781 | } |
| 9782 | fcn->function_ref[hlua_state_id] = ref; |
| 9783 | |
| 9784 | /* pop the environment string. */ |
| 9785 | lua_pop(L, 1); |
| 9786 | continue; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9787 | } |
| 9788 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9789 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 9790 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9791 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9792 | if (!akl) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9793 | goto alloc_error;; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 9794 | fcn = new_hlua_function(); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9795 | if (!fcn) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9796 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9797 | |
| 9798 | /* Fill fcn. */ |
| 9799 | fcn->name = strdup(name); |
| 9800 | if (!fcn->name) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9801 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9802 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9803 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 9804 | /* Set the expected number of arguments. */ |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9805 | fcn->nargs = nargs; |
| 9806 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9807 | /* List head */ |
| 9808 | akl->list.n = akl->list.p = NULL; |
| 9809 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9810 | /* action keyword. */ |
| 9811 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9812 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9813 | if (!akl->kw[0].kw) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9814 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9815 | |
| 9816 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 9817 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 9818 | akl->kw[0].flags = 0; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9819 | akl->kw[0].private = fcn; |
| 9820 | akl->kw[0].parse = action_register_lua; |
| 9821 | |
| 9822 | /* select the action registering point. */ |
| 9823 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 9824 | tcp_req_cont_keywords_register(akl); |
| 9825 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 9826 | tcp_res_cont_keywords_register(akl); |
| 9827 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 9828 | http_req_keywords_register(akl); |
| 9829 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 9830 | http_res_keywords_register(akl); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9831 | else { |
| 9832 | release_hlua_function(fcn); |
| 9833 | if (akl) |
| 9834 | ha_free((char **)&(akl->kw[0].kw)); |
| 9835 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 9836 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9837 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 9838 | "are expected.", lua_tostring(L, -1))); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9839 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9840 | |
| 9841 | /* pop the environment string. */ |
| 9842 | lua_pop(L, 1); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9843 | |
| 9844 | /* reset for next loop */ |
| 9845 | akl = NULL; |
| 9846 | fcn = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9847 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9848 | return ACT_RET_PRS_OK; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9849 | |
| 9850 | alloc_error: |
| 9851 | release_hlua_function(fcn); |
| 9852 | ha_free(&akl); |
| 9853 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 9854 | return 0; /* Never reached */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9855 | } |
| 9856 | |
| 9857 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 9858 | struct act_rule *rule, char **err) |
| 9859 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9860 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9861 | |
Christopher Faulet | 280f85b | 2019-07-15 15:02:04 +0200 | [diff] [blame] | 9862 | if (px->mode == PR_MODE_HTTP) { |
| 9863 | memprintf(err, "Lua TCP services cannot be used on HTTP proxies"); |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 9864 | return ACT_RET_PRS_ERR; |
| 9865 | } |
| 9866 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9867 | /* Memory for the rule. */ |
| 9868 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 9869 | if (!rule->arg.hlua_rule) { |
| 9870 | memprintf(err, "out of memory error"); |
| 9871 | return ACT_RET_PRS_ERR; |
| 9872 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9873 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9874 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9875 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9876 | |
| 9877 | /* TODO: later accept arguments. */ |
| 9878 | rule->arg.hlua_rule->args = NULL; |
| 9879 | |
| 9880 | /* Add applet pointer in the rule. */ |
| 9881 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 9882 | rule->applet.name = fcn->name; |
| 9883 | rule->applet.init = hlua_applet_tcp_init; |
| 9884 | rule->applet.fct = hlua_applet_tcp_fct; |
| 9885 | rule->applet.release = hlua_applet_tcp_release; |
| 9886 | rule->applet.timeout = hlua_timeout_applet; |
| 9887 | |
| 9888 | return 0; |
| 9889 | } |
| 9890 | |
| 9891 | /* This function is an LUA binding used for registering |
| 9892 | * "sample-conv" functions. It expects a converter name used |
| 9893 | * in the haproxy configuration file, and an LUA function. |
| 9894 | */ |
| 9895 | __LJMP static int hlua_register_service(lua_State *L) |
| 9896 | { |
| 9897 | struct action_kw_list *akl; |
| 9898 | const char *name; |
| 9899 | const char *env; |
| 9900 | int ref; |
| 9901 | int len; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9902 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9903 | struct buffer *trash; |
| 9904 | struct action_kw *akw; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9905 | |
| 9906 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 9907 | |
| 9908 | /* First argument : converter name. */ |
| 9909 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 9910 | |
| 9911 | /* Second argument : environment. */ |
| 9912 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 9913 | |
| 9914 | /* Third argument : lua function. */ |
| 9915 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 9916 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9917 | /* Check for service already registered */ |
| 9918 | trash = get_trash_chunk(); |
| 9919 | chunk_printf(trash, "lua.%s", name); |
| 9920 | akw = service_find(trash->area); |
| 9921 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9922 | fcn = akw->private; |
| 9923 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 9924 | ha_warning("Trying to register service 'lua.%s' more than once. " |
| 9925 | "This will become a hard error in version 2.5.\n", name); |
| 9926 | } |
| 9927 | fcn->function_ref[hlua_state_id] = ref; |
| 9928 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9929 | } |
| 9930 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9931 | /* Allocate and fill the sample fetch keyword struct. */ |
| 9932 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 9933 | if (!akl) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9934 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 9935 | fcn = new_hlua_function(); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9936 | if (!fcn) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9937 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9938 | |
| 9939 | /* Fill fcn. */ |
| 9940 | len = strlen("<lua.>") + strlen(name) + 1; |
| 9941 | fcn->name = calloc(1, len); |
| 9942 | if (!fcn->name) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9943 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9944 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9945 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9946 | |
| 9947 | /* List head */ |
| 9948 | akl->list.n = akl->list.p = NULL; |
| 9949 | |
| 9950 | /* converter keyword. */ |
| 9951 | len = strlen("lua.") + strlen(name) + 1; |
| 9952 | akl->kw[0].kw = calloc(1, len); |
| 9953 | if (!akl->kw[0].kw) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9954 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9955 | |
| 9956 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 9957 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 9958 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9959 | if (strcmp(env, "tcp") == 0) |
| 9960 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9961 | else if (strcmp(env, "http") == 0) |
| 9962 | akl->kw[0].parse = action_register_service_http; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9963 | else { |
| 9964 | release_hlua_function(fcn); |
| 9965 | if (akl) |
| 9966 | ha_free((char **)&(akl->kw[0].kw)); |
| 9967 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 9968 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 9969 | "'tcp' or 'http' are expected.", env)); |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9970 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9971 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 9972 | akl->kw[0].flags = 0; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9973 | akl->kw[0].private = fcn; |
| 9974 | |
| 9975 | /* End of array. */ |
| 9976 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 9977 | |
| 9978 | /* Register this new converter */ |
| 9979 | service_keywords_register(akl); |
| 9980 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9981 | return 0; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9982 | |
| 9983 | alloc_error: |
| 9984 | release_hlua_function(fcn); |
| 9985 | ha_free(&akl); |
| 9986 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 9987 | return 0; /* Never reached */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9988 | } |
| 9989 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9990 | /* This function initialises Lua cli handler. It copies the |
| 9991 | * arguments in the Lua stack and create channel IO objects. |
| 9992 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 9993 | 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] | 9994 | { |
| 9995 | struct hlua *hlua; |
| 9996 | struct hlua_function *fcn; |
| 9997 | int i; |
| 9998 | const char *error; |
| 9999 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10000 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10001 | appctx->ctx.hlua_cli.fcn = private; |
| 10002 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 10003 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10004 | if (!hlua) { |
| 10005 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 10006 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10007 | } |
| 10008 | HLUA_INIT(hlua); |
| 10009 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10010 | |
| 10011 | /* Create task used by signal to wakeup applets. |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 10012 | * 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] | 10013 | * applet_http. It is absolutely compatible. |
| 10014 | */ |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 10015 | appctx->ctx.hlua_cli.task = task_new_here(); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10016 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 10017 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 10018 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10019 | } |
| 10020 | appctx->ctx.hlua_cli.task->nice = 0; |
| 10021 | appctx->ctx.hlua_cli.task->context = appctx; |
| 10022 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 10023 | |
| 10024 | /* Initialises the Lua context */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 10025 | 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] | 10026 | 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] | 10027 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10028 | } |
| 10029 | |
| 10030 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10031 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10032 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 10033 | error = lua_tostring(hlua->T, -1); |
| 10034 | else |
| 10035 | error = "critical error"; |
| 10036 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 10037 | goto error; |
| 10038 | } |
| 10039 | |
| 10040 | /* Check stack available size. */ |
| 10041 | if (!lua_checkstack(hlua->T, 2)) { |
| 10042 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 10043 | goto error; |
| 10044 | } |
| 10045 | |
| 10046 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 10047 | 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] | 10048 | |
| 10049 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 10050 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10051 | */ |
| 10052 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 10053 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 10054 | goto error; |
| 10055 | } |
| 10056 | hlua->nargs = 1; |
| 10057 | |
| 10058 | /* push keywords in the stack. */ |
| 10059 | for (i = 0; *args[i]; i++) { |
| 10060 | /* Check stack available size. */ |
| 10061 | if (!lua_checkstack(hlua->T, 1)) { |
| 10062 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 10063 | goto error; |
| 10064 | } |
| 10065 | lua_pushstring(hlua->T, args[i]); |
| 10066 | hlua->nargs++; |
| 10067 | } |
| 10068 | |
| 10069 | /* We must initialize the execution timeouts. */ |
| 10070 | hlua->max_time = hlua_timeout_session; |
| 10071 | |
| 10072 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10073 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10074 | |
| 10075 | /* It's ok */ |
| 10076 | return 0; |
| 10077 | |
| 10078 | /* It's not ok. */ |
| 10079 | error: |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10080 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10081 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 10082 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10083 | return 1; |
| 10084 | } |
| 10085 | |
| 10086 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 10087 | { |
| 10088 | struct hlua *hlua; |
| 10089 | struct stream_interface *si; |
| 10090 | struct hlua_function *fcn; |
| 10091 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10092 | hlua = appctx->ctx.hlua_cli.hlua; |
Christopher Faulet | 86e1c33 | 2021-12-20 17:09:39 +0100 | [diff] [blame] | 10093 | si = cs_si(appctx->owner); |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 10094 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10095 | |
| 10096 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 10097 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 10098 | return 1; |
| 10099 | |
| 10100 | /* Execute the function. */ |
| 10101 | switch (hlua_ctx_resume(hlua, 1)) { |
| 10102 | |
| 10103 | /* finished. */ |
| 10104 | case HLUA_E_OK: |
| 10105 | return 1; |
| 10106 | |
| 10107 | /* yield. */ |
| 10108 | case HLUA_E_AGAIN: |
| 10109 | /* We want write. */ |
| 10110 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 10111 | si_rx_room_blk(si); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10112 | /* Set the timeout. */ |
| 10113 | if (hlua->wake_time != TICK_ETERNITY) |
| 10114 | task_schedule(hlua->task, hlua->wake_time); |
| 10115 | return 0; |
| 10116 | |
| 10117 | /* finished with error. */ |
| 10118 | case HLUA_E_ERRMSG: |
| 10119 | /* Display log. */ |
| 10120 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 10121 | fcn->name, lua_tostring(hlua->T, -1)); |
| 10122 | lua_pop(hlua->T, 1); |
| 10123 | return 1; |
| 10124 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 10125 | case HLUA_E_ETMOUT: |
| 10126 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 10127 | fcn->name); |
| 10128 | return 1; |
| 10129 | |
| 10130 | case HLUA_E_NOMEM: |
| 10131 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 10132 | fcn->name); |
| 10133 | return 1; |
| 10134 | |
| 10135 | case HLUA_E_YIELD: /* unexpected */ |
| 10136 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 10137 | fcn->name); |
| 10138 | return 1; |
| 10139 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10140 | case HLUA_E_ERR: |
| 10141 | /* Display log. */ |
| 10142 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 10143 | fcn->name); |
| 10144 | return 1; |
| 10145 | |
| 10146 | default: |
| 10147 | return 1; |
| 10148 | } |
| 10149 | |
| 10150 | return 1; |
| 10151 | } |
| 10152 | |
| 10153 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 10154 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10155 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10156 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10157 | } |
| 10158 | |
| 10159 | /* This function is an LUA binding used for registering |
| 10160 | * new keywords in the cli. It expects a list of keywords |
| 10161 | * which are the "path". It is limited to 5 keywords. A |
| 10162 | * description of the command, a function to be executed |
| 10163 | * for the parsing and a function for io handlers. |
| 10164 | */ |
| 10165 | __LJMP static int hlua_register_cli(lua_State *L) |
| 10166 | { |
| 10167 | struct cli_kw_list *cli_kws; |
| 10168 | const char *message; |
| 10169 | int ref_io; |
| 10170 | int len; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10171 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10172 | int index; |
| 10173 | int i; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10174 | struct buffer *trash; |
| 10175 | const char *kw[5]; |
| 10176 | struct cli_kw *cli_kw; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10177 | const char *errmsg; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10178 | |
| 10179 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 10180 | |
| 10181 | /* First argument : an array of maximum 5 keywords. */ |
| 10182 | if (!lua_istable(L, 1)) |
| 10183 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 10184 | |
| 10185 | /* Second argument : string with contextual message. */ |
| 10186 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 10187 | |
| 10188 | /* Third and fourth argument : lua function. */ |
| 10189 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 10190 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10191 | /* Check for CLI service already registered */ |
| 10192 | trash = get_trash_chunk(); |
| 10193 | index = 0; |
| 10194 | lua_pushnil(L); |
| 10195 | memset(kw, 0, sizeof(kw)); |
| 10196 | while (lua_next(L, 1) != 0) { |
| 10197 | if (index >= CLI_PREFIX_KW_NB) |
| 10198 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 10199 | if (lua_type(L, -1) != LUA_TSTRING) |
| 10200 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 10201 | kw[index] = lua_tostring(L, -1); |
| 10202 | if (index == 0) |
| 10203 | chunk_printf(trash, "%s", kw[index]); |
| 10204 | else |
| 10205 | chunk_appendf(trash, " %s", kw[index]); |
| 10206 | index++; |
| 10207 | lua_pop(L, 1); |
| 10208 | } |
| 10209 | cli_kw = cli_find_kw_exact((char **)kw); |
| 10210 | if (cli_kw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10211 | fcn = cli_kw->private; |
| 10212 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 10213 | ha_warning("Trying to register CLI keyword 'lua.%s' more than once. " |
| 10214 | "This will become a hard error in version 2.5.\n", trash->area); |
| 10215 | } |
| 10216 | fcn->function_ref[hlua_state_id] = ref_io; |
| 10217 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10218 | } |
| 10219 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10220 | /* Allocate and fill the sample fetch keyword struct. */ |
| 10221 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10222 | if (!cli_kws) { |
| 10223 | errmsg = "Lua out of memory error."; |
| 10224 | goto error; |
| 10225 | } |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 10226 | fcn = new_hlua_function(); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10227 | if (!fcn) { |
| 10228 | errmsg = "Lua out of memory error."; |
| 10229 | goto error; |
| 10230 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10231 | |
| 10232 | /* Fill path. */ |
| 10233 | index = 0; |
| 10234 | lua_pushnil(L); |
| 10235 | while(lua_next(L, 1) != 0) { |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10236 | if (index >= 5) { |
| 10237 | errmsg = "1st argument must be a table with a maximum of 5 entries"; |
| 10238 | goto error; |
| 10239 | } |
| 10240 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 10241 | errmsg = "1st argument must be a table filled with strings"; |
| 10242 | goto error; |
| 10243 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10244 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10245 | if (!cli_kws->kw[0].str_kw[index]) { |
| 10246 | errmsg = "Lua out of memory error."; |
| 10247 | goto error; |
| 10248 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10249 | index++; |
| 10250 | lua_pop(L, 1); |
| 10251 | } |
| 10252 | |
| 10253 | /* Copy help message. */ |
| 10254 | cli_kws->kw[0].usage = strdup(message); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10255 | if (!cli_kws->kw[0].usage) { |
| 10256 | errmsg = "Lua out of memory error."; |
| 10257 | goto error; |
| 10258 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10259 | |
| 10260 | /* Fill fcn io handler. */ |
| 10261 | len = strlen("<lua.cli>") + 1; |
| 10262 | for (i = 0; i < index; i++) |
| 10263 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 10264 | fcn->name = calloc(1, len); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10265 | if (!fcn->name) { |
| 10266 | errmsg = "Lua out of memory error."; |
| 10267 | goto error; |
| 10268 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10269 | strncat((char *)fcn->name, "<lua.cli", len); |
| 10270 | for (i = 0; i < index; i++) { |
| 10271 | strncat((char *)fcn->name, ".", len); |
| 10272 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 10273 | } |
| 10274 | strncat((char *)fcn->name, ">", len); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 10275 | fcn->function_ref[hlua_state_id] = ref_io; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10276 | |
| 10277 | /* Fill last entries. */ |
| 10278 | cli_kws->kw[0].private = fcn; |
| 10279 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 10280 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 10281 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 10282 | |
| 10283 | /* Register this new converter */ |
| 10284 | cli_register_kw(cli_kws); |
| 10285 | |
| 10286 | return 0; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10287 | |
| 10288 | error: |
| 10289 | release_hlua_function(fcn); |
| 10290 | if (cli_kws) { |
| 10291 | for (i = 0; i < index; i++) |
| 10292 | ha_free((char **)&(cli_kws->kw[0].str_kw[i])); |
| 10293 | ha_free((char **)&(cli_kws->kw[0].usage)); |
| 10294 | } |
| 10295 | ha_free(&cli_kws); |
| 10296 | WILL_LJMP(luaL_error(L, errmsg)); |
| 10297 | return 0; /* Never reached */ |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10298 | } |
| 10299 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10300 | static int hlua_filter_init_per_thread(struct proxy *px, struct flt_conf *fconf) |
| 10301 | { |
| 10302 | struct hlua_flt_config *conf = fconf->conf; |
| 10303 | lua_State *L; |
| 10304 | int error, pos, state_id, flt_ref; |
| 10305 | |
| 10306 | state_id = reg_flt_to_stack_id(conf->reg); |
| 10307 | L = hlua_states[state_id]; |
| 10308 | pos = lua_gettop(L); |
| 10309 | |
| 10310 | /* The filter parsing function */ |
| 10311 | lua_rawgeti(L, LUA_REGISTRYINDEX, conf->reg->fun_ref[state_id]); |
| 10312 | |
| 10313 | /* Push the filter class on the stack and resolve all callbacks */ |
| 10314 | lua_rawgeti(L, LUA_REGISTRYINDEX, conf->reg->flt_ref[state_id]); |
| 10315 | |
| 10316 | /* Duplicate the filter class so each filter will have its own copy */ |
| 10317 | lua_newtable(L); |
| 10318 | lua_pushnil(L); |
| 10319 | |
| 10320 | while (lua_next(L, pos+2)) { |
| 10321 | lua_pushvalue(L, -2); |
| 10322 | lua_insert(L, -2); |
| 10323 | lua_settable(L, -4); |
| 10324 | } |
| 10325 | flt_ref = luaL_ref(L, LUA_REGISTRYINDEX); |
| 10326 | |
| 10327 | /* Remove the original lua filter class from the stack */ |
| 10328 | lua_pop(L, 1); |
| 10329 | |
| 10330 | /* Push the copy on the stack */ |
| 10331 | lua_rawgeti(L, LUA_REGISTRYINDEX, flt_ref); |
| 10332 | |
| 10333 | /* extra args are pushed in a table */ |
| 10334 | lua_newtable(L); |
| 10335 | for (pos = 0; conf->args[pos]; pos++) { |
| 10336 | /* Check stack available size. */ |
| 10337 | if (!lua_checkstack(L, 1)) { |
| 10338 | ha_alert("Lua filter '%s' : Lua error : full stack.", conf->reg->name); |
| 10339 | goto error; |
| 10340 | } |
| 10341 | lua_pushstring(L, conf->args[pos]); |
| 10342 | lua_rawseti(L, -2, lua_rawlen(L, -2) + 1); |
| 10343 | } |
| 10344 | |
| 10345 | error = lua_pcall(L, 2, LUA_MULTRET, 0); |
| 10346 | switch (error) { |
| 10347 | case LUA_OK: |
| 10348 | /* replace the filter ref */ |
| 10349 | conf->ref[state_id] = flt_ref; |
| 10350 | break; |
| 10351 | case LUA_ERRRUN: |
| 10352 | ha_alert("Lua filter '%s' : runtime error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 10353 | goto error; |
| 10354 | case LUA_ERRMEM: |
| 10355 | ha_alert("Lua filter '%s' : out of memory error", conf->reg->name); |
| 10356 | goto error; |
| 10357 | case LUA_ERRERR: |
| 10358 | ha_alert("Lua filter '%s' : message handler error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 10359 | goto error; |
| 10360 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
| 10361 | case LUA_ERRGCMM: |
| 10362 | ha_alert("Lua filter '%s' : garbage collector error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 10363 | goto error; |
| 10364 | #endif |
| 10365 | default: |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 10366 | 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] | 10367 | goto error; |
| 10368 | } |
| 10369 | |
| 10370 | lua_settop(L, 0); |
| 10371 | return 0; |
| 10372 | |
| 10373 | error: |
| 10374 | lua_settop(L, 0); |
| 10375 | return -1; |
| 10376 | } |
| 10377 | |
| 10378 | static void hlua_filter_deinit_per_thread(struct proxy *px, struct flt_conf *fconf) |
| 10379 | { |
| 10380 | struct hlua_flt_config *conf = fconf->conf; |
| 10381 | lua_State *L; |
| 10382 | int state_id; |
| 10383 | |
| 10384 | if (!conf) |
| 10385 | return; |
| 10386 | |
| 10387 | state_id = reg_flt_to_stack_id(conf->reg); |
| 10388 | L = hlua_states[state_id]; |
| 10389 | luaL_unref(L, LUA_REGISTRYINDEX, conf->ref[state_id]); |
| 10390 | } |
| 10391 | |
| 10392 | static int hlua_filter_init(struct proxy *px, struct flt_conf *fconf) |
| 10393 | { |
| 10394 | struct hlua_flt_config *conf = fconf->conf; |
| 10395 | int state_id = reg_flt_to_stack_id(conf->reg); |
| 10396 | |
| 10397 | /* Rely on per-thread init for global scripts */ |
| 10398 | if (!state_id) |
| 10399 | return hlua_filter_init_per_thread(px, fconf); |
| 10400 | return 0; |
| 10401 | } |
| 10402 | |
| 10403 | static void hlua_filter_deinit(struct proxy *px, struct flt_conf *fconf) |
| 10404 | { |
| 10405 | |
| 10406 | if (fconf->conf) { |
| 10407 | struct hlua_flt_config *conf = fconf->conf; |
| 10408 | int state_id = reg_flt_to_stack_id(conf->reg); |
| 10409 | int pos; |
| 10410 | |
| 10411 | /* Rely on per-thread deinit for global scripts */ |
| 10412 | if (!state_id) |
| 10413 | hlua_filter_deinit_per_thread(px, fconf); |
| 10414 | |
| 10415 | for (pos = 0; conf->args[pos]; pos++) |
| 10416 | free(conf->args[pos]); |
| 10417 | free(conf->args); |
| 10418 | } |
| 10419 | ha_free(&fconf->conf); |
| 10420 | ha_free((char **)&fconf->id); |
| 10421 | ha_free(&fconf->ops); |
| 10422 | } |
| 10423 | |
| 10424 | static int hlua_filter_new(struct stream *s, struct filter *filter) |
| 10425 | { |
| 10426 | struct hlua_flt_config *conf = FLT_CONF(filter); |
| 10427 | struct hlua_flt_ctx *flt_ctx = NULL; |
| 10428 | int ret = 1; |
| 10429 | |
| 10430 | /* In the execution wrappers linked with a stream, the |
| 10431 | * Lua context can be not initialized. This behavior |
| 10432 | * permits to save performances because a systematic |
| 10433 | * Lua initialization cause 5% performances loss. |
| 10434 | */ |
| 10435 | if (!s->hlua) { |
| 10436 | struct hlua *hlua; |
| 10437 | |
| 10438 | hlua = pool_alloc(pool_head_hlua); |
| 10439 | if (!hlua) { |
| 10440 | SEND_ERR(s->be, "Lua filter '%s': can't initialize Lua context.\n", |
| 10441 | conf->reg->name); |
| 10442 | ret = 0; |
| 10443 | goto end; |
| 10444 | } |
| 10445 | HLUA_INIT(hlua); |
| 10446 | s->hlua = hlua; |
| 10447 | if (!hlua_ctx_init(s->hlua, reg_flt_to_stack_id(conf->reg), s->task, 0)) { |
| 10448 | SEND_ERR(s->be, "Lua filter '%s': can't initialize Lua context.\n", |
| 10449 | conf->reg->name); |
| 10450 | ret = 0; |
| 10451 | goto end; |
| 10452 | } |
| 10453 | } |
| 10454 | |
| 10455 | flt_ctx = pool_zalloc(pool_head_hlua_flt_ctx); |
| 10456 | if (!flt_ctx) { |
| 10457 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 10458 | conf->reg->name); |
| 10459 | ret = 0; |
| 10460 | goto end; |
| 10461 | } |
| 10462 | flt_ctx->hlua[0] = pool_alloc(pool_head_hlua); |
| 10463 | flt_ctx->hlua[1] = pool_alloc(pool_head_hlua); |
| 10464 | if (!flt_ctx->hlua[0] || !flt_ctx->hlua[1]) { |
| 10465 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 10466 | conf->reg->name); |
| 10467 | ret = 0; |
| 10468 | goto end; |
| 10469 | } |
| 10470 | HLUA_INIT(flt_ctx->hlua[0]); |
| 10471 | HLUA_INIT(flt_ctx->hlua[1]); |
| 10472 | if (!hlua_ctx_init(flt_ctx->hlua[0], reg_flt_to_stack_id(conf->reg), s->task, 0) || |
| 10473 | !hlua_ctx_init(flt_ctx->hlua[1], reg_flt_to_stack_id(conf->reg), s->task, 0)) { |
| 10474 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 10475 | conf->reg->name); |
| 10476 | ret = 0; |
| 10477 | goto end; |
| 10478 | } |
| 10479 | |
| 10480 | if (!HLUA_IS_RUNNING(s->hlua)) { |
| 10481 | /* The following Lua calls can fail. */ |
| 10482 | if (!SET_SAFE_LJMP(s->hlua)) { |
| 10483 | const char *error; |
| 10484 | |
| 10485 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 10486 | error = lua_tostring(s->hlua->T, -1); |
| 10487 | else |
| 10488 | error = "critical error"; |
| 10489 | SEND_ERR(s->be, "Lua filter '%s': %s.\n", conf->reg->name, error); |
| 10490 | ret = 0; |
| 10491 | goto end; |
| 10492 | } |
| 10493 | |
| 10494 | /* Check stack size. */ |
| 10495 | if (!lua_checkstack(s->hlua->T, 1)) { |
| 10496 | 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] | 10497 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10498 | ret = 0; |
| 10499 | goto end; |
| 10500 | } |
| 10501 | |
| 10502 | lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, conf->ref[s->hlua->state_id]); |
| 10503 | if (lua_getfield(s->hlua->T, -1, "new") != LUA_TFUNCTION) { |
| 10504 | SEND_ERR(s->be, "Lua filter '%s': 'new' field is not a function.\n", |
| 10505 | conf->reg->name); |
| 10506 | RESET_SAFE_LJMP(s->hlua); |
| 10507 | ret = 0; |
| 10508 | goto end; |
| 10509 | } |
| 10510 | lua_insert(s->hlua->T, -2); |
| 10511 | |
| 10512 | /* Push the copy on the stack */ |
| 10513 | s->hlua->nargs = 1; |
| 10514 | |
| 10515 | /* We must initialize the execution timeouts. */ |
| 10516 | s->hlua->max_time = hlua_timeout_session; |
| 10517 | |
| 10518 | /* At this point the execution is safe. */ |
| 10519 | RESET_SAFE_LJMP(s->hlua); |
| 10520 | } |
| 10521 | |
| 10522 | switch (hlua_ctx_resume(s->hlua, 0)) { |
| 10523 | case HLUA_E_OK: |
| 10524 | /* Nothing returned or not a table, ignore the filter for current stream */ |
| 10525 | if (!lua_gettop(s->hlua->T) || !lua_istable(s->hlua->T, 1)) { |
| 10526 | ret = 0; |
| 10527 | goto end; |
| 10528 | } |
| 10529 | |
| 10530 | /* Attached the filter pointer to the ctx */ |
| 10531 | lua_pushstring(s->hlua->T, "__filter"); |
| 10532 | lua_pushlightuserdata(s->hlua->T, filter); |
| 10533 | lua_settable(s->hlua->T, -3); |
| 10534 | |
| 10535 | /* Save a ref on the filter ctx */ |
| 10536 | lua_pushvalue(s->hlua->T, 1); |
| 10537 | flt_ctx->ref = luaL_ref(s->hlua->T, LUA_REGISTRYINDEX); |
| 10538 | filter->ctx = flt_ctx; |
| 10539 | break; |
| 10540 | case HLUA_E_ERRMSG: |
| 10541 | SEND_ERR(s->be, "Lua filter '%s' : %s.\n", conf->reg->name, lua_tostring(s->hlua->T, -1)); |
| 10542 | ret = -1; |
| 10543 | goto end; |
| 10544 | case HLUA_E_ETMOUT: |
| 10545 | SEND_ERR(s->be, "Lua filter '%s' : 'new' execution timeout.\n", conf->reg->name); |
| 10546 | ret = 0; |
| 10547 | goto end; |
| 10548 | case HLUA_E_NOMEM: |
| 10549 | SEND_ERR(s->be, "Lua filter '%s' : out of memory error.\n", conf->reg->name); |
| 10550 | ret = 0; |
| 10551 | goto end; |
| 10552 | case HLUA_E_AGAIN: |
| 10553 | case HLUA_E_YIELD: |
| 10554 | SEND_ERR(s->be, "Lua filter '%s': yield functions like core.tcp() or core.sleep()" |
| 10555 | " are not allowed from 'new' function.\n", conf->reg->name); |
| 10556 | ret = 0; |
| 10557 | goto end; |
| 10558 | case HLUA_E_ERR: |
| 10559 | SEND_ERR(s->be, "Lua filter '%s': 'new' returns an unknown error.\n", conf->reg->name); |
| 10560 | ret = 0; |
| 10561 | goto end; |
| 10562 | default: |
| 10563 | ret = 0; |
| 10564 | goto end; |
| 10565 | } |
| 10566 | |
| 10567 | end: |
| 10568 | if (s->hlua) |
| 10569 | lua_settop(s->hlua->T, 0); |
| 10570 | if (ret <= 0) { |
| 10571 | if (flt_ctx) { |
| 10572 | hlua_ctx_destroy(flt_ctx->hlua[0]); |
| 10573 | hlua_ctx_destroy(flt_ctx->hlua[1]); |
| 10574 | pool_free(pool_head_hlua_flt_ctx, flt_ctx); |
| 10575 | } |
| 10576 | } |
| 10577 | return ret; |
| 10578 | } |
| 10579 | |
| 10580 | static void hlua_filter_delete(struct stream *s, struct filter *filter) |
| 10581 | { |
| 10582 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10583 | |
| 10584 | luaL_unref(s->hlua->T, LUA_REGISTRYINDEX, flt_ctx->ref); |
| 10585 | hlua_ctx_destroy(flt_ctx->hlua[0]); |
| 10586 | hlua_ctx_destroy(flt_ctx->hlua[1]); |
| 10587 | pool_free(pool_head_hlua_flt_ctx, flt_ctx); |
| 10588 | filter->ctx = NULL; |
| 10589 | } |
| 10590 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 10591 | static int hlua_filter_from_payload(struct filter *filter) |
| 10592 | { |
| 10593 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10594 | |
| 10595 | return (flt_ctx && !!(flt_ctx->flags & HLUA_FLT_CTX_FL_PAYLOAD)); |
| 10596 | } |
| 10597 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10598 | static int hlua_filter_callback(struct stream *s, struct filter *filter, const char *fun, |
| 10599 | int dir, unsigned int flags) |
| 10600 | { |
| 10601 | struct hlua *flt_hlua; |
| 10602 | struct hlua_flt_config *conf = FLT_CONF(filter); |
| 10603 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10604 | unsigned int hflags = HLUA_TXN_FLT_CTX; |
| 10605 | int ret = 1; |
| 10606 | |
| 10607 | flt_hlua = flt_ctx->hlua[(dir == SMP_OPT_DIR_REQ ? 0 : 1)]; |
| 10608 | if (!flt_hlua) |
| 10609 | goto end; |
| 10610 | |
| 10611 | if (!HLUA_IS_RUNNING(flt_hlua)) { |
| 10612 | int extra_idx = lua_gettop(flt_hlua->T); |
| 10613 | |
| 10614 | /* The following Lua calls can fail. */ |
| 10615 | if (!SET_SAFE_LJMP(flt_hlua)) { |
| 10616 | const char *error; |
| 10617 | |
| 10618 | if (lua_type(flt_hlua->T, -1) == LUA_TSTRING) |
| 10619 | error = lua_tostring(flt_hlua->T, -1); |
| 10620 | else |
| 10621 | error = "critical error"; |
| 10622 | SEND_ERR(s->be, "Lua filter '%s': %s.\n", conf->reg->name, error); |
| 10623 | goto end; |
| 10624 | } |
| 10625 | |
| 10626 | /* Check stack size. */ |
| 10627 | if (!lua_checkstack(flt_hlua->T, 3)) { |
| 10628 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 10629 | RESET_SAFE_LJMP(flt_hlua); |
| 10630 | goto end; |
| 10631 | } |
| 10632 | |
| 10633 | lua_rawgeti(flt_hlua->T, LUA_REGISTRYINDEX, flt_ctx->ref); |
| 10634 | if (lua_getfield(flt_hlua->T, -1, fun) != LUA_TFUNCTION) { |
| 10635 | RESET_SAFE_LJMP(flt_hlua); |
| 10636 | goto end; |
| 10637 | } |
| 10638 | lua_insert(flt_hlua->T, -2); |
| 10639 | |
| 10640 | if (!hlua_txn_new(flt_hlua->T, s, s->be, dir, hflags)) { |
| 10641 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 10642 | RESET_SAFE_LJMP(flt_hlua); |
| 10643 | goto end; |
| 10644 | } |
| 10645 | flt_hlua->nargs = 2; |
| 10646 | |
| 10647 | if (flags & HLUA_FLT_CB_ARG_CHN) { |
| 10648 | if (dir == SMP_OPT_DIR_REQ) |
| 10649 | lua_getfield(flt_hlua->T, -1, "req"); |
| 10650 | else |
| 10651 | lua_getfield(flt_hlua->T, -1, "res"); |
| 10652 | if (lua_type(flt_hlua->T, -1) == LUA_TTABLE) { |
| 10653 | lua_pushstring(flt_hlua->T, "__filter"); |
| 10654 | lua_pushlightuserdata(flt_hlua->T, filter); |
| 10655 | lua_settable(flt_hlua->T, -3); |
| 10656 | } |
| 10657 | flt_hlua->nargs++; |
| 10658 | } |
| 10659 | else if (flags & HLUA_FLT_CB_ARG_HTTP_MSG) { |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 10660 | if (dir == SMP_OPT_DIR_REQ) |
| 10661 | lua_getfield(flt_hlua->T, -1, "http_req"); |
| 10662 | else |
| 10663 | lua_getfield(flt_hlua->T, -1, "http_res"); |
| 10664 | if (lua_type(flt_hlua->T, -1) == LUA_TTABLE) { |
| 10665 | lua_pushstring(flt_hlua->T, "__filter"); |
| 10666 | lua_pushlightuserdata(flt_hlua->T, filter); |
| 10667 | lua_settable(flt_hlua->T, -3); |
| 10668 | } |
| 10669 | flt_hlua->nargs++; |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10670 | } |
| 10671 | |
| 10672 | /* Check stack size. */ |
| 10673 | if (!lua_checkstack(flt_hlua->T, 1)) { |
| 10674 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 10675 | RESET_SAFE_LJMP(flt_hlua); |
| 10676 | goto end; |
| 10677 | } |
| 10678 | |
| 10679 | while (extra_idx--) { |
| 10680 | lua_pushvalue(flt_hlua->T, 1); |
| 10681 | lua_remove(flt_hlua->T, 1); |
| 10682 | flt_hlua->nargs++; |
| 10683 | } |
| 10684 | |
| 10685 | /* We must initialize the execution timeouts. */ |
| 10686 | flt_hlua->max_time = hlua_timeout_session; |
| 10687 | |
| 10688 | /* At this point the execution is safe. */ |
| 10689 | RESET_SAFE_LJMP(flt_hlua); |
| 10690 | } |
| 10691 | |
| 10692 | switch (hlua_ctx_resume(flt_hlua, !(flags & HLUA_FLT_CB_FINAL))) { |
| 10693 | case HLUA_E_OK: |
| 10694 | /* Catch the return value if it required */ |
| 10695 | if ((flags & HLUA_FLT_CB_RETVAL) && lua_gettop(flt_hlua->T) > 0) { |
| 10696 | ret = lua_tointeger(flt_hlua->T, -1); |
| 10697 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 10698 | } |
| 10699 | |
| 10700 | /* Set timeout in the required channel. */ |
| 10701 | if (flt_hlua->wake_time != TICK_ETERNITY) { |
| 10702 | if (dir == SMP_OPT_DIR_REQ) |
| 10703 | s->req.analyse_exp = flt_hlua->wake_time; |
| 10704 | else |
| 10705 | s->res.analyse_exp = flt_hlua->wake_time; |
| 10706 | } |
| 10707 | break; |
| 10708 | case HLUA_E_AGAIN: |
| 10709 | /* Set timeout in the required channel. */ |
| 10710 | if (flt_hlua->wake_time != TICK_ETERNITY) { |
| 10711 | if (dir == SMP_OPT_DIR_REQ) |
| 10712 | s->req.analyse_exp = flt_hlua->wake_time; |
| 10713 | else |
| 10714 | s->res.analyse_exp = flt_hlua->wake_time; |
| 10715 | } |
| 10716 | /* Some actions can be wake up when a "write" event |
| 10717 | * is detected on a response channel. This is useful |
| 10718 | * only for actions targeted on the requests. |
| 10719 | */ |
| 10720 | if (HLUA_IS_WAKERESWR(flt_hlua)) |
| 10721 | s->res.flags |= CF_WAKE_WRITE; |
| 10722 | if (HLUA_IS_WAKEREQWR(flt_hlua)) |
| 10723 | s->req.flags |= CF_WAKE_WRITE; |
| 10724 | ret = 0; |
| 10725 | goto end; |
| 10726 | case HLUA_E_ERRMSG: |
| 10727 | SEND_ERR(s->be, "Lua filter '%s' : %s.\n", conf->reg->name, lua_tostring(flt_hlua->T, -1)); |
| 10728 | ret = -1; |
| 10729 | goto end; |
| 10730 | case HLUA_E_ETMOUT: |
| 10731 | SEND_ERR(s->be, "Lua filter '%s' : '%s' callback execution timeout.\n", conf->reg->name, fun); |
| 10732 | goto end; |
| 10733 | case HLUA_E_NOMEM: |
| 10734 | SEND_ERR(s->be, "Lua filter '%s' : out of memory error.\n", conf->reg->name); |
| 10735 | goto end; |
| 10736 | case HLUA_E_YIELD: |
| 10737 | SEND_ERR(s->be, "Lua filter '%s': yield functions like core.tcp() or core.sleep()" |
| 10738 | " are not allowed from '%s' callback.\n", conf->reg->name, fun); |
| 10739 | goto end; |
| 10740 | case HLUA_E_ERR: |
| 10741 | SEND_ERR(s->be, "Lua filter '%s': '%s' returns an unknown error.\n", conf->reg->name, fun); |
| 10742 | goto end; |
| 10743 | default: |
| 10744 | goto end; |
| 10745 | } |
| 10746 | |
| 10747 | |
| 10748 | end: |
| 10749 | return ret; |
| 10750 | } |
| 10751 | |
| 10752 | static int hlua_filter_start_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 10753 | { |
| 10754 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10755 | |
| 10756 | flt_ctx->flags = 0; |
| 10757 | return hlua_filter_callback(s, filter, "start_analyze", |
| 10758 | (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 10759 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_CHN)); |
| 10760 | } |
| 10761 | |
| 10762 | static int hlua_filter_end_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 10763 | { |
| 10764 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10765 | |
| 10766 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 10767 | return hlua_filter_callback(s, filter, "end_analyze", |
| 10768 | (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 10769 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_CHN)); |
| 10770 | } |
| 10771 | |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 10772 | static int hlua_filter_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg) |
| 10773 | { |
| 10774 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10775 | |
| 10776 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 10777 | return hlua_filter_callback(s, filter, "http_headers", |
| 10778 | (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 10779 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 10780 | } |
| 10781 | |
| 10782 | static int hlua_filter_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg, |
| 10783 | unsigned int offset, unsigned int len) |
| 10784 | { |
| 10785 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10786 | struct hlua *flt_hlua; |
| 10787 | int dir = (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES); |
| 10788 | int idx = (dir == SMP_OPT_DIR_REQ ? 0 : 1); |
| 10789 | int ret; |
| 10790 | |
| 10791 | flt_hlua = flt_ctx->hlua[idx]; |
| 10792 | flt_ctx->cur_off[idx] = offset; |
| 10793 | flt_ctx->cur_len[idx] = len; |
| 10794 | flt_ctx->flags |= HLUA_FLT_CTX_FL_PAYLOAD; |
| 10795 | ret = hlua_filter_callback(s, filter, "http_payload", dir, (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 10796 | if (ret != -1) { |
| 10797 | ret = flt_ctx->cur_len[idx]; |
| 10798 | if (lua_gettop(flt_hlua->T) > 0) { |
| 10799 | ret = lua_tointeger(flt_hlua->T, -1); |
| 10800 | if (ret > flt_ctx->cur_len[idx]) |
| 10801 | ret = flt_ctx->cur_len[idx]; |
| 10802 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 10803 | } |
| 10804 | } |
| 10805 | return ret; |
| 10806 | } |
| 10807 | |
| 10808 | static int hlua_filter_http_end(struct stream *s, struct filter *filter, struct http_msg *msg) |
| 10809 | { |
| 10810 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10811 | |
| 10812 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 10813 | return hlua_filter_callback(s, filter, "http_end", |
| 10814 | (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 10815 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 10816 | } |
| 10817 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10818 | static int hlua_filter_tcp_payload(struct stream *s, struct filter *filter, struct channel *chn, |
| 10819 | unsigned int offset, unsigned int len) |
| 10820 | { |
| 10821 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10822 | struct hlua *flt_hlua; |
| 10823 | int dir = (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES); |
| 10824 | int idx = (dir == SMP_OPT_DIR_REQ ? 0 : 1); |
| 10825 | int ret; |
| 10826 | |
| 10827 | flt_hlua = flt_ctx->hlua[idx]; |
| 10828 | flt_ctx->cur_off[idx] = offset; |
| 10829 | flt_ctx->cur_len[idx] = len; |
| 10830 | flt_ctx->flags |= HLUA_FLT_CTX_FL_PAYLOAD; |
| 10831 | ret = hlua_filter_callback(s, filter, "tcp_payload", dir, (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_ARG_CHN)); |
| 10832 | if (ret != -1) { |
| 10833 | ret = flt_ctx->cur_len[idx]; |
| 10834 | if (lua_gettop(flt_hlua->T) > 0) { |
| 10835 | ret = lua_tointeger(flt_hlua->T, -1); |
| 10836 | if (ret > flt_ctx->cur_len[idx]) |
| 10837 | ret = flt_ctx->cur_len[idx]; |
| 10838 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 10839 | } |
| 10840 | } |
| 10841 | return ret; |
| 10842 | } |
| 10843 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10844 | static int hlua_filter_parse_fct(char **args, int *cur_arg, struct proxy *px, |
| 10845 | struct flt_conf *fconf, char **err, void *private) |
| 10846 | { |
| 10847 | struct hlua_reg_filter *reg_flt = private; |
| 10848 | lua_State *L; |
| 10849 | struct hlua_flt_config *conf = NULL; |
| 10850 | const char *flt_id = NULL; |
| 10851 | int state_id, pos, flt_flags = 0; |
| 10852 | struct flt_ops *hlua_flt_ops = NULL; |
| 10853 | |
| 10854 | state_id = reg_flt_to_stack_id(reg_flt); |
| 10855 | L = hlua_states[state_id]; |
| 10856 | |
| 10857 | /* Initialize the filter ops with default callbacks */ |
| 10858 | hlua_flt_ops = calloc(1, sizeof(*hlua_flt_ops)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10859 | if (!hlua_flt_ops) |
| 10860 | goto error; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10861 | hlua_flt_ops->init = hlua_filter_init; |
| 10862 | hlua_flt_ops->deinit = hlua_filter_deinit; |
| 10863 | if (state_id) { |
| 10864 | /* Set per-thread callback if script is loaded per-thread */ |
| 10865 | hlua_flt_ops->init_per_thread = hlua_filter_init_per_thread; |
| 10866 | hlua_flt_ops->deinit_per_thread = hlua_filter_deinit_per_thread; |
| 10867 | } |
| 10868 | hlua_flt_ops->attach = hlua_filter_new; |
| 10869 | hlua_flt_ops->detach = hlua_filter_delete; |
| 10870 | |
| 10871 | /* Push the filter class on the stack and resolve all callbacks */ |
| 10872 | lua_rawgeti(L, LUA_REGISTRYINDEX, reg_flt->flt_ref[state_id]); |
| 10873 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10874 | if (lua_getfield(L, -1, "start_analyze") == LUA_TFUNCTION) |
| 10875 | hlua_flt_ops->channel_start_analyze = hlua_filter_start_analyze; |
| 10876 | lua_pop(L, 1); |
| 10877 | if (lua_getfield(L, -1, "end_analyze") == LUA_TFUNCTION) |
| 10878 | hlua_flt_ops->channel_end_analyze = hlua_filter_end_analyze; |
| 10879 | lua_pop(L, 1); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 10880 | if (lua_getfield(L, -1, "http_headers") == LUA_TFUNCTION) |
| 10881 | hlua_flt_ops->http_headers = hlua_filter_http_headers; |
| 10882 | lua_pop(L, 1); |
| 10883 | if (lua_getfield(L, -1, "http_payload") == LUA_TFUNCTION) |
| 10884 | hlua_flt_ops->http_payload = hlua_filter_http_payload; |
| 10885 | lua_pop(L, 1); |
| 10886 | if (lua_getfield(L, -1, "http_end") == LUA_TFUNCTION) |
| 10887 | hlua_flt_ops->http_end = hlua_filter_http_end; |
| 10888 | lua_pop(L, 1); |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10889 | if (lua_getfield(L, -1, "tcp_payload") == LUA_TFUNCTION) |
| 10890 | hlua_flt_ops->tcp_payload = hlua_filter_tcp_payload; |
| 10891 | lua_pop(L, 1); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10892 | |
| 10893 | /* Get id and flags of the filter class */ |
| 10894 | if (lua_getfield(L, -1, "id") == LUA_TSTRING) |
| 10895 | flt_id = lua_tostring(L, -1); |
| 10896 | lua_pop(L, 1); |
| 10897 | if (lua_getfield(L, -1, "flags") == LUA_TNUMBER) |
| 10898 | flt_flags = lua_tointeger(L, -1); |
| 10899 | lua_pop(L, 1); |
| 10900 | |
| 10901 | /* Create the filter config */ |
| 10902 | conf = calloc(1, sizeof(*conf)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10903 | if (!conf) |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10904 | goto error; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10905 | conf->reg = reg_flt; |
| 10906 | |
| 10907 | /* duplicate args */ |
| 10908 | for (pos = 0; *args[*cur_arg + 1 + pos]; pos++); |
| 10909 | conf->args = calloc(pos + 1, sizeof(*conf->args)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10910 | if (!conf->args) |
| 10911 | goto error; |
| 10912 | for (pos = 0; *args[*cur_arg + 1 + pos]; pos++) { |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10913 | conf->args[pos] = strdup(args[*cur_arg + 1 + pos]); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10914 | if (!conf->args[pos]) |
| 10915 | goto error; |
| 10916 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10917 | conf->args[pos] = NULL; |
| 10918 | *cur_arg += pos + 1; |
| 10919 | |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10920 | if (flt_id) { |
| 10921 | fconf->id = strdup(flt_id); |
| 10922 | if (!fconf->id) |
| 10923 | goto error; |
| 10924 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10925 | fconf->flags = flt_flags; |
| 10926 | fconf->conf = conf; |
| 10927 | fconf->ops = hlua_flt_ops; |
| 10928 | |
| 10929 | lua_settop(L, 0); |
| 10930 | return 0; |
| 10931 | |
| 10932 | error: |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10933 | 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] | 10934 | free(hlua_flt_ops); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10935 | if (conf && conf->args) { |
| 10936 | for (pos = 0; conf->args[pos]; pos++) |
| 10937 | free(conf->args[pos]); |
| 10938 | free(conf->args); |
| 10939 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10940 | free(conf); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10941 | free((char *)fconf->id); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10942 | lua_settop(L, 0); |
| 10943 | return -1; |
| 10944 | } |
| 10945 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10946 | __LJMP static int hlua_register_data_filter(lua_State *L) |
| 10947 | { |
| 10948 | struct filter *filter; |
| 10949 | struct channel *chn; |
| 10950 | |
| 10951 | MAY_LJMP(check_args(L, 2, "register_data_filter")); |
| 10952 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
| 10953 | chn = MAY_LJMP(hlua_checkchannel(L, 2)); |
| 10954 | |
| 10955 | lua_getfield(L, 1, "__filter"); |
| 10956 | MAY_LJMP(luaL_checktype(L, -1, LUA_TLIGHTUSERDATA)); |
| 10957 | filter = lua_touserdata (L, -1); |
| 10958 | lua_pop(L, 1); |
| 10959 | |
| 10960 | register_data_filter(chn_strm(chn), chn, filter); |
| 10961 | return 1; |
| 10962 | } |
| 10963 | |
| 10964 | __LJMP static int hlua_unregister_data_filter(lua_State *L) |
| 10965 | { |
| 10966 | struct filter *filter; |
| 10967 | struct channel *chn; |
| 10968 | |
| 10969 | MAY_LJMP(check_args(L, 2, "unregister_data_filter")); |
| 10970 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
| 10971 | chn = MAY_LJMP(hlua_checkchannel(L, 2)); |
| 10972 | |
| 10973 | lua_getfield(L, 1, "__filter"); |
| 10974 | MAY_LJMP(luaL_checktype(L, -1, LUA_TLIGHTUSERDATA)); |
| 10975 | filter = lua_touserdata (L, -1); |
| 10976 | lua_pop(L, 1); |
| 10977 | |
| 10978 | unregister_data_filter(chn_strm(chn), chn, filter); |
| 10979 | return 1; |
| 10980 | } |
| 10981 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10982 | /* 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] | 10983 | * 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] | 10984 | * parse configuration arguments. |
| 10985 | */ |
| 10986 | __LJMP static int hlua_register_filter(lua_State *L) |
| 10987 | { |
| 10988 | struct buffer *trash; |
| 10989 | struct flt_kw_list *fkl; |
| 10990 | struct flt_kw *fkw; |
| 10991 | const char *name; |
| 10992 | struct hlua_reg_filter *reg_flt= NULL; |
| 10993 | int flt_ref, fun_ref; |
| 10994 | int len; |
| 10995 | |
| 10996 | MAY_LJMP(check_args(L, 3, "register_filter")); |
| 10997 | |
| 10998 | /* First argument : filter name. */ |
| 10999 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 11000 | |
| 11001 | /* Second argument : The filter class */ |
| 11002 | flt_ref = MAY_LJMP(hlua_checktable(L, 2)); |
| 11003 | |
| 11004 | /* Third argument : lua function. */ |
| 11005 | fun_ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 11006 | |
| 11007 | trash = get_trash_chunk(); |
| 11008 | chunk_printf(trash, "lua.%s", name); |
| 11009 | fkw = flt_find_kw(trash->area); |
| 11010 | if (fkw != NULL) { |
| 11011 | reg_flt = fkw->private; |
| 11012 | if (reg_flt->flt_ref[hlua_state_id] != -1 || reg_flt->fun_ref[hlua_state_id] != -1) { |
| 11013 | ha_warning("Trying to register filter 'lua.%s' more than once. " |
| 11014 | "This will become a hard error in version 2.5.\n", name); |
| 11015 | } |
| 11016 | reg_flt->flt_ref[hlua_state_id] = flt_ref; |
| 11017 | reg_flt->fun_ref[hlua_state_id] = fun_ref; |
| 11018 | return 0; |
| 11019 | } |
| 11020 | |
| 11021 | fkl = calloc(1, sizeof(*fkl) + sizeof(struct flt_kw) * 2); |
| 11022 | if (!fkl) |
| 11023 | goto alloc_error; |
| 11024 | fkl->scope = "HLUA"; |
| 11025 | |
| 11026 | reg_flt = new_hlua_reg_filter(name); |
| 11027 | if (!reg_flt) |
| 11028 | goto alloc_error; |
| 11029 | |
| 11030 | reg_flt->flt_ref[hlua_state_id] = flt_ref; |
| 11031 | reg_flt->fun_ref[hlua_state_id] = fun_ref; |
| 11032 | |
| 11033 | /* The filter keyword */ |
| 11034 | len = strlen("lua.") + strlen(name) + 1; |
| 11035 | fkl->kw[0].kw = calloc(1, len); |
| 11036 | if (!fkl->kw[0].kw) |
| 11037 | goto alloc_error; |
| 11038 | |
| 11039 | snprintf((char *)fkl->kw[0].kw, len, "lua.%s", name); |
| 11040 | |
| 11041 | fkl->kw[0].parse = hlua_filter_parse_fct; |
| 11042 | fkl->kw[0].private = reg_flt; |
| 11043 | memset(&fkl->kw[1], 0, sizeof(*fkl->kw)); |
| 11044 | |
| 11045 | /* Register this new filter */ |
| 11046 | flt_register_keywords(fkl); |
| 11047 | |
| 11048 | return 0; |
| 11049 | |
| 11050 | alloc_error: |
| 11051 | release_hlua_reg_filter(reg_flt); |
| 11052 | ha_free(&fkl); |
| 11053 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 11054 | return 0; /* Never reached */ |
| 11055 | } |
| 11056 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11057 | 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] | 11058 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11059 | char **err, unsigned int *timeout) |
| 11060 | { |
| 11061 | const char *error; |
| 11062 | |
| 11063 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 11064 | if (error == PARSE_TIME_OVER) { |
| 11065 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 11066 | args[1], args[0]); |
| 11067 | return -1; |
| 11068 | } |
| 11069 | else if (error == PARSE_TIME_UNDER) { |
| 11070 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 11071 | args[1], args[0]); |
| 11072 | return -1; |
| 11073 | } |
| 11074 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11075 | memprintf(err, "%s: invalid timeout", args[0]); |
| 11076 | return -1; |
| 11077 | } |
| 11078 | return 0; |
| 11079 | } |
| 11080 | |
| 11081 | 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] | 11082 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11083 | char **err) |
| 11084 | { |
| 11085 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 11086 | file, line, err, &hlua_timeout_session); |
| 11087 | } |
| 11088 | |
| 11089 | 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] | 11090 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11091 | char **err) |
| 11092 | { |
| 11093 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 11094 | file, line, err, &hlua_timeout_task); |
| 11095 | } |
| 11096 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11097 | 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] | 11098 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11099 | char **err) |
| 11100 | { |
| 11101 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 11102 | file, line, err, &hlua_timeout_applet); |
| 11103 | } |
| 11104 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 11105 | 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] | 11106 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 11107 | char **err) |
| 11108 | { |
| 11109 | char *error; |
| 11110 | |
| 11111 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 11112 | if (*error != '\0') { |
| 11113 | memprintf(err, "%s: invalid number", args[0]); |
| 11114 | return -1; |
| 11115 | } |
| 11116 | return 0; |
| 11117 | } |
| 11118 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11119 | 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] | 11120 | const struct proxy *defpx, const char *file, int line, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11121 | char **err) |
| 11122 | { |
| 11123 | char *error; |
| 11124 | |
| 11125 | if (*(args[1]) == 0) { |
| 11126 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 11127 | return -1; |
| 11128 | } |
| 11129 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 11130 | if (*error != '\0') { |
| 11131 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 11132 | return -1; |
| 11133 | } |
| 11134 | return 0; |
| 11135 | } |
| 11136 | |
| 11137 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11138 | /* This function is called by the main configuration key "lua-load". It loads and |
| 11139 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 11140 | * the main lua entry point. |
| 11141 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 11142 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 11143 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11144 | * |
| 11145 | * In some error case, LUA set an error message in top of the stack. This function |
| 11146 | * 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] | 11147 | * |
| 11148 | * This function can fail with an abort() due to an Lua critical error. |
| 11149 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 11150 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11151 | */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11152 | static int hlua_load_state(char *filename, lua_State *L, char **err) |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11153 | { |
| 11154 | int error; |
| 11155 | |
| 11156 | /* Just load and compile the file. */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11157 | error = luaL_loadfile(L, filename); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11158 | if (error) { |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11159 | memprintf(err, "error in Lua file '%s': %s", filename, lua_tostring(L, -1)); |
| 11160 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11161 | return -1; |
| 11162 | } |
| 11163 | |
| 11164 | /* If no syntax error where detected, execute the code. */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11165 | error = lua_pcall(L, 0, LUA_MULTRET, 0); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11166 | switch (error) { |
| 11167 | case LUA_OK: |
| 11168 | break; |
| 11169 | case LUA_ERRRUN: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11170 | memprintf(err, "Lua runtime error: %s\n", lua_tostring(L, -1)); |
| 11171 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11172 | return -1; |
| 11173 | case LUA_ERRMEM: |
Thierry Fournier | de6145f | 2020-11-29 00:55:53 +0100 | [diff] [blame] | 11174 | memprintf(err, "Lua out of memory error\n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11175 | return -1; |
| 11176 | case LUA_ERRERR: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11177 | memprintf(err, "Lua message handler error: %s\n", lua_tostring(L, -1)); |
| 11178 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11179 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 11180 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11181 | case LUA_ERRGCMM: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11182 | memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(L, -1)); |
| 11183 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11184 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 11185 | #endif |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11186 | default: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11187 | memprintf(err, "Lua unknown error: %s\n", lua_tostring(L, -1)); |
| 11188 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11189 | return -1; |
| 11190 | } |
| 11191 | |
| 11192 | return 0; |
| 11193 | } |
| 11194 | |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11195 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 11196 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11197 | char **err) |
| 11198 | { |
| 11199 | if (*(args[1]) == 0) { |
| 11200 | memprintf(err, "'%s' expects a file name as parameter.\n", args[0]); |
| 11201 | return -1; |
| 11202 | } |
| 11203 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11204 | /* loading for global state */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 11205 | hlua_state_id = 0; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11206 | ha_set_thread(NULL); |
Thierry Fournier | afc63e2 | 2020-11-28 17:06:51 +0100 | [diff] [blame] | 11207 | return hlua_load_state(args[1], hlua_states[0], err); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11208 | } |
| 11209 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11210 | 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] | 11211 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11212 | char **err) |
| 11213 | { |
| 11214 | int len; |
| 11215 | |
| 11216 | if (*(args[1]) == 0) { |
| 11217 | memprintf(err, "'%s' expects a file as parameter.\n", args[0]); |
| 11218 | return -1; |
| 11219 | } |
| 11220 | |
| 11221 | if (per_thread_load == NULL) { |
| 11222 | /* allocate the first entry large enough to store the final NULL */ |
| 11223 | per_thread_load = calloc(1, sizeof(*per_thread_load)); |
| 11224 | if (per_thread_load == NULL) { |
| 11225 | memprintf(err, "out of memory error"); |
| 11226 | return -1; |
| 11227 | } |
| 11228 | } |
| 11229 | |
| 11230 | /* count used entries */ |
| 11231 | for (len = 0; per_thread_load[len] != NULL; len++) |
| 11232 | ; |
| 11233 | |
| 11234 | per_thread_load = realloc(per_thread_load, (len + 2) * sizeof(*per_thread_load)); |
| 11235 | if (per_thread_load == NULL) { |
| 11236 | memprintf(err, "out of memory error"); |
| 11237 | return -1; |
| 11238 | } |
| 11239 | |
| 11240 | per_thread_load[len] = strdup(args[1]); |
| 11241 | per_thread_load[len + 1] = NULL; |
| 11242 | |
| 11243 | if (per_thread_load[len] == NULL) { |
| 11244 | memprintf(err, "out of memory error"); |
| 11245 | return -1; |
| 11246 | } |
| 11247 | |
| 11248 | /* loading for thread 1 only */ |
| 11249 | hlua_state_id = 1; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11250 | ha_set_thread(NULL); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11251 | return hlua_load_state(args[1], hlua_states[1], err); |
| 11252 | } |
| 11253 | |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 11254 | /* Prepend the given <path> followed by a semicolon to the `package.<type>` variable |
| 11255 | * in the given <ctx>. |
| 11256 | */ |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 11257 | static int hlua_prepend_path(lua_State *L, char *type, char *path) |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 11258 | { |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 11259 | lua_getglobal(L, "package"); /* push package variable */ |
| 11260 | lua_pushstring(L, path); /* push given path */ |
| 11261 | lua_pushstring(L, ";"); /* push semicolon */ |
| 11262 | lua_getfield(L, -3, type); /* push old path */ |
| 11263 | lua_concat(L, 3); /* concatenate to new path */ |
| 11264 | lua_setfield(L, -2, type); /* store new path */ |
| 11265 | lua_pop(L, 1); /* pop package variable */ |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 11266 | |
| 11267 | return 0; |
| 11268 | } |
| 11269 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11270 | 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] | 11271 | const struct proxy *defpx, const char *file, int line, |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11272 | char **err) |
| 11273 | { |
| 11274 | char *path; |
| 11275 | char *type = "path"; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11276 | struct prepend_path *p = NULL; |
Bertrand Jacquin | 7fbc770 | 2021-11-24 21:16:06 +0000 | [diff] [blame] | 11277 | size_t i; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11278 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11279 | if (too_many_args(2, args, err, NULL)) { |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11280 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11281 | } |
| 11282 | |
| 11283 | if (!(*args[1])) { |
| 11284 | memprintf(err, "'%s' expects to receive a <path> as argument", args[0]); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11285 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11286 | } |
| 11287 | path = args[1]; |
| 11288 | |
| 11289 | if (*args[2]) { |
| 11290 | if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) { |
| 11291 | 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] | 11292 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11293 | } |
| 11294 | type = args[2]; |
| 11295 | } |
| 11296 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11297 | p = calloc(1, sizeof(*p)); |
| 11298 | if (p == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 11299 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11300 | goto err; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11301 | } |
| 11302 | p->path = strdup(path); |
| 11303 | if (p->path == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 11304 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11305 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11306 | } |
| 11307 | p->type = strdup(type); |
| 11308 | if (p->type == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 11309 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11310 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11311 | } |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 11312 | LIST_APPEND(&prepend_path_list, &p->l); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11313 | |
Tim Duesterhus | 9e5e586 | 2021-10-11 18:51:08 +0200 | [diff] [blame] | 11314 | /* Handle the global state and the per-thread state for the first |
| 11315 | * thread. The remaining threads will be initialized based on |
| 11316 | * prepend_path_list. |
| 11317 | */ |
Bertrand Jacquin | 7fbc770 | 2021-11-24 21:16:06 +0000 | [diff] [blame] | 11318 | for (i = 0; i < 2; i++) { |
Tim Duesterhus | 9e5e586 | 2021-10-11 18:51:08 +0200 | [diff] [blame] | 11319 | lua_State *L = hlua_states[i]; |
| 11320 | const char *error; |
| 11321 | |
| 11322 | if (setjmp(safe_ljmp_env) != 0) { |
| 11323 | lua_atpanic(L, hlua_panic_safe); |
| 11324 | if (lua_type(L, -1) == LUA_TSTRING) |
| 11325 | error = lua_tostring(L, -1); |
| 11326 | else |
| 11327 | error = "critical error"; |
| 11328 | fprintf(stderr, "lua-prepend-path: %s.\n", error); |
| 11329 | exit(1); |
| 11330 | } else { |
| 11331 | lua_atpanic(L, hlua_panic_ljmp); |
| 11332 | } |
| 11333 | |
| 11334 | hlua_prepend_path(L, type, path); |
| 11335 | |
| 11336 | lua_atpanic(L, hlua_panic_safe); |
| 11337 | } |
| 11338 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11339 | return 0; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11340 | |
| 11341 | err2: |
| 11342 | free(p->type); |
| 11343 | free(p->path); |
| 11344 | err: |
| 11345 | free(p); |
| 11346 | return -1; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11347 | } |
| 11348 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11349 | /* configuration keywords declaration */ |
| 11350 | static struct cfg_kw_list cfg_kws = {{ },{ |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11351 | { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11352 | { CFG_GLOBAL, "lua-load", hlua_load }, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11353 | { CFG_GLOBAL, "lua-load-per-thread", hlua_load_per_thread }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11354 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 11355 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 11356 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 11357 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11358 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11359 | { 0, NULL, NULL }, |
| 11360 | }}; |
| 11361 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 11362 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 11363 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 11364 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 11365 | /* This function can fail with an abort() due to an Lua critical error. |
| 11366 | * We are in the initialisation process of HAProxy, this abort() is |
| 11367 | * tolerated. |
| 11368 | */ |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11369 | int hlua_post_init_state(lua_State *L) |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11370 | { |
| 11371 | struct hlua_init_function *init; |
| 11372 | const char *msg; |
| 11373 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 11374 | const char *error; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11375 | const char *kind; |
| 11376 | const char *trace; |
| 11377 | int return_status = 1; |
| 11378 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 11379 | int nres; |
| 11380 | #endif |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11381 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11382 | /* disable memory limit checks if limit is not set */ |
| 11383 | if (!hlua_global_allocator.limit) |
| 11384 | hlua_global_allocator.limit = ~hlua_global_allocator.limit; |
| 11385 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 11386 | /* Call post initialisation function in safe environment. */ |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11387 | if (setjmp(safe_ljmp_env) != 0) { |
| 11388 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11389 | if (lua_type(L, -1) == LUA_TSTRING) |
| 11390 | error = lua_tostring(L, -1); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 11391 | else |
| 11392 | error = "critical error"; |
| 11393 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 11394 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11395 | } else { |
| 11396 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 11397 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 11398 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11399 | hlua_fcn_post_init(L); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 11400 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 11401 | list_for_each_entry(init, &hlua_init_functions[hlua_state_id], l) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11402 | lua_rawgeti(L, LUA_REGISTRYINDEX, init->function_ref); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11403 | |
| 11404 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11405 | ret = lua_resume(L, L, 0, &nres); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11406 | #else |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11407 | ret = lua_resume(L, L, 0); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11408 | #endif |
| 11409 | kind = NULL; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11410 | switch (ret) { |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11411 | |
| 11412 | case LUA_OK: |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11413 | lua_pop(L, -1); |
Thierry Fournier | 13d08b7 | 2020-11-28 11:02:58 +0100 | [diff] [blame] | 11414 | break; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11415 | |
| 11416 | case LUA_ERRERR: |
| 11417 | kind = "message handler error"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 11418 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11419 | case LUA_ERRRUN: |
| 11420 | if (!kind) |
| 11421 | kind = "runtime error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11422 | msg = lua_tostring(L, -1); |
| 11423 | lua_settop(L, 0); /* Empty the stack. */ |
| 11424 | lua_pop(L, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 11425 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11426 | if (msg) |
| 11427 | ha_alert("Lua init: %s: '%s' from %s\n", kind, msg, trace); |
| 11428 | else |
| 11429 | ha_alert("Lua init: unknown %s from %s\n", kind, trace); |
| 11430 | return_status = 0; |
| 11431 | break; |
| 11432 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11433 | default: |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11434 | /* Unknown error */ |
| 11435 | kind = "Unknown error"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 11436 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11437 | case LUA_YIELD: |
| 11438 | /* yield is not configured at this step, this state doesn't happen */ |
| 11439 | if (!kind) |
| 11440 | kind = "yield not allowed"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 11441 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11442 | case LUA_ERRMEM: |
| 11443 | if (!kind) |
| 11444 | kind = "out of memory error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11445 | lua_settop(L, 0); |
| 11446 | lua_pop(L, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 11447 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11448 | ha_alert("Lua init: %s: %s\n", kind, trace); |
| 11449 | return_status = 0; |
| 11450 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11451 | } |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11452 | if (!return_status) |
| 11453 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11454 | } |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11455 | |
| 11456 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11457 | return return_status; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11458 | } |
| 11459 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11460 | int hlua_post_init() |
| 11461 | { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11462 | int ret; |
| 11463 | int i; |
| 11464 | int errors; |
| 11465 | char *err = NULL; |
| 11466 | struct hlua_function *fcn; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11467 | struct hlua_reg_filter *reg_flt; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11468 | |
Willy Tarreau | b131049 | 2021-08-30 09:35:18 +0200 | [diff] [blame] | 11469 | #if defined(USE_OPENSSL) |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11470 | /* Initialize SSL server. */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 11471 | if (socket_ssl->xprt->prepare_srv) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11472 | int saved_used_backed = global.ssl_used_backend; |
| 11473 | // don't affect maxconn automatic computation |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 11474 | socket_ssl->xprt->prepare_srv(socket_ssl); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11475 | global.ssl_used_backend = saved_used_backed; |
| 11476 | } |
| 11477 | #endif |
| 11478 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 11479 | /* Perform post init of common thread */ |
| 11480 | hlua_state_id = 0; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11481 | ha_set_thread(&ha_thread_info[0]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11482 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 11483 | if (ret == 0) |
| 11484 | return 0; |
| 11485 | |
| 11486 | /* init remaining lua states and load files */ |
| 11487 | for (hlua_state_id = 2; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 11488 | |
| 11489 | /* set thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11490 | ha_set_thread(&ha_thread_info[hlua_state_id - 1]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11491 | |
| 11492 | /* Init lua state */ |
| 11493 | hlua_states[hlua_state_id] = hlua_init_state(hlua_state_id); |
| 11494 | |
| 11495 | /* Load lua files */ |
| 11496 | for (i = 0; per_thread_load && per_thread_load[i]; i++) { |
| 11497 | ret = hlua_load_state(per_thread_load[i], hlua_states[hlua_state_id], &err); |
| 11498 | if (ret != 0) { |
| 11499 | ha_alert("Lua init: %s\n", err); |
| 11500 | return 0; |
| 11501 | } |
| 11502 | } |
| 11503 | } |
| 11504 | |
| 11505 | /* Reset thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11506 | ha_set_thread(NULL); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11507 | |
| 11508 | /* Execute post init for all states */ |
| 11509 | for (hlua_state_id = 1; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 11510 | |
| 11511 | /* set thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11512 | ha_set_thread(&ha_thread_info[hlua_state_id - 1]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11513 | |
| 11514 | /* run post init */ |
| 11515 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 11516 | if (ret == 0) |
| 11517 | return 0; |
| 11518 | } |
| 11519 | |
| 11520 | /* Reset thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11521 | ha_set_thread(NULL); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11522 | |
| 11523 | /* control functions registering. Each function must have: |
| 11524 | * - only the function_ref[0] set positive and all other to -1 |
| 11525 | * - only the function_ref[0] set to -1 and all other positive |
| 11526 | * This ensure a same reference is not used both in shared |
| 11527 | * lua state and thread dedicated lua state. Note: is the case |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 11528 | * reach, the shared state is priority, but the bug will be |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11529 | * complicated to found for the end user. |
| 11530 | */ |
| 11531 | errors = 0; |
| 11532 | list_for_each_entry(fcn, &referenced_functions, l) { |
| 11533 | ret = 0; |
| 11534 | for (i = 1; i < global.nbthread + 1; i++) { |
| 11535 | if (fcn->function_ref[i] == -1) |
| 11536 | ret--; |
| 11537 | else |
| 11538 | ret++; |
| 11539 | } |
| 11540 | if (abs(ret) != global.nbthread) { |
| 11541 | ha_alert("Lua function '%s' is not referenced in all thread. " |
| 11542 | "Expect function in all thread or in none thread.\n", fcn->name); |
| 11543 | errors++; |
| 11544 | continue; |
| 11545 | } |
| 11546 | |
| 11547 | if ((fcn->function_ref[0] == -1) == (ret < 0)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 11548 | ha_alert("Lua function '%s' is referenced both ins shared Lua context (through lua-load) " |
| 11549 | "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] | 11550 | "exclusive.\n", fcn->name); |
| 11551 | errors++; |
| 11552 | } |
| 11553 | } |
| 11554 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11555 | /* Do the same with registered filters */ |
| 11556 | list_for_each_entry(reg_flt, &referenced_filters, l) { |
| 11557 | ret = 0; |
| 11558 | for (i = 1; i < global.nbthread + 1; i++) { |
| 11559 | if (reg_flt->flt_ref[i] == -1) |
| 11560 | ret--; |
| 11561 | else |
| 11562 | ret++; |
| 11563 | } |
| 11564 | if (abs(ret) != global.nbthread) { |
| 11565 | ha_alert("Lua filter '%s' is not referenced in all thread. " |
| 11566 | "Expect function in all thread or in none thread.\n", reg_flt->name); |
| 11567 | errors++; |
| 11568 | continue; |
| 11569 | } |
| 11570 | |
| 11571 | if ((reg_flt->flt_ref[0] == -1) == (ret < 0)) { |
| 11572 | ha_alert("Lua filter '%s' is referenced both ins shared Lua context (through lua-load) " |
| 11573 | "and per-thread Lua context (through lua-load-per-thread). these two context " |
| 11574 | "exclusive.\n", fcn->name); |
| 11575 | errors++; |
| 11576 | } |
| 11577 | } |
| 11578 | |
| 11579 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11580 | if (errors > 0) |
| 11581 | return 0; |
| 11582 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 11583 | /* 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] | 11584 | * -1 in order to have probably a segfault if someone use it |
| 11585 | */ |
| 11586 | hlua_state_id = -1; |
| 11587 | |
| 11588 | return 1; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11589 | } |
| 11590 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11591 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 11592 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 11593 | * 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] | 11594 | * allocation. <nsize> is the requested new size. A new allocation is |
| 11595 | * indicated by <ptr> being NULL. A free is indicated by <nsize> being |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11596 | * zero. This one verifies that the limits are respected but is optimized |
| 11597 | * for the fast case where limits are not used, hence stats are not updated. |
Willy Tarreau | a5efdff | 2021-10-22 16:00:02 +0200 | [diff] [blame] | 11598 | * |
| 11599 | * Warning: while this API ressembles glibc's realloc() a lot, glibc surpasses |
| 11600 | * POSIX by making realloc(ptr,0) an effective free(), but others do not do |
| 11601 | * that and will simply allocate zero as if it were the result of malloc(0), |
| 11602 | * so mapping this onto realloc() will lead to memory leaks on non-glibc |
| 11603 | * systems. |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11604 | */ |
| 11605 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 11606 | { |
| 11607 | struct hlua_mem_allocator *zone = ud; |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11608 | size_t limit, old, new; |
| 11609 | |
| 11610 | /* a limit of ~0 means unlimited and boot complete, so there's no need |
| 11611 | * for accounting anymore. |
| 11612 | */ |
Willy Tarreau | a5efdff | 2021-10-22 16:00:02 +0200 | [diff] [blame] | 11613 | if (likely(~zone->limit == 0)) { |
| 11614 | if (!nsize) |
| 11615 | ha_free(&ptr); |
| 11616 | else |
| 11617 | ptr = realloc(ptr, nsize); |
| 11618 | return ptr; |
| 11619 | } |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11620 | |
Willy Tarreau | d36c7fa | 2020-12-02 12:26:29 +0100 | [diff] [blame] | 11621 | if (!ptr) |
| 11622 | osize = 0; |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11623 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11624 | /* enforce strict limits across all threads */ |
| 11625 | limit = zone->limit; |
| 11626 | old = _HA_ATOMIC_LOAD(&zone->allocated); |
| 11627 | do { |
| 11628 | new = old + nsize - osize; |
| 11629 | if (unlikely(nsize && limit && new > limit)) |
| 11630 | return NULL; |
| 11631 | } while (!_HA_ATOMIC_CAS(&zone->allocated, &old, new)); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11632 | |
Willy Tarreau | a5efdff | 2021-10-22 16:00:02 +0200 | [diff] [blame] | 11633 | if (!nsize) |
| 11634 | ha_free(&ptr); |
| 11635 | else |
| 11636 | ptr = realloc(ptr, nsize); |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11637 | |
| 11638 | if (unlikely(!ptr && nsize)) // failed |
| 11639 | _HA_ATOMIC_SUB(&zone->allocated, nsize - osize); |
| 11640 | |
| 11641 | __ha_barrier_atomic_store(); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11642 | return ptr; |
| 11643 | } |
| 11644 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 11645 | /* 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] | 11646 | * We are in the initialisation process of HAProxy, this abort() is |
| 11647 | * tolerated. |
| 11648 | */ |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 11649 | lua_State *hlua_init_state(int thread_num) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 11650 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11651 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 11652 | int idx; |
| 11653 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 11654 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 11655 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 11656 | const char *error_msg; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 11657 | void **context; |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11658 | lua_State *L; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11659 | struct prepend_path *pp; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11660 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 11661 | /* Init main lua stack. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11662 | L = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 11663 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 11664 | /* Initialise Lua context to NULL */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11665 | context = lua_getextraspace(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 11666 | *context = NULL; |
| 11667 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 11668 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 11669 | * the Lua function can fail with an abort. We are in the initialisation |
| 11670 | * process of HAProxy, this abort() is tolerated. |
| 11671 | */ |
| 11672 | |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11673 | /* Call post initialisation function in safe environment. */ |
| 11674 | if (setjmp(safe_ljmp_env) != 0) { |
| 11675 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11676 | if (lua_type(L, -1) == LUA_TSTRING) |
| 11677 | error_msg = lua_tostring(L, -1); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 11678 | else |
| 11679 | error_msg = "critical error"; |
| 11680 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
| 11681 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11682 | } else { |
| 11683 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 11684 | } |
| 11685 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 11686 | /* Initialise lua. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11687 | luaL_openlibs(L); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 11688 | #define HLUA_PREPEND_PATH_TOSTRING1(x) #x |
| 11689 | #define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x) |
| 11690 | #ifdef HLUA_PREPEND_PATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11691 | hlua_prepend_path(L, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 11692 | #endif |
| 11693 | #ifdef HLUA_PREPEND_CPATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11694 | hlua_prepend_path(L, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 11695 | #endif |
| 11696 | #undef HLUA_PREPEND_PATH_TOSTRING |
| 11697 | #undef HLUA_PREPEND_PATH_TOSTRING1 |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11698 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11699 | /* Apply configured prepend path */ |
| 11700 | list_for_each_entry(pp, &prepend_path_list, l) |
| 11701 | hlua_prepend_path(L, pp->type, pp->path); |
| 11702 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11703 | /* |
| 11704 | * |
| 11705 | * Create "core" object. |
| 11706 | * |
| 11707 | */ |
| 11708 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 11709 | /* This table entry is the object "core" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11710 | lua_newtable(L); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11711 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 11712 | /* set the thread id */ |
| 11713 | hlua_class_const_int(L, "thread", thread_num); |
| 11714 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11715 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 11716 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11717 | hlua_class_const_int(L, log_levels[i], i); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11718 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11719 | /* Register special functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11720 | hlua_class_function(L, "register_init", hlua_register_init); |
| 11721 | hlua_class_function(L, "register_task", hlua_register_task); |
| 11722 | hlua_class_function(L, "register_fetches", hlua_register_fetches); |
| 11723 | hlua_class_function(L, "register_converters", hlua_register_converters); |
| 11724 | hlua_class_function(L, "register_action", hlua_register_action); |
| 11725 | hlua_class_function(L, "register_service", hlua_register_service); |
| 11726 | hlua_class_function(L, "register_cli", hlua_register_cli); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11727 | hlua_class_function(L, "register_filter", hlua_register_filter); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11728 | hlua_class_function(L, "yield", hlua_yield); |
| 11729 | hlua_class_function(L, "set_nice", hlua_set_nice); |
| 11730 | hlua_class_function(L, "sleep", hlua_sleep); |
| 11731 | hlua_class_function(L, "msleep", hlua_msleep); |
| 11732 | hlua_class_function(L, "add_acl", hlua_add_acl); |
| 11733 | hlua_class_function(L, "del_acl", hlua_del_acl); |
| 11734 | hlua_class_function(L, "set_map", hlua_set_map); |
| 11735 | hlua_class_function(L, "del_map", hlua_del_map); |
| 11736 | hlua_class_function(L, "tcp", hlua_socket_new); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 11737 | hlua_class_function(L, "httpclient", hlua_httpclient_new); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11738 | hlua_class_function(L, "log", hlua_log); |
| 11739 | hlua_class_function(L, "Debug", hlua_log_debug); |
| 11740 | hlua_class_function(L, "Info", hlua_log_info); |
| 11741 | hlua_class_function(L, "Warning", hlua_log_warning); |
| 11742 | hlua_class_function(L, "Alert", hlua_log_alert); |
| 11743 | hlua_class_function(L, "done", hlua_done); |
| 11744 | hlua_fcn_reg_core_fcn(L); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11745 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11746 | lua_setglobal(L, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 11747 | |
| 11748 | /* |
| 11749 | * |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 11750 | * Create "act" object. |
| 11751 | * |
| 11752 | */ |
| 11753 | |
| 11754 | /* This table entry is the object "act" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11755 | lua_newtable(L); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 11756 | |
| 11757 | /* push action return constants */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11758 | hlua_class_const_int(L, "CONTINUE", ACT_RET_CONT); |
| 11759 | hlua_class_const_int(L, "STOP", ACT_RET_STOP); |
| 11760 | hlua_class_const_int(L, "YIELD", ACT_RET_YIELD); |
| 11761 | hlua_class_const_int(L, "ERROR", ACT_RET_ERR); |
| 11762 | hlua_class_const_int(L, "DONE", ACT_RET_DONE); |
| 11763 | hlua_class_const_int(L, "DENY", ACT_RET_DENY); |
| 11764 | hlua_class_const_int(L, "ABORT", ACT_RET_ABRT); |
| 11765 | hlua_class_const_int(L, "INVALID", ACT_RET_INV); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 11766 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11767 | hlua_class_function(L, "wake_time", hlua_set_wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 11768 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11769 | lua_setglobal(L, "act"); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 11770 | |
| 11771 | /* |
| 11772 | * |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11773 | * Create "Filter" object. |
| 11774 | * |
| 11775 | */ |
| 11776 | |
| 11777 | /* This table entry is the object "filter" base. */ |
| 11778 | lua_newtable(L); |
| 11779 | |
| 11780 | /* push flags and constants */ |
| 11781 | hlua_class_const_int(L, "CONTINUE", 1); |
| 11782 | hlua_class_const_int(L, "WAIT", 0); |
| 11783 | hlua_class_const_int(L, "ERROR", -1); |
| 11784 | |
| 11785 | hlua_class_const_int(L, "FLT_CFG_FL_HTX", FLT_CFG_FL_HTX); |
| 11786 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 11787 | hlua_class_function(L, "wake_time", hlua_set_wake_time); |
| 11788 | hlua_class_function(L, "register_data_filter", hlua_register_data_filter); |
| 11789 | hlua_class_function(L, "unregister_data_filter", hlua_unregister_data_filter); |
| 11790 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11791 | lua_setglobal(L, "filter"); |
| 11792 | |
| 11793 | /* |
| 11794 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11795 | * Register class Map |
| 11796 | * |
| 11797 | */ |
| 11798 | |
| 11799 | /* This table entry is the object "Map" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11800 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11801 | |
| 11802 | /* register pattern types. */ |
| 11803 | for (i=0; i<PAT_MATCH_NUM; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11804 | hlua_class_const_int(L, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 11805 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 11806 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11807 | hlua_class_const_int(L, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 11808 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11809 | |
| 11810 | /* register constructor. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11811 | hlua_class_function(L, "new", hlua_map_new); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11812 | |
| 11813 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11814 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11815 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 11816 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11817 | lua_pushstring(L, "__index"); |
| 11818 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11819 | |
| 11820 | /* Register . */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11821 | hlua_class_function(L, "lookup", hlua_map_lookup); |
| 11822 | hlua_class_function(L, "slookup", hlua_map_slookup); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11823 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11824 | lua_rawset(L, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11825 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 11826 | /* Register previous table in the registry with reference and named entry. |
| 11827 | * The function hlua_register_metatable() pops the stack, so we |
| 11828 | * previously create a copy of the table. |
| 11829 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11830 | lua_pushvalue(L, -1); /* Copy the -1 entry and push it on the stack. */ |
| 11831 | class_map_ref = hlua_register_metatable(L, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11832 | |
| 11833 | /* Assign the metatable to the mai Map object. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11834 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11835 | |
| 11836 | /* Set a name to the table. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11837 | lua_setglobal(L, "Map"); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11838 | |
| 11839 | /* |
| 11840 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 11841 | * Register class Channel |
| 11842 | * |
| 11843 | */ |
| 11844 | |
| 11845 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11846 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 11847 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 11848 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11849 | lua_pushstring(L, "__index"); |
| 11850 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 11851 | |
| 11852 | /* Register . */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 11853 | hlua_class_function(L, "data", hlua_channel_get_data); |
| 11854 | hlua_class_function(L, "line", hlua_channel_get_line); |
| 11855 | hlua_class_function(L, "set", hlua_channel_set_data); |
| 11856 | hlua_class_function(L, "remove", hlua_channel_del_data); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11857 | hlua_class_function(L, "append", hlua_channel_append); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 11858 | hlua_class_function(L, "prepend", hlua_channel_prepend); |
| 11859 | hlua_class_function(L, "insert", hlua_channel_insert_data); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11860 | hlua_class_function(L, "send", hlua_channel_send); |
| 11861 | hlua_class_function(L, "forward", hlua_channel_forward); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 11862 | hlua_class_function(L, "input", hlua_channel_get_in_len); |
| 11863 | hlua_class_function(L, "output", hlua_channel_get_out_len); |
| 11864 | hlua_class_function(L, "may_recv", hlua_channel_may_recv); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11865 | hlua_class_function(L, "is_full", hlua_channel_is_full); |
| 11866 | hlua_class_function(L, "is_resp", hlua_channel_is_resp); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 11867 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 11868 | /* Deprecated API */ |
| 11869 | hlua_class_function(L, "get", hlua_channel_get); |
| 11870 | hlua_class_function(L, "dup", hlua_channel_dup); |
| 11871 | hlua_class_function(L, "getline", hlua_channel_getline); |
| 11872 | hlua_class_function(L, "get_in_len", hlua_channel_get_in_len); |
| 11873 | hlua_class_function(L, "get_out_len", hlua_channel_get_out_len); |
| 11874 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11875 | lua_rawset(L, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 11876 | |
| 11877 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11878 | class_channel_ref = hlua_register_metatable(L, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 11879 | |
| 11880 | /* |
| 11881 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 11882 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 11883 | * |
| 11884 | */ |
| 11885 | |
| 11886 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11887 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 11888 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 11889 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11890 | lua_pushstring(L, "__index"); |
| 11891 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 11892 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 11893 | /* Browse existing fetches and create the associated |
| 11894 | * object method. |
| 11895 | */ |
| 11896 | sf = NULL; |
| 11897 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 11898 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 11899 | * by an underscore. |
| 11900 | */ |
Willy Tarreau | 5ef9656 | 2021-08-26 16:48:53 +0200 | [diff] [blame] | 11901 | strlcpy2(trash.area, sf->kw, trash.size); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 11902 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 11903 | if (*p == '.' || *p == '-' || *p == '+') |
| 11904 | *p = '_'; |
| 11905 | |
| 11906 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11907 | lua_pushstring(L, trash.area); |
| 11908 | lua_pushlightuserdata(L, sf); |
| 11909 | lua_pushcclosure(L, hlua_run_sample_fetch, 1); |
| 11910 | lua_rawset(L, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 11911 | } |
| 11912 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11913 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 11914 | |
| 11915 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11916 | class_fetches_ref = hlua_register_metatable(L, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 11917 | |
| 11918 | /* |
| 11919 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 11920 | * Register class Converters |
| 11921 | * |
| 11922 | */ |
| 11923 | |
| 11924 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11925 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 11926 | |
| 11927 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11928 | lua_pushstring(L, "__index"); |
| 11929 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 11930 | |
| 11931 | /* Browse existing converters and create the associated |
| 11932 | * object method. |
| 11933 | */ |
| 11934 | sc = NULL; |
| 11935 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 11936 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 11937 | * by an underscore. |
| 11938 | */ |
Willy Tarreau | 5ef9656 | 2021-08-26 16:48:53 +0200 | [diff] [blame] | 11939 | strlcpy2(trash.area, sc->kw, trash.size); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 11940 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 11941 | if (*p == '.' || *p == '-' || *p == '+') |
| 11942 | *p = '_'; |
| 11943 | |
| 11944 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11945 | lua_pushstring(L, trash.area); |
| 11946 | lua_pushlightuserdata(L, sc); |
| 11947 | lua_pushcclosure(L, hlua_run_sample_conv, 1); |
| 11948 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 11949 | } |
| 11950 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11951 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +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_converters_ref = hlua_register_metatable(L, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 11955 | |
| 11956 | /* |
| 11957 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 11958 | * Register class HTTP |
| 11959 | * |
| 11960 | */ |
| 11961 | |
| 11962 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11963 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 11964 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 11965 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11966 | lua_pushstring(L, "__index"); |
| 11967 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 11968 | |
| 11969 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11970 | hlua_class_function(L, "req_get_headers",hlua_http_req_get_headers); |
| 11971 | hlua_class_function(L, "req_del_header", hlua_http_req_del_hdr); |
| 11972 | hlua_class_function(L, "req_rep_header", hlua_http_req_rep_hdr); |
| 11973 | hlua_class_function(L, "req_rep_value", hlua_http_req_rep_val); |
| 11974 | hlua_class_function(L, "req_add_header", hlua_http_req_add_hdr); |
| 11975 | hlua_class_function(L, "req_set_header", hlua_http_req_set_hdr); |
| 11976 | hlua_class_function(L, "req_set_method", hlua_http_req_set_meth); |
| 11977 | hlua_class_function(L, "req_set_path", hlua_http_req_set_path); |
| 11978 | hlua_class_function(L, "req_set_query", hlua_http_req_set_query); |
| 11979 | hlua_class_function(L, "req_set_uri", hlua_http_req_set_uri); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 11980 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11981 | hlua_class_function(L, "res_get_headers",hlua_http_res_get_headers); |
| 11982 | hlua_class_function(L, "res_del_header", hlua_http_res_del_hdr); |
| 11983 | hlua_class_function(L, "res_rep_header", hlua_http_res_rep_hdr); |
| 11984 | hlua_class_function(L, "res_rep_value", hlua_http_res_rep_val); |
| 11985 | hlua_class_function(L, "res_add_header", hlua_http_res_add_hdr); |
| 11986 | hlua_class_function(L, "res_set_header", hlua_http_res_set_hdr); |
| 11987 | hlua_class_function(L, "res_set_status", hlua_http_res_set_status); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 11988 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11989 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 11990 | |
| 11991 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11992 | class_http_ref = hlua_register_metatable(L, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 11993 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 11994 | /* |
| 11995 | * |
| 11996 | * Register class HTTPMessage |
| 11997 | * |
| 11998 | */ |
| 11999 | |
| 12000 | /* Create and fill the metatable. */ |
| 12001 | lua_newtable(L); |
| 12002 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 12003 | /* Create and fill the __index entry. */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 12004 | lua_pushstring(L, "__index"); |
| 12005 | lua_newtable(L); |
| 12006 | |
| 12007 | /* Register Lua functions. */ |
| 12008 | hlua_class_function(L, "is_resp", hlua_http_msg_is_resp); |
| 12009 | hlua_class_function(L, "get_stline", hlua_http_msg_get_stline); |
| 12010 | hlua_class_function(L, "get_headers", hlua_http_msg_get_headers); |
| 12011 | hlua_class_function(L, "del_header", hlua_http_msg_del_hdr); |
| 12012 | hlua_class_function(L, "rep_header", hlua_http_msg_rep_hdr); |
| 12013 | hlua_class_function(L, "rep_value", hlua_http_msg_rep_val); |
| 12014 | hlua_class_function(L, "add_header", hlua_http_msg_add_hdr); |
| 12015 | hlua_class_function(L, "set_header", hlua_http_msg_set_hdr); |
| 12016 | hlua_class_function(L, "set_method", hlua_http_msg_set_meth); |
| 12017 | hlua_class_function(L, "set_path", hlua_http_msg_set_path); |
| 12018 | hlua_class_function(L, "set_query", hlua_http_msg_set_query); |
| 12019 | hlua_class_function(L, "set_uri", hlua_http_msg_set_uri); |
| 12020 | hlua_class_function(L, "set_status", hlua_http_msg_set_status); |
| 12021 | hlua_class_function(L, "is_full", hlua_http_msg_is_full); |
| 12022 | hlua_class_function(L, "may_recv", hlua_http_msg_may_recv); |
| 12023 | hlua_class_function(L, "eom", hlua_http_msg_is_eom); |
| 12024 | hlua_class_function(L, "input", hlua_http_msg_get_in_len); |
| 12025 | hlua_class_function(L, "output", hlua_http_msg_get_out_len); |
| 12026 | |
| 12027 | hlua_class_function(L, "body", hlua_http_msg_get_body); |
| 12028 | hlua_class_function(L, "set", hlua_http_msg_set_data); |
| 12029 | hlua_class_function(L, "remove", hlua_http_msg_del_data); |
| 12030 | hlua_class_function(L, "append", hlua_http_msg_append); |
| 12031 | hlua_class_function(L, "prepend", hlua_http_msg_prepend); |
| 12032 | hlua_class_function(L, "insert", hlua_http_msg_insert_data); |
| 12033 | hlua_class_function(L, "set_eom", hlua_http_msg_set_eom); |
| 12034 | hlua_class_function(L, "unset_eom", hlua_http_msg_unset_eom); |
| 12035 | |
| 12036 | hlua_class_function(L, "send", hlua_http_msg_send); |
| 12037 | hlua_class_function(L, "forward", hlua_http_msg_forward); |
| 12038 | |
| 12039 | lua_rawset(L, -3); |
| 12040 | |
| 12041 | /* Register previous table in the registry with reference and named entry. */ |
| 12042 | class_http_msg_ref = hlua_register_metatable(L, CLASS_HTTP_MSG); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 12043 | |
| 12044 | /* |
| 12045 | * |
| 12046 | * Register class HTTPClient |
| 12047 | * |
| 12048 | */ |
| 12049 | |
| 12050 | /* Create and fill the metatable. */ |
| 12051 | lua_newtable(L); |
| 12052 | lua_pushstring(L, "__index"); |
| 12053 | lua_newtable(L); |
| 12054 | hlua_class_function(L, "get", hlua_httpclient_get); |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 12055 | hlua_class_function(L, "head", hlua_httpclient_head); |
| 12056 | hlua_class_function(L, "put", hlua_httpclient_put); |
| 12057 | hlua_class_function(L, "post", hlua_httpclient_post); |
| 12058 | hlua_class_function(L, "delete", hlua_httpclient_delete); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 12059 | lua_settable(L, -3); /* Sets the __index entry. */ |
William Lallemand | f77f1de | 2021-09-28 19:10:38 +0200 | [diff] [blame] | 12060 | /* Register the garbage collector entry. */ |
| 12061 | lua_pushstring(L, "__gc"); |
| 12062 | lua_pushcclosure(L, hlua_httpclient_gc, 0); |
| 12063 | lua_settable(L, -3); /* Push the last 2 entries in the table at index -3 */ |
| 12064 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 12065 | |
| 12066 | |
| 12067 | class_httpclient_ref = hlua_register_metatable(L, CLASS_HTTPCLIENT); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12068 | /* |
| 12069 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12070 | * Register class AppletTCP |
| 12071 | * |
| 12072 | */ |
| 12073 | |
| 12074 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12075 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12076 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12077 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12078 | lua_pushstring(L, "__index"); |
| 12079 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12080 | |
| 12081 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12082 | hlua_class_function(L, "getline", hlua_applet_tcp_getline); |
| 12083 | hlua_class_function(L, "receive", hlua_applet_tcp_recv); |
| 12084 | hlua_class_function(L, "send", hlua_applet_tcp_send); |
| 12085 | hlua_class_function(L, "set_priv", hlua_applet_tcp_set_priv); |
| 12086 | hlua_class_function(L, "get_priv", hlua_applet_tcp_get_priv); |
| 12087 | hlua_class_function(L, "set_var", hlua_applet_tcp_set_var); |
| 12088 | hlua_class_function(L, "unset_var", hlua_applet_tcp_unset_var); |
| 12089 | hlua_class_function(L, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12090 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12091 | lua_settable(L, -3); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12092 | |
| 12093 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12094 | class_applet_tcp_ref = hlua_register_metatable(L, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12095 | |
| 12096 | /* |
| 12097 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12098 | * Register class AppletHTTP |
| 12099 | * |
| 12100 | */ |
| 12101 | |
| 12102 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12103 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12104 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12105 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12106 | lua_pushstring(L, "__index"); |
| 12107 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12108 | |
| 12109 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12110 | hlua_class_function(L, "set_priv", hlua_applet_http_set_priv); |
| 12111 | hlua_class_function(L, "get_priv", hlua_applet_http_get_priv); |
| 12112 | hlua_class_function(L, "set_var", hlua_applet_http_set_var); |
| 12113 | hlua_class_function(L, "unset_var", hlua_applet_http_unset_var); |
| 12114 | hlua_class_function(L, "get_var", hlua_applet_http_get_var); |
| 12115 | hlua_class_function(L, "getline", hlua_applet_http_getline); |
| 12116 | hlua_class_function(L, "receive", hlua_applet_http_recv); |
| 12117 | hlua_class_function(L, "send", hlua_applet_http_send); |
| 12118 | hlua_class_function(L, "add_header", hlua_applet_http_addheader); |
| 12119 | hlua_class_function(L, "set_status", hlua_applet_http_status); |
| 12120 | hlua_class_function(L, "start_response", hlua_applet_http_start_response); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12121 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12122 | lua_settable(L, -3); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12123 | |
| 12124 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12125 | class_applet_http_ref = hlua_register_metatable(L, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12126 | |
| 12127 | /* |
| 12128 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12129 | * Register class TXN |
| 12130 | * |
| 12131 | */ |
| 12132 | |
| 12133 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12134 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12135 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12136 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12137 | lua_pushstring(L, "__index"); |
| 12138 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12139 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 12140 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12141 | hlua_class_function(L, "set_priv", hlua_set_priv); |
| 12142 | hlua_class_function(L, "get_priv", hlua_get_priv); |
| 12143 | hlua_class_function(L, "set_var", hlua_set_var); |
| 12144 | hlua_class_function(L, "unset_var", hlua_unset_var); |
| 12145 | hlua_class_function(L, "get_var", hlua_get_var); |
| 12146 | hlua_class_function(L, "done", hlua_txn_done); |
| 12147 | hlua_class_function(L, "reply", hlua_txn_reply_new); |
| 12148 | hlua_class_function(L, "set_loglevel", hlua_txn_set_loglevel); |
| 12149 | hlua_class_function(L, "set_tos", hlua_txn_set_tos); |
| 12150 | hlua_class_function(L, "set_mark", hlua_txn_set_mark); |
| 12151 | hlua_class_function(L, "set_priority_class", hlua_txn_set_priority_class); |
| 12152 | hlua_class_function(L, "set_priority_offset", hlua_txn_set_priority_offset); |
| 12153 | hlua_class_function(L, "deflog", hlua_txn_deflog); |
| 12154 | hlua_class_function(L, "log", hlua_txn_log); |
| 12155 | hlua_class_function(L, "Debug", hlua_txn_log_debug); |
| 12156 | hlua_class_function(L, "Info", hlua_txn_log_info); |
| 12157 | hlua_class_function(L, "Warning", hlua_txn_log_warning); |
| 12158 | hlua_class_function(L, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 12159 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12160 | lua_rawset(L, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 12161 | |
| 12162 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12163 | class_txn_ref = hlua_register_metatable(L, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12164 | |
| 12165 | /* |
| 12166 | * |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 12167 | * Register class reply |
| 12168 | * |
| 12169 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12170 | lua_newtable(L); |
| 12171 | lua_pushstring(L, "__index"); |
| 12172 | lua_newtable(L); |
| 12173 | hlua_class_function(L, "set_status", hlua_txn_reply_set_status); |
| 12174 | hlua_class_function(L, "add_header", hlua_txn_reply_add_header); |
| 12175 | hlua_class_function(L, "del_header", hlua_txn_reply_del_header); |
| 12176 | hlua_class_function(L, "set_body", hlua_txn_reply_set_body); |
| 12177 | lua_settable(L, -3); /* Sets the __index entry. */ |
| 12178 | class_txn_reply_ref = luaL_ref(L, LUA_REGISTRYINDEX); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 12179 | |
| 12180 | |
| 12181 | /* |
| 12182 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12183 | * Register class Socket |
| 12184 | * |
| 12185 | */ |
| 12186 | |
| 12187 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12188 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12189 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12190 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12191 | lua_pushstring(L, "__index"); |
| 12192 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12193 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 12194 | #ifdef USE_OPENSSL |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12195 | hlua_class_function(L, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 12196 | #endif |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12197 | hlua_class_function(L, "connect", hlua_socket_connect); |
| 12198 | hlua_class_function(L, "send", hlua_socket_send); |
| 12199 | hlua_class_function(L, "receive", hlua_socket_receive); |
| 12200 | hlua_class_function(L, "close", hlua_socket_close); |
| 12201 | hlua_class_function(L, "getpeername", hlua_socket_getpeername); |
| 12202 | hlua_class_function(L, "getsockname", hlua_socket_getsockname); |
| 12203 | hlua_class_function(L, "setoption", hlua_socket_setoption); |
| 12204 | hlua_class_function(L, "settimeout", hlua_socket_settimeout); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12205 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12206 | 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] | 12207 | |
| 12208 | /* Register the garbage collector entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12209 | lua_pushstring(L, "__gc"); |
| 12210 | lua_pushcclosure(L, hlua_socket_gc, 0); |
| 12211 | 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] | 12212 | |
| 12213 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12214 | class_socket_ref = hlua_register_metatable(L, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12215 | |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 12216 | lua_atpanic(L, hlua_panic_safe); |
| 12217 | |
| 12218 | return L; |
| 12219 | } |
| 12220 | |
| 12221 | void hlua_init(void) { |
| 12222 | int i; |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 12223 | char *errmsg; |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 12224 | #ifdef USE_OPENSSL |
| 12225 | struct srv_kw *kw; |
| 12226 | int tmp_error; |
| 12227 | char *error; |
| 12228 | char *args[] = { /* SSL client configuration. */ |
| 12229 | "ssl", |
| 12230 | "verify", |
| 12231 | "none", |
| 12232 | NULL |
| 12233 | }; |
| 12234 | #endif |
| 12235 | |
| 12236 | /* Init post init function list head */ |
| 12237 | for (i = 0; i < MAX_THREADS + 1; i++) |
| 12238 | LIST_INIT(&hlua_init_functions[i]); |
| 12239 | |
| 12240 | /* Init state for common/shared lua parts */ |
| 12241 | hlua_state_id = 0; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 12242 | ha_set_thread(NULL); |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 12243 | hlua_states[0] = hlua_init_state(0); |
| 12244 | |
| 12245 | /* Init state 1 for thread 0. We have at least one thread. */ |
| 12246 | hlua_state_id = 1; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 12247 | ha_set_thread(NULL); |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 12248 | hlua_states[1] = hlua_init_state(1); |
| 12249 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12250 | /* Proxy and server configuration initialisation. */ |
William Lallemand | 6bb77b9 | 2021-07-28 15:48:16 +0200 | [diff] [blame] | 12251 | 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] | 12252 | if (!socket_proxy) { |
| 12253 | fprintf(stderr, "Lua init: %s\n", errmsg); |
| 12254 | exit(1); |
| 12255 | } |
| 12256 | proxy_preset_defaults(socket_proxy); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12257 | |
| 12258 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12259 | socket_tcp = new_server(socket_proxy); |
| 12260 | if (!socket_tcp) { |
| 12261 | fprintf(stderr, "Lua init: failed to allocate tcp server socket\n"); |
| 12262 | exit(1); |
| 12263 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12264 | |
| 12265 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12266 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12267 | socket_ssl = new_server(socket_proxy); |
| 12268 | if (!socket_ssl) { |
| 12269 | fprintf(stderr, "Lua init: failed to allocate ssl server socket\n"); |
| 12270 | exit(1); |
| 12271 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12272 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12273 | socket_ssl->use_ssl = 1; |
| 12274 | socket_ssl->xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12275 | |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 12276 | for (i = 0; args[i] != NULL; i++) { |
| 12277 | 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] | 12278 | /* |
| 12279 | * |
| 12280 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 12281 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12282 | * features like client certificates and ssl_verify. |
| 12283 | * |
| 12284 | */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12285 | tmp_error = kw->parse(args, &i, socket_proxy, socket_ssl, &error); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12286 | if (tmp_error != 0) { |
| 12287 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 12288 | abort(); /* This must be never arrives because the command line |
| 12289 | not editable by the user. */ |
| 12290 | } |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 12291 | i += kw->skip; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12292 | } |
| 12293 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12294 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 12295 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 12296 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 12297 | |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 12298 | static void hlua_deinit() |
| 12299 | { |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 12300 | int thr; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12301 | struct hlua_reg_filter *reg_flt, *reg_flt_bck; |
| 12302 | |
| 12303 | list_for_each_entry_safe(reg_flt, reg_flt_bck, &referenced_filters, l) |
| 12304 | release_hlua_reg_filter(reg_flt); |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 12305 | |
| 12306 | for (thr = 0; thr < MAX_THREADS+1; thr++) { |
| 12307 | if (hlua_states[thr]) |
| 12308 | lua_close(hlua_states[thr]); |
| 12309 | } |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 12310 | |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 12311 | srv_drop(socket_tcp); |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 12312 | |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 12313 | #ifdef USE_OPENSSL |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 12314 | srv_drop(socket_ssl); |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 12315 | #endif |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 12316 | |
| 12317 | free_proxy(socket_proxy); |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 12318 | } |
| 12319 | |
| 12320 | REGISTER_POST_DEINIT(hlua_deinit); |
| 12321 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 12322 | static void hlua_register_build_options(void) |
| 12323 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 12324 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 12325 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 12326 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 12327 | hap_register_build_opts(ptr, 1); |
| 12328 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 12329 | |
| 12330 | INITCALL0(STG_REGISTER, hlua_register_build_options); |