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 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1908 | struct stream_interface *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); |
| 2104 | si = appctx->owner; |
| 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); |
| 2343 | si = appctx->owner; |
| 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") |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2503 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 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; |
| 2556 | struct stream *s; |
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); |
| 2576 | si = appctx->owner; |
| 2577 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2578 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2579 | if (!s->target_addr) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2580 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2581 | lua_pushnil(L); |
| 2582 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2583 | } |
| 2584 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2585 | ret = MAY_LJMP(hlua_socket_info(L, s->target_addr)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2586 | xref_unlock(&socket->xref, peer); |
| 2587 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2588 | } |
| 2589 | |
| 2590 | /* Returns information about my connection side. */ |
| 2591 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2592 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2593 | struct hlua_socket *socket; |
| 2594 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2595 | struct appctx *appctx; |
| 2596 | struct xref *peer; |
| 2597 | struct stream_interface *si; |
| 2598 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2599 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2600 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2601 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2602 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2603 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2604 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2605 | /* Check if we run on the same thread than the xreator thread. |
| 2606 | * We cannot access to the socket if the thread is different. |
| 2607 | */ |
| 2608 | if (socket->tid != tid) |
| 2609 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2610 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2611 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2612 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2613 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2614 | lua_pushnil(L); |
| 2615 | return 1; |
| 2616 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2617 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2618 | si = appctx->owner; |
| 2619 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2620 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2621 | conn = cs_conn(objt_cs(s->si[1].end)); |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2622 | if (!conn || !conn_get_src(conn)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2623 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2624 | lua_pushnil(L); |
| 2625 | return 1; |
| 2626 | } |
| 2627 | |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2628 | ret = hlua_socket_info(L, conn->src); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2629 | xref_unlock(&socket->xref, peer); |
| 2630 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2631 | } |
| 2632 | |
| 2633 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2634 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2635 | .obj_type = OBJ_TYPE_APPLET, |
| 2636 | .name = "<LUA_TCP>", |
| 2637 | .fct = hlua_socket_handler, |
| 2638 | .release = hlua_socket_release, |
| 2639 | }; |
| 2640 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2641 | __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] | 2642 | { |
| 2643 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2644 | struct hlua *hlua; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2645 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2646 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2647 | struct stream_interface *si; |
| 2648 | struct stream *s; |
| 2649 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2650 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2651 | hlua = hlua_gethlua(L); |
| 2652 | if (!hlua) |
| 2653 | return 0; |
| 2654 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2655 | /* Check if we run on the same thread than the xreator thread. |
| 2656 | * We cannot access to the socket if the thread is different. |
| 2657 | */ |
| 2658 | if (socket->tid != tid) |
| 2659 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2660 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2661 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2662 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2663 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2664 | lua_pushnil(L); |
| 2665 | lua_pushstring(L, "Can't connect"); |
| 2666 | return 2; |
| 2667 | } |
| 2668 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2669 | si = appctx->owner; |
| 2670 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2671 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2672 | /* Check if we run on the same thread than the xreator thread. |
| 2673 | * We cannot access to the socket if the thread is different. |
| 2674 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2675 | if (socket->tid != tid) { |
| 2676 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2677 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2678 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2679 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2680 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2681 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2682 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2683 | lua_pushnil(L); |
| 2684 | lua_pushstring(L, "Can't connect"); |
| 2685 | return 2; |
| 2686 | } |
| 2687 | |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2688 | appctx = __objt_appctx(s->si[0].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2689 | |
| 2690 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2691 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2692 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2693 | lua_pushinteger(L, 1); |
| 2694 | return 1; |
| 2695 | } |
| 2696 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2697 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2698 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2699 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2700 | } |
| 2701 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2702 | 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] | 2703 | return 0; |
| 2704 | } |
| 2705 | |
| 2706 | /* This function fail or initite the connection. */ |
| 2707 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2708 | { |
| 2709 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2710 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2711 | const char *ip; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2712 | struct hlua *hlua; |
| 2713 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2714 | int low, high; |
| 2715 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2716 | struct xref *peer; |
| 2717 | struct stream_interface *si; |
| 2718 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2719 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2720 | if (lua_gettop(L) < 2) |
| 2721 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2722 | |
| 2723 | /* Get args. */ |
| 2724 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2725 | |
| 2726 | /* Check if we run on the same thread than the xreator thread. |
| 2727 | * We cannot access to the socket if the thread is different. |
| 2728 | */ |
| 2729 | if (socket->tid != tid) |
| 2730 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2731 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2732 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2733 | if (lua_gettop(L) >= 3) { |
| 2734 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2735 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2736 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2737 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2738 | * that are not enclosed within square brackets. |
| 2739 | */ |
| 2740 | if (port > 0) { |
| 2741 | luaL_buffinit(L, &b); |
| 2742 | luaL_addstring(&b, ip); |
| 2743 | luaL_addchar(&b, ':'); |
| 2744 | luaL_pushresult(&b); |
| 2745 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2746 | } |
| 2747 | } |
| 2748 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2749 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2750 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2751 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2752 | lua_pushnil(L); |
| 2753 | return 1; |
| 2754 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2755 | |
| 2756 | /* Parse ip address. */ |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 2757 | 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] | 2758 | if (!addr) { |
| 2759 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2760 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2761 | } |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2762 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2763 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2764 | if (low == 0) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2765 | if (addr->ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2766 | if (port == -1) { |
| 2767 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2768 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2769 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2770 | ((struct sockaddr_in *)addr)->sin_port = htons(port); |
| 2771 | } else if (addr->ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2772 | if (port == -1) { |
| 2773 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2774 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2775 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2776 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(port); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2777 | } |
| 2778 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2779 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2780 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2781 | si = appctx->owner; |
| 2782 | s = si_strm(si); |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2783 | |
Willy Tarreau | 9b7587a | 2020-10-15 07:32:10 +0200 | [diff] [blame] | 2784 | if (!sockaddr_alloc(&s->target_addr, addr, sizeof(*addr))) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2785 | xref_unlock(&socket->xref, peer); |
| 2786 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
| 2787 | } |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2788 | s->flags |= SF_ADDR_SET; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2789 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2790 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2791 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2792 | if (!hlua) |
| 2793 | return 0; |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2794 | |
| 2795 | /* inform the stream that we want to be notified whenever the |
| 2796 | * connection completes. |
| 2797 | */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 2798 | si_cant_get(&s->si[0]); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2799 | si_rx_endp_more(&s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2800 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2801 | |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2802 | hlua->gc_count++; |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2803 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2804 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2805 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2806 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2807 | } |
| 2808 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2809 | |
| 2810 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2811 | /* Return yield waiting for connection. */ |
| 2812 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2813 | 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] | 2814 | |
| 2815 | return 0; |
| 2816 | } |
| 2817 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2818 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2819 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2820 | { |
| 2821 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2822 | struct xref *peer; |
| 2823 | struct appctx *appctx; |
| 2824 | struct stream_interface *si; |
| 2825 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2826 | |
| 2827 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2828 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2829 | |
| 2830 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2831 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2832 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2833 | lua_pushnil(L); |
| 2834 | return 1; |
| 2835 | } |
| 2836 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2837 | si = appctx->owner; |
| 2838 | s = si_strm(si); |
| 2839 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 2840 | s->target = &socket_ssl->obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2841 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2842 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2843 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2844 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2845 | |
| 2846 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2847 | { |
| 2848 | return 0; |
| 2849 | } |
| 2850 | |
| 2851 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2852 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2853 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2854 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2855 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2856 | struct xref *peer; |
| 2857 | struct appctx *appctx; |
| 2858 | struct stream_interface *si; |
| 2859 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2860 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2861 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2862 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2863 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2864 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2865 | /* convert the timeout to millis */ |
| 2866 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2867 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2868 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2869 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2870 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2871 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2872 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2873 | 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] | 2874 | |
| 2875 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2876 | if (tmout == 0) |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2877 | tmout++; /* very small timeouts are adjusted to a minimum of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2878 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2879 | /* Check if we run on the same thread than the xreator thread. |
| 2880 | * We cannot access to the socket if the thread is different. |
| 2881 | */ |
| 2882 | if (socket->tid != tid) |
| 2883 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2884 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2885 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2886 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2887 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2888 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2889 | WILL_LJMP(lua_error(L)); |
| 2890 | return 0; |
| 2891 | } |
| 2892 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2893 | si = appctx->owner; |
| 2894 | s = si_strm(si); |
| 2895 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2896 | s->sess->fe->timeout.connect = tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2897 | s->req.rto = tmout; |
| 2898 | s->req.wto = tmout; |
| 2899 | s->res.rto = tmout; |
| 2900 | s->res.wto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2901 | s->req.rex = tick_add_ifset(now_ms, tmout); |
| 2902 | s->req.wex = tick_add_ifset(now_ms, tmout); |
| 2903 | s->res.rex = tick_add_ifset(now_ms, tmout); |
| 2904 | s->res.wex = tick_add_ifset(now_ms, tmout); |
| 2905 | |
| 2906 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 2907 | task_queue(s->task); |
| 2908 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2909 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2910 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 2911 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 2912 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2913 | } |
| 2914 | |
| 2915 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2916 | { |
| 2917 | struct hlua_socket *socket; |
| 2918 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2919 | struct session *sess; |
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"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2961 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2962 | } |
| 2963 | |
Christopher Faulet | 26256f8 | 2020-09-14 11:40:13 +0200 | [diff] [blame] | 2964 | strm = stream_new(sess, &appctx->obj_type, &BUF_NULL); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2965 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2966 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2967 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2968 | } |
| 2969 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2970 | /* Initialise cross reference between stream and Lua socket object. */ |
| 2971 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2972 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2973 | /* Configure "right" stream interface. this "si" is used to connect |
| 2974 | * and retrieve data from the server. The connection is initialized |
| 2975 | * with the "struct server". |
| 2976 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2977 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2978 | |
| 2979 | /* Force destination server. */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2980 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED; |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 2981 | strm->target = &socket_tcp->obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2982 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2983 | return 1; |
| 2984 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2985 | out_fail_stream: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 2986 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2987 | out_fail_sess: |
| 2988 | appctx_free(appctx); |
| 2989 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2990 | WILL_LJMP(lua_error(L)); |
| 2991 | return 0; |
| 2992 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2993 | |
| 2994 | /* |
| 2995 | * |
| 2996 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2997 | * Class Channel |
| 2998 | * |
| 2999 | * |
| 3000 | */ |
| 3001 | |
| 3002 | /* Returns the struct hlua_channel join to the class channel in the |
| 3003 | * stack entry "ud" or throws an argument error. |
| 3004 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3005 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3006 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3007 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3008 | } |
| 3009 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3010 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 3011 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3012 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 3013 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3014 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3015 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3016 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3017 | return 0; |
| 3018 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3019 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3020 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3021 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3022 | |
| 3023 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3024 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 3025 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3026 | return 1; |
| 3027 | } |
| 3028 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 3029 | /* Helper function returning a filter attached to a channel at the position <ud> |
| 3030 | * 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] | 3031 | * filter is attached, NULL is returned and <offset> and <len> are not |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 3032 | * initialized. |
| 3033 | */ |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3034 | 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] | 3035 | { |
| 3036 | struct filter *filter = NULL; |
| 3037 | |
| 3038 | if (lua_getfield(L, ud, "__filter") == LUA_TLIGHTUSERDATA) { |
| 3039 | struct hlua_flt_ctx *flt_ctx; |
| 3040 | |
| 3041 | filter = lua_touserdata (L, -1); |
| 3042 | flt_ctx = filter->ctx; |
| 3043 | if (hlua_filter_from_payload(filter)) { |
| 3044 | *offset = flt_ctx->cur_off[CHN_IDX(chn)]; |
| 3045 | *len = flt_ctx->cur_len[CHN_IDX(chn)]; |
| 3046 | } |
| 3047 | } |
| 3048 | |
| 3049 | lua_pop(L, 1); |
| 3050 | return filter; |
| 3051 | } |
| 3052 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3053 | /* Copies <len> bytes of data present in the channel's buffer, starting at the |
| 3054 | * 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] | 3055 | * responsibility to ensure <len> and <offset> are valid. It always return the |
| 3056 | * length of the built string. <len> may be 0, in this case, an empty string is |
| 3057 | * created and 0 is returned. |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3058 | */ |
| 3059 | 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] | 3060 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3061 | size_t block1, block2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3062 | luaL_Buffer b; |
| 3063 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3064 | block1 = len; |
| 3065 | if (block1 > b_contig_data(&chn->buf, b_peek_ofs(&chn->buf, offset))) |
| 3066 | block1 = b_contig_data(&chn->buf, b_peek_ofs(&chn->buf, offset)); |
| 3067 | block2 = len - block1; |
| 3068 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3069 | luaL_buffinit(L, &b); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3070 | luaL_addlstring(&b, b_peek(&chn->buf, offset), block1); |
| 3071 | if (block2) |
| 3072 | luaL_addlstring(&b, b_orig(&chn->buf), block2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3073 | luaL_pushresult(&b); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3074 | return len; |
| 3075 | } |
| 3076 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3077 | /* Inserts the string <str> to the channel's buffer at the offset <offset>. This |
| 3078 | * function returns -1 if data cannot be copied. Otherwise, it returns the |
| 3079 | * number of bytes copied. |
| 3080 | */ |
| 3081 | static int _hlua_channel_insert(struct channel *chn, lua_State *L, struct ist str, size_t offset) |
| 3082 | { |
| 3083 | int ret = 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3084 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3085 | /* Nothing to do, just return */ |
| 3086 | if (unlikely(istlen(str) == 0)) |
| 3087 | goto end; |
| 3088 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3089 | if (istlen(str) > c_room(chn)) { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3090 | ret = -1; |
| 3091 | goto end; |
| 3092 | } |
| 3093 | ret = b_insert_blk(&chn->buf, offset, istptr(str), istlen(str)); |
| 3094 | |
| 3095 | end: |
| 3096 | return ret; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3097 | } |
| 3098 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3099 | /* Removes <len> bytes of data at the absolute position <offset>. |
| 3100 | */ |
| 3101 | static void _hlua_channel_delete(struct channel *chn, size_t offset, size_t len) |
| 3102 | { |
| 3103 | size_t end = offset + len; |
| 3104 | |
| 3105 | if (b_peek(&chn->buf, end) != b_tail(&chn->buf)) |
| 3106 | b_move(&chn->buf, b_peek_ofs(&chn->buf, end), |
| 3107 | b_data(&chn->buf) - end, -len); |
| 3108 | b_sub(&chn->buf, len); |
| 3109 | } |
| 3110 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3111 | /* Copies input data in the channel's buffer. It is possible to set a specific |
| 3112 | * offset (0 by default) and a length (all remaining input data starting for the |
| 3113 | * offset by default). If there is not enough input data and more data can be |
| 3114 | * received, this function yields. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3115 | * |
| 3116 | * From an action, All input data are considered. For a filter, the offset and |
| 3117 | * 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] | 3118 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3119 | __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] | 3120 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3121 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3122 | struct filter *filter; |
| 3123 | size_t input, output; |
| 3124 | int offset, len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3125 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3126 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3127 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3128 | output = co_data(chn); |
| 3129 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3130 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3131 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3132 | if (filter && !hlua_filter_from_payload(filter)) |
| 3133 | WILL_LJMP(lua_error(L)); |
| 3134 | |
| 3135 | offset = output; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3136 | if (lua_gettop(L) > 1) { |
| 3137 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3138 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3139 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3140 | offset += output; |
| 3141 | if (offset < output || offset > input + output) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3142 | lua_pushfstring(L, "offset out of range."); |
| 3143 | WILL_LJMP(lua_error(L)); |
| 3144 | } |
| 3145 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3146 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3147 | if (lua_gettop(L) == 3) { |
| 3148 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3149 | if (!len) |
| 3150 | goto dup; |
| 3151 | if (len == -1) |
| 3152 | len = global.tune.bufsize; |
| 3153 | if (len < 0) { |
| 3154 | lua_pushfstring(L, "length out of range."); |
| 3155 | WILL_LJMP(lua_error(L)); |
| 3156 | } |
| 3157 | } |
| 3158 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3159 | if (offset + len > output + input) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3160 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3161 | /* Yield waiting for more data, as requested */ |
| 3162 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_data_yield, TICK_ETERNITY, 0)); |
| 3163 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3164 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3165 | } |
| 3166 | |
| 3167 | dup: |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3168 | _hlua_channel_dup(chn, L, offset, len); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3169 | return 1; |
| 3170 | } |
| 3171 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3172 | /* Copies the first line (including the trailing LF) of input data in the |
| 3173 | * channel's buffer. It is possible to set a specific offset (0 by default) and |
| 3174 | * a length (all remaining input data starting for the offset by default). If |
| 3175 | * there is not enough input data and more data can be received, the function |
| 3176 | * yields. If a length is explicitly specified, no more data are |
| 3177 | * copied. Otherwise, if no LF is found and more data can be received, this |
| 3178 | * function yields. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3179 | * |
| 3180 | * From an action, All input data are considered. For a filter, the offset and |
| 3181 | * 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] | 3182 | */ |
| 3183 | __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] | 3184 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3185 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3186 | struct filter *filter; |
| 3187 | size_t l, input, output; |
| 3188 | int offset, len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3189 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3190 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3191 | output = co_data(chn); |
| 3192 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3193 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3194 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3195 | if (filter && !hlua_filter_from_payload(filter)) |
| 3196 | WILL_LJMP(lua_error(L)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3197 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3198 | offset = output; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3199 | if (lua_gettop(L) > 1) { |
| 3200 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3201 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3202 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3203 | offset += output; |
| 3204 | if (offset < output || offset > input + output) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3205 | lua_pushfstring(L, "offset out of range."); |
| 3206 | WILL_LJMP(lua_error(L)); |
| 3207 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3208 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3209 | |
| 3210 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3211 | if (lua_gettop(L) == 3) { |
| 3212 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3213 | if (!len) |
| 3214 | goto dup; |
| 3215 | if (len == -1) |
| 3216 | len = global.tune.bufsize; |
| 3217 | if (len < 0) { |
| 3218 | lua_pushfstring(L, "length out of range."); |
| 3219 | WILL_LJMP(lua_error(L)); |
| 3220 | } |
| 3221 | } |
| 3222 | |
| 3223 | for (l = 0; l < len; l++) { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3224 | if (l + offset >= output + input) |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3225 | break; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3226 | if (*(b_peek(&chn->buf, offset + l)) == '\n') { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3227 | len = l+1; |
| 3228 | goto dup; |
| 3229 | } |
| 3230 | } |
| 3231 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3232 | if (offset + len > output + input) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3233 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3234 | /* Yield waiting for more data */ |
| 3235 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_line_yield, TICK_ETERNITY, 0)); |
| 3236 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3237 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3238 | } |
| 3239 | |
| 3240 | dup: |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3241 | _hlua_channel_dup(chn, L, offset, len); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3242 | return 1; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3243 | } |
| 3244 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3245 | /* [ DEPRECATED ] |
| 3246 | * |
| 3247 | * Duplicate all input data foud in the channel's buffer. The data are not |
| 3248 | * removed from the buffer. This function relies on _hlua_channel_dup(). |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3249 | * |
| 3250 | * From an action, All input data are considered. For a filter, the offset and |
| 3251 | * 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] | 3252 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3253 | __LJMP static int hlua_channel_dup(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3254 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3255 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3256 | struct filter *filter; |
| 3257 | size_t offset, len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3258 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3259 | MAY_LJMP(check_args(L, 1, "dup")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3260 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3261 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3262 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3263 | WILL_LJMP(lua_error(L)); |
| 3264 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3265 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3266 | offset = co_data(chn); |
| 3267 | len = ci_data(chn); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3268 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3269 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3270 | if (filter && !hlua_filter_from_payload(filter)) |
| 3271 | WILL_LJMP(lua_error(L)); |
| 3272 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3273 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3274 | lua_pushnil(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3275 | return 1; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3276 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3277 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3278 | _hlua_channel_dup(chn, L, offset, len); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3279 | return 1; |
| 3280 | } |
| 3281 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3282 | /* [ DEPRECATED ] |
| 3283 | * |
| 3284 | * Get all input data foud in the channel's buffer. The data are removed from |
| 3285 | * the buffer after the copy. This function relies on _hlua_channel_dup() and |
| 3286 | * _hlua_channel_delete(). |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3287 | * |
| 3288 | * From an action, All input data are considered. For a filter, the offset and |
| 3289 | * 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] | 3290 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3291 | __LJMP static int hlua_channel_get(lua_State *L) |
| 3292 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3293 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3294 | struct filter *filter; |
| 3295 | size_t offset, len; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3296 | int ret; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3297 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3298 | MAY_LJMP(check_args(L, 1, "get")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3299 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3300 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3301 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3302 | WILL_LJMP(lua_error(L)); |
| 3303 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3304 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3305 | offset = co_data(chn); |
| 3306 | len = ci_data(chn); |
| 3307 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3308 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3309 | if (filter && !hlua_filter_from_payload(filter)) |
| 3310 | WILL_LJMP(lua_error(L)); |
| 3311 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3312 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3313 | lua_pushnil(L); |
| 3314 | return 1; |
| 3315 | } |
| 3316 | |
| 3317 | ret = _hlua_channel_dup(chn, L, offset, len); |
| 3318 | _hlua_channel_delete(chn, offset, ret); |
| 3319 | return 1; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3320 | } |
| 3321 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3322 | /* This functions consumes and returns one line. If the channel is closed, |
| 3323 | * 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] | 3324 | * 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] | 3325 | * value. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3326 | * |
| 3327 | * From an action, All input data are considered. For a filter, the offset and |
| 3328 | * 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] | 3329 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3330 | __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] | 3331 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3332 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3333 | struct filter *filter; |
| 3334 | size_t l, offset, len; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3335 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3336 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3337 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3338 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3339 | offset = co_data(chn); |
| 3340 | len = ci_data(chn); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3341 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3342 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3343 | if (filter && !hlua_filter_from_payload(filter)) |
| 3344 | WILL_LJMP(lua_error(L)); |
| 3345 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3346 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3347 | lua_pushnil(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3348 | return 1; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3349 | } |
| 3350 | |
| 3351 | for (l = 0; l < len; l++) { |
| 3352 | if (*(b_peek(&chn->buf, offset+l)) == '\n') { |
| 3353 | len = l+1; |
| 3354 | goto dup; |
| 3355 | } |
| 3356 | } |
| 3357 | |
| 3358 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3359 | /* Yield waiting for more data */ |
| 3360 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0)); |
| 3361 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3362 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3363 | dup: |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3364 | ret = _hlua_channel_dup(chn, L, offset, len); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3365 | _hlua_channel_delete(chn, offset, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3366 | return 1; |
| 3367 | } |
| 3368 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3369 | /* [ DEPRECATED ] |
| 3370 | * |
| 3371 | * Check arguments for the function "hlua_channel_getline_yield". |
| 3372 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3373 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 3374 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3375 | struct channel *chn; |
| 3376 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3377 | MAY_LJMP(check_args(L, 1, "getline")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3378 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3379 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3380 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3381 | WILL_LJMP(lua_error(L)); |
| 3382 | } |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3383 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 3384 | } |
| 3385 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3386 | /* Retrieves a given amount of input data at the given offset. By default all |
| 3387 | * available input data are returned. The offset may be negactive to start from |
| 3388 | * the end of input data. The length may be -1 to set it to the maximum buffer |
| 3389 | * size. |
| 3390 | */ |
| 3391 | __LJMP static int hlua_channel_get_data(lua_State *L) |
| 3392 | { |
| 3393 | struct channel *chn; |
| 3394 | |
| 3395 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3396 | WILL_LJMP(luaL_error(L, "'data' expects at most 2 arguments")); |
| 3397 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3398 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3399 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3400 | WILL_LJMP(lua_error(L)); |
| 3401 | } |
| 3402 | return MAY_LJMP(hlua_channel_get_data_yield(L, 0, 0)); |
| 3403 | } |
| 3404 | |
| 3405 | /* Retrieves a given amount of input data at the given offset. By default all |
| 3406 | * available input data are returned. The offset may be negactive to start from |
| 3407 | * the end of input data. The length may be -1 to set it to the maximum buffer |
| 3408 | * size. |
| 3409 | */ |
| 3410 | __LJMP static int hlua_channel_get_line(lua_State *L) |
| 3411 | { |
| 3412 | struct channel *chn; |
| 3413 | |
| 3414 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3415 | WILL_LJMP(luaL_error(L, "'line' expects at most 2 arguments")); |
| 3416 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3417 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3418 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3419 | WILL_LJMP(lua_error(L)); |
| 3420 | } |
| 3421 | return MAY_LJMP(hlua_channel_get_line_yield(L, 0, 0)); |
| 3422 | } |
| 3423 | |
| 3424 | /* Appends a string into the input side of channel. It returns the length of the |
| 3425 | * written string, or -1 if the channel is closed or if the buffer size is too |
| 3426 | * little for the data. 0 may be returned if nothing is copied. This function |
| 3427 | * does not yield. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3428 | * |
| 3429 | * For a filter, the context is updated on success. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3430 | */ |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3431 | __LJMP static int hlua_channel_append(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3432 | { |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3433 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3434 | struct filter *filter; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3435 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3436 | size_t sz, offset, len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3437 | int ret; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3438 | |
| 3439 | MAY_LJMP(check_args(L, 2, "append")); |
| 3440 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3441 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3442 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3443 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3444 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3445 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3446 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3447 | offset = co_data(chn); |
| 3448 | len = ci_data(chn); |
| 3449 | |
| 3450 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3451 | if (filter && !hlua_filter_from_payload(filter)) |
| 3452 | WILL_LJMP(lua_error(L)); |
| 3453 | |
| 3454 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3455 | if (ret > 0 && filter) { |
| 3456 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3457 | |
| 3458 | flt_update_offsets(filter, chn, ret); |
| 3459 | flt_ctx->cur_len[CHN_IDX(chn)] += ret; |
| 3460 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3461 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3462 | return 1; |
| 3463 | } |
| 3464 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3465 | /* Prepends a string into the input side of channel. It returns the length of the |
| 3466 | * written string, or -1 if the channel is closed or if the buffer size is too |
| 3467 | * little for the data. 0 may be returned if nothing is copied. This function |
| 3468 | * does not yield. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3469 | * |
| 3470 | * For a filter, the context is updated on success. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3471 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3472 | __LJMP static int hlua_channel_prepend(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3473 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3474 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3475 | struct filter *filter; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3476 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3477 | size_t sz, offset, len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3478 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3479 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3480 | MAY_LJMP(check_args(L, 2, "prepend")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3481 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3482 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 3483 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3484 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3485 | WILL_LJMP(lua_error(L)); |
| 3486 | } |
| 3487 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3488 | offset = co_data(chn); |
| 3489 | len = ci_data(chn); |
| 3490 | |
| 3491 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3492 | if (filter && !hlua_filter_from_payload(filter)) |
| 3493 | WILL_LJMP(lua_error(L)); |
| 3494 | |
| 3495 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3496 | if (ret > 0 && filter) { |
| 3497 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3498 | |
| 3499 | flt_update_offsets(filter, chn, ret); |
| 3500 | flt_ctx->cur_len[CHN_IDX(chn)] += ret; |
| 3501 | } |
| 3502 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3503 | lua_pushinteger(L, ret); |
| 3504 | return 1; |
| 3505 | } |
| 3506 | |
| 3507 | /* Inserts a given amount of input data at the given offset by a string |
| 3508 | * content. By default the string is appended at the end of input data. It |
| 3509 | * returns the length of the written string, or -1 if the channel is closed or |
| 3510 | * if the buffer size is too little for the data. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3511 | * |
| 3512 | * For a filter, the context is updated on success. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3513 | */ |
| 3514 | __LJMP static int hlua_channel_insert_data(lua_State *L) |
| 3515 | { |
| 3516 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3517 | struct filter *filter; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3518 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3519 | size_t sz, input, output; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3520 | int ret, offset; |
| 3521 | |
| 3522 | if (lua_gettop(L) < 2 || lua_gettop(L) > 3) |
| 3523 | WILL_LJMP(luaL_error(L, "'insert' expects at least 1 argument and at most 2 arguments")); |
| 3524 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3525 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3526 | |
| 3527 | output = co_data(chn); |
| 3528 | input = ci_data(chn); |
| 3529 | |
| 3530 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3531 | if (filter && !hlua_filter_from_payload(filter)) |
| 3532 | WILL_LJMP(lua_error(L)); |
| 3533 | |
| 3534 | offset = input + output; |
| 3535 | if (lua_gettop(L) > 2) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3536 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3537 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3538 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3539 | offset += output; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3540 | if (offset < output || offset > output + input) { |
| 3541 | lua_pushfstring(L, "offset out of range."); |
| 3542 | WILL_LJMP(lua_error(L)); |
| 3543 | } |
| 3544 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3545 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3546 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3547 | WILL_LJMP(lua_error(L)); |
| 3548 | } |
| 3549 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3550 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3551 | if (ret > 0 && filter) { |
| 3552 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3553 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3554 | flt_update_offsets(filter, chn, ret); |
| 3555 | flt_ctx->cur_len[CHN_IDX(chn)] += ret; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3556 | } |
| 3557 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3558 | lua_pushinteger(L, ret); |
| 3559 | return 1; |
| 3560 | } |
| 3561 | /* Replaces a given amount of input data at the given offset by a string |
| 3562 | * content. By default all remaining data are removed (offset = 0 and len = |
| 3563 | * -1). It returns the length of the written string, or -1 if the channel is |
| 3564 | * closed or if the buffer size is too little for the data. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3565 | * |
| 3566 | * For a filter, the context is updated on success. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3567 | */ |
| 3568 | __LJMP static int hlua_channel_set_data(lua_State *L) |
| 3569 | { |
| 3570 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3571 | struct filter *filter; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3572 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3573 | size_t sz, input, output; |
| 3574 | int ret, offset, len; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3575 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3576 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 3577 | WILL_LJMP(luaL_error(L, "'set' expects at least 1 argument and at most 3 arguments")); |
| 3578 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3579 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3580 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3581 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3582 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3583 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3584 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3585 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3586 | output = co_data(chn); |
| 3587 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3588 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3589 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3590 | if (filter && !hlua_filter_from_payload(filter)) |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3591 | WILL_LJMP(lua_error(L)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3592 | |
| 3593 | offset = output; |
| 3594 | if (lua_gettop(L) > 2) { |
| 3595 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3596 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3597 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3598 | offset += output; |
| 3599 | if (offset < output || offset > input + output) { |
| 3600 | lua_pushfstring(L, "offset out of range."); |
| 3601 | WILL_LJMP(lua_error(L)); |
| 3602 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3603 | } |
| 3604 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3605 | len = output + input - offset; |
| 3606 | if (lua_gettop(L) == 4) { |
| 3607 | len = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 3608 | if (!len) |
| 3609 | goto set; |
| 3610 | if (len == -1) |
| 3611 | len = output + input - offset; |
| 3612 | if (len < 0 || offset + len > output + input) { |
| 3613 | lua_pushfstring(L, "length out of range."); |
| 3614 | WILL_LJMP(lua_error(L)); |
| 3615 | } |
| 3616 | } |
| 3617 | |
| 3618 | set: |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3619 | /* 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] | 3620 | if (sz > c_room(chn) + len) |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3621 | lua_pushinteger(L, -1); |
| 3622 | else { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3623 | _hlua_channel_delete(chn, offset, len); |
| 3624 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3625 | if (filter) { |
| 3626 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3627 | |
| 3628 | len -= (ret > 0 ? ret : 0); |
| 3629 | flt_update_offsets(filter, chn, -len); |
| 3630 | flt_ctx->cur_len[CHN_IDX(chn)] -= len; |
| 3631 | } |
| 3632 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3633 | lua_pushinteger(L, ret); |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3634 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3635 | return 1; |
| 3636 | } |
| 3637 | |
| 3638 | /* 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] | 3639 | * 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] | 3640 | * the removed data. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3641 | * |
| 3642 | * For a filter, the context is updated on success. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3643 | */ |
| 3644 | __LJMP static int hlua_channel_del_data(lua_State *L) |
| 3645 | { |
| 3646 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3647 | struct filter *filter; |
| 3648 | size_t input, output; |
| 3649 | int offset, len; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3650 | |
| 3651 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3652 | WILL_LJMP(luaL_error(L, "'remove' expects at most 2 arguments")); |
| 3653 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3654 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3655 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3656 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3657 | WILL_LJMP(lua_error(L)); |
| 3658 | } |
| 3659 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3660 | output = co_data(chn); |
| 3661 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3662 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3663 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3664 | if (filter && !hlua_filter_from_payload(filter)) |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3665 | WILL_LJMP(lua_error(L)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3666 | |
| 3667 | offset = output; |
| 3668 | if (lua_gettop(L) > 2) { |
| 3669 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3670 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3671 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3672 | offset += output; |
| 3673 | if (offset < output || offset > input + output) { |
| 3674 | lua_pushfstring(L, "offset out of range."); |
| 3675 | WILL_LJMP(lua_error(L)); |
| 3676 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3677 | } |
| 3678 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3679 | len = output + input - offset; |
| 3680 | if (lua_gettop(L) == 4) { |
| 3681 | len = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 3682 | if (!len) |
| 3683 | goto end; |
| 3684 | if (len == -1) |
| 3685 | len = output + input - offset; |
| 3686 | if (len < 0 || offset + len > output + input) { |
| 3687 | lua_pushfstring(L, "length out of range."); |
| 3688 | WILL_LJMP(lua_error(L)); |
| 3689 | } |
| 3690 | } |
| 3691 | |
| 3692 | _hlua_channel_delete(chn, offset, len); |
| 3693 | if (filter) { |
| 3694 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3695 | |
| 3696 | flt_update_offsets(filter, chn, -len); |
| 3697 | flt_ctx->cur_len[CHN_IDX(chn)] -= len; |
| 3698 | } |
| 3699 | |
| 3700 | end: |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3701 | lua_pushinteger(L, len); |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3702 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3703 | } |
| 3704 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3705 | /* Append data in the output side of the buffer. This data is immediately |
| 3706 | * sent. The function returns the amount of data written. If the buffer |
| 3707 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3708 | * if the channel is closed. |
| 3709 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3710 | __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] | 3711 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3712 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3713 | struct filter *filter; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3714 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3715 | size_t offset, len, sz; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3716 | int l, ret; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3717 | struct hlua *hlua; |
| 3718 | |
| 3719 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3720 | hlua = hlua_gethlua(L); |
| 3721 | if (!hlua) { |
| 3722 | lua_pushnil(L); |
| 3723 | return 1; |
| 3724 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3725 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3726 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3727 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 3728 | l = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3729 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3730 | offset = co_data(chn); |
| 3731 | len = ci_data(chn); |
| 3732 | |
| 3733 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3734 | if (filter && !hlua_filter_from_payload(filter)) |
| 3735 | WILL_LJMP(lua_error(L)); |
| 3736 | |
| 3737 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3738 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3739 | lua_pushinteger(L, -1); |
| 3740 | return 1; |
| 3741 | } |
| 3742 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3743 | len = c_room(chn); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3744 | if (len > sz -l) { |
| 3745 | if (filter) { |
| 3746 | lua_pushinteger(L, -1); |
| 3747 | return 1; |
| 3748 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3749 | len = sz - l; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3750 | } |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3751 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3752 | ret = _hlua_channel_insert(chn, L, ist2(str, len), offset); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3753 | if (ret == -1) { |
| 3754 | lua_pop(L, 1); |
| 3755 | lua_pushinteger(L, -1); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3756 | return 1; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3757 | } |
| 3758 | if (ret) { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3759 | if (filter) { |
| 3760 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3761 | |
| 3762 | |
| 3763 | flt_update_offsets(filter, chn, ret); |
| 3764 | FLT_OFF(filter, chn) += ret; |
| 3765 | flt_ctx->cur_off[CHN_IDX(chn)] += ret; |
| 3766 | } |
| 3767 | else |
| 3768 | c_adv(chn, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3769 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3770 | l += ret; |
| 3771 | lua_pop(L, 1); |
| 3772 | lua_pushinteger(L, l); |
| 3773 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3774 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3775 | if (l < sz) { |
| 3776 | /* Yield only if the channel's output is not empty. |
| 3777 | * Otherwise it means we cannot add more data. */ |
| 3778 | if (co_data(chn) == 0 || HLUA_CANT_YIELD(hlua_gethlua(L))) |
Christopher Faulet | 2e60aa4 | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3779 | return 1; |
| 3780 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3781 | /* If we are waiting for space in the response buffer, we |
| 3782 | * must set the flag WAKERESWR. This flag required the task |
| 3783 | * wake up if any activity is detected on the response buffer. |
| 3784 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3785 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3786 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3787 | else |
| 3788 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3789 | 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] | 3790 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3791 | |
| 3792 | return 1; |
| 3793 | } |
| 3794 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3795 | /* Just a wrapper of "_hlua_channel_send". This wrapper permits |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3796 | * yield the LUA process, and resume it without checking the |
| 3797 | * input arguments. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3798 | * |
| 3799 | * This function cannot be called from a filter. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3800 | */ |
| 3801 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3802 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3803 | struct channel *chn; |
| 3804 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3805 | MAY_LJMP(check_args(L, 2, "send")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3806 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3807 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3808 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3809 | WILL_LJMP(lua_error(L)); |
| 3810 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3811 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3812 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3813 | } |
| 3814 | |
| 3815 | /* This function forward and amount of butes. The data pass from |
| 3816 | * the input side of the buffer to the output side, and can be |
| 3817 | * forwarded. This function never fails. |
| 3818 | * |
| 3819 | * The Lua function takes an amount of bytes to be forwarded in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3820 | * input. It returns the number of bytes forwarded. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3821 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3822 | __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] | 3823 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3824 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3825 | struct filter *filter; |
| 3826 | size_t offset, len, fwd; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3827 | int l, max; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3828 | struct hlua *hlua; |
| 3829 | |
| 3830 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3831 | hlua = hlua_gethlua(L); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3832 | if (!hlua) { |
| 3833 | lua_pushnil(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3834 | return 1; |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3835 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3836 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3837 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3838 | fwd = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3839 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3840 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3841 | offset = co_data(chn); |
| 3842 | len = ci_data(chn); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3843 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3844 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3845 | if (filter && !hlua_filter_from_payload(filter)) |
| 3846 | WILL_LJMP(lua_error(L)); |
| 3847 | |
| 3848 | max = fwd - l; |
| 3849 | if (max > len) |
| 3850 | max = len; |
| 3851 | |
| 3852 | if (filter) { |
| 3853 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3854 | |
| 3855 | FLT_OFF(filter, chn) += max; |
| 3856 | flt_ctx->cur_off[CHN_IDX(chn)] += max; |
| 3857 | flt_ctx->cur_len[CHN_IDX(chn)] -= max; |
| 3858 | } |
| 3859 | else |
| 3860 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3861 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3862 | l += max; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3863 | lua_pop(L, 1); |
| 3864 | lua_pushinteger(L, l); |
| 3865 | |
| 3866 | /* Check if it miss bytes to forward. */ |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3867 | if (l < fwd) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3868 | /* The the input channel or the output channel are closed, we |
| 3869 | * must return the amount of data forwarded. |
| 3870 | */ |
Christopher Faulet | 2e60aa4 | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3871 | 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] | 3872 | return 1; |
| 3873 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3874 | /* If we are waiting for space data in the response buffer, we |
| 3875 | * must set the flag WAKERESWR. This flag required the task |
| 3876 | * wake up if any activity is detected on the response buffer. |
| 3877 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3878 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3879 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3880 | else |
| 3881 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3882 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3883 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3884 | 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] | 3885 | } |
| 3886 | |
| 3887 | return 1; |
| 3888 | } |
| 3889 | |
| 3890 | /* Just check the input and prepare the stack for the previous |
| 3891 | * function "hlua_channel_forward_yield" |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3892 | * |
| 3893 | * This function cannot be called from a filter. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3894 | */ |
| 3895 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3896 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3897 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3898 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3899 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3900 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3901 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3902 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3903 | WILL_LJMP(lua_error(L)); |
| 3904 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3905 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3906 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3907 | } |
| 3908 | |
| 3909 | /* Just returns the number of bytes available in the input |
| 3910 | * side of the buffer. This function never fails. |
| 3911 | */ |
| 3912 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3913 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3914 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3915 | struct filter *filter; |
| 3916 | size_t output, input; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3917 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3918 | MAY_LJMP(check_args(L, 1, "input")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3919 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3920 | |
| 3921 | output = co_data(chn); |
| 3922 | input = ci_data(chn); |
| 3923 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3924 | if (filter || !IS_HTX_STRM(chn_strm(chn))) |
| 3925 | lua_pushinteger(L, input); |
| 3926 | else { |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3927 | struct htx *htx = htxbuf(&chn->buf); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3928 | |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3929 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 3930 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3931 | return 1; |
| 3932 | } |
| 3933 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3934 | /* Returns true if the channel is full. */ |
| 3935 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3936 | { |
| 3937 | struct channel *chn; |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3938 | |
| 3939 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3940 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 0ec740e | 2020-02-26 11:59:19 +0100 | [diff] [blame] | 3941 | /* ignore the reserve, we are not on a producer side (ie in an |
| 3942 | * applet). |
| 3943 | */ |
| 3944 | lua_pushboolean(L, channel_full(chn, 0)); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3945 | return 1; |
| 3946 | } |
| 3947 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3948 | /* Returns true if the channel may still receive data. */ |
| 3949 | __LJMP static int hlua_channel_may_recv(lua_State *L) |
| 3950 | { |
| 3951 | struct channel *chn; |
| 3952 | |
| 3953 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 3954 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3955 | lua_pushboolean(L, (!channel_input_closed(chn) && channel_may_recv(chn))); |
| 3956 | return 1; |
| 3957 | } |
| 3958 | |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 3959 | /* Returns true if the channel is the response channel. */ |
| 3960 | __LJMP static int hlua_channel_is_resp(lua_State *L) |
| 3961 | { |
| 3962 | struct channel *chn; |
| 3963 | |
| 3964 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 3965 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3966 | |
| 3967 | lua_pushboolean(L, !!(chn->flags & CF_ISRESP)); |
| 3968 | return 1; |
| 3969 | } |
| 3970 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3971 | /* Just returns the number of bytes available in the output |
| 3972 | * side of the buffer. This function never fails. |
| 3973 | */ |
| 3974 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 3975 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3976 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3977 | size_t output, input; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3978 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3979 | MAY_LJMP(check_args(L, 1, "output")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3980 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3981 | |
| 3982 | output = co_data(chn); |
| 3983 | input = ci_data(chn); |
| 3984 | hlua_channel_filter(L, 1, chn, &output, &input); |
| 3985 | |
| 3986 | lua_pushinteger(L, output); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3987 | return 1; |
| 3988 | } |
| 3989 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3990 | /* |
| 3991 | * |
| 3992 | * |
| 3993 | * Class Fetches |
| 3994 | * |
| 3995 | * |
| 3996 | */ |
| 3997 | |
| 3998 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3999 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4000 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4001 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4002 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4003 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4004 | } |
| 4005 | |
| 4006 | /* This function creates and push in the stack a fetch object according |
| 4007 | * with a current TXN. |
| 4008 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4009 | 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] | 4010 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4011 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4012 | |
| 4013 | /* Check stack size. */ |
| 4014 | if (!lua_checkstack(L, 3)) |
| 4015 | return 0; |
| 4016 | |
| 4017 | /* Create the object: obj[0] = userdata. |
| 4018 | * Note that the base of the Fetches object is the |
| 4019 | * transaction object. |
| 4020 | */ |
| 4021 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4022 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4023 | lua_rawseti(L, -2, 0); |
| 4024 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4025 | hsmp->s = txn->s; |
| 4026 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 4027 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4028 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4029 | |
| 4030 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 4031 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 4032 | lua_setmetatable(L, -2); |
| 4033 | |
| 4034 | return 1; |
| 4035 | } |
| 4036 | |
| 4037 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 4038 | * It uses closure argument to store the associated sample-fetch. It |
| 4039 | * returns only one argument or throws an error. An error is thrown |
| 4040 | * only if an error is encountered during the argument parsing. If |
| 4041 | * the "sample-fetch" function fails, nil is returned. |
| 4042 | */ |
| 4043 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 4044 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4045 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 4046 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 4047 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4048 | int i; |
| 4049 | struct sample smp; |
| 4050 | |
| 4051 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4052 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4053 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4054 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4055 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4056 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 4057 | /* Check execution authorization. */ |
| 4058 | if (f->use & SMP_USE_HTTP_ANY && |
| 4059 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 4060 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 4061 | "is not available in Lua services", f->kw); |
| 4062 | WILL_LJMP(lua_error(L)); |
| 4063 | } |
| 4064 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4065 | /* Get extra arguments. */ |
| 4066 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 4067 | if (i >= ARGM_NBARGS) |
| 4068 | break; |
| 4069 | hlua_lua2arg(L, i + 2, &args[i]); |
| 4070 | } |
| 4071 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4072 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4073 | |
| 4074 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4075 | 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] | 4076 | |
| 4077 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 4078 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4079 | lua_pushfstring(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4080 | goto error; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4081 | } |
| 4082 | |
| 4083 | /* Initialise the sample. */ |
| 4084 | memset(&smp, 0, sizeof(smp)); |
| 4085 | |
| 4086 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 4087 | 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] | 4088 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4089 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4090 | lua_pushstring(L, ""); |
| 4091 | else |
| 4092 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4093 | goto end; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4094 | } |
| 4095 | |
| 4096 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4097 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4098 | hlua_smp2lua_str(L, &smp); |
| 4099 | else |
| 4100 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4101 | |
| 4102 | end: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4103 | free_args(args); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4104 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4105 | |
| 4106 | error: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4107 | free_args(args); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4108 | WILL_LJMP(lua_error(L)); |
| 4109 | return 0; /* Never reached */ |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4110 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4111 | |
| 4112 | /* |
| 4113 | * |
| 4114 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4115 | * Class Converters |
| 4116 | * |
| 4117 | * |
| 4118 | */ |
| 4119 | |
| 4120 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4121 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4122 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4123 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4124 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4125 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4126 | } |
| 4127 | |
| 4128 | /* This function creates and push in the stack a Converters object |
| 4129 | * according with a current TXN. |
| 4130 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4131 | 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] | 4132 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4133 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4134 | |
| 4135 | /* Check stack size. */ |
| 4136 | if (!lua_checkstack(L, 3)) |
| 4137 | return 0; |
| 4138 | |
| 4139 | /* Create the object: obj[0] = userdata. |
| 4140 | * Note that the base of the Converters object is the |
| 4141 | * same than the TXN object. |
| 4142 | */ |
| 4143 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4144 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4145 | lua_rawseti(L, -2, 0); |
| 4146 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4147 | hsmp->s = txn->s; |
| 4148 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 4149 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4150 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4151 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4152 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4153 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 4154 | lua_setmetatable(L, -2); |
| 4155 | |
| 4156 | return 1; |
| 4157 | } |
| 4158 | |
| 4159 | /* This function is an LUA binding. It is called with each converter. |
| 4160 | * It uses closure argument to store the associated converter. It |
| 4161 | * returns only one argument or throws an error. An error is thrown |
| 4162 | * only if an error is encountered during the argument parsing. If |
| 4163 | * the converter function function fails, nil is returned. |
| 4164 | */ |
| 4165 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 4166 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 4167 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4168 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 4169 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4170 | int i; |
| 4171 | struct sample smp; |
| 4172 | |
| 4173 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4174 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4175 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4176 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 4177 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4178 | |
| 4179 | /* Get extra arguments. */ |
| 4180 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 4181 | if (i >= ARGM_NBARGS) |
| 4182 | break; |
| 4183 | hlua_lua2arg(L, i + 3, &args[i]); |
| 4184 | } |
| 4185 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4186 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4187 | |
| 4188 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 4189 | 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] | 4190 | |
| 4191 | /* Run the special args checker. */ |
| 4192 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 4193 | hlua_pusherror(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4194 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4195 | } |
| 4196 | |
| 4197 | /* Initialise the sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4198 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4199 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 4200 | hlua_pusherror(L, "error in the input argument"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4201 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4202 | } |
| 4203 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 4204 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 4205 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4206 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4207 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4208 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4209 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
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 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4212 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 4213 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4214 | hlua_pusherror(L, "error during the input argument casting"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4215 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4216 | } |
| 4217 | |
| 4218 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4219 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4220 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4221 | lua_pushstring(L, ""); |
| 4222 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 4223 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4224 | goto end; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4225 | } |
| 4226 | |
| 4227 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4228 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4229 | hlua_smp2lua_str(L, &smp); |
| 4230 | else |
| 4231 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4232 | end: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4233 | free_args(args); |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 4234 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4235 | |
| 4236 | error: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4237 | free_args(args); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4238 | WILL_LJMP(lua_error(L)); |
| 4239 | return 0; /* Never reached */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4240 | } |
| 4241 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4242 | /* |
| 4243 | * |
| 4244 | * |
| 4245 | * Class AppletTCP |
| 4246 | * |
| 4247 | * |
| 4248 | */ |
| 4249 | |
| 4250 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4251 | * a class stream, otherwise it throws an error. |
| 4252 | */ |
| 4253 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 4254 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4255 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4256 | } |
| 4257 | |
| 4258 | /* This function creates and push in the stack an Applet object |
| 4259 | * according with a current TXN. |
| 4260 | */ |
| 4261 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 4262 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4263 | struct hlua_appctx *luactx; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4264 | struct stream_interface *si = ctx->owner; |
| 4265 | struct stream *s = si_strm(si); |
| 4266 | struct proxy *p = s->be; |
| 4267 | |
| 4268 | /* Check stack size. */ |
| 4269 | if (!lua_checkstack(L, 3)) |
| 4270 | return 0; |
| 4271 | |
| 4272 | /* Create the object: obj[0] = userdata. |
| 4273 | * Note that the base of the Converters object is the |
| 4274 | * same than the TXN object. |
| 4275 | */ |
| 4276 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4277 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4278 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4279 | luactx->appctx = ctx; |
| 4280 | luactx->htxn.s = s; |
| 4281 | luactx->htxn.p = p; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4282 | |
| 4283 | /* Create the "f" field that contains a list of fetches. */ |
| 4284 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4285 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4286 | return 0; |
| 4287 | lua_settable(L, -3); |
| 4288 | |
| 4289 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4290 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4291 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4292 | return 0; |
| 4293 | lua_settable(L, -3); |
| 4294 | |
| 4295 | /* Create the "c" field that contains a list of converters. */ |
| 4296 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4297 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4298 | return 0; |
| 4299 | lua_settable(L, -3); |
| 4300 | |
| 4301 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4302 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4303 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4304 | return 0; |
| 4305 | lua_settable(L, -3); |
| 4306 | |
| 4307 | /* Pop a class stream metatable and affect it to the table. */ |
| 4308 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 4309 | lua_setmetatable(L, -2); |
| 4310 | |
| 4311 | return 1; |
| 4312 | } |
| 4313 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4314 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 4315 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4316 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4317 | struct stream *s; |
| 4318 | const char *name; |
| 4319 | size_t len; |
| 4320 | struct sample smp; |
| 4321 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4322 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 4323 | 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] | 4324 | |
| 4325 | /* It is useles to retrieve the stream, but this function |
| 4326 | * runs only in a stream context. |
| 4327 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4328 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4329 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4330 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4331 | |
| 4332 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4333 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4334 | hlua_lua2smp(L, 3, &smp); |
| 4335 | |
| 4336 | /* Store the sample in a variable. */ |
| 4337 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4338 | |
| 4339 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 4340 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 4341 | else |
| 4342 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 4343 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4344 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4345 | } |
| 4346 | |
| 4347 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 4348 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4349 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4350 | struct stream *s; |
| 4351 | const char *name; |
| 4352 | size_t len; |
| 4353 | struct sample smp; |
| 4354 | |
| 4355 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4356 | |
| 4357 | /* It is useles to retrieve the stream, but this function |
| 4358 | * runs only in a stream context. |
| 4359 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4360 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4361 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4362 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4363 | |
| 4364 | /* Unset the variable. */ |
| 4365 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4366 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4367 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4368 | } |
| 4369 | |
| 4370 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 4371 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4372 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4373 | struct stream *s; |
| 4374 | const char *name; |
| 4375 | size_t len; |
| 4376 | struct sample smp; |
| 4377 | |
| 4378 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4379 | |
| 4380 | /* It is useles to retrieve the stream, but this function |
| 4381 | * runs only in a stream context. |
| 4382 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4383 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4384 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4385 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4386 | |
| 4387 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Willy Tarreau | e352b9d | 2021-09-03 11:52:38 +0200 | [diff] [blame] | 4388 | if (!vars_get_by_name(name, len, &smp, NULL)) { |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4389 | lua_pushnil(L); |
| 4390 | return 1; |
| 4391 | } |
| 4392 | |
| 4393 | return hlua_smp2lua(L, &smp); |
| 4394 | } |
| 4395 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4396 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 4397 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4398 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4399 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4400 | struct hlua *hlua; |
| 4401 | |
| 4402 | /* 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] | 4403 | if (!s->hlua) |
| 4404 | return 0; |
| 4405 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4406 | |
| 4407 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4408 | |
| 4409 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4410 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4411 | |
| 4412 | /* Get and store new value. */ |
| 4413 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4414 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4415 | |
| 4416 | return 0; |
| 4417 | } |
| 4418 | |
| 4419 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 4420 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4421 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4422 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4423 | struct hlua *hlua; |
| 4424 | |
| 4425 | /* 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] | 4426 | if (!s->hlua) { |
| 4427 | lua_pushnil(L); |
| 4428 | return 1; |
| 4429 | } |
| 4430 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4431 | |
| 4432 | /* Push configuration index in the stack. */ |
| 4433 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4434 | |
| 4435 | return 1; |
| 4436 | } |
| 4437 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4438 | /* If expected data not yet available, it returns a yield. This function |
| 4439 | * consumes the data in the buffer. It returns a string containing the |
| 4440 | * data. This string can be empty. |
| 4441 | */ |
| 4442 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 4443 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4444 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4445 | struct stream_interface *si = luactx->appctx->owner; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4446 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4447 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4448 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4449 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4450 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4451 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4452 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4453 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4454 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4455 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4456 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4457 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4458 | 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] | 4459 | } |
| 4460 | |
| 4461 | /* End of data: commit the total strings and return. */ |
| 4462 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4463 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4464 | return 1; |
| 4465 | } |
| 4466 | |
| 4467 | /* Ensure that the block 2 length is usable. */ |
| 4468 | if (ret == 1) |
| 4469 | len2 = 0; |
| 4470 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 4471 | /* don't check the max length read and don't check. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4472 | luaL_addlstring(&luactx->b, blk1, len1); |
| 4473 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4474 | |
| 4475 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4476 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4477 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4478 | return 1; |
| 4479 | } |
| 4480 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4481 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4482 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 4483 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4484 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4485 | |
| 4486 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4487 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4488 | |
| 4489 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 4490 | } |
| 4491 | |
| 4492 | /* If expected data not yet available, it returns a yield. This function |
| 4493 | * consumes the data in the buffer. It returns a string containing the |
| 4494 | * data. This string can be empty. |
| 4495 | */ |
| 4496 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 4497 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4498 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4499 | struct stream_interface *si = luactx->appctx->owner; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4500 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4501 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4502 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4503 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4504 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4505 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4506 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4507 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4508 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4509 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4510 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4511 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4512 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4513 | 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] | 4514 | } |
| 4515 | |
| 4516 | /* End of data: commit the total strings and return. */ |
| 4517 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4518 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4519 | return 1; |
| 4520 | } |
| 4521 | |
| 4522 | /* Ensure that the block 2 length is usable. */ |
| 4523 | if (ret == 1) |
| 4524 | len2 = 0; |
| 4525 | |
| 4526 | if (len == -1) { |
| 4527 | |
| 4528 | /* If len == -1, catenate all the data avalaile and |
| 4529 | * yield because we want to get all the data until |
| 4530 | * the end of data stream. |
| 4531 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4532 | luaL_addlstring(&luactx->b, blk1, len1); |
| 4533 | luaL_addlstring(&luactx->b, blk2, len2); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4534 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4535 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4536 | 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] | 4537 | |
| 4538 | } else { |
| 4539 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4540 | /* Copy the first block caping to the length required. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4541 | if (len1 > len) |
| 4542 | len1 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4543 | luaL_addlstring(&luactx->b, blk1, len1); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4544 | len -= len1; |
| 4545 | |
| 4546 | /* Copy the second block. */ |
| 4547 | if (len2 > len) |
| 4548 | len2 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4549 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4550 | len -= len2; |
| 4551 | |
| 4552 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4553 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4554 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4555 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4556 | if (len > 0) { |
| 4557 | lua_pushinteger(L, len); |
| 4558 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4559 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4560 | 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] | 4561 | } |
| 4562 | |
| 4563 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4564 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4565 | return 1; |
| 4566 | } |
| 4567 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4568 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4569 | hlua_pusherror(L, "Lua: internal error"); |
| 4570 | WILL_LJMP(lua_error(L)); |
| 4571 | return 0; |
| 4572 | } |
| 4573 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4574 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4575 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 4576 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4577 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4578 | int len = -1; |
| 4579 | |
| 4580 | if (lua_gettop(L) > 2) |
| 4581 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4582 | if (lua_gettop(L) >= 2) { |
| 4583 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4584 | lua_pop(L, 1); |
| 4585 | } |
| 4586 | |
| 4587 | /* Confirm or set the required length */ |
| 4588 | lua_pushinteger(L, len); |
| 4589 | |
| 4590 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4591 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4592 | |
| 4593 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 4594 | } |
| 4595 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4596 | /* Append data in the output side of the buffer. This data is immediately |
| 4597 | * sent. The function returns the amount of data written. If the buffer |
| 4598 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4599 | * if the channel is closed. |
| 4600 | */ |
| 4601 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4602 | { |
| 4603 | size_t len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4604 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4605 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4606 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4607 | struct stream_interface *si = luactx->appctx->owner; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4608 | struct channel *chn = si_ic(si); |
| 4609 | int max; |
| 4610 | |
| 4611 | /* Get the max amount of data which can write as input in the channel. */ |
| 4612 | max = channel_recv_max(chn); |
| 4613 | if (max > (len - l)) |
| 4614 | max = len - l; |
| 4615 | |
| 4616 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4617 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4618 | |
| 4619 | /* update counters. */ |
| 4620 | l += max; |
| 4621 | lua_pop(L, 1); |
| 4622 | lua_pushinteger(L, l); |
| 4623 | |
| 4624 | /* If some data is not send, declares the situation to the |
| 4625 | * applet, and returns a yield. |
| 4626 | */ |
| 4627 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 4628 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4629 | 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] | 4630 | } |
| 4631 | |
| 4632 | return 1; |
| 4633 | } |
| 4634 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4635 | /* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4636 | * yield the LUA process, and resume it without checking the |
| 4637 | * input arguments. |
| 4638 | */ |
| 4639 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 4640 | { |
| 4641 | MAY_LJMP(check_args(L, 2, "send")); |
| 4642 | lua_pushinteger(L, 0); |
| 4643 | |
| 4644 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 4645 | } |
| 4646 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4647 | /* |
| 4648 | * |
| 4649 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4650 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4651 | * |
| 4652 | * |
| 4653 | */ |
| 4654 | |
| 4655 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4656 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4657 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4658 | __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] | 4659 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4660 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4661 | } |
| 4662 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4663 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4664 | * according with a current TXN. |
| 4665 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4666 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4667 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4668 | struct hlua_appctx *luactx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 4669 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4670 | struct stream_interface *si = ctx->owner; |
| 4671 | struct stream *s = si_strm(si); |
| 4672 | struct proxy *px = s->be; |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4673 | struct htx *htx; |
| 4674 | struct htx_blk *blk; |
| 4675 | struct htx_sl *sl; |
| 4676 | struct ist path; |
| 4677 | unsigned long long len = 0; |
| 4678 | int32_t pos; |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 4679 | struct http_uri_parser parser; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4680 | |
| 4681 | /* Check stack size. */ |
| 4682 | if (!lua_checkstack(L, 3)) |
| 4683 | return 0; |
| 4684 | |
| 4685 | /* Create the object: obj[0] = userdata. |
| 4686 | * Note that the base of the Converters object is the |
| 4687 | * same than the TXN object. |
| 4688 | */ |
| 4689 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4690 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4691 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4692 | luactx->appctx = ctx; |
| 4693 | luactx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
| 4694 | luactx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */ |
| 4695 | luactx->htxn.s = s; |
| 4696 | luactx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4697 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4698 | /* Create the "f" field that contains a list of fetches. */ |
| 4699 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4700 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4701 | return 0; |
| 4702 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4703 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4704 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4705 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4706 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4707 | return 0; |
| 4708 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4709 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4710 | /* Create the "c" field that contains a list of converters. */ |
| 4711 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4712 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4713 | return 0; |
| 4714 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4715 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4716 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4717 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4718 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4719 | return 0; |
| 4720 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4721 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4722 | htx = htxbuf(&s->req.buf); |
| 4723 | blk = htx_get_first_blk(htx); |
Christopher Faulet | ea00973 | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 4724 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4725 | sl = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4726 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4727 | /* Stores the request method. */ |
| 4728 | lua_pushstring(L, "method"); |
| 4729 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 4730 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4731 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4732 | /* Stores the http version. */ |
| 4733 | lua_pushstring(L, "version"); |
| 4734 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 4735 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4736 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4737 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 4738 | * the array on the top of the stack. |
| 4739 | */ |
| 4740 | lua_pushstring(L, "headers"); |
| 4741 | htxn.s = s; |
| 4742 | htxn.p = px; |
| 4743 | htxn.dir = SMP_OPT_DIR_REQ; |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4744 | if (!hlua_http_get_headers(L, &htxn.s->txn->req)) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4745 | return 0; |
| 4746 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4747 | |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 4748 | parser = http_uri_parser_init(htx_sl_req_uri(sl)); |
| 4749 | path = http_parse_path(&parser); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 4750 | if (isttest(path)) { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4751 | char *p, *q, *end; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4752 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4753 | p = path.ptr; |
| 4754 | end = path.ptr + path.len; |
| 4755 | q = p; |
| 4756 | while (q < end && *q != '?') |
| 4757 | q++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4758 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4759 | /* Stores the request path. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4760 | lua_pushstring(L, "path"); |
| 4761 | lua_pushlstring(L, p, q - p); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4762 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4763 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4764 | /* Stores the query string. */ |
| 4765 | lua_pushstring(L, "qs"); |
| 4766 | if (*q == '?') |
| 4767 | q++; |
| 4768 | lua_pushlstring(L, q, end - q); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4769 | lua_settable(L, -3); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4770 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4771 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4772 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4773 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4774 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4775 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4776 | if (type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4777 | break; |
| 4778 | if (type == HTX_BLK_DATA) |
| 4779 | len += htx_get_blksz(blk); |
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 | if (htx->extra != ULLONG_MAX) |
| 4782 | len += htx->extra; |
| 4783 | |
| 4784 | /* Stores the request path. */ |
| 4785 | lua_pushstring(L, "length"); |
| 4786 | lua_pushinteger(L, len); |
| 4787 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4788 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4789 | /* Create an empty array of HTTP request headers. */ |
| 4790 | lua_pushstring(L, "response"); |
| 4791 | lua_newtable(L); |
| 4792 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4793 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4794 | /* Pop a class stream metatable and affect it to the table. */ |
| 4795 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 4796 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4797 | |
| 4798 | return 1; |
| 4799 | } |
| 4800 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4801 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 4802 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4803 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4804 | struct stream *s; |
| 4805 | const char *name; |
| 4806 | size_t len; |
| 4807 | struct sample smp; |
| 4808 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4809 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 4810 | 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] | 4811 | |
| 4812 | /* It is useles to retrieve the stream, but this function |
| 4813 | * runs only in a stream context. |
| 4814 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4815 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4816 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4817 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4818 | |
| 4819 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4820 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4821 | hlua_lua2smp(L, 3, &smp); |
| 4822 | |
| 4823 | /* Store the sample in a variable. */ |
| 4824 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4825 | |
| 4826 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 4827 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 4828 | else |
| 4829 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 4830 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4831 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4832 | } |
| 4833 | |
| 4834 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 4835 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4836 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4837 | struct stream *s; |
| 4838 | const char *name; |
| 4839 | size_t len; |
| 4840 | struct sample smp; |
| 4841 | |
| 4842 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4843 | |
| 4844 | /* It is useles to retrieve the stream, but this function |
| 4845 | * runs only in a stream context. |
| 4846 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4847 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4848 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4849 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4850 | |
| 4851 | /* Unset the variable. */ |
| 4852 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4853 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4854 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4855 | } |
| 4856 | |
| 4857 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4858 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4859 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4860 | struct stream *s; |
| 4861 | const char *name; |
| 4862 | size_t len; |
| 4863 | struct sample smp; |
| 4864 | |
| 4865 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4866 | |
| 4867 | /* It is useles to retrieve the stream, but this function |
| 4868 | * runs only in a stream context. |
| 4869 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4870 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4871 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4872 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4873 | |
| 4874 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Willy Tarreau | e352b9d | 2021-09-03 11:52:38 +0200 | [diff] [blame] | 4875 | if (!vars_get_by_name(name, len, &smp, NULL)) { |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4876 | lua_pushnil(L); |
| 4877 | return 1; |
| 4878 | } |
| 4879 | |
| 4880 | return hlua_smp2lua(L, &smp); |
| 4881 | } |
| 4882 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4883 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 4884 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4885 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4886 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4887 | struct hlua *hlua; |
| 4888 | |
| 4889 | /* 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] | 4890 | if (!s->hlua) |
| 4891 | return 0; |
| 4892 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4893 | |
| 4894 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4895 | |
| 4896 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4897 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4898 | |
| 4899 | /* Get and store new value. */ |
| 4900 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4901 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4902 | |
| 4903 | return 0; |
| 4904 | } |
| 4905 | |
| 4906 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4907 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4908 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4909 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4910 | struct hlua *hlua; |
| 4911 | |
| 4912 | /* 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] | 4913 | if (!s->hlua) { |
| 4914 | lua_pushnil(L); |
| 4915 | return 1; |
| 4916 | } |
| 4917 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4918 | |
| 4919 | /* Push configuration index in the stack. */ |
| 4920 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4921 | |
| 4922 | return 1; |
| 4923 | } |
| 4924 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4925 | /* If expected data not yet available, it returns a yield. This function |
| 4926 | * consumes the data in the buffer. It returns a string containing the |
| 4927 | * data. This string can be empty. |
| 4928 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4929 | __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] | 4930 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4931 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4932 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4933 | struct channel *req = si_oc(si); |
| 4934 | struct htx *htx; |
| 4935 | struct htx_blk *blk; |
| 4936 | size_t count; |
| 4937 | int stop = 0; |
| 4938 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4939 | htx = htx_from_buf(&req->buf); |
| 4940 | count = co_data(req); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4941 | blk = htx_get_first_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 4942 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4943 | while (count && !stop && blk) { |
| 4944 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4945 | uint32_t sz = htx_get_blksz(blk); |
| 4946 | struct ist v; |
| 4947 | uint32_t vlen; |
| 4948 | char *nl; |
| 4949 | |
| 4950 | vlen = sz; |
| 4951 | if (vlen > count) { |
| 4952 | if (type != HTX_BLK_DATA) |
| 4953 | break; |
| 4954 | vlen = count; |
| 4955 | } |
| 4956 | |
| 4957 | switch (type) { |
| 4958 | case HTX_BLK_UNUSED: |
| 4959 | break; |
| 4960 | |
| 4961 | case HTX_BLK_DATA: |
| 4962 | v = htx_get_blk_value(htx, blk); |
| 4963 | v.len = vlen; |
| 4964 | nl = istchr(v, '\n'); |
| 4965 | if (nl != NULL) { |
| 4966 | stop = 1; |
| 4967 | vlen = nl - v.ptr + 1; |
| 4968 | } |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4969 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4970 | break; |
| 4971 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4972 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4973 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4974 | stop = 1; |
| 4975 | break; |
| 4976 | |
| 4977 | default: |
| 4978 | break; |
| 4979 | } |
| 4980 | |
| 4981 | co_set_data(req, co_data(req) - vlen); |
| 4982 | count -= vlen; |
| 4983 | if (sz == vlen) |
| 4984 | blk = htx_remove_blk(htx, blk); |
| 4985 | else { |
| 4986 | htx_cut_data_blk(htx, blk, vlen); |
| 4987 | break; |
| 4988 | } |
| 4989 | } |
| 4990 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 4991 | /* The message was fully consumed and no more data are expected |
| 4992 | * (EOM flag set). |
| 4993 | */ |
| 4994 | if (htx_is_empty(htx) && (htx->flags & HTX_FL_EOM)) |
| 4995 | stop = 1; |
| 4996 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4997 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4998 | if (!stop) { |
| 4999 | si_cant_get(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5000 | 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] | 5001 | } |
| 5002 | |
| 5003 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5004 | luaL_pushresult(&luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5005 | return 1; |
| 5006 | } |
| 5007 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5008 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5009 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5010 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5011 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5012 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5013 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5014 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5015 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5016 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5017 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5018 | } |
| 5019 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5020 | /* If expected data not yet available, it returns a yield. This function |
| 5021 | * consumes the data in the buffer. It returns a string containing the |
| 5022 | * data. This string can be empty. |
| 5023 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5024 | __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] | 5025 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5026 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 5027 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5028 | struct channel *req = si_oc(si); |
| 5029 | struct htx *htx; |
| 5030 | struct htx_blk *blk; |
| 5031 | size_t count; |
| 5032 | int len; |
| 5033 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5034 | htx = htx_from_buf(&req->buf); |
| 5035 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5036 | count = co_data(req); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 5037 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5038 | while (count && len && blk) { |
| 5039 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5040 | uint32_t sz = htx_get_blksz(blk); |
| 5041 | struct ist v; |
| 5042 | uint32_t vlen; |
| 5043 | |
| 5044 | vlen = sz; |
| 5045 | if (len > 0 && vlen > len) |
| 5046 | vlen = len; |
| 5047 | if (vlen > count) { |
| 5048 | if (type != HTX_BLK_DATA) |
| 5049 | break; |
| 5050 | vlen = count; |
| 5051 | } |
| 5052 | |
| 5053 | switch (type) { |
| 5054 | case HTX_BLK_UNUSED: |
| 5055 | break; |
| 5056 | |
| 5057 | case HTX_BLK_DATA: |
| 5058 | v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5059 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5060 | break; |
| 5061 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5062 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5063 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5064 | len = 0; |
| 5065 | break; |
| 5066 | |
| 5067 | default: |
| 5068 | break; |
| 5069 | } |
| 5070 | |
| 5071 | co_set_data(req, co_data(req) - vlen); |
| 5072 | count -= vlen; |
| 5073 | if (len > 0) |
| 5074 | len -= vlen; |
| 5075 | if (sz == vlen) |
| 5076 | blk = htx_remove_blk(htx, blk); |
| 5077 | else { |
| 5078 | htx_cut_data_blk(htx, blk, vlen); |
| 5079 | break; |
| 5080 | } |
| 5081 | } |
| 5082 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 5083 | /* The message was fully consumed and no more data are expected |
| 5084 | * (EOM flag set). |
| 5085 | */ |
| 5086 | if (htx_is_empty(htx) && (htx->flags & HTX_FL_EOM)) |
| 5087 | len = 0; |
| 5088 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 5089 | htx_to_buf(htx, &req->buf); |
| 5090 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5091 | /* If we are no other data available, yield waiting for new data. */ |
| 5092 | if (len) { |
| 5093 | if (len > 0) { |
| 5094 | lua_pushinteger(L, len); |
| 5095 | lua_replace(L, 2); |
| 5096 | } |
| 5097 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5098 | 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] | 5099 | } |
| 5100 | |
| 5101 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5102 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5103 | return 1; |
| 5104 | } |
| 5105 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5106 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5107 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 5108 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5109 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5110 | int len = -1; |
| 5111 | |
| 5112 | /* Check arguments. */ |
| 5113 | if (lua_gettop(L) > 2) |
| 5114 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 5115 | if (lua_gettop(L) >= 2) { |
| 5116 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5117 | lua_pop(L, 1); |
| 5118 | } |
| 5119 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5120 | lua_pushinteger(L, len); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5121 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5122 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5123 | luaL_buffinit(L, &luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5124 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5125 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5126 | } |
| 5127 | |
| 5128 | /* Append data in the output side of the buffer. This data is immediately |
| 5129 | * sent. The function returns the amount of data written. If the buffer |
| 5130 | * cannot contain the data, the function yields. The function returns -1 |
| 5131 | * if the channel is closed. |
| 5132 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5133 | __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] | 5134 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5135 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 5136 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5137 | struct channel *res = si_ic(si); |
| 5138 | struct htx *htx = htx_from_buf(&res->buf); |
| 5139 | const char *data; |
| 5140 | size_t len; |
| 5141 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 5142 | int max; |
| 5143 | |
Christopher Faulet | 9060fc0 | 2019-07-03 11:39:30 +0200 | [diff] [blame] | 5144 | max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx)); |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 5145 | if (!max) |
| 5146 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5147 | |
| 5148 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5149 | |
| 5150 | /* Get the max amount of data which can write as input in the channel. */ |
| 5151 | if (max > (len - l)) |
| 5152 | max = len - l; |
| 5153 | |
| 5154 | /* Copy data. */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 5155 | max = htx_add_data(htx, ist2(data + l, max)); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 5156 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5157 | |
| 5158 | /* update counters. */ |
| 5159 | l += max; |
| 5160 | lua_pop(L, 1); |
| 5161 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5162 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5163 | /* If some data is not send, declares the situation to the |
| 5164 | * applet, and returns a yield. |
| 5165 | */ |
| 5166 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 5167 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 5168 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5169 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5170 | 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] | 5171 | } |
| 5172 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5173 | htx_to_buf(htx, &res->buf); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5174 | return 1; |
| 5175 | } |
| 5176 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5177 | /* Just a wrapper of "hlua_applet_send_yield". This wrapper permits |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5178 | * yield the LUA process, and resume it without checking the |
| 5179 | * input arguments. |
| 5180 | */ |
| 5181 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 5182 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5183 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5184 | |
| 5185 | /* We want to send some data. Headers must be sent. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5186 | if (!(luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5187 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 5188 | WILL_LJMP(lua_error(L)); |
| 5189 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5190 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5191 | /* This integer is used for followinf the amount of data sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5192 | lua_pushinteger(L, 0); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5193 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5194 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5195 | } |
| 5196 | |
| 5197 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 5198 | { |
| 5199 | const char *name; |
| 5200 | int ret; |
| 5201 | |
| 5202 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 5203 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5204 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 5205 | |
| 5206 | /* Push in the stack the "response" entry. */ |
| 5207 | ret = lua_getfield(L, 1, "response"); |
| 5208 | if (ret != LUA_TTABLE) { |
| 5209 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 5210 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 5211 | WILL_LJMP(lua_error(L)); |
| 5212 | } |
| 5213 | |
| 5214 | /* check if the header is already registered if it is not |
| 5215 | * the case, register it. |
| 5216 | */ |
| 5217 | ret = lua_getfield(L, -1, name); |
| 5218 | if (ret == LUA_TNIL) { |
| 5219 | |
| 5220 | /* Entry not found. */ |
| 5221 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 5222 | |
| 5223 | /* Insert the new header name in the array in the top of the stack. |
| 5224 | * It left the new array in the top of the stack. |
| 5225 | */ |
| 5226 | lua_newtable(L); |
| 5227 | lua_pushvalue(L, 2); |
| 5228 | lua_pushvalue(L, -2); |
| 5229 | lua_settable(L, -4); |
| 5230 | |
| 5231 | } else if (ret != LUA_TTABLE) { |
| 5232 | |
| 5233 | /* corruption error. */ |
| 5234 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 5235 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 5236 | WILL_LJMP(lua_error(L)); |
| 5237 | } |
| 5238 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 5239 | /* Now the top of thestack is an array of values. We push |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5240 | * the header value as new entry. |
| 5241 | */ |
| 5242 | lua_pushvalue(L, 3); |
| 5243 | ret = lua_rawlen(L, -2); |
| 5244 | lua_rawseti(L, -2, ret + 1); |
| 5245 | lua_pushboolean(L, 1); |
| 5246 | return 1; |
| 5247 | } |
| 5248 | |
| 5249 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 5250 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5251 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5252 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5253 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5254 | |
| 5255 | if (status < 100 || status > 599) { |
| 5256 | lua_pushboolean(L, 0); |
| 5257 | return 1; |
| 5258 | } |
| 5259 | |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5260 | luactx->appctx->ctx.hlua_apphttp.status = status; |
| 5261 | luactx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5262 | lua_pushboolean(L, 1); |
| 5263 | return 1; |
| 5264 | } |
| 5265 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5266 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5267 | __LJMP static int hlua_applet_http_send_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5268 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5269 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 5270 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5271 | struct channel *res = si_ic(si); |
| 5272 | struct htx *htx; |
| 5273 | struct htx_sl *sl; |
| 5274 | struct h1m h1m; |
| 5275 | const char *status, *reason; |
| 5276 | const char *name, *value; |
| 5277 | size_t nlen, vlen; |
| 5278 | unsigned int flags; |
| 5279 | |
| 5280 | /* Send the message at once. */ |
| 5281 | htx = htx_from_buf(&res->buf); |
| 5282 | h1m_init_res(&h1m); |
| 5283 | |
| 5284 | /* Use the same http version than the request. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5285 | status = ultoa_r(luactx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size); |
| 5286 | reason = luactx->appctx->ctx.hlua_apphttp.reason; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5287 | if (reason == NULL) |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5288 | reason = http_get_reason(luactx->appctx->ctx.hlua_apphttp.status); |
| 5289 | if (luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5290 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 5291 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 5292 | } |
| 5293 | else { |
| 5294 | flags = HTX_SL_F_IS_RESP; |
| 5295 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 5296 | } |
| 5297 | if (!sl) { |
| 5298 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5299 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5300 | WILL_LJMP(lua_error(L)); |
| 5301 | } |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5302 | sl->info.res.status = luactx->appctx->ctx.hlua_apphttp.status; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5303 | |
| 5304 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 5305 | lua_pushvalue(L, 0); |
| 5306 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 5307 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\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 | } |
| 5311 | |
| 5312 | /* Browse the list of headers. */ |
| 5313 | lua_pushnil(L); |
| 5314 | while(lua_next(L, -2) != 0) { |
| 5315 | /* We expect a string as -2. */ |
| 5316 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 5317 | 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] | 5318 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5319 | lua_typename(L, lua_type(L, -2))); |
| 5320 | WILL_LJMP(lua_error(L)); |
| 5321 | } |
| 5322 | name = lua_tolstring(L, -2, &nlen); |
| 5323 | |
| 5324 | /* We expect an array as -1. */ |
| 5325 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 5326 | 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] | 5327 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5328 | name, |
| 5329 | lua_typename(L, lua_type(L, -1))); |
| 5330 | WILL_LJMP(lua_error(L)); |
| 5331 | } |
| 5332 | |
| 5333 | /* Browse the table who is on the top of the stack. */ |
| 5334 | lua_pushnil(L); |
| 5335 | while(lua_next(L, -2) != 0) { |
| 5336 | int id; |
| 5337 | |
| 5338 | /* We expect a number as -2. */ |
| 5339 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 5340 | 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] | 5341 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5342 | name, |
| 5343 | lua_typename(L, lua_type(L, -2))); |
| 5344 | WILL_LJMP(lua_error(L)); |
| 5345 | } |
| 5346 | id = lua_tointeger(L, -2); |
| 5347 | |
| 5348 | /* We expect a string as -2. */ |
| 5349 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 5350 | 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] | 5351 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5352 | name, id, |
| 5353 | lua_typename(L, lua_type(L, -1))); |
| 5354 | WILL_LJMP(lua_error(L)); |
| 5355 | } |
| 5356 | value = lua_tolstring(L, -1, &vlen); |
| 5357 | |
| 5358 | /* Simple Protocol checks. */ |
Christopher Faulet | 545fbba | 2021-09-28 09:36:25 +0200 | [diff] [blame] | 5359 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) { |
| 5360 | int ret; |
| 5361 | |
| 5362 | ret = h1_parse_xfer_enc_header(&h1m, ist2(value, vlen)); |
| 5363 | if (ret < 0) { |
| 5364 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
| 5365 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
| 5366 | name); |
| 5367 | WILL_LJMP(lua_error(L)); |
| 5368 | } |
| 5369 | else if (ret == 0) |
| 5370 | goto next; /* Skip it */ |
| 5371 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5372 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 5373 | struct ist v = ist2(value, vlen); |
| 5374 | int ret; |
| 5375 | |
| 5376 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 5377 | if (ret < 0) { |
| 5378 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5379 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5380 | name); |
| 5381 | WILL_LJMP(lua_error(L)); |
| 5382 | } |
| 5383 | else if (ret == 0) |
| 5384 | goto next; /* Skip it */ |
| 5385 | } |
| 5386 | |
| 5387 | /* Add a new header */ |
| 5388 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 5389 | 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] | 5390 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5391 | name); |
| 5392 | WILL_LJMP(lua_error(L)); |
| 5393 | } |
| 5394 | next: |
| 5395 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5396 | lua_pop(L, 1); |
| 5397 | } |
| 5398 | |
| 5399 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5400 | lua_pop(L, 1); |
| 5401 | } |
| 5402 | |
| 5403 | if (h1m.flags & H1_MF_CHNK) |
| 5404 | h1m.flags &= ~H1_MF_CLEN; |
| 5405 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 5406 | h1m.flags |= H1_MF_XFER_LEN; |
| 5407 | |
| 5408 | /* Uset HTX start-line flags */ |
| 5409 | if (h1m.flags & H1_MF_XFER_ENC) |
| 5410 | flags |= HTX_SL_F_XFER_ENC; |
| 5411 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 5412 | flags |= HTX_SL_F_XFER_LEN; |
| 5413 | if (h1m.flags & H1_MF_CHNK) |
| 5414 | flags |= HTX_SL_F_CHNK; |
| 5415 | else if (h1m.flags & H1_MF_CLEN) |
| 5416 | flags |= HTX_SL_F_CLEN; |
| 5417 | if (h1m.body_len == 0) |
| 5418 | flags |= HTX_SL_F_BODYLESS; |
| 5419 | } |
| 5420 | sl->flags |= flags; |
| 5421 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 5422 | /* 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] | 5423 | * and the status code implies the presence of a message body, we must |
| 5424 | * announce a transfer encoding chunked. This is required by haproxy |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5425 | * for the keepalive compliance. If the applet announces a transfer-encoding |
| 5426 | * chunked itself, don't do anything. |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5427 | */ |
| 5428 | 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] | 5429 | luactx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 5430 | luactx->appctx->ctx.hlua_apphttp.status != 204 && |
| 5431 | luactx->appctx->ctx.hlua_apphttp.status != 304) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5432 | /* Add a new header */ |
| 5433 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 5434 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 5435 | 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] | 5436 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5437 | WILL_LJMP(lua_error(L)); |
| 5438 | } |
| 5439 | } |
| 5440 | |
| 5441 | /* Finalize headers. */ |
| 5442 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 5443 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5444 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5445 | WILL_LJMP(lua_error(L)); |
| 5446 | } |
| 5447 | |
| 5448 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 5449 | b_reset(&res->buf); |
| 5450 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 5451 | WILL_LJMP(lua_error(L)); |
| 5452 | } |
| 5453 | |
| 5454 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 5455 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5456 | |
| 5457 | /* Headers sent, set the flag. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5458 | luactx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5459 | return 0; |
| 5460 | |
| 5461 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5462 | /* We will build the status line and the headers of the HTTP response. |
| 5463 | * We will try send at once if its not possible, we give back the hand |
| 5464 | * waiting for more room. |
| 5465 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5466 | __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] | 5467 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5468 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 5469 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5470 | struct channel *res = si_ic(si); |
| 5471 | |
| 5472 | if (co_data(res)) { |
| 5473 | si_rx_room_blk(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5474 | 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] | 5475 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5476 | return MAY_LJMP(hlua_applet_http_send_response(L)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5477 | } |
| 5478 | |
| 5479 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5480 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5481 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5482 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5483 | } |
| 5484 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5485 | /* |
| 5486 | * |
| 5487 | * |
| 5488 | * Class HTTP |
| 5489 | * |
| 5490 | * |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5491 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5492 | |
| 5493 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 5494 | * a class stream, otherwise it throws an error. |
| 5495 | */ |
| 5496 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5497 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5498 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
| 5499 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5500 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5501 | /* This function creates and push in the stack a HTTP object |
| 5502 | * according with a current TXN. |
| 5503 | */ |
| 5504 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 5505 | { |
| 5506 | struct hlua_txn *htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5507 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5508 | /* Check stack size. */ |
| 5509 | if (!lua_checkstack(L, 3)) |
| 5510 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5511 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5512 | /* Create the object: obj[0] = userdata. |
| 5513 | * Note that the base of the Converters object is the |
| 5514 | * same than the TXN object. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5515 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5516 | lua_newtable(L); |
| 5517 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 5518 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5519 | |
| 5520 | htxn->s = txn->s; |
| 5521 | htxn->p = txn->p; |
Christopher Faulet | 256b69a | 2019-05-23 11:14:21 +0200 | [diff] [blame] | 5522 | htxn->dir = txn->dir; |
| 5523 | htxn->flags = txn->flags; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5524 | |
| 5525 | /* Pop a class stream metatable and affect it to the table. */ |
| 5526 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 5527 | lua_setmetatable(L, -2); |
| 5528 | |
| 5529 | return 1; |
| 5530 | } |
| 5531 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 5532 | /* This function creates and returns an array containing the status-line |
| 5533 | * elements. This function does not fails. |
| 5534 | */ |
| 5535 | __LJMP static int hlua_http_get_stline(lua_State *L, struct htx_sl *sl) |
| 5536 | { |
| 5537 | /* Create the table. */ |
| 5538 | lua_newtable(L); |
| 5539 | |
| 5540 | if (sl->flags & HTX_SL_F_IS_RESP) { |
| 5541 | lua_pushstring(L, "version"); |
| 5542 | lua_pushlstring(L, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); |
| 5543 | lua_settable(L, -3); |
| 5544 | lua_pushstring(L, "code"); |
| 5545 | lua_pushlstring(L, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl)); |
| 5546 | lua_settable(L, -3); |
| 5547 | lua_pushstring(L, "reason"); |
| 5548 | lua_pushlstring(L, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl)); |
| 5549 | lua_settable(L, -3); |
| 5550 | } |
| 5551 | else { |
| 5552 | lua_pushstring(L, "method"); |
| 5553 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 5554 | lua_settable(L, -3); |
| 5555 | lua_pushstring(L, "uri"); |
| 5556 | lua_pushlstring(L, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl)); |
| 5557 | lua_settable(L, -3); |
| 5558 | lua_pushstring(L, "version"); |
| 5559 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 5560 | lua_settable(L, -3); |
| 5561 | } |
| 5562 | return 1; |
| 5563 | } |
| 5564 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5565 | /* This function creates ans returns an array of HTTP headers. |
| 5566 | * This function does not fails. It is used as wrapper with the |
| 5567 | * 2 following functions. |
| 5568 | */ |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5569 | __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] | 5570 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5571 | struct htx *htx; |
| 5572 | int32_t pos; |
| 5573 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5574 | /* Create the table. */ |
| 5575 | lua_newtable(L); |
| 5576 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5577 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5578 | htx = htxbuf(&msg->chn->buf); |
| 5579 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 5580 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 5581 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5582 | struct ist n, v; |
| 5583 | int len; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5584 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5585 | if (type == HTX_BLK_HDR) { |
| 5586 | n = htx_get_blk_name(htx,blk); |
| 5587 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5588 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5589 | else if (type == HTX_BLK_EOH) |
| 5590 | break; |
| 5591 | else |
| 5592 | continue; |
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 | /* Check for existing entry: |
| 5595 | * assume that the table is on the top of the stack, and |
| 5596 | * push the key in the stack, the function lua_gettable() |
| 5597 | * perform the lookup. |
| 5598 | */ |
| 5599 | lua_pushlstring(L, n.ptr, n.len); |
| 5600 | lua_gettable(L, -2); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5601 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5602 | switch (lua_type(L, -1)) { |
| 5603 | case LUA_TNIL: |
| 5604 | /* Table not found, create it. */ |
| 5605 | lua_pop(L, 1); /* remove the nil value. */ |
| 5606 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 5607 | lua_newtable(L); /* create and push empty table. */ |
| 5608 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5609 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 5610 | 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] | 5611 | break; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5612 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5613 | case LUA_TTABLE: |
| 5614 | /* Entry found: push the value in the table. */ |
| 5615 | len = lua_rawlen(L, -1); |
| 5616 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5617 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 5618 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 5619 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5620 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5621 | default: |
| 5622 | /* Other cases are errors. */ |
| 5623 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 5624 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5625 | } |
| 5626 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5627 | return 1; |
| 5628 | } |
| 5629 | |
| 5630 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 5631 | { |
| 5632 | struct hlua_txn *htxn; |
| 5633 | |
| 5634 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 5635 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5636 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5637 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5638 | WILL_LJMP(lua_error(L)); |
| 5639 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5640 | return hlua_http_get_headers(L, &htxn->s->txn->req); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5641 | } |
| 5642 | |
| 5643 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 5644 | { |
| 5645 | struct hlua_txn *htxn; |
| 5646 | |
| 5647 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 5648 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5649 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5650 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5651 | WILL_LJMP(lua_error(L)); |
| 5652 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5653 | return hlua_http_get_headers(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5654 | } |
| 5655 | |
| 5656 | /* This function replace full header, or just a value in |
| 5657 | * the request or in the response. It is a wrapper fir the |
| 5658 | * 4 following functions. |
| 5659 | */ |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5660 | __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] | 5661 | { |
| 5662 | size_t name_len; |
| 5663 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5664 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5665 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5666 | struct htx *htx; |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5667 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5668 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5669 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5670 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 5671 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5672 | htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5673 | 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] | 5674 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5675 | return 0; |
| 5676 | } |
| 5677 | |
| 5678 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 5679 | { |
| 5680 | struct hlua_txn *htxn; |
| 5681 | |
| 5682 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5683 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5684 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5685 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5686 | WILL_LJMP(lua_error(L)); |
| 5687 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5688 | 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] | 5689 | } |
| 5690 | |
| 5691 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 5692 | { |
| 5693 | struct hlua_txn *htxn; |
| 5694 | |
| 5695 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 5696 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5697 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5698 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5699 | WILL_LJMP(lua_error(L)); |
| 5700 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5701 | 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] | 5702 | } |
| 5703 | |
| 5704 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 5705 | { |
| 5706 | struct hlua_txn *htxn; |
| 5707 | |
| 5708 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5709 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5710 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5711 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5712 | WILL_LJMP(lua_error(L)); |
| 5713 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5714 | 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] | 5715 | } |
| 5716 | |
| 5717 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 5718 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5719 | struct hlua_txn *htxn; |
| 5720 | |
| 5721 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 5722 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5723 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5724 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5725 | WILL_LJMP(lua_error(L)); |
| 5726 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5727 | 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] | 5728 | } |
| 5729 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5730 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5731 | * It is a wrapper for the 2 following functions. |
| 5732 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5733 | __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] | 5734 | { |
| 5735 | size_t len; |
| 5736 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5737 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5738 | struct http_hdr_ctx ctx; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5739 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5740 | ctx.blk = NULL; |
| 5741 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 5742 | http_remove_header(htx, &ctx); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5743 | return 0; |
| 5744 | } |
| 5745 | |
| 5746 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 5747 | { |
| 5748 | struct hlua_txn *htxn; |
| 5749 | |
| 5750 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 5751 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5752 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5753 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5754 | WILL_LJMP(lua_error(L)); |
| 5755 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5756 | return hlua_http_del_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5757 | } |
| 5758 | |
| 5759 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 5760 | { |
| 5761 | struct hlua_txn *htxn; |
| 5762 | |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5763 | MAY_LJMP(check_args(L, 2, "res_del_hdr")); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5764 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5765 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5766 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5767 | WILL_LJMP(lua_error(L)); |
| 5768 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5769 | return hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5770 | } |
| 5771 | |
| 5772 | /* This function adds an header. It is a wrapper used by |
| 5773 | * the 2 following functions. |
| 5774 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5775 | __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] | 5776 | { |
| 5777 | size_t name_len; |
| 5778 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5779 | size_t value_len; |
| 5780 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5781 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5782 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5783 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 5784 | ist2(value, value_len))); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5785 | return 0; |
| 5786 | } |
| 5787 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 5788 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 5789 | { |
| 5790 | struct hlua_txn *htxn; |
| 5791 | |
| 5792 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 5793 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5794 | |
| 5795 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5796 | WILL_LJMP(lua_error(L)); |
| 5797 | |
| 5798 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
| 5799 | } |
| 5800 | |
| 5801 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 5802 | { |
| 5803 | struct hlua_txn *htxn; |
| 5804 | |
| 5805 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 5806 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5807 | |
| 5808 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
| 5809 | WILL_LJMP(lua_error(L)); |
| 5810 | |
| 5811 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
| 5812 | } |
| 5813 | |
| 5814 | static int hlua_http_req_set_hdr(lua_State *L) |
| 5815 | { |
| 5816 | struct hlua_txn *htxn; |
| 5817 | |
| 5818 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 5819 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5820 | |
| 5821 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5822 | WILL_LJMP(lua_error(L)); |
| 5823 | |
| 5824 | hlua_http_del_hdr(L, &htxn->s->txn->req); |
| 5825 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
| 5826 | } |
| 5827 | |
| 5828 | static int hlua_http_res_set_hdr(lua_State *L) |
| 5829 | { |
| 5830 | struct hlua_txn *htxn; |
| 5831 | |
| 5832 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 5833 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5834 | |
| 5835 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
| 5836 | WILL_LJMP(lua_error(L)); |
| 5837 | |
| 5838 | hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
| 5839 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
| 5840 | } |
| 5841 | |
| 5842 | /* This function set the method. */ |
| 5843 | static int hlua_http_req_set_meth(lua_State *L) |
| 5844 | { |
| 5845 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5846 | size_t name_len; |
| 5847 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5848 | |
| 5849 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5850 | WILL_LJMP(lua_error(L)); |
| 5851 | |
| 5852 | lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1); |
| 5853 | return 1; |
| 5854 | } |
| 5855 | |
| 5856 | /* This function set the method. */ |
| 5857 | static int hlua_http_req_set_path(lua_State *L) |
| 5858 | { |
| 5859 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5860 | size_t name_len; |
| 5861 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5862 | |
| 5863 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5864 | WILL_LJMP(lua_error(L)); |
| 5865 | |
| 5866 | lua_pushboolean(L, http_req_replace_stline(1, name, name_len, htxn->p, htxn->s) != -1); |
| 5867 | return 1; |
| 5868 | } |
| 5869 | |
| 5870 | /* This function set the query-string. */ |
| 5871 | static int hlua_http_req_set_query(lua_State *L) |
| 5872 | { |
| 5873 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5874 | size_t name_len; |
| 5875 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5876 | |
| 5877 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5878 | WILL_LJMP(lua_error(L)); |
| 5879 | |
| 5880 | /* Check length. */ |
| 5881 | if (name_len > trash.size - 1) { |
| 5882 | lua_pushboolean(L, 0); |
| 5883 | return 1; |
| 5884 | } |
| 5885 | |
| 5886 | /* Add the mark question as prefix. */ |
| 5887 | chunk_reset(&trash); |
| 5888 | trash.area[trash.data++] = '?'; |
| 5889 | memcpy(trash.area + trash.data, name, name_len); |
| 5890 | trash.data += name_len; |
| 5891 | |
| 5892 | lua_pushboolean(L, |
| 5893 | http_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1); |
| 5894 | return 1; |
| 5895 | } |
| 5896 | |
| 5897 | /* This function set the uri. */ |
| 5898 | static int hlua_http_req_set_uri(lua_State *L) |
| 5899 | { |
| 5900 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5901 | size_t name_len; |
| 5902 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5903 | |
| 5904 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5905 | WILL_LJMP(lua_error(L)); |
| 5906 | |
| 5907 | lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1); |
| 5908 | return 1; |
| 5909 | } |
| 5910 | |
| 5911 | /* This function set the response code & optionally reason. */ |
| 5912 | static int hlua_http_res_set_status(lua_State *L) |
| 5913 | { |
| 5914 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5915 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5916 | const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 5917 | const struct ist reason = ist2(str, (str ? strlen(str) : 0)); |
| 5918 | |
| 5919 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
| 5920 | WILL_LJMP(lua_error(L)); |
| 5921 | |
| 5922 | http_res_set_status(code, reason, htxn->s); |
| 5923 | return 0; |
| 5924 | } |
| 5925 | |
| 5926 | /* |
| 5927 | * |
| 5928 | * |
| 5929 | * Class HTTPMessage |
| 5930 | * |
| 5931 | * |
| 5932 | */ |
| 5933 | |
| 5934 | /* Returns a struct http_msg if the stack entry "ud" is a class HTTPMessage, |
| 5935 | * otherwise it throws an error. |
| 5936 | */ |
| 5937 | __LJMP static struct http_msg *hlua_checkhttpmsg(lua_State *L, int ud) |
| 5938 | { |
| 5939 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_msg_ref)); |
| 5940 | } |
| 5941 | |
| 5942 | /* Creates and pushes on the stack a HTTP object according with a current TXN. |
| 5943 | */ |
Christopher Faulet | 78c3547 | 2020-02-26 17:14:08 +0100 | [diff] [blame] | 5944 | 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] | 5945 | { |
| 5946 | /* Check stack size. */ |
| 5947 | if (!lua_checkstack(L, 3)) |
| 5948 | return 0; |
| 5949 | |
| 5950 | lua_newtable(L); |
| 5951 | lua_pushlightuserdata(L, msg); |
| 5952 | lua_rawseti(L, -2, 0); |
| 5953 | |
| 5954 | /* Create the "channel" field that contains the request channel object. */ |
| 5955 | lua_pushstring(L, "channel"); |
| 5956 | if (!hlua_channel_new(L, msg->chn)) |
| 5957 | return 0; |
| 5958 | lua_rawset(L, -3); |
| 5959 | |
| 5960 | /* Pop a class stream metatable and affect it to the table. */ |
| 5961 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_msg_ref); |
| 5962 | lua_setmetatable(L, -2); |
| 5963 | |
| 5964 | return 1; |
| 5965 | } |
| 5966 | |
| 5967 | /* Helper function returning a filter attached to the HTTP message at the |
| 5968 | * 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] | 5969 | * 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] | 5970 | * filled with output and input length respectively. |
| 5971 | */ |
| 5972 | static struct filter *hlua_http_msg_filter(lua_State *L, int ud, struct http_msg *msg, size_t *offset, size_t *len) |
| 5973 | { |
| 5974 | struct channel *chn = msg->chn; |
| 5975 | struct htx *htx = htxbuf(&chn->buf); |
| 5976 | struct filter *filter = NULL; |
| 5977 | |
| 5978 | *offset = co_data(msg->chn); |
| 5979 | *len = htx->data - co_data(msg->chn); |
| 5980 | |
| 5981 | if (lua_getfield(L, ud, "__filter") == LUA_TLIGHTUSERDATA) { |
| 5982 | filter = lua_touserdata (L, -1); |
| 5983 | if (msg->msg_state >= HTTP_MSG_DATA) { |
| 5984 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 5985 | |
| 5986 | *offset = flt_ctx->cur_off[CHN_IDX(chn)]; |
| 5987 | *len = flt_ctx->cur_len[CHN_IDX(chn)]; |
| 5988 | } |
| 5989 | } |
| 5990 | |
| 5991 | lua_pop(L, 1); |
| 5992 | return filter; |
| 5993 | } |
| 5994 | |
| 5995 | /* Returns true if the channel attached to the HTTP message is the response |
| 5996 | * channel. |
| 5997 | */ |
| 5998 | __LJMP static int hlua_http_msg_is_resp(lua_State *L) |
| 5999 | { |
| 6000 | struct http_msg *msg; |
| 6001 | |
| 6002 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 6003 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6004 | |
| 6005 | lua_pushboolean(L, !!(msg->chn->flags & CF_ISRESP)); |
| 6006 | return 1; |
| 6007 | } |
| 6008 | |
| 6009 | /* Returns an array containing the elements status-line of the HTTP message. It relies |
| 6010 | * on hlua_http_get_stline(). |
| 6011 | */ |
| 6012 | __LJMP static int hlua_http_msg_get_stline(lua_State *L) |
| 6013 | { |
| 6014 | struct http_msg *msg; |
| 6015 | struct htx *htx; |
| 6016 | struct htx_sl *sl; |
| 6017 | |
| 6018 | MAY_LJMP(check_args(L, 1, "get_stline")); |
| 6019 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6020 | |
| 6021 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6022 | WILL_LJMP(lua_error(L)); |
| 6023 | |
| 6024 | htx = htxbuf(&msg->chn->buf); |
| 6025 | sl = http_get_stline(htx); |
| 6026 | if (!sl) |
| 6027 | return 0; |
| 6028 | return hlua_http_get_stline(L, sl); |
| 6029 | } |
| 6030 | |
| 6031 | /* Returns an array containing all headers of the HTTP message. it relies on |
| 6032 | * hlua_http_get_headers(). |
| 6033 | */ |
| 6034 | __LJMP static int hlua_http_msg_get_headers(lua_State *L) |
| 6035 | { |
| 6036 | struct http_msg *msg; |
| 6037 | |
| 6038 | MAY_LJMP(check_args(L, 1, "get_headers")); |
| 6039 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6040 | |
| 6041 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6042 | WILL_LJMP(lua_error(L)); |
| 6043 | |
| 6044 | return hlua_http_get_headers(L, msg); |
| 6045 | } |
| 6046 | |
| 6047 | /* Deletes all occurrences of an header in the HTTP message matching on its |
| 6048 | * name. It relies on hlua_http_del_hdr(). |
| 6049 | */ |
| 6050 | __LJMP static int hlua_http_msg_del_hdr(lua_State *L) |
| 6051 | { |
| 6052 | struct http_msg *msg; |
| 6053 | |
| 6054 | MAY_LJMP(check_args(L, 2, "del_header")); |
| 6055 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6056 | |
| 6057 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6058 | WILL_LJMP(lua_error(L)); |
| 6059 | |
| 6060 | return hlua_http_del_hdr(L, msg); |
| 6061 | } |
| 6062 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6063 | /* 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] | 6064 | * message given its name against a regex and replaces it if it matches. It |
| 6065 | * relies on hlua_http_rep_hdr(). |
| 6066 | */ |
| 6067 | __LJMP static int hlua_http_msg_rep_hdr(lua_State *L) |
| 6068 | { |
| 6069 | struct http_msg *msg; |
| 6070 | |
| 6071 | MAY_LJMP(check_args(L, 4, "rep_header")); |
| 6072 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6073 | |
| 6074 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6075 | WILL_LJMP(lua_error(L)); |
| 6076 | |
| 6077 | return hlua_http_rep_hdr(L, msg, 1); |
| 6078 | } |
| 6079 | |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 6080 | /* 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] | 6081 | * message given its name against a regex and replaces it if it matches. It |
| 6082 | * relies on hlua_http_rep_hdr(). |
| 6083 | */ |
| 6084 | __LJMP static int hlua_http_msg_rep_val(lua_State *L) |
| 6085 | { |
| 6086 | struct http_msg *msg; |
| 6087 | |
| 6088 | MAY_LJMP(check_args(L, 4, "rep_value")); |
| 6089 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6090 | |
| 6091 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6092 | WILL_LJMP(lua_error(L)); |
| 6093 | |
| 6094 | return hlua_http_rep_hdr(L, msg, 0); |
| 6095 | } |
| 6096 | |
| 6097 | /* Add an header in the HTTP message. It relies on hlua_http_add_hdr() */ |
| 6098 | __LJMP static int hlua_http_msg_add_hdr(lua_State *L) |
| 6099 | { |
| 6100 | struct http_msg *msg; |
| 6101 | |
| 6102 | MAY_LJMP(check_args(L, 3, "add_header")); |
| 6103 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6104 | |
| 6105 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6106 | WILL_LJMP(lua_error(L)); |
| 6107 | |
| 6108 | return hlua_http_add_hdr(L, msg); |
| 6109 | } |
| 6110 | |
| 6111 | /* Add an header in the HTTP message removing existing headers with the same |
| 6112 | * name. It relies on hlua_http_del_hdr() and hlua_http_add_hdr(). |
| 6113 | */ |
| 6114 | __LJMP static int hlua_http_msg_set_hdr(lua_State *L) |
| 6115 | { |
| 6116 | struct http_msg *msg; |
| 6117 | |
| 6118 | MAY_LJMP(check_args(L, 3, "set_header")); |
| 6119 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6120 | |
| 6121 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6122 | WILL_LJMP(lua_error(L)); |
| 6123 | |
| 6124 | hlua_http_del_hdr(L, msg); |
| 6125 | return hlua_http_add_hdr(L, msg); |
| 6126 | } |
| 6127 | |
| 6128 | /* Rewrites the request method. It relies on http_req_replace_stline(). */ |
| 6129 | __LJMP static int hlua_http_msg_set_meth(lua_State *L) |
| 6130 | { |
| 6131 | struct stream *s; |
| 6132 | struct http_msg *msg; |
| 6133 | const char *name; |
| 6134 | size_t name_len; |
| 6135 | |
| 6136 | MAY_LJMP(check_args(L, 2, "set_method")); |
| 6137 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6138 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6139 | |
| 6140 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6141 | WILL_LJMP(lua_error(L)); |
| 6142 | |
| 6143 | s = chn_strm(msg->chn); |
| 6144 | lua_pushboolean(L, http_req_replace_stline(0, name, name_len, s->be, s) != -1); |
| 6145 | return 1; |
| 6146 | } |
| 6147 | |
| 6148 | /* Rewrites the request path. It relies on http_req_replace_stline(). */ |
| 6149 | __LJMP static int hlua_http_msg_set_path(lua_State *L) |
| 6150 | { |
| 6151 | struct stream *s; |
| 6152 | struct http_msg *msg; |
| 6153 | const char *name; |
| 6154 | size_t name_len; |
| 6155 | |
| 6156 | MAY_LJMP(check_args(L, 2, "set_path")); |
| 6157 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6158 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6159 | |
| 6160 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6161 | WILL_LJMP(lua_error(L)); |
| 6162 | |
| 6163 | s = chn_strm(msg->chn); |
| 6164 | lua_pushboolean(L, http_req_replace_stline(1, name, name_len, s->be, s) != -1); |
| 6165 | return 1; |
| 6166 | } |
| 6167 | |
| 6168 | /* Rewrites the request query-string. It relies on http_req_replace_stline(). */ |
| 6169 | __LJMP static int hlua_http_msg_set_query(lua_State *L) |
| 6170 | { |
| 6171 | struct stream *s; |
| 6172 | struct http_msg *msg; |
| 6173 | const char *name; |
| 6174 | size_t name_len; |
| 6175 | |
| 6176 | MAY_LJMP(check_args(L, 2, "set_query")); |
| 6177 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6178 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6179 | |
| 6180 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6181 | WILL_LJMP(lua_error(L)); |
| 6182 | |
| 6183 | /* Check length. */ |
| 6184 | if (name_len > trash.size - 1) { |
| 6185 | lua_pushboolean(L, 0); |
| 6186 | return 1; |
| 6187 | } |
| 6188 | |
| 6189 | /* Add the mark question as prefix. */ |
| 6190 | chunk_reset(&trash); |
| 6191 | trash.area[trash.data++] = '?'; |
| 6192 | memcpy(trash.area + trash.data, name, name_len); |
| 6193 | trash.data += name_len; |
| 6194 | |
| 6195 | s = chn_strm(msg->chn); |
| 6196 | lua_pushboolean(L, http_req_replace_stline(2, trash.area, trash.data, s->be, s) != -1); |
| 6197 | return 1; |
| 6198 | } |
| 6199 | |
| 6200 | /* Rewrites the request URI. It relies on http_req_replace_stline(). */ |
| 6201 | __LJMP static int hlua_http_msg_set_uri(lua_State *L) |
| 6202 | { |
| 6203 | struct stream *s; |
| 6204 | struct http_msg *msg; |
| 6205 | const char *name; |
| 6206 | size_t name_len; |
| 6207 | |
| 6208 | MAY_LJMP(check_args(L, 2, "set_uri")); |
| 6209 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6210 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6211 | |
| 6212 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6213 | WILL_LJMP(lua_error(L)); |
| 6214 | |
| 6215 | s = chn_strm(msg->chn); |
| 6216 | lua_pushboolean(L, http_req_replace_stline(3, name, name_len, s->be, s) != -1); |
| 6217 | return 1; |
| 6218 | } |
| 6219 | |
| 6220 | /* Rewrites the response status code. It relies on http_res_set_status(). */ |
| 6221 | __LJMP static int hlua_http_msg_set_status(lua_State *L) |
| 6222 | { |
| 6223 | struct http_msg *msg; |
| 6224 | unsigned int code; |
| 6225 | const char *reason; |
| 6226 | size_t reason_len; |
| 6227 | |
| 6228 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6229 | code = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6230 | reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, &reason_len)); |
| 6231 | |
| 6232 | if (!(msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6233 | WILL_LJMP(lua_error(L)); |
| 6234 | |
| 6235 | lua_pushboolean(L, http_res_set_status(code, ist2(reason, reason_len), chn_strm(msg->chn)) != -1); |
| 6236 | return 1; |
| 6237 | } |
| 6238 | |
| 6239 | /* Returns true if the HTTP message is full. */ |
| 6240 | __LJMP static int hlua_http_msg_is_full(lua_State *L) |
| 6241 | { |
| 6242 | struct http_msg *msg; |
| 6243 | |
| 6244 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 6245 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6246 | lua_pushboolean(L, channel_full(msg->chn, 0)); |
| 6247 | return 1; |
| 6248 | } |
| 6249 | |
| 6250 | /* Returns true if the HTTP message may still receive data. */ |
| 6251 | __LJMP static int hlua_http_msg_may_recv(lua_State *L) |
| 6252 | { |
| 6253 | struct http_msg *msg; |
| 6254 | struct htx *htx; |
| 6255 | |
| 6256 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 6257 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6258 | htx = htxbuf(&msg->chn->buf); |
| 6259 | lua_pushboolean(L, (htx_expect_more(htx) && !channel_input_closed(msg->chn) && channel_may_recv(msg->chn))); |
| 6260 | return 1; |
| 6261 | } |
| 6262 | |
| 6263 | /* Returns true if the HTTP message EOM was received */ |
| 6264 | __LJMP static int hlua_http_msg_is_eom(lua_State *L) |
| 6265 | { |
| 6266 | struct http_msg *msg; |
| 6267 | struct htx *htx; |
| 6268 | |
| 6269 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 6270 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6271 | htx = htxbuf(&msg->chn->buf); |
| 6272 | lua_pushboolean(L, !htx_expect_more(htx)); |
| 6273 | return 1; |
| 6274 | } |
| 6275 | |
| 6276 | /* Returns the number of bytes available in the input side of the HTTP |
| 6277 | * message. This function never fails. |
| 6278 | */ |
| 6279 | __LJMP static int hlua_http_msg_get_in_len(lua_State *L) |
| 6280 | { |
| 6281 | struct http_msg *msg; |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6282 | size_t output, input; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6283 | |
| 6284 | MAY_LJMP(check_args(L, 1, "input")); |
| 6285 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6286 | hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6287 | lua_pushinteger(L, input); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6288 | return 1; |
| 6289 | } |
| 6290 | |
| 6291 | /* Returns the number of bytes available in the output side of the HTTP |
| 6292 | * message. This function never fails. |
| 6293 | */ |
| 6294 | __LJMP static int hlua_http_msg_get_out_len(lua_State *L) |
| 6295 | { |
| 6296 | struct http_msg *msg; |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6297 | size_t output, input; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6298 | |
| 6299 | MAY_LJMP(check_args(L, 1, "output")); |
| 6300 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6301 | hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6302 | lua_pushinteger(L, output); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6303 | return 1; |
| 6304 | } |
| 6305 | |
| 6306 | /* Copies at most <len> bytes of DATA blocks from the HTTP message <msg> |
| 6307 | * 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] | 6308 | * 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] | 6309 | * block. This function is called during the payload filtering, so the headers |
| 6310 | * are already scheduled for output (from the filter point of view). |
| 6311 | */ |
| 6312 | static int _hlua_http_msg_dup(struct http_msg *msg, lua_State *L, size_t offset, size_t len) |
| 6313 | { |
| 6314 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 6315 | struct htx_blk *blk; |
| 6316 | struct htx_ret htxret; |
| 6317 | luaL_Buffer b; |
| 6318 | int ret = 0; |
| 6319 | |
| 6320 | luaL_buffinit(L, &b); |
| 6321 | htxret = htx_find_offset(htx, offset); |
| 6322 | for (blk = htxret.blk, offset = htxret.ret; blk && len; blk = htx_get_next_blk(htx, blk)) { |
| 6323 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 6324 | struct ist v; |
| 6325 | |
| 6326 | switch (type) { |
| 6327 | case HTX_BLK_UNUSED: |
| 6328 | break; |
| 6329 | |
| 6330 | case HTX_BLK_DATA: |
| 6331 | v = htx_get_blk_value(htx, blk); |
Tim Duesterhus | b113b5c | 2021-09-15 13:58:44 +0200 | [diff] [blame] | 6332 | v = istadv(v, offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6333 | if (v.len > len) |
| 6334 | v.len = len; |
| 6335 | |
| 6336 | luaL_addlstring(&b, v.ptr, v.len); |
| 6337 | ret += v.len; |
| 6338 | break; |
| 6339 | |
| 6340 | default: |
| 6341 | if (!ret) { |
| 6342 | /* Remove the empty string and push nil on the stack */ |
| 6343 | lua_pop(L, 1); |
| 6344 | lua_pushnil(L); |
| 6345 | } |
| 6346 | goto end; |
| 6347 | } |
| 6348 | offset = 0; |
| 6349 | } |
| 6350 | |
| 6351 | luaL_pushresult(&b); |
| 6352 | |
| 6353 | end: |
| 6354 | return ret; |
| 6355 | } |
| 6356 | |
| 6357 | /* Copies the string <str> to the HTTP message <msg> at the offset |
| 6358 | * <offset>. This function returns -1 if data cannot be copied. Otherwise, it |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6359 | * returns the amount of data written. This function is responsible to update |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6360 | * the filter context. |
| 6361 | */ |
| 6362 | static int _hlua_http_msg_insert(struct http_msg *msg, struct filter *filter, struct ist str, size_t offset) |
| 6363 | { |
| 6364 | struct htx *htx = htx_from_buf(&msg->chn->buf); |
| 6365 | struct htx_ret htxret; |
| 6366 | int /*max, */ret = 0; |
| 6367 | |
| 6368 | /* Nothing to do, just return */ |
| 6369 | if (unlikely(istlen(str) == 0)) |
| 6370 | goto end; |
| 6371 | |
| 6372 | if (istlen(str) > htx_free_data_space(htx)) { |
| 6373 | ret = -1; |
| 6374 | goto end; |
| 6375 | } |
| 6376 | |
| 6377 | htxret = htx_find_offset(htx, offset); |
| 6378 | if (!htxret.blk || htx_get_blk_type(htxret.blk) != HTX_BLK_DATA) { |
| 6379 | if (!htx_add_last_data(htx, str)) |
| 6380 | goto end; |
| 6381 | } |
| 6382 | else { |
| 6383 | struct ist v = htx_get_blk_value(htx, htxret.blk); |
| 6384 | v.ptr += htxret.ret; |
| 6385 | v.len = 0; |
| 6386 | if (!htx_replace_blk_value(htx, htxret.blk, v, str)) |
| 6387 | goto end; |
| 6388 | } |
| 6389 | ret = str.len; |
| 6390 | if (ret) { |
| 6391 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6392 | flt_update_offsets(filter, msg->chn, ret); |
| 6393 | flt_ctx->cur_len[CHN_IDX(msg->chn)] += ret; |
| 6394 | } |
| 6395 | |
| 6396 | end: |
| 6397 | htx_to_buf(htx, &msg->chn->buf); |
| 6398 | return ret; |
| 6399 | } |
| 6400 | |
| 6401 | /* Helper function removing at most <len> bytes of DATA blocks at the absolute |
| 6402 | * position <offset>. It stops on the first non-DATA HTX block. This function is |
| 6403 | * called during the payload filtering, so the headers are already scheduled for |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6404 | * output (from the filter point of view). This function is responsible to |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6405 | * update the filter context. |
| 6406 | */ |
| 6407 | static void _hlua_http_msg_delete(struct http_msg *msg, struct filter *filter, size_t offset, size_t len) |
| 6408 | { |
| 6409 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6410 | struct htx *htx = htx_from_buf(&msg->chn->buf); |
| 6411 | struct htx_blk *blk; |
| 6412 | struct htx_ret htxret; |
| 6413 | size_t ret = 0; |
| 6414 | |
| 6415 | /* Be sure <len> is always the amount of DATA to remove */ |
| 6416 | if (htx->data == offset+len && htx_get_tail_type(htx) == HTX_BLK_DATA) { |
| 6417 | htx_truncate(htx, offset); |
| 6418 | ret = len; |
| 6419 | goto end; |
| 6420 | } |
| 6421 | |
| 6422 | htxret = htx_find_offset(htx, offset); |
| 6423 | blk = htxret.blk; |
| 6424 | if (htxret.ret) { |
| 6425 | struct ist v; |
| 6426 | |
| 6427 | if (htx_get_blk_type(blk) != HTX_BLK_DATA) |
| 6428 | goto end; |
| 6429 | v = htx_get_blk_value(htx, blk); |
| 6430 | v.ptr += htxret.ret; |
| 6431 | if (v.len > len) |
| 6432 | v.len = len; |
Tim Duesterhus | b113b5c | 2021-09-15 13:58:44 +0200 | [diff] [blame] | 6433 | blk = htx_replace_blk_value(htx, blk, v, IST_NULL); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6434 | len -= v.len; |
| 6435 | ret += v.len; |
| 6436 | } |
| 6437 | |
| 6438 | |
| 6439 | while (blk && len) { |
| 6440 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 6441 | uint32_t sz = htx_get_blksz(blk); |
| 6442 | |
| 6443 | switch (type) { |
| 6444 | case HTX_BLK_UNUSED: |
| 6445 | break; |
| 6446 | |
| 6447 | case HTX_BLK_DATA: |
| 6448 | if (len < sz) { |
| 6449 | htx_cut_data_blk(htx, blk, len); |
| 6450 | ret += len; |
| 6451 | goto end; |
| 6452 | } |
| 6453 | break; |
| 6454 | |
| 6455 | default: |
| 6456 | goto end; |
| 6457 | } |
| 6458 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6459 | /* Remove all the data block */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6460 | len -= sz; |
| 6461 | ret += sz; |
| 6462 | blk = htx_remove_blk(htx, blk); |
| 6463 | } |
| 6464 | |
| 6465 | end: |
| 6466 | flt_update_offsets(filter, msg->chn, -ret); |
| 6467 | flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret; |
| 6468 | /* WARNING: we don't call htx_to_buf() on purpose, because we don't want |
| 6469 | * to loose the EOM flag if the message is empty. |
| 6470 | */ |
| 6471 | } |
| 6472 | |
| 6473 | /* Copies input data found in an HTTP message. Unlike the channel function used |
| 6474 | * to duplicate raw data, this one can only be called inside a filter, from |
| 6475 | * http_payload callback. So it cannot yield. An exception is returned if it is |
| 6476 | * called from another callback. If nothing was copied, a nil value is pushed on |
| 6477 | * the stack. |
| 6478 | */ |
| 6479 | __LJMP static int hlua_http_msg_get_body(lua_State *L) |
| 6480 | { |
| 6481 | struct http_msg *msg; |
| 6482 | struct filter *filter; |
| 6483 | size_t output, input; |
| 6484 | int offset, len; |
| 6485 | |
| 6486 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 6487 | WILL_LJMP(luaL_error(L, "'data' expects at most 2 arguments")); |
| 6488 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6489 | |
| 6490 | if (msg->msg_state < HTTP_MSG_DATA) |
| 6491 | WILL_LJMP(lua_error(L)); |
| 6492 | |
| 6493 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6494 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6495 | WILL_LJMP(lua_error(L)); |
| 6496 | |
| 6497 | if (!ci_data(msg->chn) && channel_input_closed(msg->chn)) { |
| 6498 | lua_pushnil(L); |
| 6499 | return 1; |
| 6500 | } |
| 6501 | |
| 6502 | offset = output; |
| 6503 | if (lua_gettop(L) > 1) { |
| 6504 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6505 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6506 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6507 | offset += output; |
| 6508 | if (offset < output || offset > input + output) { |
| 6509 | lua_pushfstring(L, "offset out of range."); |
| 6510 | WILL_LJMP(lua_error(L)); |
| 6511 | } |
| 6512 | } |
| 6513 | len = output + input - offset; |
| 6514 | if (lua_gettop(L) == 3) { |
| 6515 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6516 | if (!len) |
| 6517 | goto dup; |
| 6518 | if (len == -1) |
| 6519 | len = global.tune.bufsize; |
| 6520 | if (len < 0) { |
| 6521 | lua_pushfstring(L, "length out of range."); |
| 6522 | WILL_LJMP(lua_error(L)); |
| 6523 | } |
| 6524 | } |
| 6525 | |
| 6526 | dup: |
| 6527 | _hlua_http_msg_dup(msg, L, offset, len); |
| 6528 | return 1; |
| 6529 | } |
| 6530 | |
| 6531 | /* Appends a string to the HTTP message, after all existing DATA blocks but |
| 6532 | * before the trailers, if any. It returns the amount of data written or -1 if |
| 6533 | * nothing was copied. Unlike the channel function used to append data, this one |
| 6534 | * can only be called inside a filter, from http_payload callback. So it cannot |
| 6535 | * yield. An exception is returned if it is called from another callback. |
| 6536 | */ |
| 6537 | __LJMP static int hlua_http_msg_append(lua_State *L) |
| 6538 | { |
| 6539 | struct http_msg *msg; |
| 6540 | struct filter *filter; |
| 6541 | const char *str; |
| 6542 | size_t offset, len, sz; |
| 6543 | int ret; |
| 6544 | |
| 6545 | MAY_LJMP(check_args(L, 2, "append")); |
| 6546 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6547 | |
| 6548 | if (msg->msg_state < HTTP_MSG_DATA) |
| 6549 | WILL_LJMP(lua_error(L)); |
| 6550 | |
| 6551 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6552 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6553 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6554 | WILL_LJMP(lua_error(L)); |
| 6555 | |
| 6556 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset+len); |
| 6557 | lua_pushinteger(L, ret); |
| 6558 | return 1; |
| 6559 | } |
| 6560 | |
| 6561 | /* Prepends a string to the HTTP message, before all existing DATA blocks. It |
| 6562 | * returns the amount of data written or -1 if nothing was copied. Unlike the |
| 6563 | * channel function used to prepend data, this one can only be called inside a |
| 6564 | * filter, from http_payload callback. So it cannot yield. An exception is |
| 6565 | * returned if it is called from another callback. |
| 6566 | */ |
| 6567 | __LJMP static int hlua_http_msg_prepend(lua_State *L) |
| 6568 | { |
| 6569 | struct http_msg *msg; |
| 6570 | struct filter *filter; |
| 6571 | const char *str; |
| 6572 | size_t offset, len, sz; |
| 6573 | int ret; |
| 6574 | |
| 6575 | MAY_LJMP(check_args(L, 2, "prepend")); |
| 6576 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6577 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6578 | if (msg->msg_state < HTTP_MSG_DATA) |
| 6579 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6580 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6581 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6582 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6583 | if (!filter || !hlua_filter_from_payload(filter)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6584 | WILL_LJMP(lua_error(L)); |
| 6585 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6586 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6587 | lua_pushinteger(L, ret); |
| 6588 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6589 | } |
| 6590 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6591 | /* Inserts a string to the HTTP message at a given offset. By default the string |
| 6592 | * is appended at the end of DATA blocks. It returns the amount of data written |
| 6593 | * or -1 if nothing was copied. Unlike the channel function used to insert data, |
| 6594 | * this one can only be called inside a filter, from http_payload callback. So |
| 6595 | * it cannot yield. An exception is returned if it is called from another |
| 6596 | * callback. |
| 6597 | */ |
| 6598 | __LJMP static int hlua_http_msg_insert_data(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6599 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6600 | struct http_msg *msg; |
| 6601 | struct filter *filter; |
| 6602 | const char *str; |
| 6603 | size_t input, output, sz; |
| 6604 | int offset; |
| 6605 | int ret; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6606 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6607 | if (lua_gettop(L) < 2 || lua_gettop(L) > 3) |
| 6608 | WILL_LJMP(luaL_error(L, "'insert' expects at least 1 argument and at most 2 arguments")); |
| 6609 | MAY_LJMP(check_args(L, 2, "insert")); |
| 6610 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6611 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6612 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6613 | WILL_LJMP(lua_error(L)); |
| 6614 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6615 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6616 | filter = hlua_http_msg_filter(L, 1, msg, &input, &output); |
| 6617 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6618 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6619 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6620 | offset = input + output; |
| 6621 | if (lua_gettop(L) > 2) { |
| 6622 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6623 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6624 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6625 | offset += output; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6626 | if (offset < output || offset > output + input) { |
| 6627 | lua_pushfstring(L, "offset out of range."); |
| 6628 | WILL_LJMP(lua_error(L)); |
| 6629 | } |
| 6630 | } |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6631 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6632 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6633 | lua_pushinteger(L, ret); |
| 6634 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6635 | } |
| 6636 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6637 | /* Removes a given amount of data from the HTTP message at a given offset. By |
| 6638 | * default all DATA blocks are removed. It returns the amount of data |
| 6639 | * removed. Unlike the channel function used to remove data, this one can only |
| 6640 | * be called inside a filter, from http_payload callback. So it cannot yield. An |
| 6641 | * exception is returned if it is called from another callback. |
| 6642 | */ |
| 6643 | __LJMP static int hlua_http_msg_del_data(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6644 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6645 | struct http_msg *msg; |
| 6646 | struct filter *filter; |
| 6647 | size_t input, output; |
| 6648 | int offset, len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6649 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6650 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 6651 | WILL_LJMP(luaL_error(L, "'insert' expects at most 2 arguments")); |
| 6652 | MAY_LJMP(check_args(L, 2, "insert")); |
| 6653 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6654 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6655 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6656 | WILL_LJMP(lua_error(L)); |
| 6657 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6658 | filter = hlua_http_msg_filter(L, 1, msg, &input, &output); |
| 6659 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6660 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6661 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6662 | offset = input + output; |
| 6663 | if (lua_gettop(L) > 2) { |
| 6664 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6665 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6666 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6667 | offset += output; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6668 | if (offset < output || offset > output + input) { |
| 6669 | lua_pushfstring(L, "offset out of range."); |
| 6670 | WILL_LJMP(lua_error(L)); |
| 6671 | } |
| 6672 | } |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6673 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6674 | len = output + input - offset; |
| 6675 | if (lua_gettop(L) == 4) { |
| 6676 | len = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 6677 | if (!len) |
| 6678 | goto end; |
| 6679 | if (len == -1) |
| 6680 | len = output + input - offset; |
| 6681 | if (len < 0 || offset + len > output + input) { |
| 6682 | lua_pushfstring(L, "length out of range."); |
| 6683 | WILL_LJMP(lua_error(L)); |
| 6684 | } |
| 6685 | } |
| 6686 | |
| 6687 | _hlua_http_msg_delete(msg, filter, offset, len); |
| 6688 | |
| 6689 | end: |
| 6690 | lua_pushinteger(L, len); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6691 | return 1; |
| 6692 | } |
| 6693 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6694 | /* 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] | 6695 | * all remaining data are removed, accordingly to the filter context. It returns |
| 6696 | * the amount of data written or -1 if nothing was copied. Unlike the channel |
| 6697 | * function used to replace data, this one can only be called inside a filter, |
| 6698 | * from http_payload callback. So it cannot yield. An exception is returned if |
| 6699 | * it is called from another callback. |
| 6700 | */ |
| 6701 | __LJMP static int hlua_http_msg_set_data(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6702 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6703 | struct http_msg *msg; |
| 6704 | struct filter *filter; |
| 6705 | struct htx *htx; |
| 6706 | const char *str; |
| 6707 | size_t input, output, sz; |
| 6708 | int offset, len; |
| 6709 | int ret; |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 6710 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6711 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 6712 | WILL_LJMP(luaL_error(L, "'set' expects at least 1 argument and at most 3 arguments")); |
| 6713 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6714 | |
| 6715 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6716 | WILL_LJMP(lua_error(L)); |
| 6717 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6718 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6719 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6720 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6721 | WILL_LJMP(lua_error(L)); |
| 6722 | |
| 6723 | offset = output; |
| 6724 | if (lua_gettop(L) > 2) { |
| 6725 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6726 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6727 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6728 | offset += output; |
| 6729 | if (offset < output || offset > input + output) { |
| 6730 | lua_pushfstring(L, "offset out of range."); |
| 6731 | WILL_LJMP(lua_error(L)); |
| 6732 | } |
| 6733 | } |
| 6734 | |
| 6735 | len = output + input - offset; |
| 6736 | if (lua_gettop(L) == 4) { |
| 6737 | len = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 6738 | if (!len) |
| 6739 | goto set; |
| 6740 | if (len == -1) |
| 6741 | len = output + input - offset; |
| 6742 | if (len < 0 || offset + len > output + input) { |
| 6743 | lua_pushfstring(L, "length out of range."); |
| 6744 | WILL_LJMP(lua_error(L)); |
| 6745 | } |
| 6746 | } |
| 6747 | |
| 6748 | set: |
| 6749 | /* Be sure we can copied the string once input data will be removed. */ |
| 6750 | htx = htx_from_buf(&msg->chn->buf); |
| 6751 | if (sz > htx_free_data_space(htx) + len) |
| 6752 | lua_pushinteger(L, -1); |
| 6753 | else { |
| 6754 | _hlua_http_msg_delete(msg, filter, offset, len); |
| 6755 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6756 | lua_pushinteger(L, ret); |
| 6757 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6758 | return 1; |
| 6759 | } |
| 6760 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6761 | /* Prepends data into an HTTP message and forward it, from the filter point of |
| 6762 | * view. It returns the amount of data written or -1 if nothing was sent. Unlike |
| 6763 | * the channel function used to send data, this one can only be called inside a |
| 6764 | * filter, from http_payload callback. So it cannot yield. An exception is |
| 6765 | * returned if it is called from another callback. |
| 6766 | */ |
| 6767 | __LJMP static int hlua_http_msg_send(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6768 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6769 | struct http_msg *msg; |
| 6770 | struct filter *filter; |
| 6771 | struct htx *htx; |
| 6772 | const char *str; |
| 6773 | size_t offset, len, sz; |
| 6774 | int ret; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6775 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6776 | MAY_LJMP(check_args(L, 2, "send")); |
| 6777 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6778 | |
| 6779 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6780 | WILL_LJMP(lua_error(L)); |
| 6781 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6782 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6783 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6784 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6785 | WILL_LJMP(lua_error(L)); |
| 6786 | |
| 6787 | /* Return an error if the channel's output is closed */ |
| 6788 | if (unlikely(channel_output_closed(msg->chn))) { |
| 6789 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6790 | return 1; |
| 6791 | } |
| 6792 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6793 | htx = htx_from_buf(&msg->chn->buf); |
| 6794 | if (sz > htx_free_data_space(htx)) { |
| 6795 | lua_pushinteger(L, -1); |
| 6796 | return 1; |
| 6797 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6798 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6799 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6800 | if (ret > 0) { |
| 6801 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6802 | |
| 6803 | FLT_OFF(filter, msg->chn) += ret; |
| 6804 | flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret; |
| 6805 | flt_ctx->cur_off[CHN_IDX(msg->chn)] += ret; |
| 6806 | } |
| 6807 | |
| 6808 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6809 | return 1; |
| 6810 | } |
| 6811 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6812 | /* Forwards a given amount of bytes. It return -1 if the channel's output is |
| 6813 | * closed. Otherwise, it returns the number of bytes forwarded. Unlike the |
| 6814 | * channel function used to forward data, this one can only be called inside a |
| 6815 | * filter, from http_payload callback. So it cannot yield. An exception is |
| 6816 | * returned if it is called from another callback. All other functions deal with |
| 6817 | * DATA block, this one not. |
| 6818 | */ |
| 6819 | __LJMP static int hlua_http_msg_forward(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6820 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6821 | struct http_msg *msg; |
| 6822 | struct filter *filter; |
| 6823 | size_t offset, len; |
| 6824 | int fwd, ret = 0; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6825 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6826 | MAY_LJMP(check_args(L, 2, "forward")); |
| 6827 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6828 | |
| 6829 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6830 | WILL_LJMP(lua_error(L)); |
| 6831 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6832 | fwd = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6833 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6834 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6835 | WILL_LJMP(lua_error(L)); |
| 6836 | |
| 6837 | /* Nothing to do, just return */ |
| 6838 | if (!fwd) |
| 6839 | goto end; |
| 6840 | |
| 6841 | /* Return an error if the channel's output is closed */ |
| 6842 | if (unlikely(channel_output_closed(msg->chn))) { |
| 6843 | ret = -1; |
| 6844 | goto end; |
| 6845 | } |
| 6846 | |
| 6847 | ret = fwd; |
| 6848 | if (ret > len) |
| 6849 | ret = len; |
| 6850 | |
| 6851 | if (ret) { |
| 6852 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6853 | |
| 6854 | FLT_OFF(filter, msg->chn) += ret; |
| 6855 | flt_ctx->cur_off[CHN_IDX(msg->chn)] += ret; |
| 6856 | flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret; |
| 6857 | } |
| 6858 | |
| 6859 | end: |
| 6860 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6861 | return 1; |
| 6862 | } |
| 6863 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6864 | /* Set EOM flag on the HTX message. |
| 6865 | * |
| 6866 | * NOTE: Not sure it is a good idea to manipulate this flag but for now I don't |
| 6867 | * really know how to do without this feature. |
| 6868 | */ |
| 6869 | __LJMP static int hlua_http_msg_set_eom(lua_State *L) |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 6870 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6871 | struct http_msg *msg; |
| 6872 | struct htx *htx; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 6873 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6874 | MAY_LJMP(check_args(L, 1, "set_eom")); |
| 6875 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6876 | htx = htxbuf(&msg->chn->buf); |
| 6877 | htx->flags |= HTX_FL_EOM; |
| 6878 | return 0; |
| 6879 | } |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6880 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6881 | /* Unset EOM flag on the HTX message. |
| 6882 | * |
| 6883 | * NOTE: Not sure it is a good idea to manipulate this flag but for now I don't |
| 6884 | * really know how to do without this feature. |
| 6885 | */ |
| 6886 | __LJMP static int hlua_http_msg_unset_eom(lua_State *L) |
| 6887 | { |
| 6888 | struct http_msg *msg; |
| 6889 | struct htx *htx; |
| 6890 | |
| 6891 | MAY_LJMP(check_args(L, 1, "set_eom")); |
| 6892 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6893 | htx = htxbuf(&msg->chn->buf); |
| 6894 | htx->flags &= ~HTX_FL_EOM; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 6895 | return 0; |
| 6896 | } |
| 6897 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6898 | /* |
| 6899 | * |
| 6900 | * |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 6901 | * Class HTTPClient |
| 6902 | * |
| 6903 | * |
| 6904 | */ |
| 6905 | __LJMP static struct hlua_httpclient *hlua_checkhttpclient(lua_State *L, int ud) |
| 6906 | { |
| 6907 | return MAY_LJMP(hlua_checkudata(L, ud, class_httpclient_ref)); |
| 6908 | } |
| 6909 | |
William Lallemand | f77f1de | 2021-09-28 19:10:38 +0200 | [diff] [blame] | 6910 | |
| 6911 | /* stops the httpclient and ask it to kill itself */ |
| 6912 | __LJMP static int hlua_httpclient_gc(lua_State *L) |
| 6913 | { |
| 6914 | struct hlua_httpclient *hlua_hc; |
| 6915 | |
| 6916 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 6917 | |
| 6918 | hlua_hc = MAY_LJMP(hlua_checkhttpclient(L, 1)); |
| 6919 | |
| 6920 | httpclient_stop_and_destroy(hlua_hc->hc); |
| 6921 | |
| 6922 | hlua_hc->hc = NULL; |
| 6923 | |
| 6924 | |
| 6925 | return 0; |
| 6926 | } |
| 6927 | |
| 6928 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 6929 | __LJMP static int hlua_httpclient_new(lua_State *L) |
| 6930 | { |
| 6931 | struct hlua_httpclient *hlua_hc; |
| 6932 | struct hlua *hlua; |
| 6933 | |
| 6934 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 6935 | hlua = hlua_gethlua(L); |
| 6936 | if (!hlua) |
| 6937 | return 0; |
| 6938 | |
| 6939 | /* Check stack size. */ |
| 6940 | if (!lua_checkstack(L, 3)) { |
| 6941 | hlua_pusherror(L, "httpclient: full stack"); |
| 6942 | goto err; |
| 6943 | } |
| 6944 | /* Create the object: obj[0] = userdata. */ |
| 6945 | lua_newtable(L); |
| 6946 | hlua_hc = MAY_LJMP(lua_newuserdata(L, sizeof(*hlua_hc))); |
| 6947 | lua_rawseti(L, -2, 0); |
| 6948 | memset(hlua_hc, 0, sizeof(*hlua_hc)); |
| 6949 | |
| 6950 | hlua_hc->hc = httpclient_new(hlua, 0, IST_NULL); |
| 6951 | if (!hlua_hc->hc) |
| 6952 | goto err; |
| 6953 | |
| 6954 | /* Pop a class stream metatable and affect it to the userdata. */ |
| 6955 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_httpclient_ref); |
| 6956 | lua_setmetatable(L, -2); |
| 6957 | |
| 6958 | return 1; |
| 6959 | |
| 6960 | err: |
| 6961 | WILL_LJMP(lua_error(L)); |
| 6962 | return 0; |
| 6963 | } |
| 6964 | |
| 6965 | |
| 6966 | /* |
| 6967 | * Callback of the httpclient, this callback wakes the lua task up, once the |
| 6968 | * httpclient receives some data |
| 6969 | * |
| 6970 | */ |
| 6971 | |
| 6972 | static void hlua_httpclient_res_cb(struct httpclient *hc) |
| 6973 | { |
| 6974 | struct hlua *hlua = hc->caller; |
| 6975 | |
| 6976 | if (!hlua || !hlua->task) |
| 6977 | return; |
| 6978 | |
| 6979 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 6980 | } |
| 6981 | |
| 6982 | /* |
William Lallemand | d7df73a | 2021-09-23 17:54:00 +0200 | [diff] [blame] | 6983 | * Fill the lua stack with headers from the httpclient response |
| 6984 | * This works the same way as the hlua_http_get_headers() function |
| 6985 | */ |
| 6986 | __LJMP static int hlua_httpclient_get_headers(lua_State *L, struct hlua_httpclient *hlua_hc) |
| 6987 | { |
| 6988 | struct http_hdr *hdr; |
| 6989 | |
| 6990 | lua_newtable(L); |
| 6991 | |
William Lallemand | ef574b2 | 2021-10-05 16:19:31 +0200 | [diff] [blame] | 6992 | for (hdr = hlua_hc->hc->res.hdrs; hdr && isttest(hdr->n); hdr++) { |
William Lallemand | d7df73a | 2021-09-23 17:54:00 +0200 | [diff] [blame] | 6993 | struct ist n, v; |
| 6994 | int len; |
| 6995 | |
| 6996 | n = hdr->n; |
| 6997 | v = hdr->v; |
| 6998 | |
| 6999 | /* Check for existing entry: |
| 7000 | * assume that the table is on the top of the stack, and |
| 7001 | * push the key in the stack, the function lua_gettable() |
| 7002 | * perform the lookup. |
| 7003 | */ |
| 7004 | |
| 7005 | lua_pushlstring(L, n.ptr, n.len); |
| 7006 | lua_gettable(L, -2); |
| 7007 | |
| 7008 | switch (lua_type(L, -1)) { |
| 7009 | case LUA_TNIL: |
| 7010 | /* Table not found, create it. */ |
| 7011 | lua_pop(L, 1); /* remove the nil value. */ |
| 7012 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 7013 | lua_newtable(L); /* create and push empty table. */ |
| 7014 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 7015 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 7016 | lua_rawset(L, -3); /* index new table with header name (pop the values). */ |
| 7017 | break; |
| 7018 | |
| 7019 | case LUA_TTABLE: |
| 7020 | /* Entry found: push the value in the table. */ |
| 7021 | len = lua_rawlen(L, -1); |
| 7022 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 7023 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 7024 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 7025 | break; |
| 7026 | |
| 7027 | default: |
| 7028 | /* Other cases are errors. */ |
| 7029 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 7030 | WILL_LJMP(lua_error(L)); |
| 7031 | } |
| 7032 | } |
| 7033 | return 1; |
| 7034 | } |
| 7035 | |
| 7036 | /* |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7037 | * For each yield, checks if there is some data in the httpclient and push them |
| 7038 | * in the lua buffer, once the httpclient finished its job, push the result on |
| 7039 | * the stack |
| 7040 | */ |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7041 | __LJMP static int hlua_httpclient_send_yield(lua_State *L, int status, lua_KContext ctx) |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7042 | { |
| 7043 | struct buffer *tr; |
| 7044 | int res; |
| 7045 | struct hlua *hlua = hlua_gethlua(L); |
| 7046 | struct hlua_httpclient *hlua_hc = hlua_checkhttpclient(L, 1); |
| 7047 | |
| 7048 | |
| 7049 | tr = get_trash_chunk(); |
| 7050 | |
| 7051 | res = httpclient_res_xfer(hlua_hc->hc, tr); |
| 7052 | luaL_addlstring(&hlua_hc->b, b_orig(tr), res); |
| 7053 | |
| 7054 | if (!httpclient_data(hlua_hc->hc) && httpclient_ended(hlua_hc->hc)) { |
| 7055 | |
| 7056 | luaL_pushresult(&hlua_hc->b); |
| 7057 | lua_settable(L, -3); |
| 7058 | |
| 7059 | lua_pushstring(L, "status"); |
| 7060 | lua_pushinteger(L, hlua_hc->hc->res.status); |
| 7061 | lua_settable(L, -3); |
| 7062 | |
| 7063 | |
| 7064 | lua_pushstring(L, "reason"); |
| 7065 | lua_pushlstring(L, hlua_hc->hc->res.reason.ptr, hlua_hc->hc->res.reason.len); |
| 7066 | lua_settable(L, -3); |
| 7067 | |
William Lallemand | d7df73a | 2021-09-23 17:54:00 +0200 | [diff] [blame] | 7068 | lua_pushstring(L, "headers"); |
| 7069 | hlua_httpclient_get_headers(L, hlua_hc); |
| 7070 | lua_settable(L, -3); |
| 7071 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7072 | return 1; |
| 7073 | } |
| 7074 | |
| 7075 | if (httpclient_data(hlua_hc->hc)) |
| 7076 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 7077 | |
| 7078 | |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7079 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_send_yield, TICK_ETERNITY, 0)); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7080 | return 0; |
| 7081 | } |
| 7082 | |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7083 | |
| 7084 | /* |
| 7085 | * Allocate and return an array of http_hdr ist extracted from the <headers> lua table |
| 7086 | * |
| 7087 | * Caller must free the result |
| 7088 | */ |
| 7089 | struct http_hdr *hlua_httpclient_table_to_hdrs(lua_State *L) |
| 7090 | { |
| 7091 | struct http_hdr hdrs[global.tune.max_http_hdr]; |
| 7092 | struct http_hdr *result = NULL; |
| 7093 | uint32_t hdr_num = 0; |
| 7094 | |
| 7095 | lua_pushnil(L); |
| 7096 | while (lua_next(L, -2) != 0) { |
| 7097 | struct ist name, value; |
| 7098 | const char *n, *v; |
| 7099 | size_t nlen, vlen; |
| 7100 | |
| 7101 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 7102 | /* Skip element if the key is not a string or if the value is not a table */ |
| 7103 | goto next_hdr; |
| 7104 | } |
| 7105 | |
| 7106 | n = lua_tolstring(L, -2, &nlen); |
| 7107 | name = ist2(n, nlen); |
| 7108 | |
| 7109 | /* Loop on header's values */ |
| 7110 | lua_pushnil(L); |
| 7111 | while (lua_next(L, -2)) { |
| 7112 | if (!lua_isstring(L, -1)) { |
| 7113 | /* Skip the value if it is not a string */ |
| 7114 | goto next_value; |
| 7115 | } |
| 7116 | |
| 7117 | v = lua_tolstring(L, -1, &vlen); |
| 7118 | value = ist2(v, vlen); |
| 7119 | name = ist2(n, nlen); |
| 7120 | |
| 7121 | hdrs[hdr_num].n = istdup(name); |
| 7122 | hdrs[hdr_num].v = istdup(value); |
| 7123 | |
| 7124 | hdr_num++; |
| 7125 | |
| 7126 | next_value: |
| 7127 | lua_pop(L, 1); |
| 7128 | } |
| 7129 | |
| 7130 | next_hdr: |
| 7131 | lua_pop(L, 1); |
| 7132 | |
| 7133 | } |
| 7134 | |
| 7135 | if (hdr_num) { |
| 7136 | /* alloc and copy the headers in the httpclient struct */ |
| 7137 | result = calloc((hdr_num + 1), sizeof(*hdrs)); |
| 7138 | if (!result) |
| 7139 | goto skip_headers; |
| 7140 | memcpy(result, hdrs, sizeof(struct http_hdr) * (hdr_num + 1)); |
| 7141 | |
| 7142 | result[hdr_num].n = IST_NULL; |
| 7143 | result[hdr_num].v = IST_NULL; |
| 7144 | } |
| 7145 | |
| 7146 | skip_headers: |
| 7147 | lua_pop(L, 1); |
| 7148 | |
| 7149 | return result; |
| 7150 | } |
| 7151 | |
| 7152 | |
| 7153 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7154 | /* |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7155 | * Send an HTTP request and wait for a response |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7156 | */ |
| 7157 | |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7158 | __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] | 7159 | { |
| 7160 | struct hlua_httpclient *hlua_hc; |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7161 | struct http_hdr *hdrs = NULL; |
| 7162 | struct http_hdr *hdrs_i = NULL; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7163 | struct hlua *hlua; |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7164 | const char *url_str = NULL; |
William Lallemand | dec25c3 | 2021-10-25 19:48:37 +0200 | [diff] [blame] | 7165 | const char *body_str = NULL; |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7166 | int ret; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7167 | |
| 7168 | hlua = hlua_gethlua(L); |
| 7169 | |
| 7170 | if (!hlua || !hlua->task) |
| 7171 | WILL_LJMP(luaL_error(L, "The 'get' function is only allowed in " |
| 7172 | "'frontend', 'backend' or 'task'")); |
| 7173 | |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7174 | if (lua_gettop(L) != 2 || lua_type(L, -1) != LUA_TTABLE) |
| 7175 | WILL_LJMP(luaL_error(L, "'get' needs a table as argument")); |
| 7176 | |
| 7177 | ret = lua_getfield(L, -1, "url"); |
| 7178 | if (ret == LUA_TSTRING) { |
| 7179 | url_str = lua_tostring(L, -1); |
| 7180 | } |
| 7181 | lua_pop(L, 1); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7182 | |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7183 | ret = lua_getfield(L, -1, "headers"); |
| 7184 | if (ret == LUA_TTABLE) { |
| 7185 | hdrs = hlua_httpclient_table_to_hdrs(L); |
| 7186 | } |
| 7187 | lua_pop(L, 1); |
William Lallemand | 79416cb | 2021-09-24 14:51:44 +0200 | [diff] [blame] | 7188 | |
William Lallemand | dec25c3 | 2021-10-25 19:48:37 +0200 | [diff] [blame] | 7189 | ret = lua_getfield(L, -1, "body"); |
| 7190 | if (ret == LUA_TSTRING) { |
| 7191 | body_str = lua_tostring(L, -1); |
| 7192 | } |
| 7193 | lua_pop(L, 1); |
| 7194 | |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7195 | if (!url_str) { |
| 7196 | WILL_LJMP(luaL_error(L, "'get' need a 'url' argument")); |
| 7197 | return 0; |
| 7198 | } |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7199 | |
| 7200 | hlua_hc = hlua_checkhttpclient(L, 1); |
| 7201 | |
| 7202 | hlua_hc->hc->req.url = istdup(ist(url_str)); |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7203 | hlua_hc->hc->req.meth = meth; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7204 | |
| 7205 | /* update the httpclient callbacks */ |
| 7206 | hlua_hc->hc->ops.res_stline = hlua_httpclient_res_cb; |
| 7207 | hlua_hc->hc->ops.res_headers = hlua_httpclient_res_cb; |
| 7208 | hlua_hc->hc->ops.res_payload = hlua_httpclient_res_cb; |
| 7209 | hlua_hc->hc->ops.res_end = hlua_httpclient_res_cb; |
| 7210 | |
| 7211 | |
William Lallemand | 6137a9e | 2021-10-26 15:01:53 +0200 | [diff] [blame^] | 7212 | ret = httpclient_req_gen(hlua_hc->hc, hlua_hc->hc->req.url, meth, hdrs, ist(body_str)); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7213 | |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7214 | /* free the temporary headers array */ |
| 7215 | hdrs_i = hdrs; |
| 7216 | while (hdrs_i && isttest(hdrs_i->n)) { |
| 7217 | istfree(&hdrs_i->n); |
| 7218 | istfree(&hdrs_i->v); |
| 7219 | hdrs_i++; |
| 7220 | } |
| 7221 | ha_free(&hdrs); |
| 7222 | |
| 7223 | |
William Lallemand | 6137a9e | 2021-10-26 15:01:53 +0200 | [diff] [blame^] | 7224 | if (ret != ERR_NONE) { |
| 7225 | WILL_LJMP(luaL_error(L, "Can't generate the HTTP request")); |
| 7226 | return 0; |
| 7227 | } |
| 7228 | |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7229 | |
William Lallemand | 6137a9e | 2021-10-26 15:01:53 +0200 | [diff] [blame^] | 7230 | httpclient_start(hlua_hc->hc); |
| 7231 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7232 | /* we return a "res" object */ |
| 7233 | lua_newtable(L); |
| 7234 | |
| 7235 | luaL_buffinit(L, &hlua_hc->b); |
| 7236 | lua_pushstring(L, "body"); |
| 7237 | |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7238 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_send_yield, TICK_ETERNITY, 0)); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7239 | return 0; |
| 7240 | } |
| 7241 | |
| 7242 | /* |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7243 | * Sends an HTTP HEAD request and wait for a response |
| 7244 | * |
| 7245 | * httpclient:head(url, headers, payload) |
| 7246 | */ |
| 7247 | __LJMP static int hlua_httpclient_head(lua_State *L) |
| 7248 | { |
| 7249 | return hlua_httpclient_send(L, HTTP_METH_HEAD); |
| 7250 | } |
| 7251 | |
| 7252 | /* |
| 7253 | * Send an HTTP GET request and wait for a response |
| 7254 | * |
| 7255 | * httpclient:get(url, headers, payload) |
| 7256 | */ |
| 7257 | __LJMP static int hlua_httpclient_get(lua_State *L) |
| 7258 | { |
| 7259 | return hlua_httpclient_send(L, HTTP_METH_GET); |
| 7260 | |
| 7261 | } |
| 7262 | |
| 7263 | /* |
| 7264 | * Sends an HTTP PUT request and wait for a response |
| 7265 | * |
| 7266 | * httpclient:put(url, headers, payload) |
| 7267 | */ |
| 7268 | __LJMP static int hlua_httpclient_put(lua_State *L) |
| 7269 | { |
| 7270 | return hlua_httpclient_send(L, HTTP_METH_PUT); |
| 7271 | } |
| 7272 | |
| 7273 | /* |
| 7274 | * Send an HTTP POST request and wait for a response |
| 7275 | * |
| 7276 | * httpclient:post(url, headers, payload) |
| 7277 | */ |
| 7278 | __LJMP static int hlua_httpclient_post(lua_State *L) |
| 7279 | { |
| 7280 | return hlua_httpclient_send(L, HTTP_METH_POST); |
| 7281 | } |
| 7282 | |
| 7283 | |
| 7284 | /* |
| 7285 | * Sends an HTTP DELETE request and wait for a response |
| 7286 | * |
| 7287 | * httpclient:delete(url, headers, payload) |
| 7288 | */ |
| 7289 | __LJMP static int hlua_httpclient_delete(lua_State *L) |
| 7290 | { |
| 7291 | return hlua_httpclient_send(L, HTTP_METH_DELETE); |
| 7292 | } |
| 7293 | |
| 7294 | /* |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7295 | * |
| 7296 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7297 | * Class TXN |
| 7298 | * |
| 7299 | * |
| 7300 | */ |
| 7301 | |
| 7302 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7303 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7304 | */ |
| 7305 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 7306 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7307 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7308 | } |
| 7309 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7310 | __LJMP static int hlua_set_var(lua_State *L) |
| 7311 | { |
| 7312 | struct hlua_txn *htxn; |
| 7313 | const char *name; |
| 7314 | size_t len; |
| 7315 | struct sample smp; |
| 7316 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 7317 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 7318 | 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] | 7319 | |
| 7320 | /* It is useles to retrieve the stream, but this function |
| 7321 | * runs only in a stream context. |
| 7322 | */ |
| 7323 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7324 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 7325 | |
| 7326 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 7327 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7328 | hlua_lua2smp(L, 3, &smp); |
| 7329 | |
| 7330 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 7331 | 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] | 7332 | |
| 7333 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 7334 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 7335 | else |
| 7336 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 7337 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 7338 | return 1; |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7339 | } |
| 7340 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 7341 | __LJMP static int hlua_unset_var(lua_State *L) |
| 7342 | { |
| 7343 | struct hlua_txn *htxn; |
| 7344 | const char *name; |
| 7345 | size_t len; |
| 7346 | struct sample smp; |
| 7347 | |
| 7348 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 7349 | |
| 7350 | /* It is useles to retrieve the stream, but this function |
| 7351 | * runs only in a stream context. |
| 7352 | */ |
| 7353 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7354 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 7355 | |
| 7356 | /* Unset the variable. */ |
| 7357 | 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] | 7358 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 7359 | return 1; |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 7360 | } |
| 7361 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7362 | __LJMP static int hlua_get_var(lua_State *L) |
| 7363 | { |
| 7364 | struct hlua_txn *htxn; |
| 7365 | const char *name; |
| 7366 | size_t len; |
| 7367 | struct sample smp; |
| 7368 | |
| 7369 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 7370 | |
| 7371 | /* It is useles to retrieve the stream, but this function |
| 7372 | * runs only in a stream context. |
| 7373 | */ |
| 7374 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7375 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 7376 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 7377 | 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] | 7378 | if (!vars_get_by_name(name, len, &smp, NULL)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7379 | lua_pushnil(L); |
| 7380 | return 1; |
| 7381 | } |
| 7382 | |
| 7383 | return hlua_smp2lua(L, &smp); |
| 7384 | } |
| 7385 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7386 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7387 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7388 | struct hlua *hlua; |
| 7389 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7390 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 7391 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7392 | /* It is useles to retrieve the stream, but this function |
| 7393 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7394 | */ |
| 7395 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7396 | |
| 7397 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7398 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7399 | if (!hlua) |
| 7400 | return 0; |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7401 | |
| 7402 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 7403 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7404 | |
| 7405 | /* Get and store new value. */ |
| 7406 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 7407 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 7408 | |
| 7409 | return 0; |
| 7410 | } |
| 7411 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7412 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7413 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7414 | struct hlua *hlua; |
| 7415 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7416 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 7417 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7418 | /* It is useles to retrieve the stream, but this function |
| 7419 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7420 | */ |
| 7421 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7422 | |
| 7423 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7424 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7425 | if (!hlua) { |
| 7426 | lua_pushnil(L); |
| 7427 | return 1; |
| 7428 | } |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7429 | |
| 7430 | /* Push configuration index in the stack. */ |
| 7431 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 7432 | |
| 7433 | return 1; |
| 7434 | } |
| 7435 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7436 | /* Create stack entry containing a class TXN. This function |
| 7437 | * return 0 if the stack does not contains free slots, |
| 7438 | * otherwise it returns 1. |
| 7439 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 7440 | 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] | 7441 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7442 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7443 | |
| 7444 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 7445 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7446 | return 0; |
| 7447 | |
| 7448 | /* NOTE: The allocation never fails. The failure |
| 7449 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7450 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7451 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 7452 | /* Create the object: obj[0] = userdata. */ |
| 7453 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7454 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 7455 | lua_rawseti(L, -2, 0); |
| 7456 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7457 | htxn->s = s; |
| 7458 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 7459 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 7460 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7461 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7462 | /* Create the "f" field that contains a list of fetches. */ |
| 7463 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 7464 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7465 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7466 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7467 | |
| 7468 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 7469 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 7470 | 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] | 7471 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7472 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7473 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7474 | /* Create the "c" field that contains a list of converters. */ |
| 7475 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7476 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7477 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7478 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7479 | |
| 7480 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 7481 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 7482 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7483 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7484 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7485 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7486 | /* Create the "req" field that contains the request channel object. */ |
| 7487 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 7488 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7489 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7490 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7491 | |
| 7492 | /* Create the "res" field that contains the response channel object. */ |
| 7493 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 7494 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7495 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7496 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7497 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7498 | /* Creates the HTTP object is the current proxy allows http. */ |
| 7499 | lua_pushstring(L, "http"); |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 7500 | if (IS_HTX_STRM(s)) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7501 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7502 | return 0; |
| 7503 | } |
| 7504 | else |
| 7505 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7506 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7507 | |
Christopher Faulet | 78c3547 | 2020-02-26 17:14:08 +0100 | [diff] [blame] | 7508 | if ((htxn->flags & HLUA_TXN_CTX_MASK) == HLUA_TXN_FLT_CTX) { |
| 7509 | /* HTTPMessage object are created when a lua TXN is created from |
| 7510 | * a filter context only |
| 7511 | */ |
| 7512 | |
| 7513 | /* Creates the HTTP-Request object is the current proxy allows http. */ |
| 7514 | lua_pushstring(L, "http_req"); |
| 7515 | if (p->mode == PR_MODE_HTTP) { |
| 7516 | if (!hlua_http_msg_new(L, &s->txn->req)) |
| 7517 | return 0; |
| 7518 | } |
| 7519 | else |
| 7520 | lua_pushnil(L); |
| 7521 | lua_rawset(L, -3); |
| 7522 | |
| 7523 | /* Creates the HTTP-Response object is the current proxy allows http. */ |
| 7524 | lua_pushstring(L, "http_res"); |
| 7525 | if (p->mode == PR_MODE_HTTP) { |
| 7526 | if (!hlua_http_msg_new(L, &s->txn->rsp)) |
| 7527 | return 0; |
| 7528 | } |
| 7529 | else |
| 7530 | lua_pushnil(L); |
| 7531 | lua_rawset(L, -3); |
| 7532 | } |
| 7533 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7534 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 7535 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 7536 | lua_setmetatable(L, -2); |
| 7537 | |
| 7538 | return 1; |
| 7539 | } |
| 7540 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 7541 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 7542 | { |
| 7543 | const char *msg; |
| 7544 | struct hlua_txn *htxn; |
| 7545 | |
| 7546 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 7547 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7548 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7549 | |
| 7550 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 7551 | return 0; |
| 7552 | } |
| 7553 | |
| 7554 | __LJMP static int hlua_txn_log(lua_State *L) |
| 7555 | { |
| 7556 | int level; |
| 7557 | const char *msg; |
| 7558 | struct hlua_txn *htxn; |
| 7559 | |
| 7560 | MAY_LJMP(check_args(L, 3, "log")); |
| 7561 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7562 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7563 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 7564 | |
| 7565 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 7566 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 7567 | |
| 7568 | hlua_sendlog(htxn->s->be, level, msg); |
| 7569 | return 0; |
| 7570 | } |
| 7571 | |
| 7572 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 7573 | { |
| 7574 | const char *msg; |
| 7575 | struct hlua_txn *htxn; |
| 7576 | |
| 7577 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 7578 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7579 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7580 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 7581 | return 0; |
| 7582 | } |
| 7583 | |
| 7584 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 7585 | { |
| 7586 | const char *msg; |
| 7587 | struct hlua_txn *htxn; |
| 7588 | |
| 7589 | MAY_LJMP(check_args(L, 2, "Info")); |
| 7590 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7591 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7592 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 7593 | return 0; |
| 7594 | } |
| 7595 | |
| 7596 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 7597 | { |
| 7598 | const char *msg; |
| 7599 | struct hlua_txn *htxn; |
| 7600 | |
| 7601 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 7602 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7603 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7604 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 7605 | return 0; |
| 7606 | } |
| 7607 | |
| 7608 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 7609 | { |
| 7610 | const char *msg; |
| 7611 | struct hlua_txn *htxn; |
| 7612 | |
| 7613 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 7614 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7615 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7616 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 7617 | return 0; |
| 7618 | } |
| 7619 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7620 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 7621 | { |
| 7622 | struct hlua_txn *htxn; |
| 7623 | int ll; |
| 7624 | |
| 7625 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 7626 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7627 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7628 | |
| 7629 | if (ll < 0 || ll > 7) |
| 7630 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 7631 | |
| 7632 | htxn->s->logs.level = ll; |
| 7633 | return 0; |
| 7634 | } |
| 7635 | |
| 7636 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 7637 | { |
| 7638 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7639 | int tos; |
| 7640 | |
| 7641 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 7642 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7643 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7644 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 7645 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7646 | return 0; |
| 7647 | } |
| 7648 | |
| 7649 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 7650 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7651 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7652 | int mark; |
| 7653 | |
| 7654 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 7655 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7656 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7657 | |
Lukas Tribus | 579e3e3 | 2019-08-11 18:03:45 +0200 | [diff] [blame] | 7658 | conn_set_mark(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7659 | return 0; |
| 7660 | } |
| 7661 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 7662 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 7663 | { |
| 7664 | struct hlua_txn *htxn; |
| 7665 | |
| 7666 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 7667 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7668 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 7669 | return 0; |
| 7670 | } |
| 7671 | |
| 7672 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 7673 | { |
| 7674 | struct hlua_txn *htxn; |
| 7675 | |
| 7676 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 7677 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7678 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 7679 | return 0; |
| 7680 | } |
| 7681 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7682 | /* 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] | 7683 | * 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] | 7684 | * message and terminate the transaction. It returns 1 on success and 0 on |
| 7685 | * error. The Reply must be on top of the stack. |
| 7686 | */ |
| 7687 | __LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s) |
| 7688 | { |
| 7689 | struct htx *htx; |
| 7690 | struct htx_sl *sl; |
| 7691 | struct h1m h1m; |
| 7692 | const char *status, *reason, *body; |
| 7693 | size_t status_len, reason_len, body_len; |
| 7694 | int ret, code, flags; |
| 7695 | |
| 7696 | code = 200; |
| 7697 | status = "200"; |
| 7698 | status_len = 3; |
| 7699 | ret = lua_getfield(L, -1, "status"); |
| 7700 | if (ret == LUA_TNUMBER) { |
| 7701 | code = lua_tointeger(L, -1); |
| 7702 | status = lua_tolstring(L, -1, &status_len); |
| 7703 | } |
| 7704 | lua_pop(L, 1); |
| 7705 | |
| 7706 | reason = http_get_reason(code); |
| 7707 | reason_len = strlen(reason); |
| 7708 | ret = lua_getfield(L, -1, "reason"); |
| 7709 | if (ret == LUA_TSTRING) |
| 7710 | reason = lua_tolstring(L, -1, &reason_len); |
| 7711 | lua_pop(L, 1); |
| 7712 | |
| 7713 | body = NULL; |
| 7714 | body_len = 0; |
| 7715 | ret = lua_getfield(L, -1, "body"); |
| 7716 | if (ret == LUA_TSTRING) |
| 7717 | body = lua_tolstring(L, -1, &body_len); |
| 7718 | lua_pop(L, 1); |
| 7719 | |
| 7720 | /* Prepare the response before inserting the headers */ |
| 7721 | h1m_init_res(&h1m); |
| 7722 | htx = htx_from_buf(&s->res.buf); |
| 7723 | channel_htx_truncate(&s->res, htx); |
| 7724 | if (s->txn->req.flags & HTTP_MSGF_VER_11) { |
| 7725 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 7726 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), |
| 7727 | ist2(status, status_len), ist2(reason, reason_len)); |
| 7728 | } |
| 7729 | else { |
| 7730 | flags = HTX_SL_F_IS_RESP; |
| 7731 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), |
| 7732 | ist2(status, status_len), ist2(reason, reason_len)); |
| 7733 | } |
| 7734 | if (!sl) |
| 7735 | goto fail; |
| 7736 | sl->info.res.status = code; |
| 7737 | |
| 7738 | /* Push in the stack the "headers" entry. */ |
| 7739 | ret = lua_getfield(L, -1, "headers"); |
| 7740 | if (ret != LUA_TTABLE) |
| 7741 | goto skip_headers; |
| 7742 | |
| 7743 | lua_pushnil(L); |
| 7744 | while (lua_next(L, -2) != 0) { |
| 7745 | struct ist name, value; |
| 7746 | const char *n, *v; |
| 7747 | size_t nlen, vlen; |
| 7748 | |
| 7749 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 7750 | /* Skip element if the key is not a string or if the value is not a table */ |
| 7751 | goto next_hdr; |
| 7752 | } |
| 7753 | |
| 7754 | n = lua_tolstring(L, -2, &nlen); |
| 7755 | name = ist2(n, nlen); |
| 7756 | if (isteqi(name, ist("content-length"))) { |
| 7757 | /* Always skip content-length header. It will be added |
| 7758 | * later with the correct len |
| 7759 | */ |
| 7760 | goto next_hdr; |
| 7761 | } |
| 7762 | |
| 7763 | /* Loop on header's values */ |
| 7764 | lua_pushnil(L); |
| 7765 | while (lua_next(L, -2)) { |
| 7766 | if (!lua_isstring(L, -1)) { |
| 7767 | /* Skip the value if it is not a string */ |
| 7768 | goto next_value; |
| 7769 | } |
| 7770 | |
| 7771 | v = lua_tolstring(L, -1, &vlen); |
| 7772 | value = ist2(v, vlen); |
| 7773 | |
| 7774 | if (isteqi(name, ist("transfer-encoding"))) |
| 7775 | h1_parse_xfer_enc_header(&h1m, value); |
| 7776 | if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen))) |
| 7777 | goto fail; |
| 7778 | |
| 7779 | next_value: |
| 7780 | lua_pop(L, 1); |
| 7781 | } |
| 7782 | |
| 7783 | next_hdr: |
| 7784 | lua_pop(L, 1); |
| 7785 | } |
| 7786 | skip_headers: |
| 7787 | lua_pop(L, 1); |
| 7788 | |
| 7789 | /* Update h1m flags: CLEN is set if CHNK is not present */ |
| 7790 | if (!(h1m.flags & H1_MF_CHNK)) { |
| 7791 | const char *clen = ultoa(body_len); |
| 7792 | |
| 7793 | h1m.flags |= H1_MF_CLEN; |
| 7794 | if (!htx_add_header(htx, ist("content-length"), ist(clen))) |
| 7795 | goto fail; |
| 7796 | } |
| 7797 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 7798 | h1m.flags |= H1_MF_XFER_LEN; |
| 7799 | |
| 7800 | /* Update HTX start-line flags */ |
| 7801 | if (h1m.flags & H1_MF_XFER_ENC) |
| 7802 | flags |= HTX_SL_F_XFER_ENC; |
| 7803 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 7804 | flags |= HTX_SL_F_XFER_LEN; |
| 7805 | if (h1m.flags & H1_MF_CHNK) |
| 7806 | flags |= HTX_SL_F_CHNK; |
| 7807 | else if (h1m.flags & H1_MF_CLEN) |
| 7808 | flags |= HTX_SL_F_CLEN; |
| 7809 | if (h1m.body_len == 0) |
| 7810 | flags |= HTX_SL_F_BODYLESS; |
| 7811 | } |
| 7812 | sl->flags |= flags; |
| 7813 | |
| 7814 | |
| 7815 | if (!htx_add_endof(htx, HTX_BLK_EOH) || |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 7816 | (body_len && !htx_add_data_atonce(htx, ist2(body, body_len)))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7817 | goto fail; |
| 7818 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 7819 | htx->flags |= HTX_FL_EOM; |
| 7820 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7821 | /* Now, forward the response and terminate the transaction */ |
| 7822 | s->txn->status = code; |
| 7823 | htx_to_buf(htx, &s->res.buf); |
| 7824 | if (!http_forward_proxy_resp(s, 1)) |
| 7825 | goto fail; |
| 7826 | |
| 7827 | return 1; |
| 7828 | |
| 7829 | fail: |
| 7830 | channel_htx_truncate(&s->res, htx); |
| 7831 | return 0; |
| 7832 | } |
| 7833 | |
| 7834 | /* Terminate a transaction if called from a lua action. For TCP streams, |
| 7835 | * processing is just aborted. Nothing is returned to the client and all |
| 7836 | * arguments are ignored. For HTTP streams, if a reply is passed as argument, it |
| 7837 | * is forwarded to the client before terminating the transaction. On success, |
| 7838 | * the function exits with ACT_RET_DONE code. If an error occurred, it exits |
| 7839 | * with ACT_RET_ERR code. If this function is not called from a lua action, it |
| 7840 | * just exits without any processing. |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 7841 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 7842 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 7843 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 7844 | struct hlua_txn *htxn; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7845 | struct stream *s; |
| 7846 | int finst; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 7847 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 7848 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 7849 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7850 | /* If the flags NOTERM is set, we cannot terminate the session, so we |
| 7851 | * just end the execution of the current lua code. */ |
| 7852 | if (htxn->flags & HLUA_TXN_NOTERM) |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 7853 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 7854 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7855 | s = htxn->s; |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 7856 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7857 | struct channel *req = &s->req; |
| 7858 | struct channel *res = &s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 7859 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7860 | channel_auto_read(req); |
| 7861 | channel_abort(req); |
| 7862 | channel_auto_close(req); |
| 7863 | channel_erase(req); |
| 7864 | |
| 7865 | res->wex = tick_add_ifset(now_ms, res->wto); |
| 7866 | channel_auto_read(res); |
| 7867 | channel_auto_close(res); |
| 7868 | channel_shutr_now(res); |
| 7869 | |
| 7870 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D); |
| 7871 | goto done; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 7872 | } |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 7873 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7874 | if (lua_gettop(L) == 1 || !lua_istable(L, 2)) { |
| 7875 | /* No reply or invalid reply */ |
| 7876 | s->txn->status = 0; |
| 7877 | http_reply_and_close(s, 0, NULL); |
| 7878 | } |
| 7879 | else { |
| 7880 | /* Remove extra args to have the reply on top of the stack */ |
| 7881 | if (lua_gettop(L) > 2) |
| 7882 | lua_pop(L, lua_gettop(L) - 2); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 7883 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7884 | if (!hlua_txn_forward_reply(L, s)) { |
| 7885 | if (!(s->flags & SF_ERR_MASK)) |
| 7886 | s->flags |= SF_ERR_PRXCOND; |
| 7887 | lua_pushinteger(L, ACT_RET_ERR); |
| 7888 | WILL_LJMP(hlua_done(L)); |
| 7889 | return 0; /* Never reached */ |
| 7890 | } |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 7891 | } |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 7892 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7893 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H); |
| 7894 | if (htxn->dir == SMP_OPT_DIR_REQ) { |
| 7895 | /* let's log the request time */ |
| 7896 | s->logs.tv_request = now; |
| 7897 | 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] | 7898 | _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7899 | } |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 7900 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7901 | done: |
| 7902 | if (!(s->flags & SF_ERR_MASK)) |
| 7903 | s->flags |= SF_ERR_LOCAL; |
| 7904 | if (!(s->flags & SF_FINST_MASK)) |
| 7905 | s->flags |= finst; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 7906 | |
Christopher Faulet | e48d1dc | 2021-08-13 14:11:17 +0200 | [diff] [blame] | 7907 | if ((htxn->flags & HLUA_TXN_CTX_MASK) == HLUA_TXN_FLT_CTX) |
| 7908 | lua_pushinteger(L, -1); |
| 7909 | else |
| 7910 | lua_pushinteger(L, ACT_RET_ABRT); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 7911 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 7912 | return 0; |
| 7913 | } |
| 7914 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7915 | /* |
| 7916 | * |
| 7917 | * |
| 7918 | * Class REPLY |
| 7919 | * |
| 7920 | * |
| 7921 | */ |
| 7922 | |
| 7923 | /* Pushes the TXN reply onto the top of the stack. If the stask does not have a |
| 7924 | * free slots, the function fails and returns 0; |
| 7925 | */ |
| 7926 | static int hlua_txn_reply_new(lua_State *L) |
| 7927 | { |
| 7928 | struct hlua_txn *htxn; |
| 7929 | const char *reason, *body = NULL; |
| 7930 | int ret, status; |
| 7931 | |
| 7932 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 7933 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7934 | hlua_pusherror(L, "txn object is not an HTTP transaction."); |
| 7935 | WILL_LJMP(lua_error(L)); |
| 7936 | } |
| 7937 | |
| 7938 | /* Default value */ |
| 7939 | status = 200; |
| 7940 | reason = http_get_reason(status); |
| 7941 | |
| 7942 | if (lua_istable(L, 2)) { |
| 7943 | /* load status and reason from the table argument at index 2 */ |
| 7944 | ret = lua_getfield(L, 2, "status"); |
| 7945 | if (ret == LUA_TNIL) |
| 7946 | goto reason; |
| 7947 | else if (ret != LUA_TNUMBER) { |
| 7948 | /* invalid status: ignore the reason */ |
| 7949 | goto body; |
| 7950 | } |
| 7951 | status = lua_tointeger(L, -1); |
| 7952 | |
| 7953 | reason: |
| 7954 | lua_pop(L, 1); /* restore the stack: remove status */ |
| 7955 | ret = lua_getfield(L, 2, "reason"); |
| 7956 | if (ret == LUA_TSTRING) |
| 7957 | reason = lua_tostring(L, -1); |
| 7958 | |
| 7959 | body: |
| 7960 | lua_pop(L, 1); /* restore the stack: remove invalid status or reason */ |
| 7961 | ret = lua_getfield(L, 2, "body"); |
| 7962 | if (ret == LUA_TSTRING) |
| 7963 | body = lua_tostring(L, -1); |
| 7964 | lua_pop(L, 1); /* restore the stack: remove body */ |
| 7965 | } |
| 7966 | |
| 7967 | /* Create the Reply table */ |
| 7968 | lua_newtable(L); |
| 7969 | |
| 7970 | /* Add status element */ |
| 7971 | lua_pushstring(L, "status"); |
| 7972 | lua_pushinteger(L, status); |
| 7973 | lua_settable(L, -3); |
| 7974 | |
| 7975 | /* Add reason element */ |
| 7976 | reason = http_get_reason(status); |
| 7977 | lua_pushstring(L, "reason"); |
| 7978 | lua_pushstring(L, reason); |
| 7979 | lua_settable(L, -3); |
| 7980 | |
| 7981 | /* Add body element, nil if undefined */ |
| 7982 | lua_pushstring(L, "body"); |
| 7983 | if (body) |
| 7984 | lua_pushstring(L, body); |
| 7985 | else |
| 7986 | lua_pushnil(L); |
| 7987 | lua_settable(L, -3); |
| 7988 | |
| 7989 | /* Add headers element */ |
| 7990 | lua_pushstring(L, "headers"); |
| 7991 | lua_newtable(L); |
| 7992 | |
| 7993 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 7994 | if (lua_istable(L, 2)) { |
| 7995 | /* load headers from the table argument at index 2. If it is a table, copy it. */ |
| 7996 | ret = lua_getfield(L, 2, "headers"); |
| 7997 | if (ret == LUA_TTABLE) { |
| 7998 | /* stack: [ ... <headers:table>, <table> ] */ |
| 7999 | lua_pushnil(L); |
| 8000 | while (lua_next(L, -2) != 0) { |
| 8001 | /* stack: [ ... <headers:table>, <table>, k, v] */ |
| 8002 | if (!lua_isstring(L, -1) && !lua_istable(L, -1)) { |
| 8003 | /* invalid value type, skip it */ |
| 8004 | lua_pop(L, 1); |
| 8005 | continue; |
| 8006 | } |
| 8007 | |
| 8008 | |
| 8009 | /* Duplicate the key and swap it with the value. */ |
| 8010 | lua_pushvalue(L, -2); |
| 8011 | lua_insert(L, -2); |
| 8012 | /* stack: [ ... <headers:table>, <table>, k, k, v ] */ |
| 8013 | |
| 8014 | lua_newtable(L); |
| 8015 | lua_insert(L, -2); |
| 8016 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */ |
| 8017 | |
| 8018 | if (lua_isstring(L, -1)) { |
| 8019 | /* push the value in the inner table */ |
| 8020 | lua_rawseti(L, -2, 1); |
| 8021 | } |
| 8022 | else { /* table */ |
| 8023 | lua_pushnil(L); |
| 8024 | while (lua_next(L, -2) != 0) { |
| 8025 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */ |
| 8026 | if (!lua_isstring(L, -1)) { |
| 8027 | /* invalid value type, skip it*/ |
| 8028 | lua_pop(L, 1); |
| 8029 | continue; |
| 8030 | } |
| 8031 | /* push the value in the inner table */ |
| 8032 | lua_rawseti(L, -4, lua_rawlen(L, -4) + 1); |
| 8033 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */ |
| 8034 | } |
| 8035 | lua_pop(L, 1); |
| 8036 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */ |
| 8037 | } |
| 8038 | |
| 8039 | /* push (k,v) on the stack in the headers table: |
| 8040 | * stack: [ ... <headers:table>, <table>, k, k, v ] |
| 8041 | */ |
| 8042 | lua_settable(L, -5); |
| 8043 | /* stack: [ ... <headers:table>, <table>, k ] */ |
| 8044 | } |
| 8045 | } |
| 8046 | lua_pop(L, 1); |
| 8047 | } |
| 8048 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 8049 | lua_settable(L, -3); |
| 8050 | /* stack: [ txn, <Arg:table>, <Reply:table> ] */ |
| 8051 | |
| 8052 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 8053 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref); |
| 8054 | lua_setmetatable(L, -2); |
| 8055 | return 1; |
| 8056 | } |
| 8057 | |
| 8058 | /* Set the reply status code, and optionally the reason. If no reason is |
| 8059 | * provided, the default one corresponding to the status code is used. |
| 8060 | */ |
| 8061 | __LJMP static int hlua_txn_reply_set_status(lua_State *L) |
| 8062 | { |
| 8063 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 8064 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 8065 | |
| 8066 | /* First argument (self) must be a table */ |
| 8067 | luaL_checktype(L, 1, LUA_TTABLE); |
| 8068 | |
| 8069 | if (status < 100 || status > 599) { |
| 8070 | lua_pushboolean(L, 0); |
| 8071 | return 1; |
| 8072 | } |
| 8073 | if (!reason) |
| 8074 | reason = http_get_reason(status); |
| 8075 | |
| 8076 | lua_pushinteger(L, status); |
| 8077 | lua_setfield(L, 1, "status"); |
| 8078 | |
| 8079 | lua_pushstring(L, reason); |
| 8080 | lua_setfield(L, 1, "reason"); |
| 8081 | |
| 8082 | lua_pushboolean(L, 1); |
| 8083 | return 1; |
| 8084 | } |
| 8085 | |
| 8086 | /* Add a header into the reply object. Each header name is associated to an |
| 8087 | * array of values in the "headers" table. If the header name is not found, a |
| 8088 | * new entry is created. |
| 8089 | */ |
| 8090 | __LJMP static int hlua_txn_reply_add_header(lua_State *L) |
| 8091 | { |
| 8092 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8093 | const char *value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 8094 | int ret; |
| 8095 | |
| 8096 | /* First argument (self) must be a table */ |
| 8097 | luaL_checktype(L, 1, LUA_TTABLE); |
| 8098 | |
| 8099 | /* Push in the stack the "headers" entry. */ |
| 8100 | ret = lua_getfield(L, 1, "headers"); |
| 8101 | if (ret != LUA_TTABLE) { |
| 8102 | hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret)); |
| 8103 | WILL_LJMP(lua_error(L)); |
| 8104 | } |
| 8105 | |
| 8106 | /* check if the header is already registered. If not, register it. */ |
| 8107 | ret = lua_getfield(L, -1, name); |
| 8108 | if (ret == LUA_TNIL) { |
| 8109 | /* Entry not found. */ |
| 8110 | lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */ |
| 8111 | |
| 8112 | /* Insert the new header name in the array in the top of the stack. |
| 8113 | * It left the new array in the top of the stack. |
| 8114 | */ |
| 8115 | lua_newtable(L); |
| 8116 | lua_pushstring(L, name); |
| 8117 | lua_pushvalue(L, -2); |
| 8118 | lua_settable(L, -4); |
| 8119 | } |
| 8120 | else if (ret != LUA_TTABLE) { |
| 8121 | hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret)); |
| 8122 | WILL_LJMP(lua_error(L)); |
| 8123 | } |
| 8124 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8125 | /* Now the top of thestack is an array of values. We push |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8126 | * the header value as new entry. |
| 8127 | */ |
| 8128 | lua_pushstring(L, value); |
| 8129 | ret = lua_rawlen(L, -2); |
| 8130 | lua_rawseti(L, -2, ret + 1); |
| 8131 | |
| 8132 | lua_pushboolean(L, 1); |
| 8133 | return 1; |
| 8134 | } |
| 8135 | |
| 8136 | /* Remove all occurrences of a given header name. */ |
| 8137 | __LJMP static int hlua_txn_reply_del_header(lua_State *L) |
| 8138 | { |
| 8139 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8140 | int ret; |
| 8141 | |
| 8142 | /* First argument (self) must be a table */ |
| 8143 | luaL_checktype(L, 1, LUA_TTABLE); |
| 8144 | |
| 8145 | /* Push in the stack the "headers" entry. */ |
| 8146 | ret = lua_getfield(L, 1, "headers"); |
| 8147 | if (ret != LUA_TTABLE) { |
| 8148 | hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret)); |
| 8149 | WILL_LJMP(lua_error(L)); |
| 8150 | } |
| 8151 | |
| 8152 | lua_pushstring(L, name); |
| 8153 | lua_pushnil(L); |
| 8154 | lua_settable(L, -3); |
| 8155 | |
| 8156 | lua_pushboolean(L, 1); |
| 8157 | return 1; |
| 8158 | } |
| 8159 | |
| 8160 | /* Set the reply's body. Overwrite any existing entry. */ |
| 8161 | __LJMP static int hlua_txn_reply_set_body(lua_State *L) |
| 8162 | { |
| 8163 | const char *payload = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8164 | |
| 8165 | /* First argument (self) must be a table */ |
| 8166 | luaL_checktype(L, 1, LUA_TTABLE); |
| 8167 | |
| 8168 | lua_pushstring(L, payload); |
| 8169 | lua_setfield(L, 1, "body"); |
| 8170 | |
| 8171 | lua_pushboolean(L, 1); |
| 8172 | return 1; |
| 8173 | } |
| 8174 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 8175 | __LJMP static int hlua_log(lua_State *L) |
| 8176 | { |
| 8177 | int level; |
| 8178 | const char *msg; |
| 8179 | |
| 8180 | MAY_LJMP(check_args(L, 2, "log")); |
| 8181 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 8182 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8183 | |
| 8184 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 8185 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 8186 | |
| 8187 | hlua_sendlog(NULL, level, msg); |
| 8188 | return 0; |
| 8189 | } |
| 8190 | |
| 8191 | __LJMP static int hlua_log_debug(lua_State *L) |
| 8192 | { |
| 8193 | const char *msg; |
| 8194 | |
| 8195 | MAY_LJMP(check_args(L, 1, "debug")); |
| 8196 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8197 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 8198 | return 0; |
| 8199 | } |
| 8200 | |
| 8201 | __LJMP static int hlua_log_info(lua_State *L) |
| 8202 | { |
| 8203 | const char *msg; |
| 8204 | |
| 8205 | MAY_LJMP(check_args(L, 1, "info")); |
| 8206 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8207 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 8208 | return 0; |
| 8209 | } |
| 8210 | |
| 8211 | __LJMP static int hlua_log_warning(lua_State *L) |
| 8212 | { |
| 8213 | const char *msg; |
| 8214 | |
| 8215 | MAY_LJMP(check_args(L, 1, "warning")); |
| 8216 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8217 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 8218 | return 0; |
| 8219 | } |
| 8220 | |
| 8221 | __LJMP static int hlua_log_alert(lua_State *L) |
| 8222 | { |
| 8223 | const char *msg; |
| 8224 | |
| 8225 | MAY_LJMP(check_args(L, 1, "alert")); |
| 8226 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8227 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8228 | return 0; |
| 8229 | } |
| 8230 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8231 | __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] | 8232 | { |
| 8233 | int wakeup_ms = lua_tointeger(L, -1); |
Willy Tarreau | 12c0270 | 2021-09-30 16:12:31 +0200 | [diff] [blame] | 8234 | if (!tick_is_expired(wakeup_ms, now_ms)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8235 | 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] | 8236 | return 0; |
| 8237 | } |
| 8238 | |
| 8239 | __LJMP static int hlua_sleep(lua_State *L) |
| 8240 | { |
| 8241 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8242 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8243 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8244 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8245 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8246 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8247 | wakeup_ms = tick_add(now_ms, delay); |
| 8248 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8249 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8250 | 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] | 8251 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8252 | } |
| 8253 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8254 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8255 | { |
| 8256 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8257 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8258 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8259 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8260 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8261 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8262 | wakeup_ms = tick_add(now_ms, delay); |
| 8263 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8264 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8265 | 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] | 8266 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8267 | } |
| 8268 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8269 | /* This functionis an LUA binding. it permits to give back |
| 8270 | * the hand at the HAProxy scheduler. It is used when the |
| 8271 | * LUA processing consumes a lot of time. |
| 8272 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8273 | __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] | 8274 | { |
| 8275 | return 0; |
| 8276 | } |
| 8277 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8278 | __LJMP static int hlua_yield(lua_State *L) |
| 8279 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8280 | 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] | 8281 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8282 | } |
| 8283 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8284 | /* This function change the nice of the currently executed |
| 8285 | * task. It is used set low or high priority at the current |
| 8286 | * task. |
| 8287 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 8288 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8289 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8290 | struct hlua *hlua; |
| 8291 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8292 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8293 | MAY_LJMP(check_args(L, 1, "set_nice")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8294 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8295 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8296 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 8297 | hlua = hlua_gethlua(L); |
| 8298 | |
| 8299 | /* If the task is not set, I'm in a start mode. */ |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8300 | if (!hlua || !hlua->task) |
| 8301 | return 0; |
| 8302 | |
| 8303 | if (nice < -1024) |
| 8304 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8305 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8306 | nice = 1024; |
| 8307 | |
| 8308 | hlua->task->nice = nice; |
| 8309 | return 0; |
| 8310 | } |
| 8311 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8312 | /* 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] | 8313 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 8314 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 8315 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8316 | * |
| 8317 | * Task wrapper are longjmp safe because the only one Lua code |
| 8318 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8319 | */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 8320 | 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] | 8321 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 8322 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8323 | enum hlua_exec status; |
| 8324 | |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 8325 | if (task->thread_mask == MAX_THREADS_MASK) |
| 8326 | task_set_affinity(task, tid_bit); |
| 8327 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8328 | /* If it is the first call to the task, we must initialize the |
| 8329 | * execution timeouts. |
| 8330 | */ |
| 8331 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 8332 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8333 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8334 | /* Execute the Lua code. */ |
| 8335 | status = hlua_ctx_resume(hlua, 1); |
| 8336 | |
| 8337 | switch (status) { |
| 8338 | /* finished or yield */ |
| 8339 | case HLUA_E_OK: |
| 8340 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 8341 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 8342 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8343 | break; |
| 8344 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 8345 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 8346 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 8347 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8348 | break; |
| 8349 | |
| 8350 | /* finished with error. */ |
| 8351 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8352 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8353 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 8354 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 8355 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8356 | break; |
| 8357 | |
| 8358 | case HLUA_E_ERR: |
| 8359 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8360 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8361 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 8362 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 8363 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8364 | break; |
| 8365 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 8366 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8367 | } |
| 8368 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8369 | /* This function is an LUA binding that register LUA function to be |
| 8370 | * executed after the HAProxy configuration parsing and before the |
| 8371 | * HAProxy scheduler starts. This function expect only one LUA |
| 8372 | * argument that is a function. This function returns nothing, but |
| 8373 | * throws if an error is encountered. |
| 8374 | */ |
| 8375 | __LJMP static int hlua_register_init(lua_State *L) |
| 8376 | { |
| 8377 | struct hlua_init_function *init; |
| 8378 | int ref; |
| 8379 | |
| 8380 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 8381 | |
| 8382 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 8383 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8384 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8385 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8386 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8387 | |
| 8388 | init->function_ref = ref; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 8389 | LIST_APPEND(&hlua_init_functions[hlua_state_id], &init->l); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8390 | return 0; |
| 8391 | } |
| 8392 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8393 | /* This functio is an LUA binding. It permits to register a task |
| 8394 | * executed in parallel of the main HAroxy activity. The task is |
| 8395 | * created and it is set in the HAProxy scheduler. It can be called |
| 8396 | * from the "init" section, "post init" or during the runtime. |
| 8397 | * |
| 8398 | * Lua prototype: |
| 8399 | * |
| 8400 | * <none> core.register_task(<function>) |
| 8401 | */ |
| 8402 | static int hlua_register_task(lua_State *L) |
| 8403 | { |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8404 | struct hlua *hlua = NULL; |
| 8405 | struct task *task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8406 | int ref; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8407 | int state_id; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8408 | |
| 8409 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 8410 | |
| 8411 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 8412 | |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 8413 | /* Get the reference state. If the reference is NULL, L is the master |
| 8414 | * state, otherwise hlua->T is. |
| 8415 | */ |
| 8416 | hlua = hlua_gethlua(L); |
| 8417 | if (hlua) |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8418 | /* we are in runtime processing */ |
| 8419 | state_id = hlua->state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 8420 | else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8421 | /* we are in initialization mode */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8422 | state_id = hlua_state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 8423 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8424 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8425 | if (!hlua) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8426 | goto alloc_error; |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8427 | HLUA_INIT(hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8428 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8429 | /* We are in the common lua state, execute the task anywhere, |
| 8430 | * otherwise, inherit the current thread identifier |
| 8431 | */ |
| 8432 | if (state_id == 0) |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 8433 | task = task_new_anywhere(); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8434 | else |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 8435 | task = task_new_here(); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 8436 | if (!task) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8437 | goto alloc_error; |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 8438 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8439 | task->context = hlua; |
| 8440 | task->process = hlua_process_task; |
| 8441 | |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8442 | if (!hlua_ctx_init(hlua, state_id, task, 1)) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8443 | goto alloc_error; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8444 | |
| 8445 | /* Restore the function in the stack. */ |
| 8446 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 8447 | hlua->nargs = 0; |
| 8448 | |
| 8449 | /* Schedule task. */ |
Willy Tarreau | e3957f8 | 2021-09-30 16:17:37 +0200 | [diff] [blame] | 8450 | task_wakeup(task, TASK_WOKEN_INIT); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8451 | |
| 8452 | return 0; |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8453 | |
| 8454 | alloc_error: |
| 8455 | task_destroy(task); |
| 8456 | hlua_ctx_destroy(hlua); |
| 8457 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 8458 | return 0; /* Never reached */ |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8459 | } |
| 8460 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8461 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 8462 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 8463 | * resume converters. |
| 8464 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8465 | 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] | 8466 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 8467 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8468 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8469 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8470 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8471 | if (!stream) |
| 8472 | return 0; |
| 8473 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8474 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8475 | * Lua context can be not initialized. This behavior |
| 8476 | * permits to save performances because a systematic |
| 8477 | * Lua initialization cause 5% performances loss. |
| 8478 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8479 | if (!stream->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8480 | struct hlua *hlua; |
| 8481 | |
| 8482 | hlua = pool_alloc(pool_head_hlua); |
| 8483 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8484 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 8485 | return 0; |
| 8486 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8487 | HLUA_INIT(hlua); |
| 8488 | stream->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8489 | 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] | 8490 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 8491 | return 0; |
| 8492 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8493 | } |
| 8494 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8495 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8496 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8497 | |
| 8498 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8499 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8500 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 8501 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8502 | else |
| 8503 | error = "critical error"; |
| 8504 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8505 | return 0; |
| 8506 | } |
| 8507 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8508 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8509 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8510 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8511 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8512 | return 0; |
| 8513 | } |
| 8514 | |
| 8515 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8516 | 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] | 8517 | |
| 8518 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8519 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8520 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8521 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8522 | return 0; |
| 8523 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8524 | hlua_smp2lua(stream->hlua->T, smp); |
| 8525 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8526 | |
| 8527 | /* push keywords in the stack. */ |
| 8528 | if (arg_p) { |
| 8529 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8530 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8531 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8532 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8533 | return 0; |
| 8534 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8535 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 8536 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8537 | } |
| 8538 | } |
| 8539 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8540 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8541 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8542 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8543 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8544 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8545 | } |
| 8546 | |
| 8547 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8548 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8549 | /* finished. */ |
| 8550 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 8551 | /* If the stack is empty, the function fails. */ |
| 8552 | if (lua_gettop(stream->hlua->T) <= 0) |
| 8553 | return 0; |
| 8554 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8555 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8556 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 8557 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8558 | return 1; |
| 8559 | |
| 8560 | /* yield. */ |
| 8561 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8562 | 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] | 8563 | return 0; |
| 8564 | |
| 8565 | /* finished with error. */ |
| 8566 | case HLUA_E_ERRMSG: |
| 8567 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8568 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8569 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 8570 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8571 | return 0; |
| 8572 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8573 | case HLUA_E_ETMOUT: |
| 8574 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 8575 | return 0; |
| 8576 | |
| 8577 | case HLUA_E_NOMEM: |
| 8578 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 8579 | return 0; |
| 8580 | |
| 8581 | case HLUA_E_YIELD: |
| 8582 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 8583 | return 0; |
| 8584 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8585 | case HLUA_E_ERR: |
| 8586 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8587 | 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] | 8588 | /* fall through */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8589 | |
| 8590 | default: |
| 8591 | return 0; |
| 8592 | } |
| 8593 | } |
| 8594 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8595 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 8596 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8597 | * resume sample-fetches. This function will be called by the sample |
| 8598 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8599 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8600 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 8601 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8602 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 8603 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8604 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8605 | const char *error; |
Christopher Faulet | 8c9e6bb | 2021-08-06 16:29:41 +0200 | [diff] [blame] | 8606 | unsigned int hflags = HLUA_TXN_NOTERM | HLUA_TXN_SMP_CTX; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8607 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8608 | if (!stream) |
| 8609 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 8610 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8611 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8612 | * Lua context can be not initialized. This behavior |
| 8613 | * permits to save performances because a systematic |
| 8614 | * Lua initialization cause 5% performances loss. |
| 8615 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8616 | if (!stream->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8617 | struct hlua *hlua; |
| 8618 | |
| 8619 | hlua = pool_alloc(pool_head_hlua); |
| 8620 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8621 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 8622 | return 0; |
| 8623 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8624 | hlua->T = NULL; |
| 8625 | stream->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8626 | 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] | 8627 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 8628 | return 0; |
| 8629 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8630 | } |
| 8631 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8632 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8633 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8634 | |
| 8635 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8636 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8637 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 8638 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8639 | else |
| 8640 | error = "critical error"; |
| 8641 | 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] | 8642 | return 0; |
| 8643 | } |
| 8644 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8645 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8646 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8647 | 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] | 8648 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8649 | return 0; |
| 8650 | } |
| 8651 | |
| 8652 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8653 | 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] | 8654 | |
| 8655 | /* push arguments in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 8656 | 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] | 8657 | 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] | 8658 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8659 | return 0; |
| 8660 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8661 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8662 | |
| 8663 | /* push keywords in the stack. */ |
| 8664 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 8665 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8666 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8667 | 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] | 8668 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8669 | return 0; |
| 8670 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8671 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 8672 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8673 | } |
| 8674 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8675 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8676 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8677 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8678 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8679 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8680 | } |
| 8681 | |
| 8682 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8683 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8684 | /* finished. */ |
| 8685 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 8686 | /* If the stack is empty, the function fails. */ |
| 8687 | if (lua_gettop(stream->hlua->T) <= 0) |
| 8688 | return 0; |
| 8689 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8690 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8691 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 8692 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8693 | |
| 8694 | /* Set the end of execution flag. */ |
| 8695 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 8696 | return 1; |
| 8697 | |
| 8698 | /* yield. */ |
| 8699 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8700 | 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] | 8701 | return 0; |
| 8702 | |
| 8703 | /* finished with error. */ |
| 8704 | case HLUA_E_ERRMSG: |
| 8705 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8706 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8707 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 8708 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8709 | return 0; |
| 8710 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8711 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8712 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 8713 | return 0; |
| 8714 | |
| 8715 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8716 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 8717 | return 0; |
| 8718 | |
| 8719 | case HLUA_E_YIELD: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8720 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 8721 | return 0; |
| 8722 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8723 | case HLUA_E_ERR: |
| 8724 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8725 | 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] | 8726 | /* fall through */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8727 | |
| 8728 | default: |
| 8729 | return 0; |
| 8730 | } |
| 8731 | } |
| 8732 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8733 | /* This function is an LUA binding used for registering |
| 8734 | * "sample-conv" functions. It expects a converter name used |
| 8735 | * in the haproxy configuration file, and an LUA function. |
| 8736 | */ |
| 8737 | __LJMP static int hlua_register_converters(lua_State *L) |
| 8738 | { |
| 8739 | struct sample_conv_kw_list *sck; |
| 8740 | const char *name; |
| 8741 | int ref; |
| 8742 | int len; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8743 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8744 | struct sample_conv *sc; |
| 8745 | struct buffer *trash; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8746 | |
| 8747 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 8748 | |
| 8749 | /* First argument : converter name. */ |
| 8750 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8751 | |
| 8752 | /* Second argument : lua function. */ |
| 8753 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 8754 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8755 | /* Check if the converter is already registered */ |
| 8756 | trash = get_trash_chunk(); |
| 8757 | chunk_printf(trash, "lua.%s", name); |
| 8758 | sc = find_sample_conv(trash->area, trash->data); |
| 8759 | if (sc != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8760 | fcn = sc->private; |
| 8761 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 8762 | ha_warning("Trying to register converter 'lua.%s' more than once. " |
| 8763 | "This will become a hard error in version 2.5.\n", name); |
| 8764 | } |
| 8765 | fcn->function_ref[hlua_state_id] = ref; |
| 8766 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8767 | } |
| 8768 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8769 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8770 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8771 | if (!sck) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8772 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 8773 | fcn = new_hlua_function(); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8774 | if (!fcn) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8775 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8776 | |
| 8777 | /* Fill fcn. */ |
| 8778 | fcn->name = strdup(name); |
| 8779 | if (!fcn->name) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8780 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8781 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8782 | |
| 8783 | /* List head */ |
| 8784 | sck->list.n = sck->list.p = NULL; |
| 8785 | |
| 8786 | /* converter keyword. */ |
| 8787 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8788 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8789 | if (!sck->kw[0].kw) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8790 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8791 | |
| 8792 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 8793 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 8794 | 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] | 8795 | sck->kw[0].val_args = NULL; |
| 8796 | sck->kw[0].in_type = SMP_T_STR; |
| 8797 | sck->kw[0].out_type = SMP_T_STR; |
| 8798 | sck->kw[0].private = fcn; |
| 8799 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8800 | /* Register this new converter */ |
| 8801 | sample_register_convs(sck); |
| 8802 | |
| 8803 | return 0; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8804 | |
| 8805 | alloc_error: |
| 8806 | release_hlua_function(fcn); |
| 8807 | ha_free(&sck); |
| 8808 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 8809 | return 0; /* Never reached */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8810 | } |
| 8811 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8812 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8813 | * "sample-fetch" functions. It expects a converter name used |
| 8814 | * in the haproxy configuration file, and an LUA function. |
| 8815 | */ |
| 8816 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 8817 | { |
| 8818 | const char *name; |
| 8819 | int ref; |
| 8820 | int len; |
| 8821 | struct sample_fetch_kw_list *sfk; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8822 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8823 | struct sample_fetch *sf; |
| 8824 | struct buffer *trash; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8825 | |
| 8826 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 8827 | |
| 8828 | /* First argument : sample-fetch name. */ |
| 8829 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8830 | |
| 8831 | /* Second argument : lua function. */ |
| 8832 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 8833 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8834 | /* Check if the sample-fetch is already registered */ |
| 8835 | trash = get_trash_chunk(); |
| 8836 | chunk_printf(trash, "lua.%s", name); |
| 8837 | sf = find_sample_fetch(trash->area, trash->data); |
| 8838 | if (sf != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8839 | fcn = sf->private; |
| 8840 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 8841 | ha_warning("Trying to register sample-fetch 'lua.%s' more than once. " |
| 8842 | "This will become a hard error in version 2.5.\n", name); |
| 8843 | } |
| 8844 | fcn->function_ref[hlua_state_id] = ref; |
| 8845 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8846 | } |
| 8847 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8848 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8849 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8850 | if (!sfk) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8851 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 8852 | fcn = new_hlua_function(); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8853 | if (!fcn) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8854 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8855 | |
| 8856 | /* Fill fcn. */ |
| 8857 | fcn->name = strdup(name); |
| 8858 | if (!fcn->name) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8859 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8860 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8861 | |
| 8862 | /* List head */ |
| 8863 | sfk->list.n = sfk->list.p = NULL; |
| 8864 | |
| 8865 | /* sample-fetch keyword. */ |
| 8866 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8867 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8868 | if (!sfk->kw[0].kw) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8869 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8870 | |
| 8871 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 8872 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 8873 | 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] | 8874 | sfk->kw[0].val_args = NULL; |
| 8875 | sfk->kw[0].out_type = SMP_T_STR; |
| 8876 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 8877 | sfk->kw[0].val = 0; |
| 8878 | sfk->kw[0].private = fcn; |
| 8879 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8880 | /* Register this new fetch. */ |
| 8881 | sample_register_fetches(sfk); |
| 8882 | |
| 8883 | return 0; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8884 | |
| 8885 | alloc_error: |
| 8886 | release_hlua_function(fcn); |
| 8887 | ha_free(&sfk); |
| 8888 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 8889 | return 0; /* Never reached */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8890 | } |
| 8891 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 8892 | /* This function is a lua binding to set the wake_time. |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8893 | */ |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 8894 | __LJMP static int hlua_set_wake_time(lua_State *L) |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8895 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8896 | struct hlua *hlua; |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8897 | unsigned int delay; |
| 8898 | unsigned int wakeup_ms; |
| 8899 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8900 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 8901 | hlua = hlua_gethlua(L); |
| 8902 | if (!hlua) { |
| 8903 | return 0; |
| 8904 | } |
| 8905 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8906 | MAY_LJMP(check_args(L, 1, "wake_time")); |
| 8907 | |
| 8908 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 8909 | wakeup_ms = tick_add(now_ms, delay); |
| 8910 | hlua->wake_time = wakeup_ms; |
| 8911 | return 0; |
| 8912 | } |
| 8913 | |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8914 | /* This function is a wrapper to execute each LUA function declared as an action |
| 8915 | * wrapper during the initialisation period. This function may return any |
| 8916 | * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is |
| 8917 | * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the |
| 8918 | * return value is the first element on the stack. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8919 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 8920 | 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] | 8921 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8922 | { |
| 8923 | char **arg; |
Christopher Faulet | 8c9e6bb | 2021-08-06 16:29:41 +0200 | [diff] [blame] | 8924 | unsigned int hflags = HLUA_TXN_ACT_CTX; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8925 | int dir, act_ret = ACT_RET_CONT; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8926 | const char *error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 8927 | |
| 8928 | switch (rule->from) { |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 8929 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 8930 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 8931 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 8932 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 8933 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8934 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8935 | goto end; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 8936 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8937 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8938 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8939 | * Lua context can be not initialized. This behavior |
| 8940 | * permits to save performances because a systematic |
| 8941 | * Lua initialization cause 5% performances loss. |
| 8942 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8943 | if (!s->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8944 | struct hlua *hlua; |
| 8945 | |
| 8946 | hlua = pool_alloc(pool_head_hlua); |
| 8947 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8948 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8949 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8950 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8951 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8952 | HLUA_INIT(hlua); |
| 8953 | s->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8954 | 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] | 8955 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8956 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8957 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8958 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8959 | } |
| 8960 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8961 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8962 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8963 | |
| 8964 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8965 | if (!SET_SAFE_LJMP(s->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8966 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 8967 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8968 | else |
| 8969 | error = "critical error"; |
| 8970 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8971 | rule->arg.hlua_rule->fcn->name, error); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8972 | goto end; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8973 | } |
| 8974 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8975 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8976 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8977 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8978 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8979 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8980 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8981 | } |
| 8982 | |
| 8983 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8984 | 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] | 8985 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8986 | /* Create and and push object stream in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 8987 | if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8988 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8989 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8990 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8991 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8992 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8993 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8994 | |
| 8995 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 8996 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8997 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8998 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 8999 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9000 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9001 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9002 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9003 | lua_pushstring(s->hlua->T, *arg); |
| 9004 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9005 | } |
| 9006 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9007 | /* Now the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9008 | RESET_SAFE_LJMP(s->hlua); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9009 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 9010 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9011 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9012 | } |
| 9013 | |
| 9014 | /* Execute the function. */ |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 9015 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9016 | /* finished. */ |
| 9017 | case HLUA_E_OK: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9018 | /* Catch the return value */ |
| 9019 | if (lua_gettop(s->hlua->T) > 0) |
| 9020 | act_ret = lua_tointeger(s->hlua->T, -1); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9021 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9022 | /* Set timeout in the required channel. */ |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 9023 | if (act_ret == ACT_RET_YIELD) { |
| 9024 | if (flags & ACT_OPT_FINAL) |
| 9025 | goto err_yield; |
| 9026 | |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 9027 | if (dir == SMP_OPT_DIR_REQ) |
| 9028 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 9029 | s->hlua->wake_time); |
| 9030 | else |
| 9031 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 9032 | s->hlua->wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9033 | } |
| 9034 | goto end; |
| 9035 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9036 | /* yield. */ |
| 9037 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 9038 | /* Set timeout in the required channel. */ |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 9039 | if (dir == SMP_OPT_DIR_REQ) |
| 9040 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 9041 | s->hlua->wake_time); |
| 9042 | else |
| 9043 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 9044 | s->hlua->wake_time); |
| 9045 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9046 | /* Some actions can be wake up when a "write" event |
| 9047 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9048 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9049 | */ |
Christopher Faulet | 51fa358 | 2019-07-26 14:54:52 +0200 | [diff] [blame] | 9050 | if (HLUA_IS_WAKERESWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9051 | s->res.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9052 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9053 | s->req.flags |= CF_WAKE_WRITE; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9054 | act_ret = ACT_RET_YIELD; |
| 9055 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9056 | |
| 9057 | /* finished with error. */ |
| 9058 | case HLUA_E_ERRMSG: |
| 9059 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9060 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9061 | rule->arg.hlua_rule->fcn->name, lua_tostring(s->hlua->T, -1)); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9062 | lua_pop(s->hlua->T, 1); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9063 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9064 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9065 | case HLUA_E_ETMOUT: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9066 | 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] | 9067 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9068 | |
| 9069 | case HLUA_E_NOMEM: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9070 | 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] | 9071 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9072 | |
| 9073 | case HLUA_E_YIELD: |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 9074 | err_yield: |
| 9075 | act_ret = ACT_RET_CONT; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9076 | 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] | 9077 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9078 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9079 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9080 | case HLUA_E_ERR: |
| 9081 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9082 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9083 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9084 | |
| 9085 | default: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9086 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9087 | } |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9088 | |
| 9089 | end: |
Christopher Faulet | 2361fd9 | 2020-07-30 10:40:58 +0200 | [diff] [blame] | 9090 | if (act_ret != ACT_RET_YIELD && s->hlua) |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 9091 | s->hlua->wake_time = TICK_ETERNITY; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9092 | return act_ret; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9093 | } |
| 9094 | |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 9095 | 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] | 9096 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 9097 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9098 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9099 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 9100 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 9101 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9102 | } |
| 9103 | |
| 9104 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 9105 | { |
| 9106 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9107 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9108 | struct task *task; |
| 9109 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9110 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9111 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 9112 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9113 | if (!hlua) { |
| 9114 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9115 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9116 | return 0; |
| 9117 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9118 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9119 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9120 | ctx->ctx.hlua_apptcp.flags = 0; |
| 9121 | |
| 9122 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 9123 | task = task_new_here(); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9124 | if (!task) { |
| 9125 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9126 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9127 | return 0; |
| 9128 | } |
| 9129 | task->nice = 0; |
| 9130 | task->context = ctx; |
| 9131 | task->process = hlua_applet_wakeup; |
| 9132 | ctx->ctx.hlua_apptcp.task = task; |
| 9133 | |
| 9134 | /* In the execution wrappers linked with a stream, the |
| 9135 | * Lua context can be not initialized. This behavior |
| 9136 | * permits to save performances because a systematic |
| 9137 | * Lua initialization cause 5% performances loss. |
| 9138 | */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9139 | 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] | 9140 | 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] | 9141 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9142 | return 0; |
| 9143 | } |
| 9144 | |
| 9145 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 9146 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9147 | |
| 9148 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9149 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9150 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 9151 | error = lua_tostring(hlua->T, -1); |
| 9152 | else |
| 9153 | error = "critical error"; |
| 9154 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9155 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9156 | return 0; |
| 9157 | } |
| 9158 | |
| 9159 | /* Check stack available size. */ |
| 9160 | if (!lua_checkstack(hlua->T, 1)) { |
| 9161 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9162 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9163 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9164 | return 0; |
| 9165 | } |
| 9166 | |
| 9167 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9168 | 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] | 9169 | |
| 9170 | /* Create and and push object stream in the stack. */ |
| 9171 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 9172 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9173 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9174 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9175 | return 0; |
| 9176 | } |
| 9177 | hlua->nargs = 1; |
| 9178 | |
| 9179 | /* push keywords in the stack. */ |
| 9180 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 9181 | if (!lua_checkstack(hlua->T, 1)) { |
| 9182 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9183 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9184 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9185 | return 0; |
| 9186 | } |
| 9187 | lua_pushstring(hlua->T, *arg); |
| 9188 | hlua->nargs++; |
| 9189 | } |
| 9190 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9191 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9192 | |
| 9193 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 9194 | si_cant_get(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 9195 | si_rx_endp_more(si); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9196 | |
| 9197 | return 1; |
| 9198 | } |
| 9199 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 9200 | void hlua_applet_tcp_fct(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9201 | { |
| 9202 | struct stream_interface *si = ctx->owner; |
| 9203 | struct stream *strm = si_strm(si); |
| 9204 | struct channel *res = si_ic(si); |
| 9205 | struct act_rule *rule = ctx->rule; |
| 9206 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9207 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9208 | |
| 9209 | /* The applet execution is already done. */ |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 9210 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) { |
| 9211 | /* eat the whole request */ |
| 9212 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9213 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 9214 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9215 | |
| 9216 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 9217 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 9218 | return; |
| 9219 | |
| 9220 | /* Execute the function. */ |
| 9221 | switch (hlua_ctx_resume(hlua, 1)) { |
| 9222 | /* finished. */ |
| 9223 | case HLUA_E_OK: |
| 9224 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 9225 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9226 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 9227 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9228 | res->flags |= CF_READ_NULL; |
| 9229 | si_shutr(si); |
| 9230 | return; |
| 9231 | |
| 9232 | /* yield. */ |
| 9233 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 9234 | if (hlua->wake_time != TICK_ETERNITY) |
| 9235 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9236 | return; |
| 9237 | |
| 9238 | /* finished with error. */ |
| 9239 | case HLUA_E_ERRMSG: |
| 9240 | /* Display log. */ |
| 9241 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9242 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9243 | lua_pop(hlua->T, 1); |
| 9244 | goto error; |
| 9245 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9246 | case HLUA_E_ETMOUT: |
| 9247 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9248 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9249 | goto error; |
| 9250 | |
| 9251 | case HLUA_E_NOMEM: |
| 9252 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9253 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9254 | goto error; |
| 9255 | |
| 9256 | case HLUA_E_YIELD: /* unexpected */ |
| 9257 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9258 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9259 | goto error; |
| 9260 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9261 | case HLUA_E_ERR: |
| 9262 | /* Display log. */ |
| 9263 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9264 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9265 | goto error; |
| 9266 | |
| 9267 | default: |
| 9268 | goto error; |
| 9269 | } |
| 9270 | |
| 9271 | error: |
| 9272 | |
| 9273 | /* For all other cases, just close the stream. */ |
| 9274 | si_shutw(si); |
| 9275 | si_shutr(si); |
| 9276 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 9277 | } |
| 9278 | |
| 9279 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 9280 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 9281 | task_destroy(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9282 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9283 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9284 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9285 | } |
| 9286 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9287 | /* The function returns 1 if the initialisation is complete, 0 if |
| 9288 | * an errors occurs and -1 if more data are required for initializing |
| 9289 | * the applet. |
| 9290 | */ |
| 9291 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 9292 | { |
| 9293 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9294 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9295 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9296 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9297 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9298 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9299 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9300 | txn = strm->txn; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 9301 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9302 | if (!hlua) { |
| 9303 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9304 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9305 | return 0; |
| 9306 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9307 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9308 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9309 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 9310 | ctx->ctx.hlua_apphttp.flags = 0; |
| 9311 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 9312 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 9313 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 9314 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9315 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 9316 | task = task_new_here(); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9317 | if (!task) { |
| 9318 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9319 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9320 | return 0; |
| 9321 | } |
| 9322 | task->nice = 0; |
| 9323 | task->context = ctx; |
| 9324 | task->process = hlua_applet_wakeup; |
| 9325 | ctx->ctx.hlua_apphttp.task = task; |
| 9326 | |
| 9327 | /* In the execution wrappers linked with a stream, the |
| 9328 | * Lua context can be not initialized. This behavior |
| 9329 | * permits to save performances because a systematic |
| 9330 | * Lua initialization cause 5% performances loss. |
| 9331 | */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9332 | 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] | 9333 | 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] | 9334 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9335 | return 0; |
| 9336 | } |
| 9337 | |
| 9338 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 9339 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9340 | |
| 9341 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9342 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9343 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 9344 | error = lua_tostring(hlua->T, -1); |
| 9345 | else |
| 9346 | error = "critical error"; |
| 9347 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9348 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9349 | return 0; |
| 9350 | } |
| 9351 | |
| 9352 | /* Check stack available size. */ |
| 9353 | if (!lua_checkstack(hlua->T, 1)) { |
| 9354 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9355 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9356 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9357 | return 0; |
| 9358 | } |
| 9359 | |
| 9360 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9361 | 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] | 9362 | |
| 9363 | /* Create and and push object stream in the stack. */ |
| 9364 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 9365 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9366 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9367 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9368 | return 0; |
| 9369 | } |
| 9370 | hlua->nargs = 1; |
| 9371 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9372 | /* push keywords in the stack. */ |
| 9373 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 9374 | if (!lua_checkstack(hlua->T, 1)) { |
| 9375 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9376 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9377 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9378 | return 0; |
| 9379 | } |
| 9380 | lua_pushstring(hlua->T, *arg); |
| 9381 | hlua->nargs++; |
| 9382 | } |
| 9383 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9384 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9385 | |
| 9386 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 9387 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9388 | |
| 9389 | return 1; |
| 9390 | } |
| 9391 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 9392 | void hlua_applet_http_fct(struct appctx *ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9393 | { |
| 9394 | struct stream_interface *si = ctx->owner; |
| 9395 | struct stream *strm = si_strm(si); |
| 9396 | struct channel *req = si_oc(si); |
| 9397 | struct channel *res = si_ic(si); |
| 9398 | struct act_rule *rule = ctx->rule; |
| 9399 | struct proxy *px = strm->be; |
| 9400 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
| 9401 | struct htx *req_htx, *res_htx; |
| 9402 | |
| 9403 | res_htx = htx_from_buf(&res->buf); |
| 9404 | |
| 9405 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 9406 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 9407 | goto out; |
| 9408 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9409 | /* Check if the input buffer is available. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9410 | if (!b_size(&res->buf)) { |
| 9411 | si_rx_room_blk(si); |
| 9412 | goto out; |
| 9413 | } |
| 9414 | /* check that the output is not closed */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9415 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9416 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 9417 | |
| 9418 | /* Set the currently running flag. */ |
| 9419 | if (!HLUA_IS_RUNNING(hlua) && |
| 9420 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
Christopher Faulet | bd878d2 | 2021-04-28 10:50:21 +0200 | [diff] [blame] | 9421 | if (!co_data(req)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9422 | si_cant_get(si); |
| 9423 | goto out; |
| 9424 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9425 | } |
| 9426 | |
| 9427 | /* Executes The applet if it is not done. */ |
| 9428 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 9429 | |
| 9430 | /* Execute the function. */ |
| 9431 | switch (hlua_ctx_resume(hlua, 1)) { |
| 9432 | /* finished. */ |
| 9433 | case HLUA_E_OK: |
| 9434 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 9435 | break; |
| 9436 | |
| 9437 | /* yield. */ |
| 9438 | case HLUA_E_AGAIN: |
| 9439 | if (hlua->wake_time != TICK_ETERNITY) |
| 9440 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9441 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9442 | |
| 9443 | /* finished with error. */ |
| 9444 | case HLUA_E_ERRMSG: |
| 9445 | /* Display log. */ |
| 9446 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9447 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9448 | lua_pop(hlua->T, 1); |
| 9449 | goto error; |
| 9450 | |
| 9451 | case HLUA_E_ETMOUT: |
| 9452 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9453 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9454 | goto error; |
| 9455 | |
| 9456 | case HLUA_E_NOMEM: |
| 9457 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9458 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9459 | goto error; |
| 9460 | |
| 9461 | case HLUA_E_YIELD: /* unexpected */ |
| 9462 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9463 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9464 | goto error; |
| 9465 | |
| 9466 | case HLUA_E_ERR: |
| 9467 | /* Display log. */ |
| 9468 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9469 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9470 | goto error; |
| 9471 | |
| 9472 | default: |
| 9473 | goto error; |
| 9474 | } |
| 9475 | } |
| 9476 | |
| 9477 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9478 | if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT) |
| 9479 | goto done; |
| 9480 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9481 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) |
| 9482 | goto error; |
| 9483 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 9484 | /* no more data are expected. Don't add TLR because mux-h1 will take care of it */ |
| 9485 | res_htx->flags |= HTX_FL_EOM; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9486 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 9487 | ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9488 | } |
| 9489 | |
| 9490 | done: |
| 9491 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9492 | if (!(res->flags & CF_SHUTR)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9493 | res->flags |= CF_READ_NULL; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9494 | si_shutr(si); |
| 9495 | } |
| 9496 | |
| 9497 | /* eat the whole request */ |
| 9498 | if (co_data(req)) { |
| 9499 | req_htx = htx_from_buf(&req->buf); |
| 9500 | co_htx_skip(req, req_htx, co_data(req)); |
| 9501 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9502 | } |
| 9503 | } |
| 9504 | |
| 9505 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9506 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9507 | return; |
| 9508 | |
| 9509 | error: |
| 9510 | |
| 9511 | /* If we are in HTTP mode, and we are not send any |
| 9512 | * data, return a 500 server error in best effort: |
| 9513 | * if there is no room available in the buffer, |
| 9514 | * just close the connection. |
| 9515 | */ |
| 9516 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 9517 | struct buffer *err = &http_err_chunks[HTTP_ERR_500]; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9518 | |
| 9519 | channel_erase(res); |
| 9520 | res->buf.data = b_data(err); |
| 9521 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 9522 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 9523 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9524 | } |
| 9525 | if (!(strm->flags & SF_ERR_MASK)) |
| 9526 | strm->flags |= SF_ERR_RESOURCE; |
| 9527 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 9528 | goto done; |
| 9529 | } |
| 9530 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9531 | static void hlua_applet_http_release(struct appctx *ctx) |
| 9532 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 9533 | task_destroy(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9534 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9535 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9536 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9537 | } |
| 9538 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9539 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9540 | * success case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9541 | * |
| 9542 | * This function can fail with an abort() due to an Lua critical error. |
| 9543 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 9544 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9545 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9546 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 9547 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9548 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9549 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9550 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9551 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9552 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9553 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9554 | if (!rule->arg.hlua_rule) { |
| 9555 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9556 | goto error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9557 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9558 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9559 | /* Memory for arguments. */ |
Tim Duesterhus | e52b6e5 | 2020-09-12 20:26:43 +0200 | [diff] [blame] | 9560 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, |
| 9561 | sizeof(*rule->arg.hlua_rule->args)); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9562 | if (!rule->arg.hlua_rule->args) { |
| 9563 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9564 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9565 | } |
| 9566 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9567 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9568 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9569 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9570 | /* Expect some arguments */ |
| 9571 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 9572 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9573 | memprintf(err, "expect %d arguments", fcn->nargs); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9574 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9575 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 9576 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9577 | if (!rule->arg.hlua_rule->args[i]) { |
| 9578 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9579 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9580 | } |
| 9581 | (*cur_arg)++; |
| 9582 | } |
| 9583 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9584 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 9585 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9586 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 9587 | return ACT_RET_PRS_OK; |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9588 | |
| 9589 | error: |
| 9590 | if (rule->arg.hlua_rule) { |
| 9591 | if (rule->arg.hlua_rule->args) { |
| 9592 | for (i = 0; i < fcn->nargs; i++) |
| 9593 | ha_free(&rule->arg.hlua_rule->args[i]); |
| 9594 | ha_free(&rule->arg.hlua_rule->args); |
| 9595 | } |
| 9596 | ha_free(&rule->arg.hlua_rule); |
| 9597 | } |
| 9598 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9599 | } |
| 9600 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9601 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 9602 | struct act_rule *rule, char **err) |
| 9603 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9604 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9605 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 9606 | /* HTTP applets are forbidden in tcp-request rules. |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9607 | * HTTP applet request requires everything initialized by |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 9608 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9609 | * The applet will be immediately initialized, but its before |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 9610 | * the call of this analyzer. |
| 9611 | */ |
| 9612 | if (rule->from != ACT_F_HTTP_REQ) { |
| 9613 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 9614 | return ACT_RET_PRS_ERR; |
| 9615 | } |
| 9616 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9617 | /* Memory for the rule. */ |
| 9618 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 9619 | if (!rule->arg.hlua_rule) { |
| 9620 | memprintf(err, "out of memory error"); |
| 9621 | return ACT_RET_PRS_ERR; |
| 9622 | } |
| 9623 | |
| 9624 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9625 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9626 | |
| 9627 | /* TODO: later accept arguments. */ |
| 9628 | rule->arg.hlua_rule->args = NULL; |
| 9629 | |
| 9630 | /* Add applet pointer in the rule. */ |
| 9631 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 9632 | rule->applet.name = fcn->name; |
| 9633 | rule->applet.init = hlua_applet_http_init; |
| 9634 | rule->applet.fct = hlua_applet_http_fct; |
| 9635 | rule->applet.release = hlua_applet_http_release; |
| 9636 | rule->applet.timeout = hlua_timeout_applet; |
| 9637 | |
| 9638 | return ACT_RET_PRS_OK; |
| 9639 | } |
| 9640 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9641 | /* This function is an LUA binding used for registering |
| 9642 | * "sample-conv" functions. It expects a converter name used |
| 9643 | * in the haproxy configuration file, and an LUA function. |
| 9644 | */ |
| 9645 | __LJMP static int hlua_register_action(lua_State *L) |
| 9646 | { |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9647 | struct action_kw_list *akl = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9648 | const char *name; |
| 9649 | int ref; |
| 9650 | int len; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9651 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9652 | int nargs; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9653 | struct buffer *trash; |
| 9654 | struct action_kw *akw; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9655 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9656 | /* Initialise the number of expected arguments at 0. */ |
| 9657 | nargs = 0; |
| 9658 | |
| 9659 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 9660 | 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] | 9661 | |
| 9662 | /* First argument : converter name. */ |
| 9663 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 9664 | |
| 9665 | /* Second argument : environment. */ |
| 9666 | if (lua_type(L, 2) != LUA_TTABLE) |
| 9667 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 9668 | |
| 9669 | /* Third argument : lua function. */ |
| 9670 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 9671 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9672 | /* 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] | 9673 | if (lua_gettop(L) >= 4) |
| 9674 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 9675 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9676 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9677 | lua_pushnil(L); |
| 9678 | while (lua_next(L, 2) != 0) { |
| 9679 | if (lua_type(L, -1) != LUA_TSTRING) |
| 9680 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 9681 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9682 | /* Check if action exists */ |
| 9683 | trash = get_trash_chunk(); |
| 9684 | chunk_printf(trash, "lua.%s", name); |
| 9685 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) { |
| 9686 | akw = tcp_req_cont_action(trash->area); |
| 9687 | } else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) { |
| 9688 | akw = tcp_res_cont_action(trash->area); |
| 9689 | } else if (strcmp(lua_tostring(L, -1), "http-req") == 0) { |
| 9690 | akw = action_http_req_custom(trash->area); |
| 9691 | } else if (strcmp(lua_tostring(L, -1), "http-res") == 0) { |
| 9692 | akw = action_http_res_custom(trash->area); |
| 9693 | } else { |
| 9694 | akw = NULL; |
| 9695 | } |
| 9696 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9697 | fcn = akw->private; |
| 9698 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 9699 | ha_warning("Trying to register action 'lua.%s' more than once. " |
| 9700 | "This will become a hard error in version 2.5.\n", name); |
| 9701 | } |
| 9702 | fcn->function_ref[hlua_state_id] = ref; |
| 9703 | |
| 9704 | /* pop the environment string. */ |
| 9705 | lua_pop(L, 1); |
| 9706 | continue; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9707 | } |
| 9708 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9709 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 9710 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9711 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9712 | if (!akl) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9713 | goto alloc_error;; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 9714 | fcn = new_hlua_function(); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9715 | if (!fcn) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9716 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9717 | |
| 9718 | /* Fill fcn. */ |
| 9719 | fcn->name = strdup(name); |
| 9720 | if (!fcn->name) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9721 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9722 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9723 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 9724 | /* Set the expected number of arguments. */ |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9725 | fcn->nargs = nargs; |
| 9726 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9727 | /* List head */ |
| 9728 | akl->list.n = akl->list.p = NULL; |
| 9729 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9730 | /* action keyword. */ |
| 9731 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9732 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9733 | if (!akl->kw[0].kw) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9734 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9735 | |
| 9736 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 9737 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 9738 | akl->kw[0].flags = 0; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9739 | akl->kw[0].private = fcn; |
| 9740 | akl->kw[0].parse = action_register_lua; |
| 9741 | |
| 9742 | /* select the action registering point. */ |
| 9743 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 9744 | tcp_req_cont_keywords_register(akl); |
| 9745 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 9746 | tcp_res_cont_keywords_register(akl); |
| 9747 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 9748 | http_req_keywords_register(akl); |
| 9749 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 9750 | http_res_keywords_register(akl); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9751 | else { |
| 9752 | release_hlua_function(fcn); |
| 9753 | if (akl) |
| 9754 | ha_free((char **)&(akl->kw[0].kw)); |
| 9755 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 9756 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9757 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 9758 | "are expected.", lua_tostring(L, -1))); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9759 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9760 | |
| 9761 | /* pop the environment string. */ |
| 9762 | lua_pop(L, 1); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9763 | |
| 9764 | /* reset for next loop */ |
| 9765 | akl = NULL; |
| 9766 | fcn = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9767 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9768 | return ACT_RET_PRS_OK; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9769 | |
| 9770 | alloc_error: |
| 9771 | release_hlua_function(fcn); |
| 9772 | ha_free(&akl); |
| 9773 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 9774 | return 0; /* Never reached */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9775 | } |
| 9776 | |
| 9777 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 9778 | struct act_rule *rule, char **err) |
| 9779 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9780 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9781 | |
Christopher Faulet | 280f85b | 2019-07-15 15:02:04 +0200 | [diff] [blame] | 9782 | if (px->mode == PR_MODE_HTTP) { |
| 9783 | memprintf(err, "Lua TCP services cannot be used on HTTP proxies"); |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 9784 | return ACT_RET_PRS_ERR; |
| 9785 | } |
| 9786 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9787 | /* Memory for the rule. */ |
| 9788 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 9789 | if (!rule->arg.hlua_rule) { |
| 9790 | memprintf(err, "out of memory error"); |
| 9791 | return ACT_RET_PRS_ERR; |
| 9792 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9793 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9794 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9795 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9796 | |
| 9797 | /* TODO: later accept arguments. */ |
| 9798 | rule->arg.hlua_rule->args = NULL; |
| 9799 | |
| 9800 | /* Add applet pointer in the rule. */ |
| 9801 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 9802 | rule->applet.name = fcn->name; |
| 9803 | rule->applet.init = hlua_applet_tcp_init; |
| 9804 | rule->applet.fct = hlua_applet_tcp_fct; |
| 9805 | rule->applet.release = hlua_applet_tcp_release; |
| 9806 | rule->applet.timeout = hlua_timeout_applet; |
| 9807 | |
| 9808 | return 0; |
| 9809 | } |
| 9810 | |
| 9811 | /* This function is an LUA binding used for registering |
| 9812 | * "sample-conv" functions. It expects a converter name used |
| 9813 | * in the haproxy configuration file, and an LUA function. |
| 9814 | */ |
| 9815 | __LJMP static int hlua_register_service(lua_State *L) |
| 9816 | { |
| 9817 | struct action_kw_list *akl; |
| 9818 | const char *name; |
| 9819 | const char *env; |
| 9820 | int ref; |
| 9821 | int len; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9822 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9823 | struct buffer *trash; |
| 9824 | struct action_kw *akw; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9825 | |
| 9826 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 9827 | |
| 9828 | /* First argument : converter name. */ |
| 9829 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 9830 | |
| 9831 | /* Second argument : environment. */ |
| 9832 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 9833 | |
| 9834 | /* Third argument : lua function. */ |
| 9835 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 9836 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9837 | /* Check for service already registered */ |
| 9838 | trash = get_trash_chunk(); |
| 9839 | chunk_printf(trash, "lua.%s", name); |
| 9840 | akw = service_find(trash->area); |
| 9841 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9842 | fcn = akw->private; |
| 9843 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 9844 | ha_warning("Trying to register service 'lua.%s' more than once. " |
| 9845 | "This will become a hard error in version 2.5.\n", name); |
| 9846 | } |
| 9847 | fcn->function_ref[hlua_state_id] = ref; |
| 9848 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9849 | } |
| 9850 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9851 | /* Allocate and fill the sample fetch keyword struct. */ |
| 9852 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 9853 | if (!akl) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9854 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 9855 | fcn = new_hlua_function(); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9856 | if (!fcn) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9857 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9858 | |
| 9859 | /* Fill fcn. */ |
| 9860 | len = strlen("<lua.>") + strlen(name) + 1; |
| 9861 | fcn->name = calloc(1, len); |
| 9862 | if (!fcn->name) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9863 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9864 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9865 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9866 | |
| 9867 | /* List head */ |
| 9868 | akl->list.n = akl->list.p = NULL; |
| 9869 | |
| 9870 | /* converter keyword. */ |
| 9871 | len = strlen("lua.") + strlen(name) + 1; |
| 9872 | akl->kw[0].kw = calloc(1, len); |
| 9873 | if (!akl->kw[0].kw) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9874 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9875 | |
| 9876 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 9877 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 9878 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9879 | if (strcmp(env, "tcp") == 0) |
| 9880 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9881 | else if (strcmp(env, "http") == 0) |
| 9882 | akl->kw[0].parse = action_register_service_http; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9883 | else { |
| 9884 | release_hlua_function(fcn); |
| 9885 | if (akl) |
| 9886 | ha_free((char **)&(akl->kw[0].kw)); |
| 9887 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 9888 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 9889 | "'tcp' or 'http' are expected.", env)); |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9890 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9891 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 9892 | akl->kw[0].flags = 0; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9893 | akl->kw[0].private = fcn; |
| 9894 | |
| 9895 | /* End of array. */ |
| 9896 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 9897 | |
| 9898 | /* Register this new converter */ |
| 9899 | service_keywords_register(akl); |
| 9900 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9901 | return 0; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9902 | |
| 9903 | alloc_error: |
| 9904 | release_hlua_function(fcn); |
| 9905 | ha_free(&akl); |
| 9906 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 9907 | return 0; /* Never reached */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9908 | } |
| 9909 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9910 | /* This function initialises Lua cli handler. It copies the |
| 9911 | * arguments in the Lua stack and create channel IO objects. |
| 9912 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 9913 | 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] | 9914 | { |
| 9915 | struct hlua *hlua; |
| 9916 | struct hlua_function *fcn; |
| 9917 | int i; |
| 9918 | const char *error; |
| 9919 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9920 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9921 | appctx->ctx.hlua_cli.fcn = private; |
| 9922 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 9923 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9924 | if (!hlua) { |
| 9925 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 9926 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9927 | } |
| 9928 | HLUA_INIT(hlua); |
| 9929 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9930 | |
| 9931 | /* Create task used by signal to wakeup applets. |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9932 | * 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] | 9933 | * applet_http. It is absolutely compatible. |
| 9934 | */ |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 9935 | appctx->ctx.hlua_cli.task = task_new_here(); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9936 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 9937 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 9938 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9939 | } |
| 9940 | appctx->ctx.hlua_cli.task->nice = 0; |
| 9941 | appctx->ctx.hlua_cli.task->context = appctx; |
| 9942 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 9943 | |
| 9944 | /* Initialises the Lua context */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9945 | 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] | 9946 | 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] | 9947 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9948 | } |
| 9949 | |
| 9950 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9951 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9952 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 9953 | error = lua_tostring(hlua->T, -1); |
| 9954 | else |
| 9955 | error = "critical error"; |
| 9956 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 9957 | goto error; |
| 9958 | } |
| 9959 | |
| 9960 | /* Check stack available size. */ |
| 9961 | if (!lua_checkstack(hlua->T, 2)) { |
| 9962 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 9963 | goto error; |
| 9964 | } |
| 9965 | |
| 9966 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9967 | 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] | 9968 | |
| 9969 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 9970 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9971 | */ |
| 9972 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 9973 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 9974 | goto error; |
| 9975 | } |
| 9976 | hlua->nargs = 1; |
| 9977 | |
| 9978 | /* push keywords in the stack. */ |
| 9979 | for (i = 0; *args[i]; i++) { |
| 9980 | /* Check stack available size. */ |
| 9981 | if (!lua_checkstack(hlua->T, 1)) { |
| 9982 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 9983 | goto error; |
| 9984 | } |
| 9985 | lua_pushstring(hlua->T, args[i]); |
| 9986 | hlua->nargs++; |
| 9987 | } |
| 9988 | |
| 9989 | /* We must initialize the execution timeouts. */ |
| 9990 | hlua->max_time = hlua_timeout_session; |
| 9991 | |
| 9992 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9993 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9994 | |
| 9995 | /* It's ok */ |
| 9996 | return 0; |
| 9997 | |
| 9998 | /* It's not ok. */ |
| 9999 | error: |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10000 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10001 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 10002 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10003 | return 1; |
| 10004 | } |
| 10005 | |
| 10006 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 10007 | { |
| 10008 | struct hlua *hlua; |
| 10009 | struct stream_interface *si; |
| 10010 | struct hlua_function *fcn; |
| 10011 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10012 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10013 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 10014 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10015 | |
| 10016 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 10017 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 10018 | return 1; |
| 10019 | |
| 10020 | /* Execute the function. */ |
| 10021 | switch (hlua_ctx_resume(hlua, 1)) { |
| 10022 | |
| 10023 | /* finished. */ |
| 10024 | case HLUA_E_OK: |
| 10025 | return 1; |
| 10026 | |
| 10027 | /* yield. */ |
| 10028 | case HLUA_E_AGAIN: |
| 10029 | /* We want write. */ |
| 10030 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 10031 | si_rx_room_blk(si); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10032 | /* Set the timeout. */ |
| 10033 | if (hlua->wake_time != TICK_ETERNITY) |
| 10034 | task_schedule(hlua->task, hlua->wake_time); |
| 10035 | return 0; |
| 10036 | |
| 10037 | /* finished with error. */ |
| 10038 | case HLUA_E_ERRMSG: |
| 10039 | /* Display log. */ |
| 10040 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 10041 | fcn->name, lua_tostring(hlua->T, -1)); |
| 10042 | lua_pop(hlua->T, 1); |
| 10043 | return 1; |
| 10044 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 10045 | case HLUA_E_ETMOUT: |
| 10046 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 10047 | fcn->name); |
| 10048 | return 1; |
| 10049 | |
| 10050 | case HLUA_E_NOMEM: |
| 10051 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 10052 | fcn->name); |
| 10053 | return 1; |
| 10054 | |
| 10055 | case HLUA_E_YIELD: /* unexpected */ |
| 10056 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 10057 | fcn->name); |
| 10058 | return 1; |
| 10059 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10060 | case HLUA_E_ERR: |
| 10061 | /* Display log. */ |
| 10062 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 10063 | fcn->name); |
| 10064 | return 1; |
| 10065 | |
| 10066 | default: |
| 10067 | return 1; |
| 10068 | } |
| 10069 | |
| 10070 | return 1; |
| 10071 | } |
| 10072 | |
| 10073 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 10074 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10075 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10076 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10077 | } |
| 10078 | |
| 10079 | /* This function is an LUA binding used for registering |
| 10080 | * new keywords in the cli. It expects a list of keywords |
| 10081 | * which are the "path". It is limited to 5 keywords. A |
| 10082 | * description of the command, a function to be executed |
| 10083 | * for the parsing and a function for io handlers. |
| 10084 | */ |
| 10085 | __LJMP static int hlua_register_cli(lua_State *L) |
| 10086 | { |
| 10087 | struct cli_kw_list *cli_kws; |
| 10088 | const char *message; |
| 10089 | int ref_io; |
| 10090 | int len; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10091 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10092 | int index; |
| 10093 | int i; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10094 | struct buffer *trash; |
| 10095 | const char *kw[5]; |
| 10096 | struct cli_kw *cli_kw; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10097 | const char *errmsg; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10098 | |
| 10099 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 10100 | |
| 10101 | /* First argument : an array of maximum 5 keywords. */ |
| 10102 | if (!lua_istable(L, 1)) |
| 10103 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 10104 | |
| 10105 | /* Second argument : string with contextual message. */ |
| 10106 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 10107 | |
| 10108 | /* Third and fourth argument : lua function. */ |
| 10109 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 10110 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10111 | /* Check for CLI service already registered */ |
| 10112 | trash = get_trash_chunk(); |
| 10113 | index = 0; |
| 10114 | lua_pushnil(L); |
| 10115 | memset(kw, 0, sizeof(kw)); |
| 10116 | while (lua_next(L, 1) != 0) { |
| 10117 | if (index >= CLI_PREFIX_KW_NB) |
| 10118 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 10119 | if (lua_type(L, -1) != LUA_TSTRING) |
| 10120 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 10121 | kw[index] = lua_tostring(L, -1); |
| 10122 | if (index == 0) |
| 10123 | chunk_printf(trash, "%s", kw[index]); |
| 10124 | else |
| 10125 | chunk_appendf(trash, " %s", kw[index]); |
| 10126 | index++; |
| 10127 | lua_pop(L, 1); |
| 10128 | } |
| 10129 | cli_kw = cli_find_kw_exact((char **)kw); |
| 10130 | if (cli_kw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10131 | fcn = cli_kw->private; |
| 10132 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 10133 | ha_warning("Trying to register CLI keyword 'lua.%s' more than once. " |
| 10134 | "This will become a hard error in version 2.5.\n", trash->area); |
| 10135 | } |
| 10136 | fcn->function_ref[hlua_state_id] = ref_io; |
| 10137 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10138 | } |
| 10139 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10140 | /* Allocate and fill the sample fetch keyword struct. */ |
| 10141 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10142 | if (!cli_kws) { |
| 10143 | errmsg = "Lua out of memory error."; |
| 10144 | goto error; |
| 10145 | } |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 10146 | fcn = new_hlua_function(); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10147 | if (!fcn) { |
| 10148 | errmsg = "Lua out of memory error."; |
| 10149 | goto error; |
| 10150 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10151 | |
| 10152 | /* Fill path. */ |
| 10153 | index = 0; |
| 10154 | lua_pushnil(L); |
| 10155 | while(lua_next(L, 1) != 0) { |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10156 | if (index >= 5) { |
| 10157 | errmsg = "1st argument must be a table with a maximum of 5 entries"; |
| 10158 | goto error; |
| 10159 | } |
| 10160 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 10161 | errmsg = "1st argument must be a table filled with strings"; |
| 10162 | goto error; |
| 10163 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10164 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10165 | if (!cli_kws->kw[0].str_kw[index]) { |
| 10166 | errmsg = "Lua out of memory error."; |
| 10167 | goto error; |
| 10168 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10169 | index++; |
| 10170 | lua_pop(L, 1); |
| 10171 | } |
| 10172 | |
| 10173 | /* Copy help message. */ |
| 10174 | cli_kws->kw[0].usage = strdup(message); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10175 | if (!cli_kws->kw[0].usage) { |
| 10176 | errmsg = "Lua out of memory error."; |
| 10177 | goto error; |
| 10178 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10179 | |
| 10180 | /* Fill fcn io handler. */ |
| 10181 | len = strlen("<lua.cli>") + 1; |
| 10182 | for (i = 0; i < index; i++) |
| 10183 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 10184 | fcn->name = calloc(1, len); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10185 | if (!fcn->name) { |
| 10186 | errmsg = "Lua out of memory error."; |
| 10187 | goto error; |
| 10188 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10189 | strncat((char *)fcn->name, "<lua.cli", len); |
| 10190 | for (i = 0; i < index; i++) { |
| 10191 | strncat((char *)fcn->name, ".", len); |
| 10192 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 10193 | } |
| 10194 | strncat((char *)fcn->name, ">", len); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 10195 | fcn->function_ref[hlua_state_id] = ref_io; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10196 | |
| 10197 | /* Fill last entries. */ |
| 10198 | cli_kws->kw[0].private = fcn; |
| 10199 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 10200 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 10201 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 10202 | |
| 10203 | /* Register this new converter */ |
| 10204 | cli_register_kw(cli_kws); |
| 10205 | |
| 10206 | return 0; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10207 | |
| 10208 | error: |
| 10209 | release_hlua_function(fcn); |
| 10210 | if (cli_kws) { |
| 10211 | for (i = 0; i < index; i++) |
| 10212 | ha_free((char **)&(cli_kws->kw[0].str_kw[i])); |
| 10213 | ha_free((char **)&(cli_kws->kw[0].usage)); |
| 10214 | } |
| 10215 | ha_free(&cli_kws); |
| 10216 | WILL_LJMP(luaL_error(L, errmsg)); |
| 10217 | return 0; /* Never reached */ |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10218 | } |
| 10219 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10220 | static int hlua_filter_init_per_thread(struct proxy *px, struct flt_conf *fconf) |
| 10221 | { |
| 10222 | struct hlua_flt_config *conf = fconf->conf; |
| 10223 | lua_State *L; |
| 10224 | int error, pos, state_id, flt_ref; |
| 10225 | |
| 10226 | state_id = reg_flt_to_stack_id(conf->reg); |
| 10227 | L = hlua_states[state_id]; |
| 10228 | pos = lua_gettop(L); |
| 10229 | |
| 10230 | /* The filter parsing function */ |
| 10231 | lua_rawgeti(L, LUA_REGISTRYINDEX, conf->reg->fun_ref[state_id]); |
| 10232 | |
| 10233 | /* Push the filter class on the stack and resolve all callbacks */ |
| 10234 | lua_rawgeti(L, LUA_REGISTRYINDEX, conf->reg->flt_ref[state_id]); |
| 10235 | |
| 10236 | /* Duplicate the filter class so each filter will have its own copy */ |
| 10237 | lua_newtable(L); |
| 10238 | lua_pushnil(L); |
| 10239 | |
| 10240 | while (lua_next(L, pos+2)) { |
| 10241 | lua_pushvalue(L, -2); |
| 10242 | lua_insert(L, -2); |
| 10243 | lua_settable(L, -4); |
| 10244 | } |
| 10245 | flt_ref = luaL_ref(L, LUA_REGISTRYINDEX); |
| 10246 | |
| 10247 | /* Remove the original lua filter class from the stack */ |
| 10248 | lua_pop(L, 1); |
| 10249 | |
| 10250 | /* Push the copy on the stack */ |
| 10251 | lua_rawgeti(L, LUA_REGISTRYINDEX, flt_ref); |
| 10252 | |
| 10253 | /* extra args are pushed in a table */ |
| 10254 | lua_newtable(L); |
| 10255 | for (pos = 0; conf->args[pos]; pos++) { |
| 10256 | /* Check stack available size. */ |
| 10257 | if (!lua_checkstack(L, 1)) { |
| 10258 | ha_alert("Lua filter '%s' : Lua error : full stack.", conf->reg->name); |
| 10259 | goto error; |
| 10260 | } |
| 10261 | lua_pushstring(L, conf->args[pos]); |
| 10262 | lua_rawseti(L, -2, lua_rawlen(L, -2) + 1); |
| 10263 | } |
| 10264 | |
| 10265 | error = lua_pcall(L, 2, LUA_MULTRET, 0); |
| 10266 | switch (error) { |
| 10267 | case LUA_OK: |
| 10268 | /* replace the filter ref */ |
| 10269 | conf->ref[state_id] = flt_ref; |
| 10270 | break; |
| 10271 | case LUA_ERRRUN: |
| 10272 | ha_alert("Lua filter '%s' : runtime error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 10273 | goto error; |
| 10274 | case LUA_ERRMEM: |
| 10275 | ha_alert("Lua filter '%s' : out of memory error", conf->reg->name); |
| 10276 | goto error; |
| 10277 | case LUA_ERRERR: |
| 10278 | ha_alert("Lua filter '%s' : message handler error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 10279 | goto error; |
| 10280 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
| 10281 | case LUA_ERRGCMM: |
| 10282 | ha_alert("Lua filter '%s' : garbage collector error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 10283 | goto error; |
| 10284 | #endif |
| 10285 | default: |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 10286 | 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] | 10287 | goto error; |
| 10288 | } |
| 10289 | |
| 10290 | lua_settop(L, 0); |
| 10291 | return 0; |
| 10292 | |
| 10293 | error: |
| 10294 | lua_settop(L, 0); |
| 10295 | return -1; |
| 10296 | } |
| 10297 | |
| 10298 | static void hlua_filter_deinit_per_thread(struct proxy *px, struct flt_conf *fconf) |
| 10299 | { |
| 10300 | struct hlua_flt_config *conf = fconf->conf; |
| 10301 | lua_State *L; |
| 10302 | int state_id; |
| 10303 | |
| 10304 | if (!conf) |
| 10305 | return; |
| 10306 | |
| 10307 | state_id = reg_flt_to_stack_id(conf->reg); |
| 10308 | L = hlua_states[state_id]; |
| 10309 | luaL_unref(L, LUA_REGISTRYINDEX, conf->ref[state_id]); |
| 10310 | } |
| 10311 | |
| 10312 | static int hlua_filter_init(struct proxy *px, struct flt_conf *fconf) |
| 10313 | { |
| 10314 | struct hlua_flt_config *conf = fconf->conf; |
| 10315 | int state_id = reg_flt_to_stack_id(conf->reg); |
| 10316 | |
| 10317 | /* Rely on per-thread init for global scripts */ |
| 10318 | if (!state_id) |
| 10319 | return hlua_filter_init_per_thread(px, fconf); |
| 10320 | return 0; |
| 10321 | } |
| 10322 | |
| 10323 | static void hlua_filter_deinit(struct proxy *px, struct flt_conf *fconf) |
| 10324 | { |
| 10325 | |
| 10326 | if (fconf->conf) { |
| 10327 | struct hlua_flt_config *conf = fconf->conf; |
| 10328 | int state_id = reg_flt_to_stack_id(conf->reg); |
| 10329 | int pos; |
| 10330 | |
| 10331 | /* Rely on per-thread deinit for global scripts */ |
| 10332 | if (!state_id) |
| 10333 | hlua_filter_deinit_per_thread(px, fconf); |
| 10334 | |
| 10335 | for (pos = 0; conf->args[pos]; pos++) |
| 10336 | free(conf->args[pos]); |
| 10337 | free(conf->args); |
| 10338 | } |
| 10339 | ha_free(&fconf->conf); |
| 10340 | ha_free((char **)&fconf->id); |
| 10341 | ha_free(&fconf->ops); |
| 10342 | } |
| 10343 | |
| 10344 | static int hlua_filter_new(struct stream *s, struct filter *filter) |
| 10345 | { |
| 10346 | struct hlua_flt_config *conf = FLT_CONF(filter); |
| 10347 | struct hlua_flt_ctx *flt_ctx = NULL; |
| 10348 | int ret = 1; |
| 10349 | |
| 10350 | /* In the execution wrappers linked with a stream, the |
| 10351 | * Lua context can be not initialized. This behavior |
| 10352 | * permits to save performances because a systematic |
| 10353 | * Lua initialization cause 5% performances loss. |
| 10354 | */ |
| 10355 | if (!s->hlua) { |
| 10356 | struct hlua *hlua; |
| 10357 | |
| 10358 | hlua = pool_alloc(pool_head_hlua); |
| 10359 | if (!hlua) { |
| 10360 | SEND_ERR(s->be, "Lua filter '%s': can't initialize Lua context.\n", |
| 10361 | conf->reg->name); |
| 10362 | ret = 0; |
| 10363 | goto end; |
| 10364 | } |
| 10365 | HLUA_INIT(hlua); |
| 10366 | s->hlua = hlua; |
| 10367 | if (!hlua_ctx_init(s->hlua, reg_flt_to_stack_id(conf->reg), s->task, 0)) { |
| 10368 | SEND_ERR(s->be, "Lua filter '%s': can't initialize Lua context.\n", |
| 10369 | conf->reg->name); |
| 10370 | ret = 0; |
| 10371 | goto end; |
| 10372 | } |
| 10373 | } |
| 10374 | |
| 10375 | flt_ctx = pool_zalloc(pool_head_hlua_flt_ctx); |
| 10376 | if (!flt_ctx) { |
| 10377 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 10378 | conf->reg->name); |
| 10379 | ret = 0; |
| 10380 | goto end; |
| 10381 | } |
| 10382 | flt_ctx->hlua[0] = pool_alloc(pool_head_hlua); |
| 10383 | flt_ctx->hlua[1] = pool_alloc(pool_head_hlua); |
| 10384 | if (!flt_ctx->hlua[0] || !flt_ctx->hlua[1]) { |
| 10385 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 10386 | conf->reg->name); |
| 10387 | ret = 0; |
| 10388 | goto end; |
| 10389 | } |
| 10390 | HLUA_INIT(flt_ctx->hlua[0]); |
| 10391 | HLUA_INIT(flt_ctx->hlua[1]); |
| 10392 | if (!hlua_ctx_init(flt_ctx->hlua[0], reg_flt_to_stack_id(conf->reg), s->task, 0) || |
| 10393 | !hlua_ctx_init(flt_ctx->hlua[1], reg_flt_to_stack_id(conf->reg), s->task, 0)) { |
| 10394 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 10395 | conf->reg->name); |
| 10396 | ret = 0; |
| 10397 | goto end; |
| 10398 | } |
| 10399 | |
| 10400 | if (!HLUA_IS_RUNNING(s->hlua)) { |
| 10401 | /* The following Lua calls can fail. */ |
| 10402 | if (!SET_SAFE_LJMP(s->hlua)) { |
| 10403 | const char *error; |
| 10404 | |
| 10405 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 10406 | error = lua_tostring(s->hlua->T, -1); |
| 10407 | else |
| 10408 | error = "critical error"; |
| 10409 | SEND_ERR(s->be, "Lua filter '%s': %s.\n", conf->reg->name, error); |
| 10410 | ret = 0; |
| 10411 | goto end; |
| 10412 | } |
| 10413 | |
| 10414 | /* Check stack size. */ |
| 10415 | if (!lua_checkstack(s->hlua->T, 1)) { |
| 10416 | 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] | 10417 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10418 | ret = 0; |
| 10419 | goto end; |
| 10420 | } |
| 10421 | |
| 10422 | lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, conf->ref[s->hlua->state_id]); |
| 10423 | if (lua_getfield(s->hlua->T, -1, "new") != LUA_TFUNCTION) { |
| 10424 | SEND_ERR(s->be, "Lua filter '%s': 'new' field is not a function.\n", |
| 10425 | conf->reg->name); |
| 10426 | RESET_SAFE_LJMP(s->hlua); |
| 10427 | ret = 0; |
| 10428 | goto end; |
| 10429 | } |
| 10430 | lua_insert(s->hlua->T, -2); |
| 10431 | |
| 10432 | /* Push the copy on the stack */ |
| 10433 | s->hlua->nargs = 1; |
| 10434 | |
| 10435 | /* We must initialize the execution timeouts. */ |
| 10436 | s->hlua->max_time = hlua_timeout_session; |
| 10437 | |
| 10438 | /* At this point the execution is safe. */ |
| 10439 | RESET_SAFE_LJMP(s->hlua); |
| 10440 | } |
| 10441 | |
| 10442 | switch (hlua_ctx_resume(s->hlua, 0)) { |
| 10443 | case HLUA_E_OK: |
| 10444 | /* Nothing returned or not a table, ignore the filter for current stream */ |
| 10445 | if (!lua_gettop(s->hlua->T) || !lua_istable(s->hlua->T, 1)) { |
| 10446 | ret = 0; |
| 10447 | goto end; |
| 10448 | } |
| 10449 | |
| 10450 | /* Attached the filter pointer to the ctx */ |
| 10451 | lua_pushstring(s->hlua->T, "__filter"); |
| 10452 | lua_pushlightuserdata(s->hlua->T, filter); |
| 10453 | lua_settable(s->hlua->T, -3); |
| 10454 | |
| 10455 | /* Save a ref on the filter ctx */ |
| 10456 | lua_pushvalue(s->hlua->T, 1); |
| 10457 | flt_ctx->ref = luaL_ref(s->hlua->T, LUA_REGISTRYINDEX); |
| 10458 | filter->ctx = flt_ctx; |
| 10459 | break; |
| 10460 | case HLUA_E_ERRMSG: |
| 10461 | SEND_ERR(s->be, "Lua filter '%s' : %s.\n", conf->reg->name, lua_tostring(s->hlua->T, -1)); |
| 10462 | ret = -1; |
| 10463 | goto end; |
| 10464 | case HLUA_E_ETMOUT: |
| 10465 | SEND_ERR(s->be, "Lua filter '%s' : 'new' execution timeout.\n", conf->reg->name); |
| 10466 | ret = 0; |
| 10467 | goto end; |
| 10468 | case HLUA_E_NOMEM: |
| 10469 | SEND_ERR(s->be, "Lua filter '%s' : out of memory error.\n", conf->reg->name); |
| 10470 | ret = 0; |
| 10471 | goto end; |
| 10472 | case HLUA_E_AGAIN: |
| 10473 | case HLUA_E_YIELD: |
| 10474 | SEND_ERR(s->be, "Lua filter '%s': yield functions like core.tcp() or core.sleep()" |
| 10475 | " are not allowed from 'new' function.\n", conf->reg->name); |
| 10476 | ret = 0; |
| 10477 | goto end; |
| 10478 | case HLUA_E_ERR: |
| 10479 | SEND_ERR(s->be, "Lua filter '%s': 'new' returns an unknown error.\n", conf->reg->name); |
| 10480 | ret = 0; |
| 10481 | goto end; |
| 10482 | default: |
| 10483 | ret = 0; |
| 10484 | goto end; |
| 10485 | } |
| 10486 | |
| 10487 | end: |
| 10488 | if (s->hlua) |
| 10489 | lua_settop(s->hlua->T, 0); |
| 10490 | if (ret <= 0) { |
| 10491 | if (flt_ctx) { |
| 10492 | hlua_ctx_destroy(flt_ctx->hlua[0]); |
| 10493 | hlua_ctx_destroy(flt_ctx->hlua[1]); |
| 10494 | pool_free(pool_head_hlua_flt_ctx, flt_ctx); |
| 10495 | } |
| 10496 | } |
| 10497 | return ret; |
| 10498 | } |
| 10499 | |
| 10500 | static void hlua_filter_delete(struct stream *s, struct filter *filter) |
| 10501 | { |
| 10502 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10503 | |
| 10504 | luaL_unref(s->hlua->T, LUA_REGISTRYINDEX, flt_ctx->ref); |
| 10505 | hlua_ctx_destroy(flt_ctx->hlua[0]); |
| 10506 | hlua_ctx_destroy(flt_ctx->hlua[1]); |
| 10507 | pool_free(pool_head_hlua_flt_ctx, flt_ctx); |
| 10508 | filter->ctx = NULL; |
| 10509 | } |
| 10510 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 10511 | static int hlua_filter_from_payload(struct filter *filter) |
| 10512 | { |
| 10513 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10514 | |
| 10515 | return (flt_ctx && !!(flt_ctx->flags & HLUA_FLT_CTX_FL_PAYLOAD)); |
| 10516 | } |
| 10517 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10518 | static int hlua_filter_callback(struct stream *s, struct filter *filter, const char *fun, |
| 10519 | int dir, unsigned int flags) |
| 10520 | { |
| 10521 | struct hlua *flt_hlua; |
| 10522 | struct hlua_flt_config *conf = FLT_CONF(filter); |
| 10523 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10524 | unsigned int hflags = HLUA_TXN_FLT_CTX; |
| 10525 | int ret = 1; |
| 10526 | |
| 10527 | flt_hlua = flt_ctx->hlua[(dir == SMP_OPT_DIR_REQ ? 0 : 1)]; |
| 10528 | if (!flt_hlua) |
| 10529 | goto end; |
| 10530 | |
| 10531 | if (!HLUA_IS_RUNNING(flt_hlua)) { |
| 10532 | int extra_idx = lua_gettop(flt_hlua->T); |
| 10533 | |
| 10534 | /* The following Lua calls can fail. */ |
| 10535 | if (!SET_SAFE_LJMP(flt_hlua)) { |
| 10536 | const char *error; |
| 10537 | |
| 10538 | if (lua_type(flt_hlua->T, -1) == LUA_TSTRING) |
| 10539 | error = lua_tostring(flt_hlua->T, -1); |
| 10540 | else |
| 10541 | error = "critical error"; |
| 10542 | SEND_ERR(s->be, "Lua filter '%s': %s.\n", conf->reg->name, error); |
| 10543 | goto end; |
| 10544 | } |
| 10545 | |
| 10546 | /* Check stack size. */ |
| 10547 | if (!lua_checkstack(flt_hlua->T, 3)) { |
| 10548 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 10549 | RESET_SAFE_LJMP(flt_hlua); |
| 10550 | goto end; |
| 10551 | } |
| 10552 | |
| 10553 | lua_rawgeti(flt_hlua->T, LUA_REGISTRYINDEX, flt_ctx->ref); |
| 10554 | if (lua_getfield(flt_hlua->T, -1, fun) != LUA_TFUNCTION) { |
| 10555 | RESET_SAFE_LJMP(flt_hlua); |
| 10556 | goto end; |
| 10557 | } |
| 10558 | lua_insert(flt_hlua->T, -2); |
| 10559 | |
| 10560 | if (!hlua_txn_new(flt_hlua->T, s, s->be, dir, hflags)) { |
| 10561 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 10562 | RESET_SAFE_LJMP(flt_hlua); |
| 10563 | goto end; |
| 10564 | } |
| 10565 | flt_hlua->nargs = 2; |
| 10566 | |
| 10567 | if (flags & HLUA_FLT_CB_ARG_CHN) { |
| 10568 | if (dir == SMP_OPT_DIR_REQ) |
| 10569 | lua_getfield(flt_hlua->T, -1, "req"); |
| 10570 | else |
| 10571 | lua_getfield(flt_hlua->T, -1, "res"); |
| 10572 | if (lua_type(flt_hlua->T, -1) == LUA_TTABLE) { |
| 10573 | lua_pushstring(flt_hlua->T, "__filter"); |
| 10574 | lua_pushlightuserdata(flt_hlua->T, filter); |
| 10575 | lua_settable(flt_hlua->T, -3); |
| 10576 | } |
| 10577 | flt_hlua->nargs++; |
| 10578 | } |
| 10579 | else if (flags & HLUA_FLT_CB_ARG_HTTP_MSG) { |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 10580 | if (dir == SMP_OPT_DIR_REQ) |
| 10581 | lua_getfield(flt_hlua->T, -1, "http_req"); |
| 10582 | else |
| 10583 | lua_getfield(flt_hlua->T, -1, "http_res"); |
| 10584 | if (lua_type(flt_hlua->T, -1) == LUA_TTABLE) { |
| 10585 | lua_pushstring(flt_hlua->T, "__filter"); |
| 10586 | lua_pushlightuserdata(flt_hlua->T, filter); |
| 10587 | lua_settable(flt_hlua->T, -3); |
| 10588 | } |
| 10589 | flt_hlua->nargs++; |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10590 | } |
| 10591 | |
| 10592 | /* Check stack size. */ |
| 10593 | if (!lua_checkstack(flt_hlua->T, 1)) { |
| 10594 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 10595 | RESET_SAFE_LJMP(flt_hlua); |
| 10596 | goto end; |
| 10597 | } |
| 10598 | |
| 10599 | while (extra_idx--) { |
| 10600 | lua_pushvalue(flt_hlua->T, 1); |
| 10601 | lua_remove(flt_hlua->T, 1); |
| 10602 | flt_hlua->nargs++; |
| 10603 | } |
| 10604 | |
| 10605 | /* We must initialize the execution timeouts. */ |
| 10606 | flt_hlua->max_time = hlua_timeout_session; |
| 10607 | |
| 10608 | /* At this point the execution is safe. */ |
| 10609 | RESET_SAFE_LJMP(flt_hlua); |
| 10610 | } |
| 10611 | |
| 10612 | switch (hlua_ctx_resume(flt_hlua, !(flags & HLUA_FLT_CB_FINAL))) { |
| 10613 | case HLUA_E_OK: |
| 10614 | /* Catch the return value if it required */ |
| 10615 | if ((flags & HLUA_FLT_CB_RETVAL) && lua_gettop(flt_hlua->T) > 0) { |
| 10616 | ret = lua_tointeger(flt_hlua->T, -1); |
| 10617 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 10618 | } |
| 10619 | |
| 10620 | /* Set timeout in the required channel. */ |
| 10621 | if (flt_hlua->wake_time != TICK_ETERNITY) { |
| 10622 | if (dir == SMP_OPT_DIR_REQ) |
| 10623 | s->req.analyse_exp = flt_hlua->wake_time; |
| 10624 | else |
| 10625 | s->res.analyse_exp = flt_hlua->wake_time; |
| 10626 | } |
| 10627 | break; |
| 10628 | case HLUA_E_AGAIN: |
| 10629 | /* Set timeout in the required channel. */ |
| 10630 | if (flt_hlua->wake_time != TICK_ETERNITY) { |
| 10631 | if (dir == SMP_OPT_DIR_REQ) |
| 10632 | s->req.analyse_exp = flt_hlua->wake_time; |
| 10633 | else |
| 10634 | s->res.analyse_exp = flt_hlua->wake_time; |
| 10635 | } |
| 10636 | /* Some actions can be wake up when a "write" event |
| 10637 | * is detected on a response channel. This is useful |
| 10638 | * only for actions targeted on the requests. |
| 10639 | */ |
| 10640 | if (HLUA_IS_WAKERESWR(flt_hlua)) |
| 10641 | s->res.flags |= CF_WAKE_WRITE; |
| 10642 | if (HLUA_IS_WAKEREQWR(flt_hlua)) |
| 10643 | s->req.flags |= CF_WAKE_WRITE; |
| 10644 | ret = 0; |
| 10645 | goto end; |
| 10646 | case HLUA_E_ERRMSG: |
| 10647 | SEND_ERR(s->be, "Lua filter '%s' : %s.\n", conf->reg->name, lua_tostring(flt_hlua->T, -1)); |
| 10648 | ret = -1; |
| 10649 | goto end; |
| 10650 | case HLUA_E_ETMOUT: |
| 10651 | SEND_ERR(s->be, "Lua filter '%s' : '%s' callback execution timeout.\n", conf->reg->name, fun); |
| 10652 | goto end; |
| 10653 | case HLUA_E_NOMEM: |
| 10654 | SEND_ERR(s->be, "Lua filter '%s' : out of memory error.\n", conf->reg->name); |
| 10655 | goto end; |
| 10656 | case HLUA_E_YIELD: |
| 10657 | SEND_ERR(s->be, "Lua filter '%s': yield functions like core.tcp() or core.sleep()" |
| 10658 | " are not allowed from '%s' callback.\n", conf->reg->name, fun); |
| 10659 | goto end; |
| 10660 | case HLUA_E_ERR: |
| 10661 | SEND_ERR(s->be, "Lua filter '%s': '%s' returns an unknown error.\n", conf->reg->name, fun); |
| 10662 | goto end; |
| 10663 | default: |
| 10664 | goto end; |
| 10665 | } |
| 10666 | |
| 10667 | |
| 10668 | end: |
| 10669 | return ret; |
| 10670 | } |
| 10671 | |
| 10672 | static int hlua_filter_start_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 10673 | { |
| 10674 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10675 | |
| 10676 | flt_ctx->flags = 0; |
| 10677 | return hlua_filter_callback(s, filter, "start_analyze", |
| 10678 | (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 10679 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_CHN)); |
| 10680 | } |
| 10681 | |
| 10682 | static int hlua_filter_end_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 10683 | { |
| 10684 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10685 | |
| 10686 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 10687 | return hlua_filter_callback(s, filter, "end_analyze", |
| 10688 | (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 10689 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_CHN)); |
| 10690 | } |
| 10691 | |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 10692 | static int hlua_filter_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg) |
| 10693 | { |
| 10694 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10695 | |
| 10696 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 10697 | return hlua_filter_callback(s, filter, "http_headers", |
| 10698 | (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 10699 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 10700 | } |
| 10701 | |
| 10702 | static int hlua_filter_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg, |
| 10703 | unsigned int offset, unsigned int len) |
| 10704 | { |
| 10705 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10706 | struct hlua *flt_hlua; |
| 10707 | int dir = (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES); |
| 10708 | int idx = (dir == SMP_OPT_DIR_REQ ? 0 : 1); |
| 10709 | int ret; |
| 10710 | |
| 10711 | flt_hlua = flt_ctx->hlua[idx]; |
| 10712 | flt_ctx->cur_off[idx] = offset; |
| 10713 | flt_ctx->cur_len[idx] = len; |
| 10714 | flt_ctx->flags |= HLUA_FLT_CTX_FL_PAYLOAD; |
| 10715 | ret = hlua_filter_callback(s, filter, "http_payload", dir, (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 10716 | if (ret != -1) { |
| 10717 | ret = flt_ctx->cur_len[idx]; |
| 10718 | if (lua_gettop(flt_hlua->T) > 0) { |
| 10719 | ret = lua_tointeger(flt_hlua->T, -1); |
| 10720 | if (ret > flt_ctx->cur_len[idx]) |
| 10721 | ret = flt_ctx->cur_len[idx]; |
| 10722 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 10723 | } |
| 10724 | } |
| 10725 | return ret; |
| 10726 | } |
| 10727 | |
| 10728 | static int hlua_filter_http_end(struct stream *s, struct filter *filter, struct http_msg *msg) |
| 10729 | { |
| 10730 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10731 | |
| 10732 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 10733 | return hlua_filter_callback(s, filter, "http_end", |
| 10734 | (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 10735 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 10736 | } |
| 10737 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10738 | static int hlua_filter_tcp_payload(struct stream *s, struct filter *filter, struct channel *chn, |
| 10739 | unsigned int offset, unsigned int len) |
| 10740 | { |
| 10741 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10742 | struct hlua *flt_hlua; |
| 10743 | int dir = (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES); |
| 10744 | int idx = (dir == SMP_OPT_DIR_REQ ? 0 : 1); |
| 10745 | int ret; |
| 10746 | |
| 10747 | flt_hlua = flt_ctx->hlua[idx]; |
| 10748 | flt_ctx->cur_off[idx] = offset; |
| 10749 | flt_ctx->cur_len[idx] = len; |
| 10750 | flt_ctx->flags |= HLUA_FLT_CTX_FL_PAYLOAD; |
| 10751 | ret = hlua_filter_callback(s, filter, "tcp_payload", dir, (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_ARG_CHN)); |
| 10752 | if (ret != -1) { |
| 10753 | ret = flt_ctx->cur_len[idx]; |
| 10754 | if (lua_gettop(flt_hlua->T) > 0) { |
| 10755 | ret = lua_tointeger(flt_hlua->T, -1); |
| 10756 | if (ret > flt_ctx->cur_len[idx]) |
| 10757 | ret = flt_ctx->cur_len[idx]; |
| 10758 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 10759 | } |
| 10760 | } |
| 10761 | return ret; |
| 10762 | } |
| 10763 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10764 | static int hlua_filter_parse_fct(char **args, int *cur_arg, struct proxy *px, |
| 10765 | struct flt_conf *fconf, char **err, void *private) |
| 10766 | { |
| 10767 | struct hlua_reg_filter *reg_flt = private; |
| 10768 | lua_State *L; |
| 10769 | struct hlua_flt_config *conf = NULL; |
| 10770 | const char *flt_id = NULL; |
| 10771 | int state_id, pos, flt_flags = 0; |
| 10772 | struct flt_ops *hlua_flt_ops = NULL; |
| 10773 | |
| 10774 | state_id = reg_flt_to_stack_id(reg_flt); |
| 10775 | L = hlua_states[state_id]; |
| 10776 | |
| 10777 | /* Initialize the filter ops with default callbacks */ |
| 10778 | hlua_flt_ops = calloc(1, sizeof(*hlua_flt_ops)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10779 | if (!hlua_flt_ops) |
| 10780 | goto error; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10781 | hlua_flt_ops->init = hlua_filter_init; |
| 10782 | hlua_flt_ops->deinit = hlua_filter_deinit; |
| 10783 | if (state_id) { |
| 10784 | /* Set per-thread callback if script is loaded per-thread */ |
| 10785 | hlua_flt_ops->init_per_thread = hlua_filter_init_per_thread; |
| 10786 | hlua_flt_ops->deinit_per_thread = hlua_filter_deinit_per_thread; |
| 10787 | } |
| 10788 | hlua_flt_ops->attach = hlua_filter_new; |
| 10789 | hlua_flt_ops->detach = hlua_filter_delete; |
| 10790 | |
| 10791 | /* Push the filter class on the stack and resolve all callbacks */ |
| 10792 | lua_rawgeti(L, LUA_REGISTRYINDEX, reg_flt->flt_ref[state_id]); |
| 10793 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10794 | if (lua_getfield(L, -1, "start_analyze") == LUA_TFUNCTION) |
| 10795 | hlua_flt_ops->channel_start_analyze = hlua_filter_start_analyze; |
| 10796 | lua_pop(L, 1); |
| 10797 | if (lua_getfield(L, -1, "end_analyze") == LUA_TFUNCTION) |
| 10798 | hlua_flt_ops->channel_end_analyze = hlua_filter_end_analyze; |
| 10799 | lua_pop(L, 1); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 10800 | if (lua_getfield(L, -1, "http_headers") == LUA_TFUNCTION) |
| 10801 | hlua_flt_ops->http_headers = hlua_filter_http_headers; |
| 10802 | lua_pop(L, 1); |
| 10803 | if (lua_getfield(L, -1, "http_payload") == LUA_TFUNCTION) |
| 10804 | hlua_flt_ops->http_payload = hlua_filter_http_payload; |
| 10805 | lua_pop(L, 1); |
| 10806 | if (lua_getfield(L, -1, "http_end") == LUA_TFUNCTION) |
| 10807 | hlua_flt_ops->http_end = hlua_filter_http_end; |
| 10808 | lua_pop(L, 1); |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10809 | if (lua_getfield(L, -1, "tcp_payload") == LUA_TFUNCTION) |
| 10810 | hlua_flt_ops->tcp_payload = hlua_filter_tcp_payload; |
| 10811 | lua_pop(L, 1); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10812 | |
| 10813 | /* Get id and flags of the filter class */ |
| 10814 | if (lua_getfield(L, -1, "id") == LUA_TSTRING) |
| 10815 | flt_id = lua_tostring(L, -1); |
| 10816 | lua_pop(L, 1); |
| 10817 | if (lua_getfield(L, -1, "flags") == LUA_TNUMBER) |
| 10818 | flt_flags = lua_tointeger(L, -1); |
| 10819 | lua_pop(L, 1); |
| 10820 | |
| 10821 | /* Create the filter config */ |
| 10822 | conf = calloc(1, sizeof(*conf)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10823 | if (!conf) |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10824 | goto error; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10825 | conf->reg = reg_flt; |
| 10826 | |
| 10827 | /* duplicate args */ |
| 10828 | for (pos = 0; *args[*cur_arg + 1 + pos]; pos++); |
| 10829 | conf->args = calloc(pos + 1, sizeof(*conf->args)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10830 | if (!conf->args) |
| 10831 | goto error; |
| 10832 | for (pos = 0; *args[*cur_arg + 1 + pos]; pos++) { |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10833 | conf->args[pos] = strdup(args[*cur_arg + 1 + pos]); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10834 | if (!conf->args[pos]) |
| 10835 | goto error; |
| 10836 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10837 | conf->args[pos] = NULL; |
| 10838 | *cur_arg += pos + 1; |
| 10839 | |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10840 | if (flt_id) { |
| 10841 | fconf->id = strdup(flt_id); |
| 10842 | if (!fconf->id) |
| 10843 | goto error; |
| 10844 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10845 | fconf->flags = flt_flags; |
| 10846 | fconf->conf = conf; |
| 10847 | fconf->ops = hlua_flt_ops; |
| 10848 | |
| 10849 | lua_settop(L, 0); |
| 10850 | return 0; |
| 10851 | |
| 10852 | error: |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10853 | 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] | 10854 | free(hlua_flt_ops); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10855 | if (conf && conf->args) { |
| 10856 | for (pos = 0; conf->args[pos]; pos++) |
| 10857 | free(conf->args[pos]); |
| 10858 | free(conf->args); |
| 10859 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10860 | free(conf); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10861 | free((char *)fconf->id); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10862 | lua_settop(L, 0); |
| 10863 | return -1; |
| 10864 | } |
| 10865 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10866 | __LJMP static int hlua_register_data_filter(lua_State *L) |
| 10867 | { |
| 10868 | struct filter *filter; |
| 10869 | struct channel *chn; |
| 10870 | |
| 10871 | MAY_LJMP(check_args(L, 2, "register_data_filter")); |
| 10872 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
| 10873 | chn = MAY_LJMP(hlua_checkchannel(L, 2)); |
| 10874 | |
| 10875 | lua_getfield(L, 1, "__filter"); |
| 10876 | MAY_LJMP(luaL_checktype(L, -1, LUA_TLIGHTUSERDATA)); |
| 10877 | filter = lua_touserdata (L, -1); |
| 10878 | lua_pop(L, 1); |
| 10879 | |
| 10880 | register_data_filter(chn_strm(chn), chn, filter); |
| 10881 | return 1; |
| 10882 | } |
| 10883 | |
| 10884 | __LJMP static int hlua_unregister_data_filter(lua_State *L) |
| 10885 | { |
| 10886 | struct filter *filter; |
| 10887 | struct channel *chn; |
| 10888 | |
| 10889 | MAY_LJMP(check_args(L, 2, "unregister_data_filter")); |
| 10890 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
| 10891 | chn = MAY_LJMP(hlua_checkchannel(L, 2)); |
| 10892 | |
| 10893 | lua_getfield(L, 1, "__filter"); |
| 10894 | MAY_LJMP(luaL_checktype(L, -1, LUA_TLIGHTUSERDATA)); |
| 10895 | filter = lua_touserdata (L, -1); |
| 10896 | lua_pop(L, 1); |
| 10897 | |
| 10898 | unregister_data_filter(chn_strm(chn), chn, filter); |
| 10899 | return 1; |
| 10900 | } |
| 10901 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10902 | /* 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] | 10903 | * 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] | 10904 | * parse configuration arguments. |
| 10905 | */ |
| 10906 | __LJMP static int hlua_register_filter(lua_State *L) |
| 10907 | { |
| 10908 | struct buffer *trash; |
| 10909 | struct flt_kw_list *fkl; |
| 10910 | struct flt_kw *fkw; |
| 10911 | const char *name; |
| 10912 | struct hlua_reg_filter *reg_flt= NULL; |
| 10913 | int flt_ref, fun_ref; |
| 10914 | int len; |
| 10915 | |
| 10916 | MAY_LJMP(check_args(L, 3, "register_filter")); |
| 10917 | |
| 10918 | /* First argument : filter name. */ |
| 10919 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 10920 | |
| 10921 | /* Second argument : The filter class */ |
| 10922 | flt_ref = MAY_LJMP(hlua_checktable(L, 2)); |
| 10923 | |
| 10924 | /* Third argument : lua function. */ |
| 10925 | fun_ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 10926 | |
| 10927 | trash = get_trash_chunk(); |
| 10928 | chunk_printf(trash, "lua.%s", name); |
| 10929 | fkw = flt_find_kw(trash->area); |
| 10930 | if (fkw != NULL) { |
| 10931 | reg_flt = fkw->private; |
| 10932 | if (reg_flt->flt_ref[hlua_state_id] != -1 || reg_flt->fun_ref[hlua_state_id] != -1) { |
| 10933 | ha_warning("Trying to register filter 'lua.%s' more than once. " |
| 10934 | "This will become a hard error in version 2.5.\n", name); |
| 10935 | } |
| 10936 | reg_flt->flt_ref[hlua_state_id] = flt_ref; |
| 10937 | reg_flt->fun_ref[hlua_state_id] = fun_ref; |
| 10938 | return 0; |
| 10939 | } |
| 10940 | |
| 10941 | fkl = calloc(1, sizeof(*fkl) + sizeof(struct flt_kw) * 2); |
| 10942 | if (!fkl) |
| 10943 | goto alloc_error; |
| 10944 | fkl->scope = "HLUA"; |
| 10945 | |
| 10946 | reg_flt = new_hlua_reg_filter(name); |
| 10947 | if (!reg_flt) |
| 10948 | goto alloc_error; |
| 10949 | |
| 10950 | reg_flt->flt_ref[hlua_state_id] = flt_ref; |
| 10951 | reg_flt->fun_ref[hlua_state_id] = fun_ref; |
| 10952 | |
| 10953 | /* The filter keyword */ |
| 10954 | len = strlen("lua.") + strlen(name) + 1; |
| 10955 | fkl->kw[0].kw = calloc(1, len); |
| 10956 | if (!fkl->kw[0].kw) |
| 10957 | goto alloc_error; |
| 10958 | |
| 10959 | snprintf((char *)fkl->kw[0].kw, len, "lua.%s", name); |
| 10960 | |
| 10961 | fkl->kw[0].parse = hlua_filter_parse_fct; |
| 10962 | fkl->kw[0].private = reg_flt; |
| 10963 | memset(&fkl->kw[1], 0, sizeof(*fkl->kw)); |
| 10964 | |
| 10965 | /* Register this new filter */ |
| 10966 | flt_register_keywords(fkl); |
| 10967 | |
| 10968 | return 0; |
| 10969 | |
| 10970 | alloc_error: |
| 10971 | release_hlua_reg_filter(reg_flt); |
| 10972 | ha_free(&fkl); |
| 10973 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 10974 | return 0; /* Never reached */ |
| 10975 | } |
| 10976 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 10977 | 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] | 10978 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 10979 | char **err, unsigned int *timeout) |
| 10980 | { |
| 10981 | const char *error; |
| 10982 | |
| 10983 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 10984 | if (error == PARSE_TIME_OVER) { |
| 10985 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 10986 | args[1], args[0]); |
| 10987 | return -1; |
| 10988 | } |
| 10989 | else if (error == PARSE_TIME_UNDER) { |
| 10990 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 10991 | args[1], args[0]); |
| 10992 | return -1; |
| 10993 | } |
| 10994 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 10995 | memprintf(err, "%s: invalid timeout", args[0]); |
| 10996 | return -1; |
| 10997 | } |
| 10998 | return 0; |
| 10999 | } |
| 11000 | |
| 11001 | 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] | 11002 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11003 | char **err) |
| 11004 | { |
| 11005 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 11006 | file, line, err, &hlua_timeout_session); |
| 11007 | } |
| 11008 | |
| 11009 | 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] | 11010 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11011 | char **err) |
| 11012 | { |
| 11013 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 11014 | file, line, err, &hlua_timeout_task); |
| 11015 | } |
| 11016 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11017 | 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] | 11018 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11019 | char **err) |
| 11020 | { |
| 11021 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 11022 | file, line, err, &hlua_timeout_applet); |
| 11023 | } |
| 11024 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 11025 | 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] | 11026 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 11027 | char **err) |
| 11028 | { |
| 11029 | char *error; |
| 11030 | |
| 11031 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 11032 | if (*error != '\0') { |
| 11033 | memprintf(err, "%s: invalid number", args[0]); |
| 11034 | return -1; |
| 11035 | } |
| 11036 | return 0; |
| 11037 | } |
| 11038 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11039 | 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] | 11040 | const struct proxy *defpx, const char *file, int line, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11041 | char **err) |
| 11042 | { |
| 11043 | char *error; |
| 11044 | |
| 11045 | if (*(args[1]) == 0) { |
| 11046 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 11047 | return -1; |
| 11048 | } |
| 11049 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 11050 | if (*error != '\0') { |
| 11051 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 11052 | return -1; |
| 11053 | } |
| 11054 | return 0; |
| 11055 | } |
| 11056 | |
| 11057 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11058 | /* This function is called by the main configuration key "lua-load". It loads and |
| 11059 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 11060 | * the main lua entry point. |
| 11061 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 11062 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 11063 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11064 | * |
| 11065 | * In some error case, LUA set an error message in top of the stack. This function |
| 11066 | * 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] | 11067 | * |
| 11068 | * This function can fail with an abort() due to an Lua critical error. |
| 11069 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 11070 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11071 | */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11072 | static int hlua_load_state(char *filename, lua_State *L, char **err) |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11073 | { |
| 11074 | int error; |
| 11075 | |
| 11076 | /* Just load and compile the file. */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11077 | error = luaL_loadfile(L, filename); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11078 | if (error) { |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11079 | memprintf(err, "error in Lua file '%s': %s", filename, lua_tostring(L, -1)); |
| 11080 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11081 | return -1; |
| 11082 | } |
| 11083 | |
| 11084 | /* If no syntax error where detected, execute the code. */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11085 | error = lua_pcall(L, 0, LUA_MULTRET, 0); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11086 | switch (error) { |
| 11087 | case LUA_OK: |
| 11088 | break; |
| 11089 | case LUA_ERRRUN: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11090 | memprintf(err, "Lua runtime error: %s\n", lua_tostring(L, -1)); |
| 11091 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11092 | return -1; |
| 11093 | case LUA_ERRMEM: |
Thierry Fournier | de6145f | 2020-11-29 00:55:53 +0100 | [diff] [blame] | 11094 | memprintf(err, "Lua out of memory error\n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11095 | return -1; |
| 11096 | case LUA_ERRERR: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11097 | memprintf(err, "Lua message handler error: %s\n", lua_tostring(L, -1)); |
| 11098 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11099 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 11100 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11101 | case LUA_ERRGCMM: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11102 | memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(L, -1)); |
| 11103 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11104 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 11105 | #endif |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11106 | default: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11107 | memprintf(err, "Lua unknown error: %s\n", lua_tostring(L, -1)); |
| 11108 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11109 | return -1; |
| 11110 | } |
| 11111 | |
| 11112 | return 0; |
| 11113 | } |
| 11114 | |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11115 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 11116 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11117 | char **err) |
| 11118 | { |
| 11119 | if (*(args[1]) == 0) { |
| 11120 | memprintf(err, "'%s' expects a file name as parameter.\n", args[0]); |
| 11121 | return -1; |
| 11122 | } |
| 11123 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11124 | /* loading for global state */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 11125 | hlua_state_id = 0; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11126 | ha_set_thread(NULL); |
Thierry Fournier | afc63e2 | 2020-11-28 17:06:51 +0100 | [diff] [blame] | 11127 | return hlua_load_state(args[1], hlua_states[0], err); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11128 | } |
| 11129 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11130 | 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] | 11131 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11132 | char **err) |
| 11133 | { |
| 11134 | int len; |
| 11135 | |
| 11136 | if (*(args[1]) == 0) { |
| 11137 | memprintf(err, "'%s' expects a file as parameter.\n", args[0]); |
| 11138 | return -1; |
| 11139 | } |
| 11140 | |
| 11141 | if (per_thread_load == NULL) { |
| 11142 | /* allocate the first entry large enough to store the final NULL */ |
| 11143 | per_thread_load = calloc(1, sizeof(*per_thread_load)); |
| 11144 | if (per_thread_load == NULL) { |
| 11145 | memprintf(err, "out of memory error"); |
| 11146 | return -1; |
| 11147 | } |
| 11148 | } |
| 11149 | |
| 11150 | /* count used entries */ |
| 11151 | for (len = 0; per_thread_load[len] != NULL; len++) |
| 11152 | ; |
| 11153 | |
| 11154 | per_thread_load = realloc(per_thread_load, (len + 2) * sizeof(*per_thread_load)); |
| 11155 | if (per_thread_load == NULL) { |
| 11156 | memprintf(err, "out of memory error"); |
| 11157 | return -1; |
| 11158 | } |
| 11159 | |
| 11160 | per_thread_load[len] = strdup(args[1]); |
| 11161 | per_thread_load[len + 1] = NULL; |
| 11162 | |
| 11163 | if (per_thread_load[len] == NULL) { |
| 11164 | memprintf(err, "out of memory error"); |
| 11165 | return -1; |
| 11166 | } |
| 11167 | |
| 11168 | /* loading for thread 1 only */ |
| 11169 | hlua_state_id = 1; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11170 | ha_set_thread(NULL); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11171 | return hlua_load_state(args[1], hlua_states[1], err); |
| 11172 | } |
| 11173 | |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 11174 | /* Prepend the given <path> followed by a semicolon to the `package.<type>` variable |
| 11175 | * in the given <ctx>. |
| 11176 | */ |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 11177 | static int hlua_prepend_path(lua_State *L, char *type, char *path) |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 11178 | { |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 11179 | lua_getglobal(L, "package"); /* push package variable */ |
| 11180 | lua_pushstring(L, path); /* push given path */ |
| 11181 | lua_pushstring(L, ";"); /* push semicolon */ |
| 11182 | lua_getfield(L, -3, type); /* push old path */ |
| 11183 | lua_concat(L, 3); /* concatenate to new path */ |
| 11184 | lua_setfield(L, -2, type); /* store new path */ |
| 11185 | lua_pop(L, 1); /* pop package variable */ |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 11186 | |
| 11187 | return 0; |
| 11188 | } |
| 11189 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11190 | 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] | 11191 | const struct proxy *defpx, const char *file, int line, |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11192 | char **err) |
| 11193 | { |
| 11194 | char *path; |
| 11195 | char *type = "path"; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11196 | struct prepend_path *p = NULL; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11197 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11198 | if (too_many_args(2, args, err, NULL)) { |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11199 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11200 | } |
| 11201 | |
| 11202 | if (!(*args[1])) { |
| 11203 | memprintf(err, "'%s' expects to receive a <path> as argument", args[0]); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11204 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11205 | } |
| 11206 | path = args[1]; |
| 11207 | |
| 11208 | if (*args[2]) { |
| 11209 | if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) { |
| 11210 | 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] | 11211 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11212 | } |
| 11213 | type = args[2]; |
| 11214 | } |
| 11215 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11216 | p = calloc(1, sizeof(*p)); |
| 11217 | if (p == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 11218 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11219 | goto err; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11220 | } |
| 11221 | p->path = strdup(path); |
| 11222 | if (p->path == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 11223 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11224 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11225 | } |
| 11226 | p->type = strdup(type); |
| 11227 | if (p->type == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 11228 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11229 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11230 | } |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 11231 | LIST_APPEND(&prepend_path_list, &p->l); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11232 | |
Tim Duesterhus | 9e5e586 | 2021-10-11 18:51:08 +0200 | [diff] [blame] | 11233 | /* Handle the global state and the per-thread state for the first |
| 11234 | * thread. The remaining threads will be initialized based on |
| 11235 | * prepend_path_list. |
| 11236 | */ |
| 11237 | for (size_t i = 0; i < 2; i++) { |
| 11238 | lua_State *L = hlua_states[i]; |
| 11239 | const char *error; |
| 11240 | |
| 11241 | if (setjmp(safe_ljmp_env) != 0) { |
| 11242 | lua_atpanic(L, hlua_panic_safe); |
| 11243 | if (lua_type(L, -1) == LUA_TSTRING) |
| 11244 | error = lua_tostring(L, -1); |
| 11245 | else |
| 11246 | error = "critical error"; |
| 11247 | fprintf(stderr, "lua-prepend-path: %s.\n", error); |
| 11248 | exit(1); |
| 11249 | } else { |
| 11250 | lua_atpanic(L, hlua_panic_ljmp); |
| 11251 | } |
| 11252 | |
| 11253 | hlua_prepend_path(L, type, path); |
| 11254 | |
| 11255 | lua_atpanic(L, hlua_panic_safe); |
| 11256 | } |
| 11257 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11258 | return 0; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11259 | |
| 11260 | err2: |
| 11261 | free(p->type); |
| 11262 | free(p->path); |
| 11263 | err: |
| 11264 | free(p); |
| 11265 | return -1; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11266 | } |
| 11267 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11268 | /* configuration keywords declaration */ |
| 11269 | static struct cfg_kw_list cfg_kws = {{ },{ |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11270 | { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11271 | { CFG_GLOBAL, "lua-load", hlua_load }, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11272 | { CFG_GLOBAL, "lua-load-per-thread", hlua_load_per_thread }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11273 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 11274 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 11275 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 11276 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11277 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11278 | { 0, NULL, NULL }, |
| 11279 | }}; |
| 11280 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 11281 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 11282 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 11283 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 11284 | /* This function can fail with an abort() due to an Lua critical error. |
| 11285 | * We are in the initialisation process of HAProxy, this abort() is |
| 11286 | * tolerated. |
| 11287 | */ |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11288 | int hlua_post_init_state(lua_State *L) |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11289 | { |
| 11290 | struct hlua_init_function *init; |
| 11291 | const char *msg; |
| 11292 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 11293 | const char *error; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11294 | const char *kind; |
| 11295 | const char *trace; |
| 11296 | int return_status = 1; |
| 11297 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 11298 | int nres; |
| 11299 | #endif |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11300 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11301 | /* disable memory limit checks if limit is not set */ |
| 11302 | if (!hlua_global_allocator.limit) |
| 11303 | hlua_global_allocator.limit = ~hlua_global_allocator.limit; |
| 11304 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 11305 | /* Call post initialisation function in safe environment. */ |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11306 | if (setjmp(safe_ljmp_env) != 0) { |
| 11307 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11308 | if (lua_type(L, -1) == LUA_TSTRING) |
| 11309 | error = lua_tostring(L, -1); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 11310 | else |
| 11311 | error = "critical error"; |
| 11312 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 11313 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11314 | } else { |
| 11315 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 11316 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 11317 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11318 | hlua_fcn_post_init(L); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 11319 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 11320 | list_for_each_entry(init, &hlua_init_functions[hlua_state_id], l) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11321 | lua_rawgeti(L, LUA_REGISTRYINDEX, init->function_ref); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11322 | |
| 11323 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11324 | ret = lua_resume(L, L, 0, &nres); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11325 | #else |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11326 | ret = lua_resume(L, L, 0); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11327 | #endif |
| 11328 | kind = NULL; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11329 | switch (ret) { |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11330 | |
| 11331 | case LUA_OK: |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11332 | lua_pop(L, -1); |
Thierry Fournier | 13d08b7 | 2020-11-28 11:02:58 +0100 | [diff] [blame] | 11333 | break; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11334 | |
| 11335 | case LUA_ERRERR: |
| 11336 | kind = "message handler error"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 11337 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11338 | case LUA_ERRRUN: |
| 11339 | if (!kind) |
| 11340 | kind = "runtime error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11341 | msg = lua_tostring(L, -1); |
| 11342 | lua_settop(L, 0); /* Empty the stack. */ |
| 11343 | lua_pop(L, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 11344 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11345 | if (msg) |
| 11346 | ha_alert("Lua init: %s: '%s' from %s\n", kind, msg, trace); |
| 11347 | else |
| 11348 | ha_alert("Lua init: unknown %s from %s\n", kind, trace); |
| 11349 | return_status = 0; |
| 11350 | break; |
| 11351 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11352 | default: |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11353 | /* Unknown error */ |
| 11354 | kind = "Unknown error"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 11355 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11356 | case LUA_YIELD: |
| 11357 | /* yield is not configured at this step, this state doesn't happen */ |
| 11358 | if (!kind) |
| 11359 | kind = "yield not allowed"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 11360 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11361 | case LUA_ERRMEM: |
| 11362 | if (!kind) |
| 11363 | kind = "out of memory error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11364 | lua_settop(L, 0); |
| 11365 | lua_pop(L, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 11366 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11367 | ha_alert("Lua init: %s: %s\n", kind, trace); |
| 11368 | return_status = 0; |
| 11369 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11370 | } |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11371 | if (!return_status) |
| 11372 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11373 | } |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11374 | |
| 11375 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11376 | return return_status; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11377 | } |
| 11378 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11379 | int hlua_post_init() |
| 11380 | { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11381 | int ret; |
| 11382 | int i; |
| 11383 | int errors; |
| 11384 | char *err = NULL; |
| 11385 | struct hlua_function *fcn; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11386 | struct hlua_reg_filter *reg_flt; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11387 | |
Willy Tarreau | b131049 | 2021-08-30 09:35:18 +0200 | [diff] [blame] | 11388 | #if defined(USE_OPENSSL) |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11389 | /* Initialize SSL server. */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 11390 | if (socket_ssl->xprt->prepare_srv) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11391 | int saved_used_backed = global.ssl_used_backend; |
| 11392 | // don't affect maxconn automatic computation |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 11393 | socket_ssl->xprt->prepare_srv(socket_ssl); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11394 | global.ssl_used_backend = saved_used_backed; |
| 11395 | } |
| 11396 | #endif |
| 11397 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 11398 | /* Perform post init of common thread */ |
| 11399 | hlua_state_id = 0; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11400 | ha_set_thread(&ha_thread_info[0]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11401 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 11402 | if (ret == 0) |
| 11403 | return 0; |
| 11404 | |
| 11405 | /* init remaining lua states and load files */ |
| 11406 | for (hlua_state_id = 2; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 11407 | |
| 11408 | /* set thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11409 | ha_set_thread(&ha_thread_info[hlua_state_id - 1]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11410 | |
| 11411 | /* Init lua state */ |
| 11412 | hlua_states[hlua_state_id] = hlua_init_state(hlua_state_id); |
| 11413 | |
| 11414 | /* Load lua files */ |
| 11415 | for (i = 0; per_thread_load && per_thread_load[i]; i++) { |
| 11416 | ret = hlua_load_state(per_thread_load[i], hlua_states[hlua_state_id], &err); |
| 11417 | if (ret != 0) { |
| 11418 | ha_alert("Lua init: %s\n", err); |
| 11419 | return 0; |
| 11420 | } |
| 11421 | } |
| 11422 | } |
| 11423 | |
| 11424 | /* Reset thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11425 | ha_set_thread(NULL); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11426 | |
| 11427 | /* Execute post init for all states */ |
| 11428 | for (hlua_state_id = 1; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 11429 | |
| 11430 | /* set thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11431 | ha_set_thread(&ha_thread_info[hlua_state_id - 1]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11432 | |
| 11433 | /* run post init */ |
| 11434 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 11435 | if (ret == 0) |
| 11436 | return 0; |
| 11437 | } |
| 11438 | |
| 11439 | /* Reset thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11440 | ha_set_thread(NULL); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11441 | |
| 11442 | /* control functions registering. Each function must have: |
| 11443 | * - only the function_ref[0] set positive and all other to -1 |
| 11444 | * - only the function_ref[0] set to -1 and all other positive |
| 11445 | * This ensure a same reference is not used both in shared |
| 11446 | * lua state and thread dedicated lua state. Note: is the case |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 11447 | * reach, the shared state is priority, but the bug will be |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11448 | * complicated to found for the end user. |
| 11449 | */ |
| 11450 | errors = 0; |
| 11451 | list_for_each_entry(fcn, &referenced_functions, l) { |
| 11452 | ret = 0; |
| 11453 | for (i = 1; i < global.nbthread + 1; i++) { |
| 11454 | if (fcn->function_ref[i] == -1) |
| 11455 | ret--; |
| 11456 | else |
| 11457 | ret++; |
| 11458 | } |
| 11459 | if (abs(ret) != global.nbthread) { |
| 11460 | ha_alert("Lua function '%s' is not referenced in all thread. " |
| 11461 | "Expect function in all thread or in none thread.\n", fcn->name); |
| 11462 | errors++; |
| 11463 | continue; |
| 11464 | } |
| 11465 | |
| 11466 | if ((fcn->function_ref[0] == -1) == (ret < 0)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 11467 | ha_alert("Lua function '%s' is referenced both ins shared Lua context (through lua-load) " |
| 11468 | "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] | 11469 | "exclusive.\n", fcn->name); |
| 11470 | errors++; |
| 11471 | } |
| 11472 | } |
| 11473 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11474 | /* Do the same with registered filters */ |
| 11475 | list_for_each_entry(reg_flt, &referenced_filters, l) { |
| 11476 | ret = 0; |
| 11477 | for (i = 1; i < global.nbthread + 1; i++) { |
| 11478 | if (reg_flt->flt_ref[i] == -1) |
| 11479 | ret--; |
| 11480 | else |
| 11481 | ret++; |
| 11482 | } |
| 11483 | if (abs(ret) != global.nbthread) { |
| 11484 | ha_alert("Lua filter '%s' is not referenced in all thread. " |
| 11485 | "Expect function in all thread or in none thread.\n", reg_flt->name); |
| 11486 | errors++; |
| 11487 | continue; |
| 11488 | } |
| 11489 | |
| 11490 | if ((reg_flt->flt_ref[0] == -1) == (ret < 0)) { |
| 11491 | ha_alert("Lua filter '%s' is referenced both ins shared Lua context (through lua-load) " |
| 11492 | "and per-thread Lua context (through lua-load-per-thread). these two context " |
| 11493 | "exclusive.\n", fcn->name); |
| 11494 | errors++; |
| 11495 | } |
| 11496 | } |
| 11497 | |
| 11498 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11499 | if (errors > 0) |
| 11500 | return 0; |
| 11501 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 11502 | /* 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] | 11503 | * -1 in order to have probably a segfault if someone use it |
| 11504 | */ |
| 11505 | hlua_state_id = -1; |
| 11506 | |
| 11507 | return 1; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11508 | } |
| 11509 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11510 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 11511 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 11512 | * 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] | 11513 | * allocation. <nsize> is the requested new size. A new allocation is |
| 11514 | * indicated by <ptr> being NULL. A free is indicated by <nsize> being |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11515 | * zero. This one verifies that the limits are respected but is optimized |
| 11516 | * 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] | 11517 | * |
| 11518 | * Warning: while this API ressembles glibc's realloc() a lot, glibc surpasses |
| 11519 | * POSIX by making realloc(ptr,0) an effective free(), but others do not do |
| 11520 | * that and will simply allocate zero as if it were the result of malloc(0), |
| 11521 | * so mapping this onto realloc() will lead to memory leaks on non-glibc |
| 11522 | * systems. |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11523 | */ |
| 11524 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 11525 | { |
| 11526 | struct hlua_mem_allocator *zone = ud; |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11527 | size_t limit, old, new; |
| 11528 | |
Tim Duesterhus | 2258652 | 2021-01-08 10:35:33 +0100 | [diff] [blame] | 11529 | if (unlikely(!ptr && !nsize)) |
| 11530 | return NULL; |
| 11531 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11532 | /* a limit of ~0 means unlimited and boot complete, so there's no need |
| 11533 | * for accounting anymore. |
| 11534 | */ |
Willy Tarreau | a5efdff | 2021-10-22 16:00:02 +0200 | [diff] [blame] | 11535 | if (likely(~zone->limit == 0)) { |
| 11536 | if (!nsize) |
| 11537 | ha_free(&ptr); |
| 11538 | else |
| 11539 | ptr = realloc(ptr, nsize); |
| 11540 | return ptr; |
| 11541 | } |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11542 | |
Willy Tarreau | d36c7fa | 2020-12-02 12:26:29 +0100 | [diff] [blame] | 11543 | if (!ptr) |
| 11544 | osize = 0; |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11545 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11546 | /* enforce strict limits across all threads */ |
| 11547 | limit = zone->limit; |
| 11548 | old = _HA_ATOMIC_LOAD(&zone->allocated); |
| 11549 | do { |
| 11550 | new = old + nsize - osize; |
| 11551 | if (unlikely(nsize && limit && new > limit)) |
| 11552 | return NULL; |
| 11553 | } while (!_HA_ATOMIC_CAS(&zone->allocated, &old, new)); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11554 | |
Willy Tarreau | a5efdff | 2021-10-22 16:00:02 +0200 | [diff] [blame] | 11555 | if (!nsize) |
| 11556 | ha_free(&ptr); |
| 11557 | else |
| 11558 | ptr = realloc(ptr, nsize); |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11559 | |
| 11560 | if (unlikely(!ptr && nsize)) // failed |
| 11561 | _HA_ATOMIC_SUB(&zone->allocated, nsize - osize); |
| 11562 | |
| 11563 | __ha_barrier_atomic_store(); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11564 | return ptr; |
| 11565 | } |
| 11566 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 11567 | /* 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] | 11568 | * We are in the initialisation process of HAProxy, this abort() is |
| 11569 | * tolerated. |
| 11570 | */ |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 11571 | lua_State *hlua_init_state(int thread_num) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 11572 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11573 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 11574 | int idx; |
| 11575 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 11576 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 11577 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 11578 | const char *error_msg; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 11579 | void **context; |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11580 | lua_State *L; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11581 | struct prepend_path *pp; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11582 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 11583 | /* Init main lua stack. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11584 | L = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 11585 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 11586 | /* Initialise Lua context to NULL */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11587 | context = lua_getextraspace(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 11588 | *context = NULL; |
| 11589 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 11590 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 11591 | * the Lua function can fail with an abort. We are in the initialisation |
| 11592 | * process of HAProxy, this abort() is tolerated. |
| 11593 | */ |
| 11594 | |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11595 | /* Call post initialisation function in safe environment. */ |
| 11596 | if (setjmp(safe_ljmp_env) != 0) { |
| 11597 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11598 | if (lua_type(L, -1) == LUA_TSTRING) |
| 11599 | error_msg = lua_tostring(L, -1); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 11600 | else |
| 11601 | error_msg = "critical error"; |
| 11602 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
| 11603 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11604 | } else { |
| 11605 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 11606 | } |
| 11607 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 11608 | /* Initialise lua. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11609 | luaL_openlibs(L); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 11610 | #define HLUA_PREPEND_PATH_TOSTRING1(x) #x |
| 11611 | #define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x) |
| 11612 | #ifdef HLUA_PREPEND_PATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11613 | hlua_prepend_path(L, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 11614 | #endif |
| 11615 | #ifdef HLUA_PREPEND_CPATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11616 | hlua_prepend_path(L, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 11617 | #endif |
| 11618 | #undef HLUA_PREPEND_PATH_TOSTRING |
| 11619 | #undef HLUA_PREPEND_PATH_TOSTRING1 |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11620 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11621 | /* Apply configured prepend path */ |
| 11622 | list_for_each_entry(pp, &prepend_path_list, l) |
| 11623 | hlua_prepend_path(L, pp->type, pp->path); |
| 11624 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11625 | /* |
| 11626 | * |
| 11627 | * Create "core" object. |
| 11628 | * |
| 11629 | */ |
| 11630 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 11631 | /* This table entry is the object "core" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11632 | lua_newtable(L); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11633 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 11634 | /* set the thread id */ |
| 11635 | hlua_class_const_int(L, "thread", thread_num); |
| 11636 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11637 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 11638 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11639 | hlua_class_const_int(L, log_levels[i], i); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11640 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11641 | /* Register special functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11642 | hlua_class_function(L, "register_init", hlua_register_init); |
| 11643 | hlua_class_function(L, "register_task", hlua_register_task); |
| 11644 | hlua_class_function(L, "register_fetches", hlua_register_fetches); |
| 11645 | hlua_class_function(L, "register_converters", hlua_register_converters); |
| 11646 | hlua_class_function(L, "register_action", hlua_register_action); |
| 11647 | hlua_class_function(L, "register_service", hlua_register_service); |
| 11648 | hlua_class_function(L, "register_cli", hlua_register_cli); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11649 | hlua_class_function(L, "register_filter", hlua_register_filter); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11650 | hlua_class_function(L, "yield", hlua_yield); |
| 11651 | hlua_class_function(L, "set_nice", hlua_set_nice); |
| 11652 | hlua_class_function(L, "sleep", hlua_sleep); |
| 11653 | hlua_class_function(L, "msleep", hlua_msleep); |
| 11654 | hlua_class_function(L, "add_acl", hlua_add_acl); |
| 11655 | hlua_class_function(L, "del_acl", hlua_del_acl); |
| 11656 | hlua_class_function(L, "set_map", hlua_set_map); |
| 11657 | hlua_class_function(L, "del_map", hlua_del_map); |
| 11658 | hlua_class_function(L, "tcp", hlua_socket_new); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 11659 | hlua_class_function(L, "httpclient", hlua_httpclient_new); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11660 | hlua_class_function(L, "log", hlua_log); |
| 11661 | hlua_class_function(L, "Debug", hlua_log_debug); |
| 11662 | hlua_class_function(L, "Info", hlua_log_info); |
| 11663 | hlua_class_function(L, "Warning", hlua_log_warning); |
| 11664 | hlua_class_function(L, "Alert", hlua_log_alert); |
| 11665 | hlua_class_function(L, "done", hlua_done); |
| 11666 | hlua_fcn_reg_core_fcn(L); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11667 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11668 | lua_setglobal(L, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 11669 | |
| 11670 | /* |
| 11671 | * |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 11672 | * Create "act" object. |
| 11673 | * |
| 11674 | */ |
| 11675 | |
| 11676 | /* This table entry is the object "act" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11677 | lua_newtable(L); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 11678 | |
| 11679 | /* push action return constants */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11680 | hlua_class_const_int(L, "CONTINUE", ACT_RET_CONT); |
| 11681 | hlua_class_const_int(L, "STOP", ACT_RET_STOP); |
| 11682 | hlua_class_const_int(L, "YIELD", ACT_RET_YIELD); |
| 11683 | hlua_class_const_int(L, "ERROR", ACT_RET_ERR); |
| 11684 | hlua_class_const_int(L, "DONE", ACT_RET_DONE); |
| 11685 | hlua_class_const_int(L, "DENY", ACT_RET_DENY); |
| 11686 | hlua_class_const_int(L, "ABORT", ACT_RET_ABRT); |
| 11687 | hlua_class_const_int(L, "INVALID", ACT_RET_INV); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 11688 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11689 | hlua_class_function(L, "wake_time", hlua_set_wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 11690 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11691 | lua_setglobal(L, "act"); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 11692 | |
| 11693 | /* |
| 11694 | * |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11695 | * Create "Filter" object. |
| 11696 | * |
| 11697 | */ |
| 11698 | |
| 11699 | /* This table entry is the object "filter" base. */ |
| 11700 | lua_newtable(L); |
| 11701 | |
| 11702 | /* push flags and constants */ |
| 11703 | hlua_class_const_int(L, "CONTINUE", 1); |
| 11704 | hlua_class_const_int(L, "WAIT", 0); |
| 11705 | hlua_class_const_int(L, "ERROR", -1); |
| 11706 | |
| 11707 | hlua_class_const_int(L, "FLT_CFG_FL_HTX", FLT_CFG_FL_HTX); |
| 11708 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 11709 | hlua_class_function(L, "wake_time", hlua_set_wake_time); |
| 11710 | hlua_class_function(L, "register_data_filter", hlua_register_data_filter); |
| 11711 | hlua_class_function(L, "unregister_data_filter", hlua_unregister_data_filter); |
| 11712 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11713 | lua_setglobal(L, "filter"); |
| 11714 | |
| 11715 | /* |
| 11716 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11717 | * Register class Map |
| 11718 | * |
| 11719 | */ |
| 11720 | |
| 11721 | /* This table entry is the object "Map" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11722 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11723 | |
| 11724 | /* register pattern types. */ |
| 11725 | for (i=0; i<PAT_MATCH_NUM; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11726 | hlua_class_const_int(L, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 11727 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 11728 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11729 | hlua_class_const_int(L, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 11730 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11731 | |
| 11732 | /* register constructor. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11733 | hlua_class_function(L, "new", hlua_map_new); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11734 | |
| 11735 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11736 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11737 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 11738 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11739 | lua_pushstring(L, "__index"); |
| 11740 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11741 | |
| 11742 | /* Register . */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11743 | hlua_class_function(L, "lookup", hlua_map_lookup); |
| 11744 | hlua_class_function(L, "slookup", hlua_map_slookup); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11745 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11746 | lua_rawset(L, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11747 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 11748 | /* Register previous table in the registry with reference and named entry. |
| 11749 | * The function hlua_register_metatable() pops the stack, so we |
| 11750 | * previously create a copy of the table. |
| 11751 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11752 | lua_pushvalue(L, -1); /* Copy the -1 entry and push it on the stack. */ |
| 11753 | class_map_ref = hlua_register_metatable(L, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11754 | |
| 11755 | /* Assign the metatable to the mai Map object. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11756 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11757 | |
| 11758 | /* Set a name to the table. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11759 | lua_setglobal(L, "Map"); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11760 | |
| 11761 | /* |
| 11762 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 11763 | * Register class Channel |
| 11764 | * |
| 11765 | */ |
| 11766 | |
| 11767 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11768 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 11769 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 11770 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11771 | lua_pushstring(L, "__index"); |
| 11772 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 11773 | |
| 11774 | /* Register . */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 11775 | hlua_class_function(L, "data", hlua_channel_get_data); |
| 11776 | hlua_class_function(L, "line", hlua_channel_get_line); |
| 11777 | hlua_class_function(L, "set", hlua_channel_set_data); |
| 11778 | hlua_class_function(L, "remove", hlua_channel_del_data); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11779 | hlua_class_function(L, "append", hlua_channel_append); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 11780 | hlua_class_function(L, "prepend", hlua_channel_prepend); |
| 11781 | hlua_class_function(L, "insert", hlua_channel_insert_data); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11782 | hlua_class_function(L, "send", hlua_channel_send); |
| 11783 | hlua_class_function(L, "forward", hlua_channel_forward); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 11784 | hlua_class_function(L, "input", hlua_channel_get_in_len); |
| 11785 | hlua_class_function(L, "output", hlua_channel_get_out_len); |
| 11786 | hlua_class_function(L, "may_recv", hlua_channel_may_recv); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11787 | hlua_class_function(L, "is_full", hlua_channel_is_full); |
| 11788 | hlua_class_function(L, "is_resp", hlua_channel_is_resp); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 11789 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 11790 | /* Deprecated API */ |
| 11791 | hlua_class_function(L, "get", hlua_channel_get); |
| 11792 | hlua_class_function(L, "dup", hlua_channel_dup); |
| 11793 | hlua_class_function(L, "getline", hlua_channel_getline); |
| 11794 | hlua_class_function(L, "get_in_len", hlua_channel_get_in_len); |
| 11795 | hlua_class_function(L, "get_out_len", hlua_channel_get_out_len); |
| 11796 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11797 | lua_rawset(L, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 11798 | |
| 11799 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11800 | class_channel_ref = hlua_register_metatable(L, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 11801 | |
| 11802 | /* |
| 11803 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 11804 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 11805 | * |
| 11806 | */ |
| 11807 | |
| 11808 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11809 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 11810 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 11811 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11812 | lua_pushstring(L, "__index"); |
| 11813 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 11814 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 11815 | /* Browse existing fetches and create the associated |
| 11816 | * object method. |
| 11817 | */ |
| 11818 | sf = NULL; |
| 11819 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 11820 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 11821 | * by an underscore. |
| 11822 | */ |
Willy Tarreau | 5ef9656 | 2021-08-26 16:48:53 +0200 | [diff] [blame] | 11823 | strlcpy2(trash.area, sf->kw, trash.size); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 11824 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 11825 | if (*p == '.' || *p == '-' || *p == '+') |
| 11826 | *p = '_'; |
| 11827 | |
| 11828 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11829 | lua_pushstring(L, trash.area); |
| 11830 | lua_pushlightuserdata(L, sf); |
| 11831 | lua_pushcclosure(L, hlua_run_sample_fetch, 1); |
| 11832 | lua_rawset(L, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 11833 | } |
| 11834 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11835 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 11836 | |
| 11837 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11838 | class_fetches_ref = hlua_register_metatable(L, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 11839 | |
| 11840 | /* |
| 11841 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 11842 | * Register class Converters |
| 11843 | * |
| 11844 | */ |
| 11845 | |
| 11846 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11847 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 11848 | |
| 11849 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11850 | lua_pushstring(L, "__index"); |
| 11851 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 11852 | |
| 11853 | /* Browse existing converters and create the associated |
| 11854 | * object method. |
| 11855 | */ |
| 11856 | sc = NULL; |
| 11857 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 11858 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 11859 | * by an underscore. |
| 11860 | */ |
Willy Tarreau | 5ef9656 | 2021-08-26 16:48:53 +0200 | [diff] [blame] | 11861 | strlcpy2(trash.area, sc->kw, trash.size); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 11862 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 11863 | if (*p == '.' || *p == '-' || *p == '+') |
| 11864 | *p = '_'; |
| 11865 | |
| 11866 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11867 | lua_pushstring(L, trash.area); |
| 11868 | lua_pushlightuserdata(L, sc); |
| 11869 | lua_pushcclosure(L, hlua_run_sample_conv, 1); |
| 11870 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 11871 | } |
| 11872 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11873 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 11874 | |
| 11875 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11876 | class_converters_ref = hlua_register_metatable(L, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 11877 | |
| 11878 | /* |
| 11879 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 11880 | * Register class HTTP |
| 11881 | * |
| 11882 | */ |
| 11883 | |
| 11884 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11885 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 11886 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 11887 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11888 | lua_pushstring(L, "__index"); |
| 11889 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 11890 | |
| 11891 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11892 | hlua_class_function(L, "req_get_headers",hlua_http_req_get_headers); |
| 11893 | hlua_class_function(L, "req_del_header", hlua_http_req_del_hdr); |
| 11894 | hlua_class_function(L, "req_rep_header", hlua_http_req_rep_hdr); |
| 11895 | hlua_class_function(L, "req_rep_value", hlua_http_req_rep_val); |
| 11896 | hlua_class_function(L, "req_add_header", hlua_http_req_add_hdr); |
| 11897 | hlua_class_function(L, "req_set_header", hlua_http_req_set_hdr); |
| 11898 | hlua_class_function(L, "req_set_method", hlua_http_req_set_meth); |
| 11899 | hlua_class_function(L, "req_set_path", hlua_http_req_set_path); |
| 11900 | hlua_class_function(L, "req_set_query", hlua_http_req_set_query); |
| 11901 | hlua_class_function(L, "req_set_uri", hlua_http_req_set_uri); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 11902 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11903 | hlua_class_function(L, "res_get_headers",hlua_http_res_get_headers); |
| 11904 | hlua_class_function(L, "res_del_header", hlua_http_res_del_hdr); |
| 11905 | hlua_class_function(L, "res_rep_header", hlua_http_res_rep_hdr); |
| 11906 | hlua_class_function(L, "res_rep_value", hlua_http_res_rep_val); |
| 11907 | hlua_class_function(L, "res_add_header", hlua_http_res_add_hdr); |
| 11908 | hlua_class_function(L, "res_set_header", hlua_http_res_set_hdr); |
| 11909 | hlua_class_function(L, "res_set_status", hlua_http_res_set_status); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 11910 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11911 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 11912 | |
| 11913 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11914 | class_http_ref = hlua_register_metatable(L, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 11915 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 11916 | /* |
| 11917 | * |
| 11918 | * Register class HTTPMessage |
| 11919 | * |
| 11920 | */ |
| 11921 | |
| 11922 | /* Create and fill the metatable. */ |
| 11923 | lua_newtable(L); |
| 11924 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 11925 | /* Create and fill the __index entry. */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 11926 | lua_pushstring(L, "__index"); |
| 11927 | lua_newtable(L); |
| 11928 | |
| 11929 | /* Register Lua functions. */ |
| 11930 | hlua_class_function(L, "is_resp", hlua_http_msg_is_resp); |
| 11931 | hlua_class_function(L, "get_stline", hlua_http_msg_get_stline); |
| 11932 | hlua_class_function(L, "get_headers", hlua_http_msg_get_headers); |
| 11933 | hlua_class_function(L, "del_header", hlua_http_msg_del_hdr); |
| 11934 | hlua_class_function(L, "rep_header", hlua_http_msg_rep_hdr); |
| 11935 | hlua_class_function(L, "rep_value", hlua_http_msg_rep_val); |
| 11936 | hlua_class_function(L, "add_header", hlua_http_msg_add_hdr); |
| 11937 | hlua_class_function(L, "set_header", hlua_http_msg_set_hdr); |
| 11938 | hlua_class_function(L, "set_method", hlua_http_msg_set_meth); |
| 11939 | hlua_class_function(L, "set_path", hlua_http_msg_set_path); |
| 11940 | hlua_class_function(L, "set_query", hlua_http_msg_set_query); |
| 11941 | hlua_class_function(L, "set_uri", hlua_http_msg_set_uri); |
| 11942 | hlua_class_function(L, "set_status", hlua_http_msg_set_status); |
| 11943 | hlua_class_function(L, "is_full", hlua_http_msg_is_full); |
| 11944 | hlua_class_function(L, "may_recv", hlua_http_msg_may_recv); |
| 11945 | hlua_class_function(L, "eom", hlua_http_msg_is_eom); |
| 11946 | hlua_class_function(L, "input", hlua_http_msg_get_in_len); |
| 11947 | hlua_class_function(L, "output", hlua_http_msg_get_out_len); |
| 11948 | |
| 11949 | hlua_class_function(L, "body", hlua_http_msg_get_body); |
| 11950 | hlua_class_function(L, "set", hlua_http_msg_set_data); |
| 11951 | hlua_class_function(L, "remove", hlua_http_msg_del_data); |
| 11952 | hlua_class_function(L, "append", hlua_http_msg_append); |
| 11953 | hlua_class_function(L, "prepend", hlua_http_msg_prepend); |
| 11954 | hlua_class_function(L, "insert", hlua_http_msg_insert_data); |
| 11955 | hlua_class_function(L, "set_eom", hlua_http_msg_set_eom); |
| 11956 | hlua_class_function(L, "unset_eom", hlua_http_msg_unset_eom); |
| 11957 | |
| 11958 | hlua_class_function(L, "send", hlua_http_msg_send); |
| 11959 | hlua_class_function(L, "forward", hlua_http_msg_forward); |
| 11960 | |
| 11961 | lua_rawset(L, -3); |
| 11962 | |
| 11963 | /* Register previous table in the registry with reference and named entry. */ |
| 11964 | class_http_msg_ref = hlua_register_metatable(L, CLASS_HTTP_MSG); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 11965 | |
| 11966 | /* |
| 11967 | * |
| 11968 | * Register class HTTPClient |
| 11969 | * |
| 11970 | */ |
| 11971 | |
| 11972 | /* Create and fill the metatable. */ |
| 11973 | lua_newtable(L); |
| 11974 | lua_pushstring(L, "__index"); |
| 11975 | lua_newtable(L); |
| 11976 | hlua_class_function(L, "get", hlua_httpclient_get); |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 11977 | hlua_class_function(L, "head", hlua_httpclient_head); |
| 11978 | hlua_class_function(L, "put", hlua_httpclient_put); |
| 11979 | hlua_class_function(L, "post", hlua_httpclient_post); |
| 11980 | hlua_class_function(L, "delete", hlua_httpclient_delete); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 11981 | lua_settable(L, -3); /* Sets the __index entry. */ |
William Lallemand | f77f1de | 2021-09-28 19:10:38 +0200 | [diff] [blame] | 11982 | /* Register the garbage collector entry. */ |
| 11983 | lua_pushstring(L, "__gc"); |
| 11984 | lua_pushcclosure(L, hlua_httpclient_gc, 0); |
| 11985 | lua_settable(L, -3); /* Push the last 2 entries in the table at index -3 */ |
| 11986 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 11987 | |
| 11988 | |
| 11989 | class_httpclient_ref = hlua_register_metatable(L, CLASS_HTTPCLIENT); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 11990 | /* |
| 11991 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11992 | * Register class AppletTCP |
| 11993 | * |
| 11994 | */ |
| 11995 | |
| 11996 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11997 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11998 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 11999 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12000 | lua_pushstring(L, "__index"); |
| 12001 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12002 | |
| 12003 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12004 | hlua_class_function(L, "getline", hlua_applet_tcp_getline); |
| 12005 | hlua_class_function(L, "receive", hlua_applet_tcp_recv); |
| 12006 | hlua_class_function(L, "send", hlua_applet_tcp_send); |
| 12007 | hlua_class_function(L, "set_priv", hlua_applet_tcp_set_priv); |
| 12008 | hlua_class_function(L, "get_priv", hlua_applet_tcp_get_priv); |
| 12009 | hlua_class_function(L, "set_var", hlua_applet_tcp_set_var); |
| 12010 | hlua_class_function(L, "unset_var", hlua_applet_tcp_unset_var); |
| 12011 | hlua_class_function(L, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12012 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12013 | lua_settable(L, -3); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12014 | |
| 12015 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12016 | class_applet_tcp_ref = hlua_register_metatable(L, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12017 | |
| 12018 | /* |
| 12019 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12020 | * Register class AppletHTTP |
| 12021 | * |
| 12022 | */ |
| 12023 | |
| 12024 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12025 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12026 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12027 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12028 | lua_pushstring(L, "__index"); |
| 12029 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12030 | |
| 12031 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12032 | hlua_class_function(L, "set_priv", hlua_applet_http_set_priv); |
| 12033 | hlua_class_function(L, "get_priv", hlua_applet_http_get_priv); |
| 12034 | hlua_class_function(L, "set_var", hlua_applet_http_set_var); |
| 12035 | hlua_class_function(L, "unset_var", hlua_applet_http_unset_var); |
| 12036 | hlua_class_function(L, "get_var", hlua_applet_http_get_var); |
| 12037 | hlua_class_function(L, "getline", hlua_applet_http_getline); |
| 12038 | hlua_class_function(L, "receive", hlua_applet_http_recv); |
| 12039 | hlua_class_function(L, "send", hlua_applet_http_send); |
| 12040 | hlua_class_function(L, "add_header", hlua_applet_http_addheader); |
| 12041 | hlua_class_function(L, "set_status", hlua_applet_http_status); |
| 12042 | hlua_class_function(L, "start_response", hlua_applet_http_start_response); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12043 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12044 | lua_settable(L, -3); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12045 | |
| 12046 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12047 | class_applet_http_ref = hlua_register_metatable(L, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12048 | |
| 12049 | /* |
| 12050 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12051 | * Register class TXN |
| 12052 | * |
| 12053 | */ |
| 12054 | |
| 12055 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12056 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12057 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12058 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12059 | lua_pushstring(L, "__index"); |
| 12060 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12061 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 12062 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12063 | hlua_class_function(L, "set_priv", hlua_set_priv); |
| 12064 | hlua_class_function(L, "get_priv", hlua_get_priv); |
| 12065 | hlua_class_function(L, "set_var", hlua_set_var); |
| 12066 | hlua_class_function(L, "unset_var", hlua_unset_var); |
| 12067 | hlua_class_function(L, "get_var", hlua_get_var); |
| 12068 | hlua_class_function(L, "done", hlua_txn_done); |
| 12069 | hlua_class_function(L, "reply", hlua_txn_reply_new); |
| 12070 | hlua_class_function(L, "set_loglevel", hlua_txn_set_loglevel); |
| 12071 | hlua_class_function(L, "set_tos", hlua_txn_set_tos); |
| 12072 | hlua_class_function(L, "set_mark", hlua_txn_set_mark); |
| 12073 | hlua_class_function(L, "set_priority_class", hlua_txn_set_priority_class); |
| 12074 | hlua_class_function(L, "set_priority_offset", hlua_txn_set_priority_offset); |
| 12075 | hlua_class_function(L, "deflog", hlua_txn_deflog); |
| 12076 | hlua_class_function(L, "log", hlua_txn_log); |
| 12077 | hlua_class_function(L, "Debug", hlua_txn_log_debug); |
| 12078 | hlua_class_function(L, "Info", hlua_txn_log_info); |
| 12079 | hlua_class_function(L, "Warning", hlua_txn_log_warning); |
| 12080 | hlua_class_function(L, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 12081 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12082 | lua_rawset(L, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 12083 | |
| 12084 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12085 | class_txn_ref = hlua_register_metatable(L, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12086 | |
| 12087 | /* |
| 12088 | * |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 12089 | * Register class reply |
| 12090 | * |
| 12091 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12092 | lua_newtable(L); |
| 12093 | lua_pushstring(L, "__index"); |
| 12094 | lua_newtable(L); |
| 12095 | hlua_class_function(L, "set_status", hlua_txn_reply_set_status); |
| 12096 | hlua_class_function(L, "add_header", hlua_txn_reply_add_header); |
| 12097 | hlua_class_function(L, "del_header", hlua_txn_reply_del_header); |
| 12098 | hlua_class_function(L, "set_body", hlua_txn_reply_set_body); |
| 12099 | lua_settable(L, -3); /* Sets the __index entry. */ |
| 12100 | class_txn_reply_ref = luaL_ref(L, LUA_REGISTRYINDEX); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 12101 | |
| 12102 | |
| 12103 | /* |
| 12104 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12105 | * Register class Socket |
| 12106 | * |
| 12107 | */ |
| 12108 | |
| 12109 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12110 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12111 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12112 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12113 | lua_pushstring(L, "__index"); |
| 12114 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12115 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 12116 | #ifdef USE_OPENSSL |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12117 | hlua_class_function(L, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 12118 | #endif |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12119 | hlua_class_function(L, "connect", hlua_socket_connect); |
| 12120 | hlua_class_function(L, "send", hlua_socket_send); |
| 12121 | hlua_class_function(L, "receive", hlua_socket_receive); |
| 12122 | hlua_class_function(L, "close", hlua_socket_close); |
| 12123 | hlua_class_function(L, "getpeername", hlua_socket_getpeername); |
| 12124 | hlua_class_function(L, "getsockname", hlua_socket_getsockname); |
| 12125 | hlua_class_function(L, "setoption", hlua_socket_setoption); |
| 12126 | hlua_class_function(L, "settimeout", hlua_socket_settimeout); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12127 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12128 | 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] | 12129 | |
| 12130 | /* Register the garbage collector entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12131 | lua_pushstring(L, "__gc"); |
| 12132 | lua_pushcclosure(L, hlua_socket_gc, 0); |
| 12133 | 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] | 12134 | |
| 12135 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12136 | class_socket_ref = hlua_register_metatable(L, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12137 | |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 12138 | lua_atpanic(L, hlua_panic_safe); |
| 12139 | |
| 12140 | return L; |
| 12141 | } |
| 12142 | |
| 12143 | void hlua_init(void) { |
| 12144 | int i; |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 12145 | char *errmsg; |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 12146 | #ifdef USE_OPENSSL |
| 12147 | struct srv_kw *kw; |
| 12148 | int tmp_error; |
| 12149 | char *error; |
| 12150 | char *args[] = { /* SSL client configuration. */ |
| 12151 | "ssl", |
| 12152 | "verify", |
| 12153 | "none", |
| 12154 | NULL |
| 12155 | }; |
| 12156 | #endif |
| 12157 | |
| 12158 | /* Init post init function list head */ |
| 12159 | for (i = 0; i < MAX_THREADS + 1; i++) |
| 12160 | LIST_INIT(&hlua_init_functions[i]); |
| 12161 | |
| 12162 | /* Init state for common/shared lua parts */ |
| 12163 | hlua_state_id = 0; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 12164 | ha_set_thread(NULL); |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 12165 | hlua_states[0] = hlua_init_state(0); |
| 12166 | |
| 12167 | /* Init state 1 for thread 0. We have at least one thread. */ |
| 12168 | hlua_state_id = 1; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 12169 | ha_set_thread(NULL); |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 12170 | hlua_states[1] = hlua_init_state(1); |
| 12171 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12172 | /* Proxy and server configuration initialisation. */ |
William Lallemand | 6bb77b9 | 2021-07-28 15:48:16 +0200 | [diff] [blame] | 12173 | 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] | 12174 | if (!socket_proxy) { |
| 12175 | fprintf(stderr, "Lua init: %s\n", errmsg); |
| 12176 | exit(1); |
| 12177 | } |
| 12178 | proxy_preset_defaults(socket_proxy); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12179 | |
| 12180 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12181 | socket_tcp = new_server(socket_proxy); |
| 12182 | if (!socket_tcp) { |
| 12183 | fprintf(stderr, "Lua init: failed to allocate tcp server socket\n"); |
| 12184 | exit(1); |
| 12185 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12186 | |
| 12187 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12188 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12189 | socket_ssl = new_server(socket_proxy); |
| 12190 | if (!socket_ssl) { |
| 12191 | fprintf(stderr, "Lua init: failed to allocate ssl server socket\n"); |
| 12192 | exit(1); |
| 12193 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12194 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12195 | socket_ssl->use_ssl = 1; |
| 12196 | socket_ssl->xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12197 | |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 12198 | for (i = 0; args[i] != NULL; i++) { |
| 12199 | 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] | 12200 | /* |
| 12201 | * |
| 12202 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 12203 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12204 | * features like client certificates and ssl_verify. |
| 12205 | * |
| 12206 | */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12207 | tmp_error = kw->parse(args, &i, socket_proxy, socket_ssl, &error); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12208 | if (tmp_error != 0) { |
| 12209 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 12210 | abort(); /* This must be never arrives because the command line |
| 12211 | not editable by the user. */ |
| 12212 | } |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 12213 | i += kw->skip; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12214 | } |
| 12215 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12216 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 12217 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 12218 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 12219 | |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 12220 | static void hlua_deinit() |
| 12221 | { |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 12222 | int thr; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12223 | struct hlua_reg_filter *reg_flt, *reg_flt_bck; |
| 12224 | |
| 12225 | list_for_each_entry_safe(reg_flt, reg_flt_bck, &referenced_filters, l) |
| 12226 | release_hlua_reg_filter(reg_flt); |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 12227 | |
| 12228 | for (thr = 0; thr < MAX_THREADS+1; thr++) { |
| 12229 | if (hlua_states[thr]) |
| 12230 | lua_close(hlua_states[thr]); |
| 12231 | } |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 12232 | |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 12233 | srv_drop(socket_tcp); |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 12234 | |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 12235 | #ifdef USE_OPENSSL |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 12236 | srv_drop(socket_ssl); |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 12237 | #endif |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 12238 | |
| 12239 | free_proxy(socket_proxy); |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 12240 | } |
| 12241 | |
| 12242 | REGISTER_POST_DEINIT(hlua_deinit); |
| 12243 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 12244 | static void hlua_register_build_options(void) |
| 12245 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 12246 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 12247 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 12248 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 12249 | hap_register_build_opts(ptr, 1); |
| 12250 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 12251 | |
| 12252 | INITCALL0(STG_REGISTER, hlua_register_build_options); |