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") |
Christopher Faulet | e6465b3 | 2021-10-22 15:36:08 +0200 | [diff] [blame] | 2503 | __LJMP static inline int hlua_socket_info(struct lua_State *L, const struct sockaddr_storage *addr) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2504 | { |
| 2505 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2506 | int ret; |
| 2507 | int len; |
| 2508 | char *p; |
| 2509 | |
| 2510 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2511 | if (ret <= 0) { |
| 2512 | lua_pushnil(L); |
| 2513 | return 1; |
| 2514 | } |
| 2515 | |
| 2516 | if (ret == AF_UNIX) { |
| 2517 | lua_pushstring(L, buffer+1); |
| 2518 | return 1; |
| 2519 | } |
| 2520 | else if (ret == AF_INET6) { |
| 2521 | buffer[0] = '['; |
| 2522 | len = strlen(buffer); |
| 2523 | buffer[len] = ']'; |
| 2524 | len++; |
| 2525 | buffer[len] = ':'; |
| 2526 | len++; |
| 2527 | p = buffer; |
| 2528 | } |
| 2529 | else if (ret == AF_INET) { |
| 2530 | p = buffer + 1; |
| 2531 | len = strlen(p); |
| 2532 | p[len] = ':'; |
| 2533 | len++; |
| 2534 | } |
| 2535 | else { |
| 2536 | lua_pushnil(L); |
| 2537 | return 1; |
| 2538 | } |
| 2539 | |
| 2540 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2541 | lua_pushnil(L); |
| 2542 | return 1; |
| 2543 | } |
| 2544 | |
| 2545 | lua_pushstring(L, p); |
| 2546 | return 1; |
| 2547 | } |
| 2548 | |
| 2549 | /* Returns information about the peer of the connection. */ |
| 2550 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2551 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2552 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2553 | struct xref *peer; |
| 2554 | struct appctx *appctx; |
| 2555 | struct stream_interface *si; |
Christopher Faulet | 16f16af | 2021-10-27 09:34:56 +0200 | [diff] [blame] | 2556 | const struct sockaddr_storage *dst; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2557 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2558 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2559 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2560 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2561 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2562 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2563 | /* Check if we run on the same thread than the xreator thread. |
| 2564 | * We cannot access to the socket if the thread is different. |
| 2565 | */ |
| 2566 | if (socket->tid != tid) |
| 2567 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2568 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2569 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2570 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2571 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2572 | lua_pushnil(L); |
| 2573 | return 1; |
| 2574 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2575 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2576 | si = appctx->owner; |
Christopher Faulet | 16f16af | 2021-10-27 09:34:56 +0200 | [diff] [blame] | 2577 | dst = si_dst(si_opposite(si)); |
| 2578 | if (!dst) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2579 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2580 | lua_pushnil(L); |
| 2581 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2582 | } |
| 2583 | |
Christopher Faulet | 16f16af | 2021-10-27 09:34:56 +0200 | [diff] [blame] | 2584 | ret = MAY_LJMP(hlua_socket_info(L, dst)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2585 | xref_unlock(&socket->xref, peer); |
| 2586 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2587 | } |
| 2588 | |
| 2589 | /* Returns information about my connection side. */ |
| 2590 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2591 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2592 | struct hlua_socket *socket; |
| 2593 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2594 | struct appctx *appctx; |
| 2595 | struct xref *peer; |
| 2596 | struct stream_interface *si; |
| 2597 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2598 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2599 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2600 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2601 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2602 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2603 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2604 | /* Check if we run on the same thread than the xreator thread. |
| 2605 | * We cannot access to the socket if the thread is different. |
| 2606 | */ |
| 2607 | if (socket->tid != tid) |
| 2608 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2609 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2610 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2611 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2612 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2613 | lua_pushnil(L); |
| 2614 | return 1; |
| 2615 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2616 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2617 | si = appctx->owner; |
| 2618 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2619 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2620 | conn = cs_conn(objt_cs(s->si[1].end)); |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2621 | if (!conn || !conn_get_src(conn)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2622 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2623 | lua_pushnil(L); |
| 2624 | return 1; |
| 2625 | } |
| 2626 | |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2627 | ret = hlua_socket_info(L, conn->src); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2628 | xref_unlock(&socket->xref, peer); |
| 2629 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2630 | } |
| 2631 | |
| 2632 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2633 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2634 | .obj_type = OBJ_TYPE_APPLET, |
| 2635 | .name = "<LUA_TCP>", |
| 2636 | .fct = hlua_socket_handler, |
| 2637 | .release = hlua_socket_release, |
| 2638 | }; |
| 2639 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2640 | __LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2641 | { |
| 2642 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2643 | struct hlua *hlua; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2644 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2645 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2646 | struct stream_interface *si; |
| 2647 | struct stream *s; |
| 2648 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2649 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2650 | hlua = hlua_gethlua(L); |
| 2651 | if (!hlua) |
| 2652 | return 0; |
| 2653 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2654 | /* Check if we run on the same thread than the xreator thread. |
| 2655 | * We cannot access to the socket if the thread is different. |
| 2656 | */ |
| 2657 | if (socket->tid != tid) |
| 2658 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2659 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2660 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2661 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2662 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2663 | lua_pushnil(L); |
| 2664 | lua_pushstring(L, "Can't connect"); |
| 2665 | return 2; |
| 2666 | } |
| 2667 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2668 | si = appctx->owner; |
| 2669 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2670 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2671 | /* Check if we run on the same thread than the xreator thread. |
| 2672 | * We cannot access to the socket if the thread is different. |
| 2673 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2674 | if (socket->tid != tid) { |
| 2675 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2676 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2677 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2678 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2679 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2680 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2681 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2682 | lua_pushnil(L); |
| 2683 | lua_pushstring(L, "Can't connect"); |
| 2684 | return 2; |
| 2685 | } |
| 2686 | |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2687 | appctx = __objt_appctx(s->si[0].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2688 | |
| 2689 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2690 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2691 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2692 | lua_pushinteger(L, 1); |
| 2693 | return 1; |
| 2694 | } |
| 2695 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2696 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2697 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2698 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2699 | } |
| 2700 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2701 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2702 | return 0; |
| 2703 | } |
| 2704 | |
| 2705 | /* This function fail or initite the connection. */ |
| 2706 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2707 | { |
| 2708 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2709 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2710 | const char *ip; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2711 | struct hlua *hlua; |
| 2712 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2713 | int low, high; |
| 2714 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2715 | struct xref *peer; |
| 2716 | struct stream_interface *si; |
| 2717 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2718 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2719 | if (lua_gettop(L) < 2) |
| 2720 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2721 | |
| 2722 | /* Get args. */ |
| 2723 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2724 | |
| 2725 | /* Check if we run on the same thread than the xreator thread. |
| 2726 | * We cannot access to the socket if the thread is different. |
| 2727 | */ |
| 2728 | if (socket->tid != tid) |
| 2729 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2730 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2731 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2732 | if (lua_gettop(L) >= 3) { |
| 2733 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2734 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2735 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2736 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2737 | * that are not enclosed within square brackets. |
| 2738 | */ |
| 2739 | if (port > 0) { |
| 2740 | luaL_buffinit(L, &b); |
| 2741 | luaL_addstring(&b, ip); |
| 2742 | luaL_addchar(&b, ':'); |
| 2743 | luaL_pushresult(&b); |
| 2744 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2745 | } |
| 2746 | } |
| 2747 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2748 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2749 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2750 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2751 | lua_pushnil(L); |
| 2752 | return 1; |
| 2753 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2754 | |
| 2755 | /* Parse ip address. */ |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 2756 | addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, NULL, NULL, PA_O_PORT_OK | PA_O_STREAM); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2757 | if (!addr) { |
| 2758 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2759 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2760 | } |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2761 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2762 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2763 | if (low == 0) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2764 | if (addr->ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2765 | if (port == -1) { |
| 2766 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2767 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2768 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2769 | ((struct sockaddr_in *)addr)->sin_port = htons(port); |
| 2770 | } else if (addr->ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2771 | if (port == -1) { |
| 2772 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2773 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2774 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2775 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(port); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2776 | } |
| 2777 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2778 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2779 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2780 | si = appctx->owner; |
| 2781 | s = si_strm(si); |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2782 | |
Christopher Faulet | 16f16af | 2021-10-27 09:34:56 +0200 | [diff] [blame] | 2783 | if (!sockaddr_alloc(&si_opposite(si)->dst, addr, sizeof(*addr))) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2784 | xref_unlock(&socket->xref, peer); |
| 2785 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
| 2786 | } |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2787 | s->flags |= SF_ADDR_SET; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2788 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2789 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2790 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2791 | if (!hlua) |
| 2792 | return 0; |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2793 | |
| 2794 | /* inform the stream that we want to be notified whenever the |
| 2795 | * connection completes. |
| 2796 | */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 2797 | si_cant_get(&s->si[0]); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2798 | si_rx_endp_more(&s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2799 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2800 | |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2801 | hlua->gc_count++; |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2802 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2803 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2804 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2805 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2806 | } |
| 2807 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2808 | |
| 2809 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2810 | /* Return yield waiting for connection. */ |
| 2811 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2812 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2813 | |
| 2814 | return 0; |
| 2815 | } |
| 2816 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2817 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2818 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2819 | { |
| 2820 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2821 | struct xref *peer; |
| 2822 | struct appctx *appctx; |
| 2823 | struct stream_interface *si; |
| 2824 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2825 | |
| 2826 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2827 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2828 | |
| 2829 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2830 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2831 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2832 | lua_pushnil(L); |
| 2833 | return 1; |
| 2834 | } |
| 2835 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2836 | si = appctx->owner; |
| 2837 | s = si_strm(si); |
| 2838 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 2839 | s->target = &socket_ssl->obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2840 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2841 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2842 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2843 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2844 | |
| 2845 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2846 | { |
| 2847 | return 0; |
| 2848 | } |
| 2849 | |
| 2850 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2851 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2852 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2853 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2854 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2855 | struct xref *peer; |
| 2856 | struct appctx *appctx; |
| 2857 | struct stream_interface *si; |
| 2858 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2859 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2860 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2861 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2862 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2863 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2864 | /* convert the timeout to millis */ |
| 2865 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2866 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2867 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2868 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2869 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2870 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2871 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2872 | WILL_LJMP(luaL_error(L, "settimeout: cannot set values larger than %d ms", INT_MAX)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2873 | |
| 2874 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2875 | if (tmout == 0) |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2876 | tmout++; /* very small timeouts are adjusted to a minimum of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2877 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2878 | /* Check if we run on the same thread than the xreator thread. |
| 2879 | * We cannot access to the socket if the thread is different. |
| 2880 | */ |
| 2881 | if (socket->tid != tid) |
| 2882 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2883 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2884 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2885 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2886 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2887 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2888 | WILL_LJMP(lua_error(L)); |
| 2889 | return 0; |
| 2890 | } |
| 2891 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2892 | si = appctx->owner; |
| 2893 | s = si_strm(si); |
| 2894 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2895 | s->sess->fe->timeout.connect = tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2896 | s->req.rto = tmout; |
| 2897 | s->req.wto = tmout; |
| 2898 | s->res.rto = tmout; |
| 2899 | s->res.wto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2900 | s->req.rex = tick_add_ifset(now_ms, tmout); |
| 2901 | s->req.wex = tick_add_ifset(now_ms, tmout); |
| 2902 | s->res.rex = tick_add_ifset(now_ms, tmout); |
| 2903 | s->res.wex = tick_add_ifset(now_ms, tmout); |
| 2904 | |
| 2905 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 2906 | task_queue(s->task); |
| 2907 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2908 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2909 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 2910 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 2911 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2912 | } |
| 2913 | |
| 2914 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2915 | { |
| 2916 | struct hlua_socket *socket; |
| 2917 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2918 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2919 | struct stream *strm; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2920 | |
| 2921 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2922 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2923 | hlua_pusherror(L, "socket: full stack"); |
| 2924 | goto out_fail_conf; |
| 2925 | } |
| 2926 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2927 | /* Create the object: obj[0] = userdata. */ |
| 2928 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2929 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2930 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2931 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2932 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2933 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2934 | /* Check if the various memory pools are initialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2935 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2936 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2937 | goto out_fail_conf; |
| 2938 | } |
| 2939 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2940 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2941 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2942 | lua_setmetatable(L, -2); |
| 2943 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2944 | /* Create the applet context */ |
Willy Tarreau | e612446 | 2021-09-13 10:07:38 +0200 | [diff] [blame] | 2945 | appctx = appctx_new(&update_applet); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2946 | if (!appctx) { |
| 2947 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2948 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2949 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2950 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2951 | appctx->ctx.hlua_cosocket.connected = 0; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2952 | appctx->ctx.hlua_cosocket.die = 0; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2953 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 2954 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2955 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2956 | /* Now create a session, task and stream for this applet */ |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 2957 | sess = session_new(socket_proxy, NULL, &appctx->obj_type); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2958 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2959 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2960 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2961 | } |
| 2962 | |
Christopher Faulet | 26256f8 | 2020-09-14 11:40:13 +0200 | [diff] [blame] | 2963 | strm = stream_new(sess, &appctx->obj_type, &BUF_NULL); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2964 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2965 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2966 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2967 | } |
| 2968 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2969 | /* Initialise cross reference between stream and Lua socket object. */ |
| 2970 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2971 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2972 | /* Configure "right" stream interface. this "si" is used to connect |
| 2973 | * and retrieve data from the server. The connection is initialized |
| 2974 | * with the "struct server". |
| 2975 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2976 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2977 | |
| 2978 | /* Force destination server. */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2979 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED; |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 2980 | strm->target = &socket_tcp->obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2981 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2982 | return 1; |
| 2983 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2984 | out_fail_stream: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 2985 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2986 | out_fail_sess: |
| 2987 | appctx_free(appctx); |
| 2988 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2989 | WILL_LJMP(lua_error(L)); |
| 2990 | return 0; |
| 2991 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2992 | |
| 2993 | /* |
| 2994 | * |
| 2995 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2996 | * Class Channel |
| 2997 | * |
| 2998 | * |
| 2999 | */ |
| 3000 | |
| 3001 | /* Returns the struct hlua_channel join to the class channel in the |
| 3002 | * stack entry "ud" or throws an argument error. |
| 3003 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3004 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3005 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3006 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3007 | } |
| 3008 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3009 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 3010 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3011 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 3012 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3013 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3014 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3015 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3016 | return 0; |
| 3017 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3018 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3019 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3020 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3021 | |
| 3022 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3023 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 3024 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3025 | return 1; |
| 3026 | } |
| 3027 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 3028 | /* Helper function returning a filter attached to a channel at the position <ud> |
| 3029 | * in the stack, filling the current offset and length of the filter. If no |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 3030 | * filter is attached, NULL is returned and <offset> and <len> are not |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 3031 | * initialized. |
| 3032 | */ |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3033 | static struct filter *hlua_channel_filter(lua_State *L, int ud, struct channel *chn, size_t *offset, size_t *len) |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 3034 | { |
| 3035 | struct filter *filter = NULL; |
| 3036 | |
| 3037 | if (lua_getfield(L, ud, "__filter") == LUA_TLIGHTUSERDATA) { |
| 3038 | struct hlua_flt_ctx *flt_ctx; |
| 3039 | |
| 3040 | filter = lua_touserdata (L, -1); |
| 3041 | flt_ctx = filter->ctx; |
| 3042 | if (hlua_filter_from_payload(filter)) { |
| 3043 | *offset = flt_ctx->cur_off[CHN_IDX(chn)]; |
| 3044 | *len = flt_ctx->cur_len[CHN_IDX(chn)]; |
| 3045 | } |
| 3046 | } |
| 3047 | |
| 3048 | lua_pop(L, 1); |
| 3049 | return filter; |
| 3050 | } |
| 3051 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3052 | /* Copies <len> bytes of data present in the channel's buffer, starting at the |
| 3053 | * offset <offset>, and put it in a LUA string variable. It is the caller |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3054 | * responsibility to ensure <len> and <offset> are valid. It always return the |
| 3055 | * length of the built string. <len> may be 0, in this case, an empty string is |
| 3056 | * created and 0 is returned. |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3057 | */ |
| 3058 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L, size_t offset, size_t len) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3059 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3060 | size_t block1, block2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3061 | luaL_Buffer b; |
| 3062 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3063 | block1 = len; |
| 3064 | if (block1 > b_contig_data(&chn->buf, b_peek_ofs(&chn->buf, offset))) |
| 3065 | block1 = b_contig_data(&chn->buf, b_peek_ofs(&chn->buf, offset)); |
| 3066 | block2 = len - block1; |
| 3067 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3068 | luaL_buffinit(L, &b); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3069 | luaL_addlstring(&b, b_peek(&chn->buf, offset), block1); |
| 3070 | if (block2) |
| 3071 | luaL_addlstring(&b, b_orig(&chn->buf), block2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3072 | luaL_pushresult(&b); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3073 | return len; |
| 3074 | } |
| 3075 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3076 | /* Inserts the string <str> to the channel's buffer at the offset <offset>. This |
| 3077 | * function returns -1 if data cannot be copied. Otherwise, it returns the |
| 3078 | * number of bytes copied. |
| 3079 | */ |
| 3080 | static int _hlua_channel_insert(struct channel *chn, lua_State *L, struct ist str, size_t offset) |
| 3081 | { |
| 3082 | int ret = 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3083 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3084 | /* Nothing to do, just return */ |
| 3085 | if (unlikely(istlen(str) == 0)) |
| 3086 | goto end; |
| 3087 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3088 | if (istlen(str) > c_room(chn)) { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3089 | ret = -1; |
| 3090 | goto end; |
| 3091 | } |
| 3092 | ret = b_insert_blk(&chn->buf, offset, istptr(str), istlen(str)); |
| 3093 | |
| 3094 | end: |
| 3095 | return ret; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3096 | } |
| 3097 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3098 | /* Removes <len> bytes of data at the absolute position <offset>. |
| 3099 | */ |
| 3100 | static void _hlua_channel_delete(struct channel *chn, size_t offset, size_t len) |
| 3101 | { |
| 3102 | size_t end = offset + len; |
| 3103 | |
| 3104 | if (b_peek(&chn->buf, end) != b_tail(&chn->buf)) |
| 3105 | b_move(&chn->buf, b_peek_ofs(&chn->buf, end), |
| 3106 | b_data(&chn->buf) - end, -len); |
| 3107 | b_sub(&chn->buf, len); |
| 3108 | } |
| 3109 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3110 | /* Copies input data in the channel's buffer. It is possible to set a specific |
| 3111 | * offset (0 by default) and a length (all remaining input data starting for the |
| 3112 | * offset by default). If there is not enough input data and more data can be |
| 3113 | * received, this function yields. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3114 | * |
| 3115 | * From an action, All input data are considered. For a filter, the offset and |
| 3116 | * the length of input data to consider are retrieved from the filter context. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3117 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3118 | __LJMP static int hlua_channel_get_data_yield(lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3119 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3120 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3121 | struct filter *filter; |
| 3122 | size_t input, output; |
| 3123 | int offset, len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3124 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3125 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3126 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3127 | output = co_data(chn); |
| 3128 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3129 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3130 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3131 | if (filter && !hlua_filter_from_payload(filter)) |
| 3132 | WILL_LJMP(lua_error(L)); |
| 3133 | |
| 3134 | offset = output; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3135 | if (lua_gettop(L) > 1) { |
| 3136 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3137 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3138 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3139 | offset += output; |
| 3140 | if (offset < output || offset > input + output) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3141 | lua_pushfstring(L, "offset out of range."); |
| 3142 | WILL_LJMP(lua_error(L)); |
| 3143 | } |
| 3144 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3145 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3146 | if (lua_gettop(L) == 3) { |
| 3147 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3148 | if (!len) |
| 3149 | goto dup; |
| 3150 | if (len == -1) |
| 3151 | len = global.tune.bufsize; |
| 3152 | if (len < 0) { |
| 3153 | lua_pushfstring(L, "length out of range."); |
| 3154 | WILL_LJMP(lua_error(L)); |
| 3155 | } |
| 3156 | } |
| 3157 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3158 | if (offset + len > output + input) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3159 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3160 | /* Yield waiting for more data, as requested */ |
| 3161 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_data_yield, TICK_ETERNITY, 0)); |
| 3162 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3163 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3164 | } |
| 3165 | |
| 3166 | dup: |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3167 | _hlua_channel_dup(chn, L, offset, len); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3168 | return 1; |
| 3169 | } |
| 3170 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3171 | /* Copies the first line (including the trailing LF) of input data in the |
| 3172 | * channel's buffer. It is possible to set a specific offset (0 by default) and |
| 3173 | * a length (all remaining input data starting for the offset by default). If |
| 3174 | * there is not enough input data and more data can be received, the function |
| 3175 | * yields. If a length is explicitly specified, no more data are |
| 3176 | * copied. Otherwise, if no LF is found and more data can be received, this |
| 3177 | * function yields. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3178 | * |
| 3179 | * From an action, All input data are considered. For a filter, the offset and |
| 3180 | * the length of input data to consider are retrieved from the filter context. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3181 | */ |
| 3182 | __LJMP static int hlua_channel_get_line_yield(lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3183 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3184 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3185 | struct filter *filter; |
| 3186 | size_t l, input, output; |
| 3187 | int offset, len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3188 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3189 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3190 | output = co_data(chn); |
| 3191 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3192 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3193 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3194 | if (filter && !hlua_filter_from_payload(filter)) |
| 3195 | WILL_LJMP(lua_error(L)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3196 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3197 | offset = output; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3198 | if (lua_gettop(L) > 1) { |
| 3199 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3200 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3201 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3202 | offset += output; |
| 3203 | if (offset < output || offset > input + output) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3204 | lua_pushfstring(L, "offset out of range."); |
| 3205 | WILL_LJMP(lua_error(L)); |
| 3206 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3207 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3208 | |
| 3209 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3210 | if (lua_gettop(L) == 3) { |
| 3211 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3212 | if (!len) |
| 3213 | goto dup; |
| 3214 | if (len == -1) |
| 3215 | len = global.tune.bufsize; |
| 3216 | if (len < 0) { |
| 3217 | lua_pushfstring(L, "length out of range."); |
| 3218 | WILL_LJMP(lua_error(L)); |
| 3219 | } |
| 3220 | } |
| 3221 | |
| 3222 | for (l = 0; l < len; l++) { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3223 | if (l + offset >= output + input) |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3224 | break; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3225 | if (*(b_peek(&chn->buf, offset + l)) == '\n') { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3226 | len = l+1; |
| 3227 | goto dup; |
| 3228 | } |
| 3229 | } |
| 3230 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3231 | if (offset + len > output + input) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3232 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3233 | /* Yield waiting for more data */ |
| 3234 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_line_yield, TICK_ETERNITY, 0)); |
| 3235 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3236 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3237 | } |
| 3238 | |
| 3239 | dup: |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3240 | _hlua_channel_dup(chn, L, offset, len); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3241 | return 1; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3242 | } |
| 3243 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3244 | /* [ DEPRECATED ] |
| 3245 | * |
| 3246 | * Duplicate all input data foud in the channel's buffer. The data are not |
| 3247 | * removed from the buffer. This function relies on _hlua_channel_dup(). |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3248 | * |
| 3249 | * From an action, All input data are considered. For a filter, the offset and |
| 3250 | * the length of input data to consider are retrieved from the filter context. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3251 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3252 | __LJMP static int hlua_channel_dup(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3253 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3254 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3255 | struct filter *filter; |
| 3256 | size_t offset, len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3257 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3258 | MAY_LJMP(check_args(L, 1, "dup")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3259 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3260 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3261 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3262 | WILL_LJMP(lua_error(L)); |
| 3263 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3264 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3265 | offset = co_data(chn); |
| 3266 | len = ci_data(chn); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3267 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3268 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3269 | if (filter && !hlua_filter_from_payload(filter)) |
| 3270 | WILL_LJMP(lua_error(L)); |
| 3271 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3272 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3273 | lua_pushnil(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3274 | return 1; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3275 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3276 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3277 | _hlua_channel_dup(chn, L, offset, len); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3278 | return 1; |
| 3279 | } |
| 3280 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3281 | /* [ DEPRECATED ] |
| 3282 | * |
| 3283 | * Get all input data foud in the channel's buffer. The data are removed from |
| 3284 | * the buffer after the copy. This function relies on _hlua_channel_dup() and |
| 3285 | * _hlua_channel_delete(). |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3286 | * |
| 3287 | * From an action, All input data are considered. For a filter, the offset and |
| 3288 | * the length of input data to consider are retrieved from the filter context. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3289 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3290 | __LJMP static int hlua_channel_get(lua_State *L) |
| 3291 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3292 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3293 | struct filter *filter; |
| 3294 | size_t offset, len; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3295 | int ret; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3296 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3297 | MAY_LJMP(check_args(L, 1, "get")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3298 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3299 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3300 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3301 | WILL_LJMP(lua_error(L)); |
| 3302 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3303 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3304 | offset = co_data(chn); |
| 3305 | len = ci_data(chn); |
| 3306 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3307 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3308 | if (filter && !hlua_filter_from_payload(filter)) |
| 3309 | WILL_LJMP(lua_error(L)); |
| 3310 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3311 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3312 | lua_pushnil(L); |
| 3313 | return 1; |
| 3314 | } |
| 3315 | |
| 3316 | ret = _hlua_channel_dup(chn, L, offset, len); |
| 3317 | _hlua_channel_delete(chn, offset, ret); |
| 3318 | return 1; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3319 | } |
| 3320 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3321 | /* This functions consumes and returns one line. If the channel is closed, |
| 3322 | * and the last data does not contains a final '\n', the data are returned |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3323 | * without the final '\n'. When no more data are available, it returns nil |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3324 | * value. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3325 | * |
| 3326 | * From an action, All input data are considered. For a filter, the offset and |
| 3327 | * the length of input data to consider are retrieved from the filter context. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3328 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3329 | __LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3330 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3331 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3332 | struct filter *filter; |
| 3333 | size_t l, offset, len; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3334 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3335 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3336 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3337 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3338 | offset = co_data(chn); |
| 3339 | len = ci_data(chn); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3340 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3341 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3342 | if (filter && !hlua_filter_from_payload(filter)) |
| 3343 | WILL_LJMP(lua_error(L)); |
| 3344 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3345 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3346 | lua_pushnil(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3347 | return 1; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3348 | } |
| 3349 | |
| 3350 | for (l = 0; l < len; l++) { |
| 3351 | if (*(b_peek(&chn->buf, offset+l)) == '\n') { |
| 3352 | len = l+1; |
| 3353 | goto dup; |
| 3354 | } |
| 3355 | } |
| 3356 | |
| 3357 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3358 | /* Yield waiting for more data */ |
| 3359 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0)); |
| 3360 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3361 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3362 | dup: |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3363 | ret = _hlua_channel_dup(chn, L, offset, len); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3364 | _hlua_channel_delete(chn, offset, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3365 | return 1; |
| 3366 | } |
| 3367 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3368 | /* [ DEPRECATED ] |
| 3369 | * |
| 3370 | * Check arguments for the function "hlua_channel_getline_yield". |
| 3371 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3372 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 3373 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3374 | struct channel *chn; |
| 3375 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3376 | MAY_LJMP(check_args(L, 1, "getline")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3377 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3378 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3379 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3380 | WILL_LJMP(lua_error(L)); |
| 3381 | } |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3382 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 3383 | } |
| 3384 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3385 | /* Retrieves a given amount of input data at the given offset. By default all |
| 3386 | * available input data are returned. The offset may be negactive to start from |
| 3387 | * the end of input data. The length may be -1 to set it to the maximum buffer |
| 3388 | * size. |
| 3389 | */ |
| 3390 | __LJMP static int hlua_channel_get_data(lua_State *L) |
| 3391 | { |
| 3392 | struct channel *chn; |
| 3393 | |
| 3394 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3395 | WILL_LJMP(luaL_error(L, "'data' expects at most 2 arguments")); |
| 3396 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3397 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3398 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3399 | WILL_LJMP(lua_error(L)); |
| 3400 | } |
| 3401 | return MAY_LJMP(hlua_channel_get_data_yield(L, 0, 0)); |
| 3402 | } |
| 3403 | |
| 3404 | /* Retrieves a given amount of input data at the given offset. By default all |
| 3405 | * available input data are returned. The offset may be negactive to start from |
| 3406 | * the end of input data. The length may be -1 to set it to the maximum buffer |
| 3407 | * size. |
| 3408 | */ |
| 3409 | __LJMP static int hlua_channel_get_line(lua_State *L) |
| 3410 | { |
| 3411 | struct channel *chn; |
| 3412 | |
| 3413 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3414 | WILL_LJMP(luaL_error(L, "'line' expects at most 2 arguments")); |
| 3415 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3416 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3417 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3418 | WILL_LJMP(lua_error(L)); |
| 3419 | } |
| 3420 | return MAY_LJMP(hlua_channel_get_line_yield(L, 0, 0)); |
| 3421 | } |
| 3422 | |
| 3423 | /* Appends a string into the input side of channel. It returns the length of the |
| 3424 | * written string, or -1 if the channel is closed or if the buffer size is too |
| 3425 | * little for the data. 0 may be returned if nothing is copied. This function |
| 3426 | * does not yield. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3427 | * |
| 3428 | * For a filter, the context is updated on success. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3429 | */ |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3430 | __LJMP static int hlua_channel_append(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3431 | { |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3432 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3433 | struct filter *filter; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3434 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3435 | size_t sz, offset, len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3436 | int ret; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3437 | |
| 3438 | MAY_LJMP(check_args(L, 2, "append")); |
| 3439 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3440 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3441 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3442 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3443 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3444 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3445 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3446 | offset = co_data(chn); |
| 3447 | len = ci_data(chn); |
| 3448 | |
| 3449 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3450 | if (filter && !hlua_filter_from_payload(filter)) |
| 3451 | WILL_LJMP(lua_error(L)); |
| 3452 | |
| 3453 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3454 | if (ret > 0 && filter) { |
| 3455 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3456 | |
| 3457 | flt_update_offsets(filter, chn, ret); |
| 3458 | flt_ctx->cur_len[CHN_IDX(chn)] += ret; |
| 3459 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3460 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3461 | return 1; |
| 3462 | } |
| 3463 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3464 | /* Prepends a string into the input side of channel. It returns the length of the |
| 3465 | * written string, or -1 if the channel is closed or if the buffer size is too |
| 3466 | * little for the data. 0 may be returned if nothing is copied. This function |
| 3467 | * does not yield. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3468 | * |
| 3469 | * For a filter, the context is updated on success. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3470 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3471 | __LJMP static int hlua_channel_prepend(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3472 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3473 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3474 | struct filter *filter; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3475 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3476 | size_t sz, offset, len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3477 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3478 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3479 | MAY_LJMP(check_args(L, 2, "prepend")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3480 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3481 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 3482 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3483 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3484 | WILL_LJMP(lua_error(L)); |
| 3485 | } |
| 3486 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3487 | offset = co_data(chn); |
| 3488 | len = ci_data(chn); |
| 3489 | |
| 3490 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3491 | if (filter && !hlua_filter_from_payload(filter)) |
| 3492 | WILL_LJMP(lua_error(L)); |
| 3493 | |
| 3494 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3495 | if (ret > 0 && filter) { |
| 3496 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3497 | |
| 3498 | flt_update_offsets(filter, chn, ret); |
| 3499 | flt_ctx->cur_len[CHN_IDX(chn)] += ret; |
| 3500 | } |
| 3501 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3502 | lua_pushinteger(L, ret); |
| 3503 | return 1; |
| 3504 | } |
| 3505 | |
| 3506 | /* Inserts a given amount of input data at the given offset by a string |
| 3507 | * content. By default the string is appended at the end of input data. It |
| 3508 | * returns the length of the written string, or -1 if the channel is closed or |
| 3509 | * if the buffer size is too little for the data. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3510 | * |
| 3511 | * For a filter, the context is updated on success. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3512 | */ |
| 3513 | __LJMP static int hlua_channel_insert_data(lua_State *L) |
| 3514 | { |
| 3515 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3516 | struct filter *filter; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3517 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3518 | size_t sz, input, output; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3519 | int ret, offset; |
| 3520 | |
| 3521 | if (lua_gettop(L) < 2 || lua_gettop(L) > 3) |
| 3522 | WILL_LJMP(luaL_error(L, "'insert' expects at least 1 argument and at most 2 arguments")); |
| 3523 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3524 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3525 | |
| 3526 | output = co_data(chn); |
| 3527 | input = ci_data(chn); |
| 3528 | |
| 3529 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3530 | if (filter && !hlua_filter_from_payload(filter)) |
| 3531 | WILL_LJMP(lua_error(L)); |
| 3532 | |
| 3533 | offset = input + output; |
| 3534 | if (lua_gettop(L) > 2) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3535 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3536 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3537 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3538 | offset += output; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3539 | if (offset < output || offset > output + input) { |
| 3540 | lua_pushfstring(L, "offset out of range."); |
| 3541 | WILL_LJMP(lua_error(L)); |
| 3542 | } |
| 3543 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3544 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3545 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3546 | WILL_LJMP(lua_error(L)); |
| 3547 | } |
| 3548 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3549 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3550 | if (ret > 0 && filter) { |
| 3551 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3552 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3553 | flt_update_offsets(filter, chn, ret); |
| 3554 | flt_ctx->cur_len[CHN_IDX(chn)] += ret; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3555 | } |
| 3556 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3557 | lua_pushinteger(L, ret); |
| 3558 | return 1; |
| 3559 | } |
| 3560 | /* Replaces a given amount of input data at the given offset by a string |
| 3561 | * content. By default all remaining data are removed (offset = 0 and len = |
| 3562 | * -1). It returns the length of the written string, or -1 if the channel is |
| 3563 | * closed or if the buffer size is too little for the data. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3564 | * |
| 3565 | * For a filter, the context is updated on success. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3566 | */ |
| 3567 | __LJMP static int hlua_channel_set_data(lua_State *L) |
| 3568 | { |
| 3569 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3570 | struct filter *filter; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3571 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3572 | size_t sz, input, output; |
| 3573 | int ret, offset, len; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3574 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3575 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 3576 | WILL_LJMP(luaL_error(L, "'set' expects at least 1 argument and at most 3 arguments")); |
| 3577 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3578 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3579 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3580 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3581 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3582 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3583 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3584 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3585 | output = co_data(chn); |
| 3586 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3587 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3588 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3589 | if (filter && !hlua_filter_from_payload(filter)) |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3590 | WILL_LJMP(lua_error(L)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3591 | |
| 3592 | offset = output; |
| 3593 | if (lua_gettop(L) > 2) { |
| 3594 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3595 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3596 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3597 | offset += output; |
| 3598 | if (offset < output || offset > input + output) { |
| 3599 | lua_pushfstring(L, "offset out of range."); |
| 3600 | WILL_LJMP(lua_error(L)); |
| 3601 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3602 | } |
| 3603 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3604 | len = output + input - offset; |
| 3605 | if (lua_gettop(L) == 4) { |
| 3606 | len = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 3607 | if (!len) |
| 3608 | goto set; |
| 3609 | if (len == -1) |
| 3610 | len = output + input - offset; |
| 3611 | if (len < 0 || offset + len > output + input) { |
| 3612 | lua_pushfstring(L, "length out of range."); |
| 3613 | WILL_LJMP(lua_error(L)); |
| 3614 | } |
| 3615 | } |
| 3616 | |
| 3617 | set: |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3618 | /* Be sure we can copied the string once input data will be removed. */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3619 | if (sz > c_room(chn) + len) |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3620 | lua_pushinteger(L, -1); |
| 3621 | else { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3622 | _hlua_channel_delete(chn, offset, len); |
| 3623 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3624 | if (filter) { |
| 3625 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3626 | |
| 3627 | len -= (ret > 0 ? ret : 0); |
| 3628 | flt_update_offsets(filter, chn, -len); |
| 3629 | flt_ctx->cur_len[CHN_IDX(chn)] -= len; |
| 3630 | } |
| 3631 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3632 | lua_pushinteger(L, ret); |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3633 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3634 | return 1; |
| 3635 | } |
| 3636 | |
| 3637 | /* Removes a given amount of input data at the given offset. By default all |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3638 | * input data are removed (offset = 0 and len = -1). It returns the amount of |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3639 | * the removed data. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3640 | * |
| 3641 | * For a filter, the context is updated on success. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3642 | */ |
| 3643 | __LJMP static int hlua_channel_del_data(lua_State *L) |
| 3644 | { |
| 3645 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3646 | struct filter *filter; |
| 3647 | size_t input, output; |
| 3648 | int offset, len; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3649 | |
| 3650 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3651 | WILL_LJMP(luaL_error(L, "'remove' expects at most 2 arguments")); |
| 3652 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3653 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3654 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3655 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3656 | WILL_LJMP(lua_error(L)); |
| 3657 | } |
| 3658 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3659 | output = co_data(chn); |
| 3660 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3661 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3662 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3663 | if (filter && !hlua_filter_from_payload(filter)) |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3664 | WILL_LJMP(lua_error(L)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3665 | |
| 3666 | offset = output; |
| 3667 | if (lua_gettop(L) > 2) { |
| 3668 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3669 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3670 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3671 | offset += output; |
| 3672 | if (offset < output || offset > input + output) { |
| 3673 | lua_pushfstring(L, "offset out of range."); |
| 3674 | WILL_LJMP(lua_error(L)); |
| 3675 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3676 | } |
| 3677 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3678 | len = output + input - offset; |
| 3679 | if (lua_gettop(L) == 4) { |
| 3680 | len = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 3681 | if (!len) |
| 3682 | goto end; |
| 3683 | if (len == -1) |
| 3684 | len = output + input - offset; |
| 3685 | if (len < 0 || offset + len > output + input) { |
| 3686 | lua_pushfstring(L, "length out of range."); |
| 3687 | WILL_LJMP(lua_error(L)); |
| 3688 | } |
| 3689 | } |
| 3690 | |
| 3691 | _hlua_channel_delete(chn, offset, len); |
| 3692 | if (filter) { |
| 3693 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3694 | |
| 3695 | flt_update_offsets(filter, chn, -len); |
| 3696 | flt_ctx->cur_len[CHN_IDX(chn)] -= len; |
| 3697 | } |
| 3698 | |
| 3699 | end: |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3700 | lua_pushinteger(L, len); |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3701 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3702 | } |
| 3703 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3704 | /* Append data in the output side of the buffer. This data is immediately |
| 3705 | * sent. The function returns the amount of data written. If the buffer |
| 3706 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3707 | * if the channel is closed. |
| 3708 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3709 | __LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3710 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3711 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3712 | struct filter *filter; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3713 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3714 | size_t offset, len, sz; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3715 | int l, ret; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3716 | struct hlua *hlua; |
| 3717 | |
| 3718 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3719 | hlua = hlua_gethlua(L); |
| 3720 | if (!hlua) { |
| 3721 | lua_pushnil(L); |
| 3722 | return 1; |
| 3723 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3724 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3725 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3726 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 3727 | l = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3728 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3729 | offset = co_data(chn); |
| 3730 | len = ci_data(chn); |
| 3731 | |
| 3732 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3733 | if (filter && !hlua_filter_from_payload(filter)) |
| 3734 | WILL_LJMP(lua_error(L)); |
| 3735 | |
| 3736 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3737 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3738 | lua_pushinteger(L, -1); |
| 3739 | return 1; |
| 3740 | } |
| 3741 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3742 | len = c_room(chn); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3743 | if (len > sz -l) { |
| 3744 | if (filter) { |
| 3745 | lua_pushinteger(L, -1); |
| 3746 | return 1; |
| 3747 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3748 | len = sz - l; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3749 | } |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3750 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3751 | ret = _hlua_channel_insert(chn, L, ist2(str, len), offset); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3752 | if (ret == -1) { |
| 3753 | lua_pop(L, 1); |
| 3754 | lua_pushinteger(L, -1); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3755 | return 1; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3756 | } |
| 3757 | if (ret) { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3758 | if (filter) { |
| 3759 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3760 | |
| 3761 | |
| 3762 | flt_update_offsets(filter, chn, ret); |
| 3763 | FLT_OFF(filter, chn) += ret; |
| 3764 | flt_ctx->cur_off[CHN_IDX(chn)] += ret; |
| 3765 | } |
| 3766 | else |
| 3767 | c_adv(chn, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3768 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3769 | l += ret; |
| 3770 | lua_pop(L, 1); |
| 3771 | lua_pushinteger(L, l); |
| 3772 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3773 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3774 | if (l < sz) { |
| 3775 | /* Yield only if the channel's output is not empty. |
| 3776 | * Otherwise it means we cannot add more data. */ |
| 3777 | if (co_data(chn) == 0 || HLUA_CANT_YIELD(hlua_gethlua(L))) |
Christopher Faulet | 2e60aa4 | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3778 | return 1; |
| 3779 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3780 | /* If we are waiting for space in the response buffer, we |
| 3781 | * must set the flag WAKERESWR. This flag required the task |
| 3782 | * wake up if any activity is detected on the response buffer. |
| 3783 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3784 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3785 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3786 | else |
| 3787 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3788 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3789 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3790 | |
| 3791 | return 1; |
| 3792 | } |
| 3793 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3794 | /* Just a wrapper of "_hlua_channel_send". This wrapper permits |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3795 | * yield the LUA process, and resume it without checking the |
| 3796 | * input arguments. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3797 | * |
| 3798 | * This function cannot be called from a filter. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3799 | */ |
| 3800 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3801 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3802 | struct channel *chn; |
| 3803 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3804 | MAY_LJMP(check_args(L, 2, "send")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3805 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3806 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3807 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3808 | WILL_LJMP(lua_error(L)); |
| 3809 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3810 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3811 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3812 | } |
| 3813 | |
| 3814 | /* This function forward and amount of butes. The data pass from |
| 3815 | * the input side of the buffer to the output side, and can be |
| 3816 | * forwarded. This function never fails. |
| 3817 | * |
| 3818 | * The Lua function takes an amount of bytes to be forwarded in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3819 | * input. It returns the number of bytes forwarded. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3820 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3821 | __LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3822 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3823 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3824 | struct filter *filter; |
| 3825 | size_t offset, len, fwd; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3826 | int l, max; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3827 | struct hlua *hlua; |
| 3828 | |
| 3829 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3830 | hlua = hlua_gethlua(L); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3831 | if (!hlua) { |
| 3832 | lua_pushnil(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3833 | return 1; |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3834 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3835 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3836 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3837 | fwd = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3838 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3839 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3840 | offset = co_data(chn); |
| 3841 | len = ci_data(chn); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3842 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3843 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3844 | if (filter && !hlua_filter_from_payload(filter)) |
| 3845 | WILL_LJMP(lua_error(L)); |
| 3846 | |
| 3847 | max = fwd - l; |
| 3848 | if (max > len) |
| 3849 | max = len; |
| 3850 | |
| 3851 | if (filter) { |
| 3852 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3853 | |
| 3854 | FLT_OFF(filter, chn) += max; |
| 3855 | flt_ctx->cur_off[CHN_IDX(chn)] += max; |
| 3856 | flt_ctx->cur_len[CHN_IDX(chn)] -= max; |
| 3857 | } |
| 3858 | else |
| 3859 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3860 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3861 | l += max; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3862 | lua_pop(L, 1); |
| 3863 | lua_pushinteger(L, l); |
| 3864 | |
| 3865 | /* Check if it miss bytes to forward. */ |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3866 | if (l < fwd) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3867 | /* The the input channel or the output channel are closed, we |
| 3868 | * must return the amount of data forwarded. |
| 3869 | */ |
Christopher Faulet | 2e60aa4 | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3870 | if (channel_input_closed(chn) || channel_output_closed(chn) || HLUA_CANT_YIELD(hlua_gethlua(L))) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3871 | return 1; |
| 3872 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3873 | /* If we are waiting for space data in the response buffer, we |
| 3874 | * must set the flag WAKERESWR. This flag required the task |
| 3875 | * wake up if any activity is detected on the response buffer. |
| 3876 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3877 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3878 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3879 | else |
| 3880 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3881 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3882 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3883 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3884 | } |
| 3885 | |
| 3886 | return 1; |
| 3887 | } |
| 3888 | |
| 3889 | /* Just check the input and prepare the stack for the previous |
| 3890 | * function "hlua_channel_forward_yield" |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3891 | * |
| 3892 | * This function cannot be called from a filter. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3893 | */ |
| 3894 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3895 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3896 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3897 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3898 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3899 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3900 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3901 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3902 | WILL_LJMP(lua_error(L)); |
| 3903 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3904 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3905 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3906 | } |
| 3907 | |
| 3908 | /* Just returns the number of bytes available in the input |
| 3909 | * side of the buffer. This function never fails. |
| 3910 | */ |
| 3911 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3912 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3913 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3914 | struct filter *filter; |
| 3915 | size_t output, input; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3916 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3917 | MAY_LJMP(check_args(L, 1, "input")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3918 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3919 | |
| 3920 | output = co_data(chn); |
| 3921 | input = ci_data(chn); |
| 3922 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3923 | if (filter || !IS_HTX_STRM(chn_strm(chn))) |
| 3924 | lua_pushinteger(L, input); |
| 3925 | else { |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3926 | struct htx *htx = htxbuf(&chn->buf); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3927 | |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3928 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 3929 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3930 | return 1; |
| 3931 | } |
| 3932 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3933 | /* Returns true if the channel is full. */ |
| 3934 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3935 | { |
| 3936 | struct channel *chn; |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3937 | |
| 3938 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3939 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 0ec740e | 2020-02-26 11:59:19 +0100 | [diff] [blame] | 3940 | /* ignore the reserve, we are not on a producer side (ie in an |
| 3941 | * applet). |
| 3942 | */ |
| 3943 | lua_pushboolean(L, channel_full(chn, 0)); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3944 | return 1; |
| 3945 | } |
| 3946 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3947 | /* Returns true if the channel may still receive data. */ |
| 3948 | __LJMP static int hlua_channel_may_recv(lua_State *L) |
| 3949 | { |
| 3950 | struct channel *chn; |
| 3951 | |
| 3952 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 3953 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3954 | lua_pushboolean(L, (!channel_input_closed(chn) && channel_may_recv(chn))); |
| 3955 | return 1; |
| 3956 | } |
| 3957 | |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 3958 | /* Returns true if the channel is the response channel. */ |
| 3959 | __LJMP static int hlua_channel_is_resp(lua_State *L) |
| 3960 | { |
| 3961 | struct channel *chn; |
| 3962 | |
| 3963 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 3964 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3965 | |
| 3966 | lua_pushboolean(L, !!(chn->flags & CF_ISRESP)); |
| 3967 | return 1; |
| 3968 | } |
| 3969 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3970 | /* Just returns the number of bytes available in the output |
| 3971 | * side of the buffer. This function never fails. |
| 3972 | */ |
| 3973 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 3974 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3975 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3976 | size_t output, input; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3977 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3978 | MAY_LJMP(check_args(L, 1, "output")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3979 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3980 | |
| 3981 | output = co_data(chn); |
| 3982 | input = ci_data(chn); |
| 3983 | hlua_channel_filter(L, 1, chn, &output, &input); |
| 3984 | |
| 3985 | lua_pushinteger(L, output); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3986 | return 1; |
| 3987 | } |
| 3988 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3989 | /* |
| 3990 | * |
| 3991 | * |
| 3992 | * Class Fetches |
| 3993 | * |
| 3994 | * |
| 3995 | */ |
| 3996 | |
| 3997 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3998 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3999 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4000 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4001 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4002 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4003 | } |
| 4004 | |
| 4005 | /* This function creates and push in the stack a fetch object according |
| 4006 | * with a current TXN. |
| 4007 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4008 | static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, unsigned int flags) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4009 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4010 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4011 | |
| 4012 | /* Check stack size. */ |
| 4013 | if (!lua_checkstack(L, 3)) |
| 4014 | return 0; |
| 4015 | |
| 4016 | /* Create the object: obj[0] = userdata. |
| 4017 | * Note that the base of the Fetches object is the |
| 4018 | * transaction object. |
| 4019 | */ |
| 4020 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4021 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4022 | lua_rawseti(L, -2, 0); |
| 4023 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4024 | hsmp->s = txn->s; |
| 4025 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 4026 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4027 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4028 | |
| 4029 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 4030 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 4031 | lua_setmetatable(L, -2); |
| 4032 | |
| 4033 | return 1; |
| 4034 | } |
| 4035 | |
| 4036 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 4037 | * It uses closure argument to store the associated sample-fetch. It |
| 4038 | * returns only one argument or throws an error. An error is thrown |
| 4039 | * only if an error is encountered during the argument parsing. If |
| 4040 | * the "sample-fetch" function fails, nil is returned. |
| 4041 | */ |
| 4042 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 4043 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4044 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 4045 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 4046 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4047 | int i; |
| 4048 | struct sample smp; |
| 4049 | |
| 4050 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4051 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4052 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4053 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4054 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4055 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 4056 | /* Check execution authorization. */ |
| 4057 | if (f->use & SMP_USE_HTTP_ANY && |
| 4058 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 4059 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 4060 | "is not available in Lua services", f->kw); |
| 4061 | WILL_LJMP(lua_error(L)); |
| 4062 | } |
| 4063 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4064 | /* Get extra arguments. */ |
| 4065 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 4066 | if (i >= ARGM_NBARGS) |
| 4067 | break; |
| 4068 | hlua_lua2arg(L, i + 2, &args[i]); |
| 4069 | } |
| 4070 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4071 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4072 | |
| 4073 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4074 | MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4075 | |
| 4076 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 4077 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4078 | lua_pushfstring(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4079 | goto error; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4080 | } |
| 4081 | |
| 4082 | /* Initialise the sample. */ |
| 4083 | memset(&smp, 0, sizeof(smp)); |
| 4084 | |
| 4085 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 4086 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 4087 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4088 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4089 | lua_pushstring(L, ""); |
| 4090 | else |
| 4091 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4092 | goto end; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4093 | } |
| 4094 | |
| 4095 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4096 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4097 | hlua_smp2lua_str(L, &smp); |
| 4098 | else |
| 4099 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4100 | |
| 4101 | end: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4102 | free_args(args); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4103 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4104 | |
| 4105 | error: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4106 | free_args(args); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4107 | WILL_LJMP(lua_error(L)); |
| 4108 | return 0; /* Never reached */ |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4109 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4110 | |
| 4111 | /* |
| 4112 | * |
| 4113 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4114 | * Class Converters |
| 4115 | * |
| 4116 | * |
| 4117 | */ |
| 4118 | |
| 4119 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4120 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4121 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4122 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4123 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4124 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4125 | } |
| 4126 | |
| 4127 | /* This function creates and push in the stack a Converters object |
| 4128 | * according with a current TXN. |
| 4129 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4130 | static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, unsigned int flags) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4131 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4132 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4133 | |
| 4134 | /* Check stack size. */ |
| 4135 | if (!lua_checkstack(L, 3)) |
| 4136 | return 0; |
| 4137 | |
| 4138 | /* Create the object: obj[0] = userdata. |
| 4139 | * Note that the base of the Converters object is the |
| 4140 | * same than the TXN object. |
| 4141 | */ |
| 4142 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4143 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4144 | lua_rawseti(L, -2, 0); |
| 4145 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4146 | hsmp->s = txn->s; |
| 4147 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 4148 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4149 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4150 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4151 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4152 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 4153 | lua_setmetatable(L, -2); |
| 4154 | |
| 4155 | return 1; |
| 4156 | } |
| 4157 | |
| 4158 | /* This function is an LUA binding. It is called with each converter. |
| 4159 | * It uses closure argument to store the associated converter. It |
| 4160 | * returns only one argument or throws an error. An error is thrown |
| 4161 | * only if an error is encountered during the argument parsing. If |
| 4162 | * the converter function function fails, nil is returned. |
| 4163 | */ |
| 4164 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 4165 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 4166 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4167 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 4168 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4169 | int i; |
| 4170 | struct sample smp; |
| 4171 | |
| 4172 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4173 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4174 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4175 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 4176 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4177 | |
| 4178 | /* Get extra arguments. */ |
| 4179 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 4180 | if (i >= ARGM_NBARGS) |
| 4181 | break; |
| 4182 | hlua_lua2arg(L, i + 3, &args[i]); |
| 4183 | } |
| 4184 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4185 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4186 | |
| 4187 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 4188 | MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4189 | |
| 4190 | /* Run the special args checker. */ |
| 4191 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 4192 | hlua_pusherror(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4193 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4194 | } |
| 4195 | |
| 4196 | /* Initialise the sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4197 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4198 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 4199 | hlua_pusherror(L, "error in the input argument"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4200 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4201 | } |
| 4202 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 4203 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 4204 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4205 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4206 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4207 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4208 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4209 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4210 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4211 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 4212 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4213 | hlua_pusherror(L, "error during the input argument casting"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4214 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4215 | } |
| 4216 | |
| 4217 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4218 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4219 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4220 | lua_pushstring(L, ""); |
| 4221 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 4222 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4223 | goto end; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4224 | } |
| 4225 | |
| 4226 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4227 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4228 | hlua_smp2lua_str(L, &smp); |
| 4229 | else |
| 4230 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4231 | end: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4232 | free_args(args); |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 4233 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4234 | |
| 4235 | error: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4236 | free_args(args); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4237 | WILL_LJMP(lua_error(L)); |
| 4238 | return 0; /* Never reached */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4239 | } |
| 4240 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4241 | /* |
| 4242 | * |
| 4243 | * |
| 4244 | * Class AppletTCP |
| 4245 | * |
| 4246 | * |
| 4247 | */ |
| 4248 | |
| 4249 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4250 | * a class stream, otherwise it throws an error. |
| 4251 | */ |
| 4252 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 4253 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4254 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4255 | } |
| 4256 | |
| 4257 | /* This function creates and push in the stack an Applet object |
| 4258 | * according with a current TXN. |
| 4259 | */ |
| 4260 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 4261 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4262 | struct hlua_appctx *luactx; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4263 | struct stream_interface *si = ctx->owner; |
| 4264 | struct stream *s = si_strm(si); |
| 4265 | struct proxy *p = s->be; |
| 4266 | |
| 4267 | /* Check stack size. */ |
| 4268 | if (!lua_checkstack(L, 3)) |
| 4269 | return 0; |
| 4270 | |
| 4271 | /* Create the object: obj[0] = userdata. |
| 4272 | * Note that the base of the Converters object is the |
| 4273 | * same than the TXN object. |
| 4274 | */ |
| 4275 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4276 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4277 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4278 | luactx->appctx = ctx; |
| 4279 | luactx->htxn.s = s; |
| 4280 | luactx->htxn.p = p; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4281 | |
| 4282 | /* Create the "f" field that contains a list of fetches. */ |
| 4283 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4284 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4285 | return 0; |
| 4286 | lua_settable(L, -3); |
| 4287 | |
| 4288 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4289 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4290 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4291 | return 0; |
| 4292 | lua_settable(L, -3); |
| 4293 | |
| 4294 | /* Create the "c" field that contains a list of converters. */ |
| 4295 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4296 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4297 | return 0; |
| 4298 | lua_settable(L, -3); |
| 4299 | |
| 4300 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4301 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4302 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4303 | return 0; |
| 4304 | lua_settable(L, -3); |
| 4305 | |
| 4306 | /* Pop a class stream metatable and affect it to the table. */ |
| 4307 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 4308 | lua_setmetatable(L, -2); |
| 4309 | |
| 4310 | return 1; |
| 4311 | } |
| 4312 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4313 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 4314 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4315 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4316 | struct stream *s; |
| 4317 | const char *name; |
| 4318 | size_t len; |
| 4319 | struct sample smp; |
| 4320 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4321 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 4322 | WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments")); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4323 | |
| 4324 | /* It is useles to retrieve the stream, but this function |
| 4325 | * runs only in a stream context. |
| 4326 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4327 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4328 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4329 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4330 | |
| 4331 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4332 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4333 | hlua_lua2smp(L, 3, &smp); |
| 4334 | |
| 4335 | /* Store the sample in a variable. */ |
| 4336 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4337 | |
| 4338 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 4339 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 4340 | else |
| 4341 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 4342 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4343 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4344 | } |
| 4345 | |
| 4346 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 4347 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4348 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4349 | struct stream *s; |
| 4350 | const char *name; |
| 4351 | size_t len; |
| 4352 | struct sample smp; |
| 4353 | |
| 4354 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4355 | |
| 4356 | /* It is useles to retrieve the stream, but this function |
| 4357 | * runs only in a stream context. |
| 4358 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4359 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4360 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4361 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4362 | |
| 4363 | /* Unset the variable. */ |
| 4364 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4365 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4366 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4367 | } |
| 4368 | |
| 4369 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 4370 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4371 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4372 | struct stream *s; |
| 4373 | const char *name; |
| 4374 | size_t len; |
| 4375 | struct sample smp; |
| 4376 | |
| 4377 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4378 | |
| 4379 | /* It is useles to retrieve the stream, but this function |
| 4380 | * runs only in a stream context. |
| 4381 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4382 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4383 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4384 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4385 | |
| 4386 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Willy Tarreau | e352b9d | 2021-09-03 11:52:38 +0200 | [diff] [blame] | 4387 | if (!vars_get_by_name(name, len, &smp, NULL)) { |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4388 | lua_pushnil(L); |
| 4389 | return 1; |
| 4390 | } |
| 4391 | |
| 4392 | return hlua_smp2lua(L, &smp); |
| 4393 | } |
| 4394 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4395 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 4396 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4397 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4398 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4399 | struct hlua *hlua; |
| 4400 | |
| 4401 | /* Note that this hlua struct is from the session and not from the applet. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 4402 | if (!s->hlua) |
| 4403 | return 0; |
| 4404 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4405 | |
| 4406 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4407 | |
| 4408 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4409 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4410 | |
| 4411 | /* Get and store new value. */ |
| 4412 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4413 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4414 | |
| 4415 | return 0; |
| 4416 | } |
| 4417 | |
| 4418 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 4419 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4420 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4421 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4422 | struct hlua *hlua; |
| 4423 | |
| 4424 | /* Note that this hlua struct is from the session and not from the applet. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 4425 | if (!s->hlua) { |
| 4426 | lua_pushnil(L); |
| 4427 | return 1; |
| 4428 | } |
| 4429 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4430 | |
| 4431 | /* Push configuration index in the stack. */ |
| 4432 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4433 | |
| 4434 | return 1; |
| 4435 | } |
| 4436 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4437 | /* If expected data not yet available, it returns a yield. This function |
| 4438 | * consumes the data in the buffer. It returns a string containing the |
| 4439 | * data. This string can be empty. |
| 4440 | */ |
| 4441 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 4442 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4443 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4444 | struct stream_interface *si = luactx->appctx->owner; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4445 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4446 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4447 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4448 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4449 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4450 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4451 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4452 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4453 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4454 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4455 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4456 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4457 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4458 | } |
| 4459 | |
| 4460 | /* End of data: commit the total strings and return. */ |
| 4461 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4462 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4463 | return 1; |
| 4464 | } |
| 4465 | |
| 4466 | /* Ensure that the block 2 length is usable. */ |
| 4467 | if (ret == 1) |
| 4468 | len2 = 0; |
| 4469 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 4470 | /* don't check the max length read and don't check. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4471 | luaL_addlstring(&luactx->b, blk1, len1); |
| 4472 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4473 | |
| 4474 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4475 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4476 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4477 | return 1; |
| 4478 | } |
| 4479 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4480 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4481 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 4482 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4483 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4484 | |
| 4485 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4486 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4487 | |
| 4488 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 4489 | } |
| 4490 | |
| 4491 | /* If expected data not yet available, it returns a yield. This function |
| 4492 | * consumes the data in the buffer. It returns a string containing the |
| 4493 | * data. This string can be empty. |
| 4494 | */ |
| 4495 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 4496 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4497 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4498 | struct stream_interface *si = luactx->appctx->owner; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4499 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4500 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4501 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4502 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4503 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4504 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4505 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4506 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4507 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4508 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4509 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4510 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4511 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4512 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4513 | } |
| 4514 | |
| 4515 | /* End of data: commit the total strings and return. */ |
| 4516 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4517 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4518 | return 1; |
| 4519 | } |
| 4520 | |
| 4521 | /* Ensure that the block 2 length is usable. */ |
| 4522 | if (ret == 1) |
| 4523 | len2 = 0; |
| 4524 | |
| 4525 | if (len == -1) { |
| 4526 | |
| 4527 | /* If len == -1, catenate all the data avalaile and |
| 4528 | * yield because we want to get all the data until |
| 4529 | * the end of data stream. |
| 4530 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4531 | luaL_addlstring(&luactx->b, blk1, len1); |
| 4532 | luaL_addlstring(&luactx->b, blk2, len2); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4533 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4534 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4535 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4536 | |
| 4537 | } else { |
| 4538 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4539 | /* Copy the first block caping to the length required. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4540 | if (len1 > len) |
| 4541 | len1 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4542 | luaL_addlstring(&luactx->b, blk1, len1); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4543 | len -= len1; |
| 4544 | |
| 4545 | /* Copy the second block. */ |
| 4546 | if (len2 > len) |
| 4547 | len2 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4548 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4549 | len -= len2; |
| 4550 | |
| 4551 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4552 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4553 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4554 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4555 | if (len > 0) { |
| 4556 | lua_pushinteger(L, len); |
| 4557 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4558 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4559 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4560 | } |
| 4561 | |
| 4562 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4563 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4564 | return 1; |
| 4565 | } |
| 4566 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4567 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4568 | hlua_pusherror(L, "Lua: internal error"); |
| 4569 | WILL_LJMP(lua_error(L)); |
| 4570 | return 0; |
| 4571 | } |
| 4572 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4573 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4574 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 4575 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4576 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4577 | int len = -1; |
| 4578 | |
| 4579 | if (lua_gettop(L) > 2) |
| 4580 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4581 | if (lua_gettop(L) >= 2) { |
| 4582 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4583 | lua_pop(L, 1); |
| 4584 | } |
| 4585 | |
| 4586 | /* Confirm or set the required length */ |
| 4587 | lua_pushinteger(L, len); |
| 4588 | |
| 4589 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4590 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4591 | |
| 4592 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 4593 | } |
| 4594 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4595 | /* Append data in the output side of the buffer. This data is immediately |
| 4596 | * sent. The function returns the amount of data written. If the buffer |
| 4597 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4598 | * if the channel is closed. |
| 4599 | */ |
| 4600 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4601 | { |
| 4602 | size_t len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4603 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4604 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4605 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4606 | struct stream_interface *si = luactx->appctx->owner; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4607 | struct channel *chn = si_ic(si); |
| 4608 | int max; |
| 4609 | |
| 4610 | /* Get the max amount of data which can write as input in the channel. */ |
| 4611 | max = channel_recv_max(chn); |
| 4612 | if (max > (len - l)) |
| 4613 | max = len - l; |
| 4614 | |
| 4615 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4616 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4617 | |
| 4618 | /* update counters. */ |
| 4619 | l += max; |
| 4620 | lua_pop(L, 1); |
| 4621 | lua_pushinteger(L, l); |
| 4622 | |
| 4623 | /* If some data is not send, declares the situation to the |
| 4624 | * applet, and returns a yield. |
| 4625 | */ |
| 4626 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 4627 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4628 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4629 | } |
| 4630 | |
| 4631 | return 1; |
| 4632 | } |
| 4633 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4634 | /* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4635 | * yield the LUA process, and resume it without checking the |
| 4636 | * input arguments. |
| 4637 | */ |
| 4638 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 4639 | { |
| 4640 | MAY_LJMP(check_args(L, 2, "send")); |
| 4641 | lua_pushinteger(L, 0); |
| 4642 | |
| 4643 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 4644 | } |
| 4645 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4646 | /* |
| 4647 | * |
| 4648 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4649 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4650 | * |
| 4651 | * |
| 4652 | */ |
| 4653 | |
| 4654 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4655 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4656 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4657 | __LJMP static struct hlua_appctx *hlua_checkapplet_http(lua_State *L, int ud) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4658 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4659 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4660 | } |
| 4661 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4662 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4663 | * according with a current TXN. |
| 4664 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4665 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4666 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4667 | struct hlua_appctx *luactx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 4668 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4669 | struct stream_interface *si = ctx->owner; |
| 4670 | struct stream *s = si_strm(si); |
| 4671 | struct proxy *px = s->be; |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4672 | struct htx *htx; |
| 4673 | struct htx_blk *blk; |
| 4674 | struct htx_sl *sl; |
| 4675 | struct ist path; |
| 4676 | unsigned long long len = 0; |
| 4677 | int32_t pos; |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 4678 | struct http_uri_parser parser; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4679 | |
| 4680 | /* Check stack size. */ |
| 4681 | if (!lua_checkstack(L, 3)) |
| 4682 | return 0; |
| 4683 | |
| 4684 | /* Create the object: obj[0] = userdata. |
| 4685 | * Note that the base of the Converters object is the |
| 4686 | * same than the TXN object. |
| 4687 | */ |
| 4688 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4689 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4690 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4691 | luactx->appctx = ctx; |
| 4692 | luactx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
| 4693 | luactx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */ |
| 4694 | luactx->htxn.s = s; |
| 4695 | luactx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4696 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4697 | /* Create the "f" field that contains a list of fetches. */ |
| 4698 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4699 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4700 | return 0; |
| 4701 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4702 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4703 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4704 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4705 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4706 | return 0; |
| 4707 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4708 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4709 | /* Create the "c" field that contains a list of converters. */ |
| 4710 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4711 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4712 | return 0; |
| 4713 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4714 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4715 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4716 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4717 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4718 | return 0; |
| 4719 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4720 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4721 | htx = htxbuf(&s->req.buf); |
| 4722 | blk = htx_get_first_blk(htx); |
Christopher Faulet | ea00973 | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 4723 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4724 | sl = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4725 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4726 | /* Stores the request method. */ |
| 4727 | lua_pushstring(L, "method"); |
| 4728 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 4729 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4730 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4731 | /* Stores the http version. */ |
| 4732 | lua_pushstring(L, "version"); |
| 4733 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 4734 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4735 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4736 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 4737 | * the array on the top of the stack. |
| 4738 | */ |
| 4739 | lua_pushstring(L, "headers"); |
| 4740 | htxn.s = s; |
| 4741 | htxn.p = px; |
| 4742 | htxn.dir = SMP_OPT_DIR_REQ; |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4743 | if (!hlua_http_get_headers(L, &htxn.s->txn->req)) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4744 | return 0; |
| 4745 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4746 | |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 4747 | parser = http_uri_parser_init(htx_sl_req_uri(sl)); |
| 4748 | path = http_parse_path(&parser); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 4749 | if (isttest(path)) { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4750 | char *p, *q, *end; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4751 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4752 | p = path.ptr; |
Tim Duesterhus | 4c8f75f | 2021-11-06 15:14:44 +0100 | [diff] [blame] | 4753 | end = istend(path); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4754 | q = p; |
| 4755 | while (q < end && *q != '?') |
| 4756 | q++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4757 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4758 | /* Stores the request path. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4759 | lua_pushstring(L, "path"); |
| 4760 | lua_pushlstring(L, p, q - p); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4761 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4762 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4763 | /* Stores the query string. */ |
| 4764 | lua_pushstring(L, "qs"); |
| 4765 | if (*q == '?') |
| 4766 | q++; |
| 4767 | lua_pushlstring(L, q, end - q); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4768 | lua_settable(L, -3); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4769 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4770 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4771 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4772 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4773 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4774 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4775 | if (type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4776 | break; |
| 4777 | if (type == HTX_BLK_DATA) |
| 4778 | len += htx_get_blksz(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4779 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4780 | if (htx->extra != ULLONG_MAX) |
| 4781 | len += htx->extra; |
| 4782 | |
| 4783 | /* Stores the request path. */ |
| 4784 | lua_pushstring(L, "length"); |
| 4785 | lua_pushinteger(L, len); |
| 4786 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4787 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4788 | /* Create an empty array of HTTP request headers. */ |
| 4789 | lua_pushstring(L, "response"); |
| 4790 | lua_newtable(L); |
| 4791 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4792 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4793 | /* Pop a class stream metatable and affect it to the table. */ |
| 4794 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 4795 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4796 | |
| 4797 | return 1; |
| 4798 | } |
| 4799 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4800 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 4801 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4802 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4803 | struct stream *s; |
| 4804 | const char *name; |
| 4805 | size_t len; |
| 4806 | struct sample smp; |
| 4807 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4808 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 4809 | WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments")); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4810 | |
| 4811 | /* It is useles to retrieve the stream, but this function |
| 4812 | * runs only in a stream context. |
| 4813 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4814 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4815 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4816 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4817 | |
| 4818 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4819 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4820 | hlua_lua2smp(L, 3, &smp); |
| 4821 | |
| 4822 | /* Store the sample in a variable. */ |
| 4823 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4824 | |
| 4825 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 4826 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 4827 | else |
| 4828 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 4829 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4830 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4831 | } |
| 4832 | |
| 4833 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 4834 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4835 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4836 | struct stream *s; |
| 4837 | const char *name; |
| 4838 | size_t len; |
| 4839 | struct sample smp; |
| 4840 | |
| 4841 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4842 | |
| 4843 | /* It is useles to retrieve the stream, but this function |
| 4844 | * runs only in a stream context. |
| 4845 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4846 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4847 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4848 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4849 | |
| 4850 | /* Unset the variable. */ |
| 4851 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4852 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4853 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4854 | } |
| 4855 | |
| 4856 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4857 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4858 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4859 | struct stream *s; |
| 4860 | const char *name; |
| 4861 | size_t len; |
| 4862 | struct sample smp; |
| 4863 | |
| 4864 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4865 | |
| 4866 | /* It is useles to retrieve the stream, but this function |
| 4867 | * runs only in a stream context. |
| 4868 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4869 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4870 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4871 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4872 | |
| 4873 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Willy Tarreau | e352b9d | 2021-09-03 11:52:38 +0200 | [diff] [blame] | 4874 | if (!vars_get_by_name(name, len, &smp, NULL)) { |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4875 | lua_pushnil(L); |
| 4876 | return 1; |
| 4877 | } |
| 4878 | |
| 4879 | return hlua_smp2lua(L, &smp); |
| 4880 | } |
| 4881 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4882 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 4883 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4884 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4885 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4886 | struct hlua *hlua; |
| 4887 | |
| 4888 | /* Note that this hlua struct is from the session and not from the applet. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 4889 | if (!s->hlua) |
| 4890 | return 0; |
| 4891 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4892 | |
| 4893 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4894 | |
| 4895 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4896 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4897 | |
| 4898 | /* Get and store new value. */ |
| 4899 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4900 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4901 | |
| 4902 | return 0; |
| 4903 | } |
| 4904 | |
| 4905 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4906 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4907 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4908 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4909 | struct hlua *hlua; |
| 4910 | |
| 4911 | /* Note that this hlua struct is from the session and not from the applet. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 4912 | if (!s->hlua) { |
| 4913 | lua_pushnil(L); |
| 4914 | return 1; |
| 4915 | } |
| 4916 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4917 | |
| 4918 | /* Push configuration index in the stack. */ |
| 4919 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4920 | |
| 4921 | return 1; |
| 4922 | } |
| 4923 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4924 | /* If expected data not yet available, it returns a yield. This function |
| 4925 | * consumes the data in the buffer. It returns a string containing the |
| 4926 | * data. This string can be empty. |
| 4927 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4928 | __LJMP static int hlua_applet_http_getline_yield(lua_State *L, int status, lua_KContext ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4929 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4930 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4931 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4932 | struct channel *req = si_oc(si); |
| 4933 | struct htx *htx; |
| 4934 | struct htx_blk *blk; |
| 4935 | size_t count; |
| 4936 | int stop = 0; |
| 4937 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4938 | htx = htx_from_buf(&req->buf); |
| 4939 | count = co_data(req); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4940 | blk = htx_get_first_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 4941 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4942 | while (count && !stop && blk) { |
| 4943 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4944 | uint32_t sz = htx_get_blksz(blk); |
| 4945 | struct ist v; |
| 4946 | uint32_t vlen; |
| 4947 | char *nl; |
| 4948 | |
| 4949 | vlen = sz; |
| 4950 | if (vlen > count) { |
| 4951 | if (type != HTX_BLK_DATA) |
| 4952 | break; |
| 4953 | vlen = count; |
| 4954 | } |
| 4955 | |
| 4956 | switch (type) { |
| 4957 | case HTX_BLK_UNUSED: |
| 4958 | break; |
| 4959 | |
| 4960 | case HTX_BLK_DATA: |
| 4961 | v = htx_get_blk_value(htx, blk); |
| 4962 | v.len = vlen; |
| 4963 | nl = istchr(v, '\n'); |
| 4964 | if (nl != NULL) { |
| 4965 | stop = 1; |
| 4966 | vlen = nl - v.ptr + 1; |
| 4967 | } |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4968 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4969 | break; |
| 4970 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4971 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4972 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4973 | stop = 1; |
| 4974 | break; |
| 4975 | |
| 4976 | default: |
| 4977 | break; |
| 4978 | } |
| 4979 | |
| 4980 | co_set_data(req, co_data(req) - vlen); |
| 4981 | count -= vlen; |
| 4982 | if (sz == vlen) |
| 4983 | blk = htx_remove_blk(htx, blk); |
| 4984 | else { |
| 4985 | htx_cut_data_blk(htx, blk, vlen); |
| 4986 | break; |
| 4987 | } |
| 4988 | } |
| 4989 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 4990 | /* The message was fully consumed and no more data are expected |
| 4991 | * (EOM flag set). |
| 4992 | */ |
| 4993 | if (htx_is_empty(htx) && (htx->flags & HTX_FL_EOM)) |
| 4994 | stop = 1; |
| 4995 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4996 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4997 | if (!stop) { |
| 4998 | si_cant_get(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4999 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5000 | } |
| 5001 | |
| 5002 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5003 | luaL_pushresult(&luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5004 | return 1; |
| 5005 | } |
| 5006 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5007 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5008 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5009 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5010 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5011 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5012 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5013 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5014 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5015 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5016 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5017 | } |
| 5018 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5019 | /* If expected data not yet available, it returns a yield. This function |
| 5020 | * consumes the data in the buffer. It returns a string containing the |
| 5021 | * data. This string can be empty. |
| 5022 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5023 | __LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5024 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5025 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 5026 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5027 | struct channel *req = si_oc(si); |
| 5028 | struct htx *htx; |
| 5029 | struct htx_blk *blk; |
| 5030 | size_t count; |
| 5031 | int len; |
| 5032 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5033 | htx = htx_from_buf(&req->buf); |
| 5034 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5035 | count = co_data(req); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 5036 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5037 | while (count && len && blk) { |
| 5038 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5039 | uint32_t sz = htx_get_blksz(blk); |
| 5040 | struct ist v; |
| 5041 | uint32_t vlen; |
| 5042 | |
| 5043 | vlen = sz; |
| 5044 | if (len > 0 && vlen > len) |
| 5045 | vlen = len; |
| 5046 | if (vlen > count) { |
| 5047 | if (type != HTX_BLK_DATA) |
| 5048 | break; |
| 5049 | vlen = count; |
| 5050 | } |
| 5051 | |
| 5052 | switch (type) { |
| 5053 | case HTX_BLK_UNUSED: |
| 5054 | break; |
| 5055 | |
| 5056 | case HTX_BLK_DATA: |
| 5057 | v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5058 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5059 | break; |
| 5060 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5061 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5062 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5063 | len = 0; |
| 5064 | break; |
| 5065 | |
| 5066 | default: |
| 5067 | break; |
| 5068 | } |
| 5069 | |
| 5070 | co_set_data(req, co_data(req) - vlen); |
| 5071 | count -= vlen; |
| 5072 | if (len > 0) |
| 5073 | len -= vlen; |
| 5074 | if (sz == vlen) |
| 5075 | blk = htx_remove_blk(htx, blk); |
| 5076 | else { |
| 5077 | htx_cut_data_blk(htx, blk, vlen); |
| 5078 | break; |
| 5079 | } |
| 5080 | } |
| 5081 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 5082 | /* The message was fully consumed and no more data are expected |
| 5083 | * (EOM flag set). |
| 5084 | */ |
| 5085 | if (htx_is_empty(htx) && (htx->flags & HTX_FL_EOM)) |
| 5086 | len = 0; |
| 5087 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 5088 | htx_to_buf(htx, &req->buf); |
| 5089 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5090 | /* If we are no other data available, yield waiting for new data. */ |
| 5091 | if (len) { |
| 5092 | if (len > 0) { |
| 5093 | lua_pushinteger(L, len); |
| 5094 | lua_replace(L, 2); |
| 5095 | } |
| 5096 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5097 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5098 | } |
| 5099 | |
| 5100 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5101 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5102 | return 1; |
| 5103 | } |
| 5104 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5105 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5106 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 5107 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5108 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5109 | int len = -1; |
| 5110 | |
| 5111 | /* Check arguments. */ |
| 5112 | if (lua_gettop(L) > 2) |
| 5113 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 5114 | if (lua_gettop(L) >= 2) { |
| 5115 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5116 | lua_pop(L, 1); |
| 5117 | } |
| 5118 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5119 | lua_pushinteger(L, len); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5120 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5121 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5122 | luaL_buffinit(L, &luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5123 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5124 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5125 | } |
| 5126 | |
| 5127 | /* Append data in the output side of the buffer. This data is immediately |
| 5128 | * sent. The function returns the amount of data written. If the buffer |
| 5129 | * cannot contain the data, the function yields. The function returns -1 |
| 5130 | * if the channel is closed. |
| 5131 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5132 | __LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5133 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5134 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 5135 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5136 | struct channel *res = si_ic(si); |
| 5137 | struct htx *htx = htx_from_buf(&res->buf); |
| 5138 | const char *data; |
| 5139 | size_t len; |
| 5140 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 5141 | int max; |
| 5142 | |
Christopher Faulet | 9060fc0 | 2019-07-03 11:39:30 +0200 | [diff] [blame] | 5143 | max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx)); |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 5144 | if (!max) |
| 5145 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5146 | |
| 5147 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5148 | |
| 5149 | /* Get the max amount of data which can write as input in the channel. */ |
| 5150 | if (max > (len - l)) |
| 5151 | max = len - l; |
| 5152 | |
| 5153 | /* Copy data. */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 5154 | max = htx_add_data(htx, ist2(data + l, max)); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 5155 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5156 | |
| 5157 | /* update counters. */ |
| 5158 | l += max; |
| 5159 | lua_pop(L, 1); |
| 5160 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5161 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5162 | /* If some data is not send, declares the situation to the |
| 5163 | * applet, and returns a yield. |
| 5164 | */ |
| 5165 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 5166 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 5167 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5168 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5169 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5170 | } |
| 5171 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5172 | htx_to_buf(htx, &res->buf); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5173 | return 1; |
| 5174 | } |
| 5175 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5176 | /* Just a wrapper of "hlua_applet_send_yield". This wrapper permits |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5177 | * yield the LUA process, and resume it without checking the |
| 5178 | * input arguments. |
| 5179 | */ |
| 5180 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 5181 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5182 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5183 | |
| 5184 | /* We want to send some data. Headers must be sent. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5185 | if (!(luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5186 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 5187 | WILL_LJMP(lua_error(L)); |
| 5188 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5189 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5190 | /* This integer is used for followinf the amount of data sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5191 | lua_pushinteger(L, 0); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5192 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5193 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5194 | } |
| 5195 | |
| 5196 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 5197 | { |
| 5198 | const char *name; |
| 5199 | int ret; |
| 5200 | |
| 5201 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 5202 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5203 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 5204 | |
| 5205 | /* Push in the stack the "response" entry. */ |
| 5206 | ret = lua_getfield(L, 1, "response"); |
| 5207 | if (ret != LUA_TTABLE) { |
| 5208 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 5209 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 5210 | WILL_LJMP(lua_error(L)); |
| 5211 | } |
| 5212 | |
| 5213 | /* check if the header is already registered if it is not |
| 5214 | * the case, register it. |
| 5215 | */ |
| 5216 | ret = lua_getfield(L, -1, name); |
| 5217 | if (ret == LUA_TNIL) { |
| 5218 | |
| 5219 | /* Entry not found. */ |
| 5220 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 5221 | |
| 5222 | /* Insert the new header name in the array in the top of the stack. |
| 5223 | * It left the new array in the top of the stack. |
| 5224 | */ |
| 5225 | lua_newtable(L); |
| 5226 | lua_pushvalue(L, 2); |
| 5227 | lua_pushvalue(L, -2); |
| 5228 | lua_settable(L, -4); |
| 5229 | |
| 5230 | } else if (ret != LUA_TTABLE) { |
| 5231 | |
| 5232 | /* corruption error. */ |
| 5233 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 5234 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 5235 | WILL_LJMP(lua_error(L)); |
| 5236 | } |
| 5237 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 5238 | /* Now the top of thestack is an array of values. We push |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5239 | * the header value as new entry. |
| 5240 | */ |
| 5241 | lua_pushvalue(L, 3); |
| 5242 | ret = lua_rawlen(L, -2); |
| 5243 | lua_rawseti(L, -2, ret + 1); |
| 5244 | lua_pushboolean(L, 1); |
| 5245 | return 1; |
| 5246 | } |
| 5247 | |
| 5248 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 5249 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5250 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5251 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5252 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5253 | |
| 5254 | if (status < 100 || status > 599) { |
| 5255 | lua_pushboolean(L, 0); |
| 5256 | return 1; |
| 5257 | } |
| 5258 | |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5259 | luactx->appctx->ctx.hlua_apphttp.status = status; |
| 5260 | luactx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5261 | lua_pushboolean(L, 1); |
| 5262 | return 1; |
| 5263 | } |
| 5264 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5265 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5266 | __LJMP static int hlua_applet_http_send_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5267 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5268 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 5269 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5270 | struct channel *res = si_ic(si); |
| 5271 | struct htx *htx; |
| 5272 | struct htx_sl *sl; |
| 5273 | struct h1m h1m; |
| 5274 | const char *status, *reason; |
| 5275 | const char *name, *value; |
| 5276 | size_t nlen, vlen; |
| 5277 | unsigned int flags; |
| 5278 | |
| 5279 | /* Send the message at once. */ |
| 5280 | htx = htx_from_buf(&res->buf); |
| 5281 | h1m_init_res(&h1m); |
| 5282 | |
| 5283 | /* Use the same http version than the request. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5284 | status = ultoa_r(luactx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size); |
| 5285 | reason = luactx->appctx->ctx.hlua_apphttp.reason; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5286 | if (reason == NULL) |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5287 | reason = http_get_reason(luactx->appctx->ctx.hlua_apphttp.status); |
| 5288 | if (luactx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5289 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 5290 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 5291 | } |
| 5292 | else { |
| 5293 | flags = HTX_SL_F_IS_RESP; |
| 5294 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 5295 | } |
| 5296 | if (!sl) { |
| 5297 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5298 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5299 | WILL_LJMP(lua_error(L)); |
| 5300 | } |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5301 | sl->info.res.status = luactx->appctx->ctx.hlua_apphttp.status; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5302 | |
| 5303 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 5304 | lua_pushvalue(L, 0); |
| 5305 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 5306 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5307 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5308 | WILL_LJMP(lua_error(L)); |
| 5309 | } |
| 5310 | |
| 5311 | /* Browse the list of headers. */ |
| 5312 | lua_pushnil(L); |
| 5313 | while(lua_next(L, -2) != 0) { |
| 5314 | /* We expect a string as -2. */ |
| 5315 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 5316 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5317 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5318 | lua_typename(L, lua_type(L, -2))); |
| 5319 | WILL_LJMP(lua_error(L)); |
| 5320 | } |
| 5321 | name = lua_tolstring(L, -2, &nlen); |
| 5322 | |
| 5323 | /* We expect an array as -1. */ |
| 5324 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 5325 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5326 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5327 | name, |
| 5328 | lua_typename(L, lua_type(L, -1))); |
| 5329 | WILL_LJMP(lua_error(L)); |
| 5330 | } |
| 5331 | |
| 5332 | /* Browse the table who is on the top of the stack. */ |
| 5333 | lua_pushnil(L); |
| 5334 | while(lua_next(L, -2) != 0) { |
| 5335 | int id; |
| 5336 | |
| 5337 | /* We expect a number as -2. */ |
| 5338 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 5339 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5340 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5341 | name, |
| 5342 | lua_typename(L, lua_type(L, -2))); |
| 5343 | WILL_LJMP(lua_error(L)); |
| 5344 | } |
| 5345 | id = lua_tointeger(L, -2); |
| 5346 | |
| 5347 | /* We expect a string as -2. */ |
| 5348 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 5349 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5350 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5351 | name, id, |
| 5352 | lua_typename(L, lua_type(L, -1))); |
| 5353 | WILL_LJMP(lua_error(L)); |
| 5354 | } |
| 5355 | value = lua_tolstring(L, -1, &vlen); |
| 5356 | |
| 5357 | /* Simple Protocol checks. */ |
Christopher Faulet | 545fbba | 2021-09-28 09:36:25 +0200 | [diff] [blame] | 5358 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) { |
| 5359 | int ret; |
| 5360 | |
| 5361 | ret = h1_parse_xfer_enc_header(&h1m, ist2(value, vlen)); |
| 5362 | if (ret < 0) { |
| 5363 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
| 5364 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
| 5365 | name); |
| 5366 | WILL_LJMP(lua_error(L)); |
| 5367 | } |
| 5368 | else if (ret == 0) |
| 5369 | goto next; /* Skip it */ |
| 5370 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5371 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 5372 | struct ist v = ist2(value, vlen); |
| 5373 | int ret; |
| 5374 | |
| 5375 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 5376 | if (ret < 0) { |
| 5377 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5378 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5379 | name); |
| 5380 | WILL_LJMP(lua_error(L)); |
| 5381 | } |
| 5382 | else if (ret == 0) |
| 5383 | goto next; /* Skip it */ |
| 5384 | } |
| 5385 | |
| 5386 | /* Add a new header */ |
| 5387 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 5388 | 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] | 5389 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5390 | name); |
| 5391 | WILL_LJMP(lua_error(L)); |
| 5392 | } |
| 5393 | next: |
| 5394 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5395 | lua_pop(L, 1); |
| 5396 | } |
| 5397 | |
| 5398 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5399 | lua_pop(L, 1); |
| 5400 | } |
| 5401 | |
| 5402 | if (h1m.flags & H1_MF_CHNK) |
| 5403 | h1m.flags &= ~H1_MF_CLEN; |
| 5404 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 5405 | h1m.flags |= H1_MF_XFER_LEN; |
| 5406 | |
| 5407 | /* Uset HTX start-line flags */ |
| 5408 | if (h1m.flags & H1_MF_XFER_ENC) |
| 5409 | flags |= HTX_SL_F_XFER_ENC; |
| 5410 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 5411 | flags |= HTX_SL_F_XFER_LEN; |
| 5412 | if (h1m.flags & H1_MF_CHNK) |
| 5413 | flags |= HTX_SL_F_CHNK; |
| 5414 | else if (h1m.flags & H1_MF_CLEN) |
| 5415 | flags |= HTX_SL_F_CLEN; |
| 5416 | if (h1m.body_len == 0) |
| 5417 | flags |= HTX_SL_F_BODYLESS; |
| 5418 | } |
| 5419 | sl->flags |= flags; |
| 5420 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 5421 | /* 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] | 5422 | * and the status code implies the presence of a message body, we must |
| 5423 | * announce a transfer encoding chunked. This is required by haproxy |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5424 | * for the keepalive compliance. If the applet announces a transfer-encoding |
| 5425 | * chunked itself, don't do anything. |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5426 | */ |
| 5427 | 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] | 5428 | luactx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 5429 | luactx->appctx->ctx.hlua_apphttp.status != 204 && |
| 5430 | luactx->appctx->ctx.hlua_apphttp.status != 304) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5431 | /* Add a new header */ |
| 5432 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 5433 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 5434 | 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] | 5435 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5436 | WILL_LJMP(lua_error(L)); |
| 5437 | } |
| 5438 | } |
| 5439 | |
| 5440 | /* Finalize headers. */ |
| 5441 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 5442 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5443 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5444 | WILL_LJMP(lua_error(L)); |
| 5445 | } |
| 5446 | |
| 5447 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 5448 | b_reset(&res->buf); |
| 5449 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 5450 | WILL_LJMP(lua_error(L)); |
| 5451 | } |
| 5452 | |
| 5453 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 5454 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5455 | |
| 5456 | /* Headers sent, set the flag. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5457 | luactx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5458 | return 0; |
| 5459 | |
| 5460 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5461 | /* We will build the status line and the headers of the HTTP response. |
| 5462 | * We will try send at once if its not possible, we give back the hand |
| 5463 | * waiting for more room. |
| 5464 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5465 | __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] | 5466 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5467 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 5468 | struct stream_interface *si = luactx->appctx->owner; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5469 | struct channel *res = si_ic(si); |
| 5470 | |
| 5471 | if (co_data(res)) { |
| 5472 | si_rx_room_blk(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5473 | 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] | 5474 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5475 | return MAY_LJMP(hlua_applet_http_send_response(L)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5476 | } |
| 5477 | |
| 5478 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5479 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5480 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5481 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5482 | } |
| 5483 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5484 | /* |
| 5485 | * |
| 5486 | * |
| 5487 | * Class HTTP |
| 5488 | * |
| 5489 | * |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5490 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5491 | |
| 5492 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 5493 | * a class stream, otherwise it throws an error. |
| 5494 | */ |
| 5495 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5496 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5497 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
| 5498 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5499 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5500 | /* This function creates and push in the stack a HTTP object |
| 5501 | * according with a current TXN. |
| 5502 | */ |
| 5503 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 5504 | { |
| 5505 | struct hlua_txn *htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5506 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5507 | /* Check stack size. */ |
| 5508 | if (!lua_checkstack(L, 3)) |
| 5509 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5510 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5511 | /* Create the object: obj[0] = userdata. |
| 5512 | * Note that the base of the Converters object is the |
| 5513 | * same than the TXN object. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5514 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5515 | lua_newtable(L); |
| 5516 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 5517 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5518 | |
| 5519 | htxn->s = txn->s; |
| 5520 | htxn->p = txn->p; |
Christopher Faulet | 256b69a | 2019-05-23 11:14:21 +0200 | [diff] [blame] | 5521 | htxn->dir = txn->dir; |
| 5522 | htxn->flags = txn->flags; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5523 | |
| 5524 | /* Pop a class stream metatable and affect it to the table. */ |
| 5525 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 5526 | lua_setmetatable(L, -2); |
| 5527 | |
| 5528 | return 1; |
| 5529 | } |
| 5530 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 5531 | /* This function creates and returns an array containing the status-line |
| 5532 | * elements. This function does not fails. |
| 5533 | */ |
| 5534 | __LJMP static int hlua_http_get_stline(lua_State *L, struct htx_sl *sl) |
| 5535 | { |
| 5536 | /* Create the table. */ |
| 5537 | lua_newtable(L); |
| 5538 | |
| 5539 | if (sl->flags & HTX_SL_F_IS_RESP) { |
| 5540 | lua_pushstring(L, "version"); |
| 5541 | lua_pushlstring(L, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); |
| 5542 | lua_settable(L, -3); |
| 5543 | lua_pushstring(L, "code"); |
| 5544 | lua_pushlstring(L, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl)); |
| 5545 | lua_settable(L, -3); |
| 5546 | lua_pushstring(L, "reason"); |
| 5547 | lua_pushlstring(L, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl)); |
| 5548 | lua_settable(L, -3); |
| 5549 | } |
| 5550 | else { |
| 5551 | lua_pushstring(L, "method"); |
| 5552 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 5553 | lua_settable(L, -3); |
| 5554 | lua_pushstring(L, "uri"); |
| 5555 | lua_pushlstring(L, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl)); |
| 5556 | lua_settable(L, -3); |
| 5557 | lua_pushstring(L, "version"); |
| 5558 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 5559 | lua_settable(L, -3); |
| 5560 | } |
| 5561 | return 1; |
| 5562 | } |
| 5563 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5564 | /* This function creates ans returns an array of HTTP headers. |
| 5565 | * This function does not fails. It is used as wrapper with the |
| 5566 | * 2 following functions. |
| 5567 | */ |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5568 | __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] | 5569 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5570 | struct htx *htx; |
| 5571 | int32_t pos; |
| 5572 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5573 | /* Create the table. */ |
| 5574 | lua_newtable(L); |
| 5575 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5576 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5577 | htx = htxbuf(&msg->chn->buf); |
| 5578 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 5579 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 5580 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5581 | struct ist n, v; |
| 5582 | int len; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5583 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5584 | if (type == HTX_BLK_HDR) { |
| 5585 | n = htx_get_blk_name(htx,blk); |
| 5586 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5587 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5588 | else if (type == HTX_BLK_EOH) |
| 5589 | break; |
| 5590 | else |
| 5591 | continue; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5592 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5593 | /* Check for existing entry: |
| 5594 | * assume that the table is on the top of the stack, and |
| 5595 | * push the key in the stack, the function lua_gettable() |
| 5596 | * perform the lookup. |
| 5597 | */ |
| 5598 | lua_pushlstring(L, n.ptr, n.len); |
| 5599 | lua_gettable(L, -2); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5600 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5601 | switch (lua_type(L, -1)) { |
| 5602 | case LUA_TNIL: |
| 5603 | /* Table not found, create it. */ |
| 5604 | lua_pop(L, 1); /* remove the nil value. */ |
| 5605 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 5606 | lua_newtable(L); /* create and push empty table. */ |
| 5607 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5608 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 5609 | 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] | 5610 | break; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5611 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5612 | case LUA_TTABLE: |
| 5613 | /* Entry found: push the value in the table. */ |
| 5614 | len = lua_rawlen(L, -1); |
| 5615 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5616 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 5617 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 5618 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5619 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5620 | default: |
| 5621 | /* Other cases are errors. */ |
| 5622 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 5623 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5624 | } |
| 5625 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5626 | return 1; |
| 5627 | } |
| 5628 | |
| 5629 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 5630 | { |
| 5631 | struct hlua_txn *htxn; |
| 5632 | |
| 5633 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 5634 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5635 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5636 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5637 | WILL_LJMP(lua_error(L)); |
| 5638 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5639 | return hlua_http_get_headers(L, &htxn->s->txn->req); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5640 | } |
| 5641 | |
| 5642 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 5643 | { |
| 5644 | struct hlua_txn *htxn; |
| 5645 | |
| 5646 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 5647 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5648 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5649 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5650 | WILL_LJMP(lua_error(L)); |
| 5651 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5652 | return hlua_http_get_headers(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5653 | } |
| 5654 | |
| 5655 | /* This function replace full header, or just a value in |
| 5656 | * the request or in the response. It is a wrapper fir the |
| 5657 | * 4 following functions. |
| 5658 | */ |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5659 | __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] | 5660 | { |
| 5661 | size_t name_len; |
| 5662 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5663 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5664 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5665 | struct htx *htx; |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5666 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5667 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5668 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5669 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 5670 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5671 | htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5672 | 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] | 5673 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5674 | return 0; |
| 5675 | } |
| 5676 | |
| 5677 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 5678 | { |
| 5679 | struct hlua_txn *htxn; |
| 5680 | |
| 5681 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5682 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5683 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5684 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5685 | WILL_LJMP(lua_error(L)); |
| 5686 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5687 | 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] | 5688 | } |
| 5689 | |
| 5690 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 5691 | { |
| 5692 | struct hlua_txn *htxn; |
| 5693 | |
| 5694 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 5695 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5696 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5697 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5698 | WILL_LJMP(lua_error(L)); |
| 5699 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5700 | 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] | 5701 | } |
| 5702 | |
| 5703 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 5704 | { |
| 5705 | struct hlua_txn *htxn; |
| 5706 | |
| 5707 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5708 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5709 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5710 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5711 | WILL_LJMP(lua_error(L)); |
| 5712 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5713 | 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] | 5714 | } |
| 5715 | |
| 5716 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 5717 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5718 | struct hlua_txn *htxn; |
| 5719 | |
| 5720 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 5721 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5722 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5723 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5724 | WILL_LJMP(lua_error(L)); |
| 5725 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5726 | 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] | 5727 | } |
| 5728 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5729 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5730 | * It is a wrapper for the 2 following functions. |
| 5731 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5732 | __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] | 5733 | { |
| 5734 | size_t len; |
| 5735 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5736 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5737 | struct http_hdr_ctx ctx; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5738 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5739 | ctx.blk = NULL; |
| 5740 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 5741 | http_remove_header(htx, &ctx); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5742 | return 0; |
| 5743 | } |
| 5744 | |
| 5745 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 5746 | { |
| 5747 | struct hlua_txn *htxn; |
| 5748 | |
| 5749 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 5750 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5751 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5752 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5753 | WILL_LJMP(lua_error(L)); |
| 5754 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5755 | return hlua_http_del_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5756 | } |
| 5757 | |
| 5758 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 5759 | { |
| 5760 | struct hlua_txn *htxn; |
| 5761 | |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5762 | MAY_LJMP(check_args(L, 2, "res_del_hdr")); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5763 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5764 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5765 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5766 | WILL_LJMP(lua_error(L)); |
| 5767 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5768 | return hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5769 | } |
| 5770 | |
| 5771 | /* This function adds an header. It is a wrapper used by |
| 5772 | * the 2 following functions. |
| 5773 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5774 | __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] | 5775 | { |
| 5776 | size_t name_len; |
| 5777 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5778 | size_t value_len; |
| 5779 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5780 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5781 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5782 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 5783 | ist2(value, value_len))); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5784 | return 0; |
| 5785 | } |
| 5786 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 5787 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 5788 | { |
| 5789 | struct hlua_txn *htxn; |
| 5790 | |
| 5791 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 5792 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5793 | |
| 5794 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5795 | WILL_LJMP(lua_error(L)); |
| 5796 | |
| 5797 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
| 5798 | } |
| 5799 | |
| 5800 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 5801 | { |
| 5802 | struct hlua_txn *htxn; |
| 5803 | |
| 5804 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 5805 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5806 | |
| 5807 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
| 5808 | WILL_LJMP(lua_error(L)); |
| 5809 | |
| 5810 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
| 5811 | } |
| 5812 | |
| 5813 | static int hlua_http_req_set_hdr(lua_State *L) |
| 5814 | { |
| 5815 | struct hlua_txn *htxn; |
| 5816 | |
| 5817 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 5818 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5819 | |
| 5820 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5821 | WILL_LJMP(lua_error(L)); |
| 5822 | |
| 5823 | hlua_http_del_hdr(L, &htxn->s->txn->req); |
| 5824 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
| 5825 | } |
| 5826 | |
| 5827 | static int hlua_http_res_set_hdr(lua_State *L) |
| 5828 | { |
| 5829 | struct hlua_txn *htxn; |
| 5830 | |
| 5831 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 5832 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5833 | |
| 5834 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
| 5835 | WILL_LJMP(lua_error(L)); |
| 5836 | |
| 5837 | hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
| 5838 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
| 5839 | } |
| 5840 | |
| 5841 | /* This function set the method. */ |
| 5842 | static int hlua_http_req_set_meth(lua_State *L) |
| 5843 | { |
| 5844 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5845 | size_t name_len; |
| 5846 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5847 | |
| 5848 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5849 | WILL_LJMP(lua_error(L)); |
| 5850 | |
| 5851 | lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1); |
| 5852 | return 1; |
| 5853 | } |
| 5854 | |
| 5855 | /* This function set the method. */ |
| 5856 | static int hlua_http_req_set_path(lua_State *L) |
| 5857 | { |
| 5858 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5859 | size_t name_len; |
| 5860 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5861 | |
| 5862 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5863 | WILL_LJMP(lua_error(L)); |
| 5864 | |
| 5865 | lua_pushboolean(L, http_req_replace_stline(1, name, name_len, htxn->p, htxn->s) != -1); |
| 5866 | return 1; |
| 5867 | } |
| 5868 | |
| 5869 | /* This function set the query-string. */ |
| 5870 | static int hlua_http_req_set_query(lua_State *L) |
| 5871 | { |
| 5872 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5873 | size_t name_len; |
| 5874 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5875 | |
| 5876 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5877 | WILL_LJMP(lua_error(L)); |
| 5878 | |
| 5879 | /* Check length. */ |
| 5880 | if (name_len > trash.size - 1) { |
| 5881 | lua_pushboolean(L, 0); |
| 5882 | return 1; |
| 5883 | } |
| 5884 | |
| 5885 | /* Add the mark question as prefix. */ |
| 5886 | chunk_reset(&trash); |
| 5887 | trash.area[trash.data++] = '?'; |
| 5888 | memcpy(trash.area + trash.data, name, name_len); |
| 5889 | trash.data += name_len; |
| 5890 | |
| 5891 | lua_pushboolean(L, |
| 5892 | http_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1); |
| 5893 | return 1; |
| 5894 | } |
| 5895 | |
| 5896 | /* This function set the uri. */ |
| 5897 | static int hlua_http_req_set_uri(lua_State *L) |
| 5898 | { |
| 5899 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5900 | size_t name_len; |
| 5901 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5902 | |
| 5903 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5904 | WILL_LJMP(lua_error(L)); |
| 5905 | |
| 5906 | lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1); |
| 5907 | return 1; |
| 5908 | } |
| 5909 | |
| 5910 | /* This function set the response code & optionally reason. */ |
| 5911 | static int hlua_http_res_set_status(lua_State *L) |
| 5912 | { |
| 5913 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5914 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5915 | const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 5916 | const struct ist reason = ist2(str, (str ? strlen(str) : 0)); |
| 5917 | |
| 5918 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
| 5919 | WILL_LJMP(lua_error(L)); |
| 5920 | |
| 5921 | http_res_set_status(code, reason, htxn->s); |
| 5922 | return 0; |
| 5923 | } |
| 5924 | |
| 5925 | /* |
| 5926 | * |
| 5927 | * |
| 5928 | * Class HTTPMessage |
| 5929 | * |
| 5930 | * |
| 5931 | */ |
| 5932 | |
| 5933 | /* Returns a struct http_msg if the stack entry "ud" is a class HTTPMessage, |
| 5934 | * otherwise it throws an error. |
| 5935 | */ |
| 5936 | __LJMP static struct http_msg *hlua_checkhttpmsg(lua_State *L, int ud) |
| 5937 | { |
| 5938 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_msg_ref)); |
| 5939 | } |
| 5940 | |
| 5941 | /* Creates and pushes on the stack a HTTP object according with a current TXN. |
| 5942 | */ |
Christopher Faulet | 78c3547 | 2020-02-26 17:14:08 +0100 | [diff] [blame] | 5943 | 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] | 5944 | { |
| 5945 | /* Check stack size. */ |
| 5946 | if (!lua_checkstack(L, 3)) |
| 5947 | return 0; |
| 5948 | |
| 5949 | lua_newtable(L); |
| 5950 | lua_pushlightuserdata(L, msg); |
| 5951 | lua_rawseti(L, -2, 0); |
| 5952 | |
| 5953 | /* Create the "channel" field that contains the request channel object. */ |
| 5954 | lua_pushstring(L, "channel"); |
| 5955 | if (!hlua_channel_new(L, msg->chn)) |
| 5956 | return 0; |
| 5957 | lua_rawset(L, -3); |
| 5958 | |
| 5959 | /* Pop a class stream metatable and affect it to the table. */ |
| 5960 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_msg_ref); |
| 5961 | lua_setmetatable(L, -2); |
| 5962 | |
| 5963 | return 1; |
| 5964 | } |
| 5965 | |
| 5966 | /* Helper function returning a filter attached to the HTTP message at the |
| 5967 | * 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] | 5968 | * 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] | 5969 | * filled with output and input length respectively. |
| 5970 | */ |
| 5971 | static struct filter *hlua_http_msg_filter(lua_State *L, int ud, struct http_msg *msg, size_t *offset, size_t *len) |
| 5972 | { |
| 5973 | struct channel *chn = msg->chn; |
| 5974 | struct htx *htx = htxbuf(&chn->buf); |
| 5975 | struct filter *filter = NULL; |
| 5976 | |
| 5977 | *offset = co_data(msg->chn); |
| 5978 | *len = htx->data - co_data(msg->chn); |
| 5979 | |
| 5980 | if (lua_getfield(L, ud, "__filter") == LUA_TLIGHTUSERDATA) { |
| 5981 | filter = lua_touserdata (L, -1); |
| 5982 | if (msg->msg_state >= HTTP_MSG_DATA) { |
| 5983 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 5984 | |
| 5985 | *offset = flt_ctx->cur_off[CHN_IDX(chn)]; |
| 5986 | *len = flt_ctx->cur_len[CHN_IDX(chn)]; |
| 5987 | } |
| 5988 | } |
| 5989 | |
| 5990 | lua_pop(L, 1); |
| 5991 | return filter; |
| 5992 | } |
| 5993 | |
| 5994 | /* Returns true if the channel attached to the HTTP message is the response |
| 5995 | * channel. |
| 5996 | */ |
| 5997 | __LJMP static int hlua_http_msg_is_resp(lua_State *L) |
| 5998 | { |
| 5999 | struct http_msg *msg; |
| 6000 | |
| 6001 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 6002 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6003 | |
| 6004 | lua_pushboolean(L, !!(msg->chn->flags & CF_ISRESP)); |
| 6005 | return 1; |
| 6006 | } |
| 6007 | |
| 6008 | /* Returns an array containing the elements status-line of the HTTP message. It relies |
| 6009 | * on hlua_http_get_stline(). |
| 6010 | */ |
| 6011 | __LJMP static int hlua_http_msg_get_stline(lua_State *L) |
| 6012 | { |
| 6013 | struct http_msg *msg; |
| 6014 | struct htx *htx; |
| 6015 | struct htx_sl *sl; |
| 6016 | |
| 6017 | MAY_LJMP(check_args(L, 1, "get_stline")); |
| 6018 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6019 | |
| 6020 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6021 | WILL_LJMP(lua_error(L)); |
| 6022 | |
| 6023 | htx = htxbuf(&msg->chn->buf); |
| 6024 | sl = http_get_stline(htx); |
| 6025 | if (!sl) |
| 6026 | return 0; |
| 6027 | return hlua_http_get_stline(L, sl); |
| 6028 | } |
| 6029 | |
| 6030 | /* Returns an array containing all headers of the HTTP message. it relies on |
| 6031 | * hlua_http_get_headers(). |
| 6032 | */ |
| 6033 | __LJMP static int hlua_http_msg_get_headers(lua_State *L) |
| 6034 | { |
| 6035 | struct http_msg *msg; |
| 6036 | |
| 6037 | MAY_LJMP(check_args(L, 1, "get_headers")); |
| 6038 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6039 | |
| 6040 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6041 | WILL_LJMP(lua_error(L)); |
| 6042 | |
| 6043 | return hlua_http_get_headers(L, msg); |
| 6044 | } |
| 6045 | |
| 6046 | /* Deletes all occurrences of an header in the HTTP message matching on its |
| 6047 | * name. It relies on hlua_http_del_hdr(). |
| 6048 | */ |
| 6049 | __LJMP static int hlua_http_msg_del_hdr(lua_State *L) |
| 6050 | { |
| 6051 | struct http_msg *msg; |
| 6052 | |
| 6053 | MAY_LJMP(check_args(L, 2, "del_header")); |
| 6054 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6055 | |
| 6056 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6057 | WILL_LJMP(lua_error(L)); |
| 6058 | |
| 6059 | return hlua_http_del_hdr(L, msg); |
| 6060 | } |
| 6061 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6062 | /* 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] | 6063 | * message given its name against a regex and replaces it if it matches. It |
| 6064 | * relies on hlua_http_rep_hdr(). |
| 6065 | */ |
| 6066 | __LJMP static int hlua_http_msg_rep_hdr(lua_State *L) |
| 6067 | { |
| 6068 | struct http_msg *msg; |
| 6069 | |
| 6070 | MAY_LJMP(check_args(L, 4, "rep_header")); |
| 6071 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6072 | |
| 6073 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6074 | WILL_LJMP(lua_error(L)); |
| 6075 | |
| 6076 | return hlua_http_rep_hdr(L, msg, 1); |
| 6077 | } |
| 6078 | |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 6079 | /* 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] | 6080 | * message given its name against a regex and replaces it if it matches. It |
| 6081 | * relies on hlua_http_rep_hdr(). |
| 6082 | */ |
| 6083 | __LJMP static int hlua_http_msg_rep_val(lua_State *L) |
| 6084 | { |
| 6085 | struct http_msg *msg; |
| 6086 | |
| 6087 | MAY_LJMP(check_args(L, 4, "rep_value")); |
| 6088 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6089 | |
| 6090 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6091 | WILL_LJMP(lua_error(L)); |
| 6092 | |
| 6093 | return hlua_http_rep_hdr(L, msg, 0); |
| 6094 | } |
| 6095 | |
| 6096 | /* Add an header in the HTTP message. It relies on hlua_http_add_hdr() */ |
| 6097 | __LJMP static int hlua_http_msg_add_hdr(lua_State *L) |
| 6098 | { |
| 6099 | struct http_msg *msg; |
| 6100 | |
| 6101 | MAY_LJMP(check_args(L, 3, "add_header")); |
| 6102 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6103 | |
| 6104 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6105 | WILL_LJMP(lua_error(L)); |
| 6106 | |
| 6107 | return hlua_http_add_hdr(L, msg); |
| 6108 | } |
| 6109 | |
| 6110 | /* Add an header in the HTTP message removing existing headers with the same |
| 6111 | * name. It relies on hlua_http_del_hdr() and hlua_http_add_hdr(). |
| 6112 | */ |
| 6113 | __LJMP static int hlua_http_msg_set_hdr(lua_State *L) |
| 6114 | { |
| 6115 | struct http_msg *msg; |
| 6116 | |
| 6117 | MAY_LJMP(check_args(L, 3, "set_header")); |
| 6118 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6119 | |
| 6120 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6121 | WILL_LJMP(lua_error(L)); |
| 6122 | |
| 6123 | hlua_http_del_hdr(L, msg); |
| 6124 | return hlua_http_add_hdr(L, msg); |
| 6125 | } |
| 6126 | |
| 6127 | /* Rewrites the request method. It relies on http_req_replace_stline(). */ |
| 6128 | __LJMP static int hlua_http_msg_set_meth(lua_State *L) |
| 6129 | { |
| 6130 | struct stream *s; |
| 6131 | struct http_msg *msg; |
| 6132 | const char *name; |
| 6133 | size_t name_len; |
| 6134 | |
| 6135 | MAY_LJMP(check_args(L, 2, "set_method")); |
| 6136 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6137 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6138 | |
| 6139 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6140 | WILL_LJMP(lua_error(L)); |
| 6141 | |
| 6142 | s = chn_strm(msg->chn); |
| 6143 | lua_pushboolean(L, http_req_replace_stline(0, name, name_len, s->be, s) != -1); |
| 6144 | return 1; |
| 6145 | } |
| 6146 | |
| 6147 | /* Rewrites the request path. It relies on http_req_replace_stline(). */ |
| 6148 | __LJMP static int hlua_http_msg_set_path(lua_State *L) |
| 6149 | { |
| 6150 | struct stream *s; |
| 6151 | struct http_msg *msg; |
| 6152 | const char *name; |
| 6153 | size_t name_len; |
| 6154 | |
| 6155 | MAY_LJMP(check_args(L, 2, "set_path")); |
| 6156 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6157 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6158 | |
| 6159 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6160 | WILL_LJMP(lua_error(L)); |
| 6161 | |
| 6162 | s = chn_strm(msg->chn); |
| 6163 | lua_pushboolean(L, http_req_replace_stline(1, name, name_len, s->be, s) != -1); |
| 6164 | return 1; |
| 6165 | } |
| 6166 | |
| 6167 | /* Rewrites the request query-string. It relies on http_req_replace_stline(). */ |
| 6168 | __LJMP static int hlua_http_msg_set_query(lua_State *L) |
| 6169 | { |
| 6170 | struct stream *s; |
| 6171 | struct http_msg *msg; |
| 6172 | const char *name; |
| 6173 | size_t name_len; |
| 6174 | |
| 6175 | MAY_LJMP(check_args(L, 2, "set_query")); |
| 6176 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6177 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6178 | |
| 6179 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6180 | WILL_LJMP(lua_error(L)); |
| 6181 | |
| 6182 | /* Check length. */ |
| 6183 | if (name_len > trash.size - 1) { |
| 6184 | lua_pushboolean(L, 0); |
| 6185 | return 1; |
| 6186 | } |
| 6187 | |
| 6188 | /* Add the mark question as prefix. */ |
| 6189 | chunk_reset(&trash); |
| 6190 | trash.area[trash.data++] = '?'; |
| 6191 | memcpy(trash.area + trash.data, name, name_len); |
| 6192 | trash.data += name_len; |
| 6193 | |
| 6194 | s = chn_strm(msg->chn); |
| 6195 | lua_pushboolean(L, http_req_replace_stline(2, trash.area, trash.data, s->be, s) != -1); |
| 6196 | return 1; |
| 6197 | } |
| 6198 | |
| 6199 | /* Rewrites the request URI. It relies on http_req_replace_stline(). */ |
| 6200 | __LJMP static int hlua_http_msg_set_uri(lua_State *L) |
| 6201 | { |
| 6202 | struct stream *s; |
| 6203 | struct http_msg *msg; |
| 6204 | const char *name; |
| 6205 | size_t name_len; |
| 6206 | |
| 6207 | MAY_LJMP(check_args(L, 2, "set_uri")); |
| 6208 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6209 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6210 | |
| 6211 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6212 | WILL_LJMP(lua_error(L)); |
| 6213 | |
| 6214 | s = chn_strm(msg->chn); |
| 6215 | lua_pushboolean(L, http_req_replace_stline(3, name, name_len, s->be, s) != -1); |
| 6216 | return 1; |
| 6217 | } |
| 6218 | |
| 6219 | /* Rewrites the response status code. It relies on http_res_set_status(). */ |
| 6220 | __LJMP static int hlua_http_msg_set_status(lua_State *L) |
| 6221 | { |
| 6222 | struct http_msg *msg; |
| 6223 | unsigned int code; |
| 6224 | const char *reason; |
| 6225 | size_t reason_len; |
| 6226 | |
| 6227 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6228 | code = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6229 | reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, &reason_len)); |
| 6230 | |
| 6231 | if (!(msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6232 | WILL_LJMP(lua_error(L)); |
| 6233 | |
| 6234 | lua_pushboolean(L, http_res_set_status(code, ist2(reason, reason_len), chn_strm(msg->chn)) != -1); |
| 6235 | return 1; |
| 6236 | } |
| 6237 | |
| 6238 | /* Returns true if the HTTP message is full. */ |
| 6239 | __LJMP static int hlua_http_msg_is_full(lua_State *L) |
| 6240 | { |
| 6241 | struct http_msg *msg; |
| 6242 | |
| 6243 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 6244 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6245 | lua_pushboolean(L, channel_full(msg->chn, 0)); |
| 6246 | return 1; |
| 6247 | } |
| 6248 | |
| 6249 | /* Returns true if the HTTP message may still receive data. */ |
| 6250 | __LJMP static int hlua_http_msg_may_recv(lua_State *L) |
| 6251 | { |
| 6252 | struct http_msg *msg; |
| 6253 | struct htx *htx; |
| 6254 | |
| 6255 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 6256 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6257 | htx = htxbuf(&msg->chn->buf); |
| 6258 | lua_pushboolean(L, (htx_expect_more(htx) && !channel_input_closed(msg->chn) && channel_may_recv(msg->chn))); |
| 6259 | return 1; |
| 6260 | } |
| 6261 | |
| 6262 | /* Returns true if the HTTP message EOM was received */ |
| 6263 | __LJMP static int hlua_http_msg_is_eom(lua_State *L) |
| 6264 | { |
| 6265 | struct http_msg *msg; |
| 6266 | struct htx *htx; |
| 6267 | |
| 6268 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 6269 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6270 | htx = htxbuf(&msg->chn->buf); |
| 6271 | lua_pushboolean(L, !htx_expect_more(htx)); |
| 6272 | return 1; |
| 6273 | } |
| 6274 | |
| 6275 | /* Returns the number of bytes available in the input side of the HTTP |
| 6276 | * message. This function never fails. |
| 6277 | */ |
| 6278 | __LJMP static int hlua_http_msg_get_in_len(lua_State *L) |
| 6279 | { |
| 6280 | struct http_msg *msg; |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6281 | size_t output, input; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6282 | |
| 6283 | MAY_LJMP(check_args(L, 1, "input")); |
| 6284 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6285 | hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6286 | lua_pushinteger(L, input); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6287 | return 1; |
| 6288 | } |
| 6289 | |
| 6290 | /* Returns the number of bytes available in the output side of the HTTP |
| 6291 | * message. This function never fails. |
| 6292 | */ |
| 6293 | __LJMP static int hlua_http_msg_get_out_len(lua_State *L) |
| 6294 | { |
| 6295 | struct http_msg *msg; |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6296 | size_t output, input; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6297 | |
| 6298 | MAY_LJMP(check_args(L, 1, "output")); |
| 6299 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6300 | hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6301 | lua_pushinteger(L, output); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6302 | return 1; |
| 6303 | } |
| 6304 | |
| 6305 | /* Copies at most <len> bytes of DATA blocks from the HTTP message <msg> |
| 6306 | * 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] | 6307 | * 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] | 6308 | * block. This function is called during the payload filtering, so the headers |
| 6309 | * are already scheduled for output (from the filter point of view). |
| 6310 | */ |
| 6311 | static int _hlua_http_msg_dup(struct http_msg *msg, lua_State *L, size_t offset, size_t len) |
| 6312 | { |
| 6313 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 6314 | struct htx_blk *blk; |
| 6315 | struct htx_ret htxret; |
| 6316 | luaL_Buffer b; |
| 6317 | int ret = 0; |
| 6318 | |
| 6319 | luaL_buffinit(L, &b); |
| 6320 | htxret = htx_find_offset(htx, offset); |
| 6321 | for (blk = htxret.blk, offset = htxret.ret; blk && len; blk = htx_get_next_blk(htx, blk)) { |
| 6322 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 6323 | struct ist v; |
| 6324 | |
| 6325 | switch (type) { |
| 6326 | case HTX_BLK_UNUSED: |
| 6327 | break; |
| 6328 | |
| 6329 | case HTX_BLK_DATA: |
| 6330 | v = htx_get_blk_value(htx, blk); |
Tim Duesterhus | b113b5c | 2021-09-15 13:58:44 +0200 | [diff] [blame] | 6331 | v = istadv(v, offset); |
Tim Duesterhus | 2471f5c | 2021-11-08 09:05:01 +0100 | [diff] [blame] | 6332 | v = isttrim(v, len); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6333 | |
| 6334 | luaL_addlstring(&b, v.ptr, v.len); |
| 6335 | ret += v.len; |
| 6336 | break; |
| 6337 | |
| 6338 | default: |
vishnu | 0af4bd7 | 2021-10-24 06:46:24 +0530 | [diff] [blame] | 6339 | if (!ret) |
| 6340 | goto no_data; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6341 | goto end; |
| 6342 | } |
| 6343 | offset = 0; |
| 6344 | } |
| 6345 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6346 | end: |
vishnu | 0af4bd7 | 2021-10-24 06:46:24 +0530 | [diff] [blame] | 6347 | if (!ret && (htx->flags & HTX_FL_EOM)) |
| 6348 | goto no_data; |
| 6349 | luaL_pushresult(&b); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6350 | return ret; |
vishnu | 0af4bd7 | 2021-10-24 06:46:24 +0530 | [diff] [blame] | 6351 | |
| 6352 | no_data: |
| 6353 | /* Remove the empty string and push nil on the stack */ |
| 6354 | lua_pop(L, 1); |
| 6355 | lua_pushnil(L); |
| 6356 | return 0; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6357 | } |
| 6358 | |
| 6359 | /* Copies the string <str> to the HTTP message <msg> at the offset |
| 6360 | * <offset>. This function returns -1 if data cannot be copied. Otherwise, it |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6361 | * returns the amount of data written. This function is responsible to update |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6362 | * the filter context. |
| 6363 | */ |
| 6364 | static int _hlua_http_msg_insert(struct http_msg *msg, struct filter *filter, struct ist str, size_t offset) |
| 6365 | { |
| 6366 | struct htx *htx = htx_from_buf(&msg->chn->buf); |
| 6367 | struct htx_ret htxret; |
| 6368 | int /*max, */ret = 0; |
| 6369 | |
| 6370 | /* Nothing to do, just return */ |
| 6371 | if (unlikely(istlen(str) == 0)) |
| 6372 | goto end; |
| 6373 | |
| 6374 | if (istlen(str) > htx_free_data_space(htx)) { |
| 6375 | ret = -1; |
| 6376 | goto end; |
| 6377 | } |
| 6378 | |
| 6379 | htxret = htx_find_offset(htx, offset); |
| 6380 | if (!htxret.blk || htx_get_blk_type(htxret.blk) != HTX_BLK_DATA) { |
| 6381 | if (!htx_add_last_data(htx, str)) |
| 6382 | goto end; |
| 6383 | } |
| 6384 | else { |
| 6385 | struct ist v = htx_get_blk_value(htx, htxret.blk); |
| 6386 | v.ptr += htxret.ret; |
| 6387 | v.len = 0; |
| 6388 | if (!htx_replace_blk_value(htx, htxret.blk, v, str)) |
| 6389 | goto end; |
| 6390 | } |
| 6391 | ret = str.len; |
| 6392 | if (ret) { |
| 6393 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6394 | flt_update_offsets(filter, msg->chn, ret); |
| 6395 | flt_ctx->cur_len[CHN_IDX(msg->chn)] += ret; |
| 6396 | } |
| 6397 | |
| 6398 | end: |
| 6399 | htx_to_buf(htx, &msg->chn->buf); |
| 6400 | return ret; |
| 6401 | } |
| 6402 | |
| 6403 | /* Helper function removing at most <len> bytes of DATA blocks at the absolute |
| 6404 | * position <offset>. It stops on the first non-DATA HTX block. This function is |
| 6405 | * called during the payload filtering, so the headers are already scheduled for |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6406 | * output (from the filter point of view). This function is responsible to |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6407 | * update the filter context. |
| 6408 | */ |
| 6409 | static void _hlua_http_msg_delete(struct http_msg *msg, struct filter *filter, size_t offset, size_t len) |
| 6410 | { |
| 6411 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6412 | struct htx *htx = htx_from_buf(&msg->chn->buf); |
| 6413 | struct htx_blk *blk; |
| 6414 | struct htx_ret htxret; |
| 6415 | size_t ret = 0; |
| 6416 | |
| 6417 | /* Be sure <len> is always the amount of DATA to remove */ |
| 6418 | if (htx->data == offset+len && htx_get_tail_type(htx) == HTX_BLK_DATA) { |
| 6419 | htx_truncate(htx, offset); |
| 6420 | ret = len; |
| 6421 | goto end; |
| 6422 | } |
| 6423 | |
| 6424 | htxret = htx_find_offset(htx, offset); |
| 6425 | blk = htxret.blk; |
| 6426 | if (htxret.ret) { |
| 6427 | struct ist v; |
| 6428 | |
| 6429 | if (htx_get_blk_type(blk) != HTX_BLK_DATA) |
| 6430 | goto end; |
| 6431 | v = htx_get_blk_value(htx, blk); |
| 6432 | v.ptr += htxret.ret; |
Tim Duesterhus | 2471f5c | 2021-11-08 09:05:01 +0100 | [diff] [blame] | 6433 | v = isttrim(v, len); |
Tim Duesterhus | b113b5c | 2021-09-15 13:58:44 +0200 | [diff] [blame] | 6434 | blk = htx_replace_blk_value(htx, blk, v, IST_NULL); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6435 | len -= v.len; |
| 6436 | ret += v.len; |
| 6437 | } |
| 6438 | |
| 6439 | |
| 6440 | while (blk && len) { |
| 6441 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 6442 | uint32_t sz = htx_get_blksz(blk); |
| 6443 | |
| 6444 | switch (type) { |
| 6445 | case HTX_BLK_UNUSED: |
| 6446 | break; |
| 6447 | |
| 6448 | case HTX_BLK_DATA: |
| 6449 | if (len < sz) { |
| 6450 | htx_cut_data_blk(htx, blk, len); |
| 6451 | ret += len; |
| 6452 | goto end; |
| 6453 | } |
| 6454 | break; |
| 6455 | |
| 6456 | default: |
| 6457 | goto end; |
| 6458 | } |
| 6459 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6460 | /* Remove all the data block */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6461 | len -= sz; |
| 6462 | ret += sz; |
| 6463 | blk = htx_remove_blk(htx, blk); |
| 6464 | } |
| 6465 | |
| 6466 | end: |
| 6467 | flt_update_offsets(filter, msg->chn, -ret); |
| 6468 | flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret; |
| 6469 | /* WARNING: we don't call htx_to_buf() on purpose, because we don't want |
| 6470 | * to loose the EOM flag if the message is empty. |
| 6471 | */ |
| 6472 | } |
| 6473 | |
| 6474 | /* Copies input data found in an HTTP message. Unlike the channel function used |
| 6475 | * to duplicate raw data, this one can only be called inside a filter, from |
| 6476 | * http_payload callback. So it cannot yield. An exception is returned if it is |
| 6477 | * called from another callback. If nothing was copied, a nil value is pushed on |
| 6478 | * the stack. |
| 6479 | */ |
| 6480 | __LJMP static int hlua_http_msg_get_body(lua_State *L) |
| 6481 | { |
| 6482 | struct http_msg *msg; |
| 6483 | struct filter *filter; |
| 6484 | size_t output, input; |
| 6485 | int offset, len; |
| 6486 | |
| 6487 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 6488 | WILL_LJMP(luaL_error(L, "'data' expects at most 2 arguments")); |
| 6489 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6490 | |
| 6491 | if (msg->msg_state < HTTP_MSG_DATA) |
| 6492 | WILL_LJMP(lua_error(L)); |
| 6493 | |
| 6494 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6495 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6496 | WILL_LJMP(lua_error(L)); |
| 6497 | |
| 6498 | if (!ci_data(msg->chn) && channel_input_closed(msg->chn)) { |
| 6499 | lua_pushnil(L); |
| 6500 | return 1; |
| 6501 | } |
| 6502 | |
| 6503 | offset = output; |
| 6504 | if (lua_gettop(L) > 1) { |
| 6505 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6506 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6507 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6508 | offset += output; |
| 6509 | if (offset < output || offset > input + output) { |
| 6510 | lua_pushfstring(L, "offset out of range."); |
| 6511 | WILL_LJMP(lua_error(L)); |
| 6512 | } |
| 6513 | } |
| 6514 | len = output + input - offset; |
| 6515 | if (lua_gettop(L) == 3) { |
| 6516 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6517 | if (!len) |
| 6518 | goto dup; |
| 6519 | if (len == -1) |
| 6520 | len = global.tune.bufsize; |
| 6521 | if (len < 0) { |
| 6522 | lua_pushfstring(L, "length out of range."); |
| 6523 | WILL_LJMP(lua_error(L)); |
| 6524 | } |
| 6525 | } |
| 6526 | |
| 6527 | dup: |
| 6528 | _hlua_http_msg_dup(msg, L, offset, len); |
| 6529 | return 1; |
| 6530 | } |
| 6531 | |
| 6532 | /* Appends a string to the HTTP message, after all existing DATA blocks but |
| 6533 | * before the trailers, if any. It returns the amount of data written or -1 if |
| 6534 | * nothing was copied. Unlike the channel function used to append data, this one |
| 6535 | * can only be called inside a filter, from http_payload callback. So it cannot |
| 6536 | * yield. An exception is returned if it is called from another callback. |
| 6537 | */ |
| 6538 | __LJMP static int hlua_http_msg_append(lua_State *L) |
| 6539 | { |
| 6540 | struct http_msg *msg; |
| 6541 | struct filter *filter; |
| 6542 | const char *str; |
| 6543 | size_t offset, len, sz; |
| 6544 | int ret; |
| 6545 | |
| 6546 | MAY_LJMP(check_args(L, 2, "append")); |
| 6547 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6548 | |
| 6549 | if (msg->msg_state < HTTP_MSG_DATA) |
| 6550 | WILL_LJMP(lua_error(L)); |
| 6551 | |
| 6552 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6553 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6554 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6555 | WILL_LJMP(lua_error(L)); |
| 6556 | |
| 6557 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset+len); |
| 6558 | lua_pushinteger(L, ret); |
| 6559 | return 1; |
| 6560 | } |
| 6561 | |
| 6562 | /* Prepends a string to the HTTP message, before all existing DATA blocks. It |
| 6563 | * returns the amount of data written or -1 if nothing was copied. Unlike the |
| 6564 | * channel function used to prepend data, this one can only be called inside a |
| 6565 | * filter, from http_payload callback. So it cannot yield. An exception is |
| 6566 | * returned if it is called from another callback. |
| 6567 | */ |
| 6568 | __LJMP static int hlua_http_msg_prepend(lua_State *L) |
| 6569 | { |
| 6570 | struct http_msg *msg; |
| 6571 | struct filter *filter; |
| 6572 | const char *str; |
| 6573 | size_t offset, len, sz; |
| 6574 | int ret; |
| 6575 | |
| 6576 | MAY_LJMP(check_args(L, 2, "prepend")); |
| 6577 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6578 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6579 | if (msg->msg_state < HTTP_MSG_DATA) |
| 6580 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6581 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6582 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6583 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6584 | if (!filter || !hlua_filter_from_payload(filter)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6585 | WILL_LJMP(lua_error(L)); |
| 6586 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6587 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6588 | lua_pushinteger(L, ret); |
| 6589 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6590 | } |
| 6591 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6592 | /* Inserts a string to the HTTP message at a given offset. By default the string |
| 6593 | * is appended at the end of DATA blocks. It returns the amount of data written |
| 6594 | * or -1 if nothing was copied. Unlike the channel function used to insert data, |
| 6595 | * this one can only be called inside a filter, from http_payload callback. So |
| 6596 | * it cannot yield. An exception is returned if it is called from another |
| 6597 | * callback. |
| 6598 | */ |
| 6599 | __LJMP static int hlua_http_msg_insert_data(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6600 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6601 | struct http_msg *msg; |
| 6602 | struct filter *filter; |
| 6603 | const char *str; |
| 6604 | size_t input, output, sz; |
| 6605 | int offset; |
| 6606 | int ret; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6607 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6608 | if (lua_gettop(L) < 2 || lua_gettop(L) > 3) |
| 6609 | WILL_LJMP(luaL_error(L, "'insert' expects at least 1 argument and at most 2 arguments")); |
| 6610 | MAY_LJMP(check_args(L, 2, "insert")); |
| 6611 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6612 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6613 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6614 | WILL_LJMP(lua_error(L)); |
| 6615 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6616 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6617 | filter = hlua_http_msg_filter(L, 1, msg, &input, &output); |
| 6618 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6619 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6620 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6621 | offset = input + output; |
| 6622 | if (lua_gettop(L) > 2) { |
| 6623 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6624 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6625 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6626 | offset += output; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6627 | if (offset < output || offset > output + input) { |
| 6628 | lua_pushfstring(L, "offset out of range."); |
| 6629 | WILL_LJMP(lua_error(L)); |
| 6630 | } |
| 6631 | } |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6632 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6633 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6634 | lua_pushinteger(L, ret); |
| 6635 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6636 | } |
| 6637 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6638 | /* Removes a given amount of data from the HTTP message at a given offset. By |
| 6639 | * default all DATA blocks are removed. It returns the amount of data |
| 6640 | * removed. Unlike the channel function used to remove data, this one can only |
| 6641 | * be called inside a filter, from http_payload callback. So it cannot yield. An |
| 6642 | * exception is returned if it is called from another callback. |
| 6643 | */ |
| 6644 | __LJMP static int hlua_http_msg_del_data(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6645 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6646 | struct http_msg *msg; |
| 6647 | struct filter *filter; |
| 6648 | size_t input, output; |
| 6649 | int offset, len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6650 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6651 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 6652 | WILL_LJMP(luaL_error(L, "'insert' expects at most 2 arguments")); |
| 6653 | MAY_LJMP(check_args(L, 2, "insert")); |
| 6654 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6655 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6656 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6657 | WILL_LJMP(lua_error(L)); |
| 6658 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6659 | filter = hlua_http_msg_filter(L, 1, msg, &input, &output); |
| 6660 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6661 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6662 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6663 | offset = input + output; |
| 6664 | if (lua_gettop(L) > 2) { |
| 6665 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6666 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6667 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6668 | offset += output; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6669 | if (offset < output || offset > output + input) { |
| 6670 | lua_pushfstring(L, "offset out of range."); |
| 6671 | WILL_LJMP(lua_error(L)); |
| 6672 | } |
| 6673 | } |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6674 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6675 | len = output + input - offset; |
| 6676 | if (lua_gettop(L) == 4) { |
| 6677 | len = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 6678 | if (!len) |
| 6679 | goto end; |
| 6680 | if (len == -1) |
| 6681 | len = output + input - offset; |
| 6682 | if (len < 0 || offset + len > output + input) { |
| 6683 | lua_pushfstring(L, "length out of range."); |
| 6684 | WILL_LJMP(lua_error(L)); |
| 6685 | } |
| 6686 | } |
| 6687 | |
| 6688 | _hlua_http_msg_delete(msg, filter, offset, len); |
| 6689 | |
| 6690 | end: |
| 6691 | lua_pushinteger(L, len); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6692 | return 1; |
| 6693 | } |
| 6694 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6695 | /* 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] | 6696 | * all remaining data are removed, accordingly to the filter context. It returns |
| 6697 | * the amount of data written or -1 if nothing was copied. Unlike the channel |
| 6698 | * function used to replace data, this one can only be called inside a filter, |
| 6699 | * from http_payload callback. So it cannot yield. An exception is returned if |
| 6700 | * it is called from another callback. |
| 6701 | */ |
| 6702 | __LJMP static int hlua_http_msg_set_data(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6703 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6704 | struct http_msg *msg; |
| 6705 | struct filter *filter; |
| 6706 | struct htx *htx; |
| 6707 | const char *str; |
| 6708 | size_t input, output, sz; |
| 6709 | int offset, len; |
| 6710 | int ret; |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 6711 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6712 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 6713 | WILL_LJMP(luaL_error(L, "'set' expects at least 1 argument and at most 3 arguments")); |
| 6714 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6715 | |
| 6716 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6717 | WILL_LJMP(lua_error(L)); |
| 6718 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6719 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6720 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6721 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6722 | WILL_LJMP(lua_error(L)); |
| 6723 | |
| 6724 | offset = output; |
| 6725 | if (lua_gettop(L) > 2) { |
| 6726 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6727 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6728 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6729 | offset += output; |
| 6730 | if (offset < output || offset > input + output) { |
| 6731 | lua_pushfstring(L, "offset out of range."); |
| 6732 | WILL_LJMP(lua_error(L)); |
| 6733 | } |
| 6734 | } |
| 6735 | |
| 6736 | len = output + input - offset; |
| 6737 | if (lua_gettop(L) == 4) { |
| 6738 | len = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 6739 | if (!len) |
| 6740 | goto set; |
| 6741 | if (len == -1) |
| 6742 | len = output + input - offset; |
| 6743 | if (len < 0 || offset + len > output + input) { |
| 6744 | lua_pushfstring(L, "length out of range."); |
| 6745 | WILL_LJMP(lua_error(L)); |
| 6746 | } |
| 6747 | } |
| 6748 | |
| 6749 | set: |
| 6750 | /* Be sure we can copied the string once input data will be removed. */ |
| 6751 | htx = htx_from_buf(&msg->chn->buf); |
| 6752 | if (sz > htx_free_data_space(htx) + len) |
| 6753 | lua_pushinteger(L, -1); |
| 6754 | else { |
| 6755 | _hlua_http_msg_delete(msg, filter, offset, len); |
| 6756 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6757 | lua_pushinteger(L, ret); |
| 6758 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6759 | return 1; |
| 6760 | } |
| 6761 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6762 | /* Prepends data into an HTTP message and forward it, from the filter point of |
| 6763 | * view. It returns the amount of data written or -1 if nothing was sent. Unlike |
| 6764 | * the channel function used to send data, this one can only be called inside a |
| 6765 | * filter, from http_payload callback. So it cannot yield. An exception is |
| 6766 | * returned if it is called from another callback. |
| 6767 | */ |
| 6768 | __LJMP static int hlua_http_msg_send(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6769 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6770 | struct http_msg *msg; |
| 6771 | struct filter *filter; |
| 6772 | struct htx *htx; |
| 6773 | const char *str; |
| 6774 | size_t offset, len, sz; |
| 6775 | int ret; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6776 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6777 | MAY_LJMP(check_args(L, 2, "send")); |
| 6778 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6779 | |
| 6780 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6781 | WILL_LJMP(lua_error(L)); |
| 6782 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6783 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6784 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6785 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6786 | WILL_LJMP(lua_error(L)); |
| 6787 | |
| 6788 | /* Return an error if the channel's output is closed */ |
| 6789 | if (unlikely(channel_output_closed(msg->chn))) { |
| 6790 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6791 | return 1; |
| 6792 | } |
| 6793 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6794 | htx = htx_from_buf(&msg->chn->buf); |
| 6795 | if (sz > htx_free_data_space(htx)) { |
| 6796 | lua_pushinteger(L, -1); |
| 6797 | return 1; |
| 6798 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6799 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6800 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6801 | if (ret > 0) { |
| 6802 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6803 | |
| 6804 | FLT_OFF(filter, msg->chn) += ret; |
| 6805 | flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret; |
| 6806 | flt_ctx->cur_off[CHN_IDX(msg->chn)] += ret; |
| 6807 | } |
| 6808 | |
| 6809 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6810 | return 1; |
| 6811 | } |
| 6812 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6813 | /* Forwards a given amount of bytes. It return -1 if the channel's output is |
| 6814 | * closed. Otherwise, it returns the number of bytes forwarded. Unlike the |
| 6815 | * channel function used to forward data, this one can only be called inside a |
| 6816 | * filter, from http_payload callback. So it cannot yield. An exception is |
| 6817 | * returned if it is called from another callback. All other functions deal with |
| 6818 | * DATA block, this one not. |
| 6819 | */ |
| 6820 | __LJMP static int hlua_http_msg_forward(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6821 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6822 | struct http_msg *msg; |
| 6823 | struct filter *filter; |
| 6824 | size_t offset, len; |
| 6825 | int fwd, ret = 0; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6826 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6827 | MAY_LJMP(check_args(L, 2, "forward")); |
| 6828 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6829 | |
| 6830 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6831 | WILL_LJMP(lua_error(L)); |
| 6832 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6833 | fwd = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6834 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6835 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6836 | WILL_LJMP(lua_error(L)); |
| 6837 | |
| 6838 | /* Nothing to do, just return */ |
| 6839 | if (!fwd) |
| 6840 | goto end; |
| 6841 | |
| 6842 | /* Return an error if the channel's output is closed */ |
| 6843 | if (unlikely(channel_output_closed(msg->chn))) { |
| 6844 | ret = -1; |
| 6845 | goto end; |
| 6846 | } |
| 6847 | |
| 6848 | ret = fwd; |
| 6849 | if (ret > len) |
| 6850 | ret = len; |
| 6851 | |
| 6852 | if (ret) { |
| 6853 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6854 | |
| 6855 | FLT_OFF(filter, msg->chn) += ret; |
| 6856 | flt_ctx->cur_off[CHN_IDX(msg->chn)] += ret; |
| 6857 | flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret; |
| 6858 | } |
| 6859 | |
| 6860 | end: |
| 6861 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6862 | return 1; |
| 6863 | } |
| 6864 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6865 | /* Set EOM flag on the HTX message. |
| 6866 | * |
| 6867 | * NOTE: Not sure it is a good idea to manipulate this flag but for now I don't |
| 6868 | * really know how to do without this feature. |
| 6869 | */ |
| 6870 | __LJMP static int hlua_http_msg_set_eom(lua_State *L) |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 6871 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6872 | struct http_msg *msg; |
| 6873 | struct htx *htx; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 6874 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6875 | MAY_LJMP(check_args(L, 1, "set_eom")); |
| 6876 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6877 | htx = htxbuf(&msg->chn->buf); |
| 6878 | htx->flags |= HTX_FL_EOM; |
| 6879 | return 0; |
| 6880 | } |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6881 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6882 | /* Unset EOM flag on the HTX message. |
| 6883 | * |
| 6884 | * NOTE: Not sure it is a good idea to manipulate this flag but for now I don't |
| 6885 | * really know how to do without this feature. |
| 6886 | */ |
| 6887 | __LJMP static int hlua_http_msg_unset_eom(lua_State *L) |
| 6888 | { |
| 6889 | struct http_msg *msg; |
| 6890 | struct htx *htx; |
| 6891 | |
| 6892 | MAY_LJMP(check_args(L, 1, "set_eom")); |
| 6893 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6894 | htx = htxbuf(&msg->chn->buf); |
| 6895 | htx->flags &= ~HTX_FL_EOM; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 6896 | return 0; |
| 6897 | } |
| 6898 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6899 | /* |
| 6900 | * |
| 6901 | * |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 6902 | * Class HTTPClient |
| 6903 | * |
| 6904 | * |
| 6905 | */ |
| 6906 | __LJMP static struct hlua_httpclient *hlua_checkhttpclient(lua_State *L, int ud) |
| 6907 | { |
| 6908 | return MAY_LJMP(hlua_checkudata(L, ud, class_httpclient_ref)); |
| 6909 | } |
| 6910 | |
William Lallemand | f77f1de | 2021-09-28 19:10:38 +0200 | [diff] [blame] | 6911 | |
| 6912 | /* stops the httpclient and ask it to kill itself */ |
| 6913 | __LJMP static int hlua_httpclient_gc(lua_State *L) |
| 6914 | { |
| 6915 | struct hlua_httpclient *hlua_hc; |
| 6916 | |
| 6917 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 6918 | |
| 6919 | hlua_hc = MAY_LJMP(hlua_checkhttpclient(L, 1)); |
| 6920 | |
| 6921 | httpclient_stop_and_destroy(hlua_hc->hc); |
| 6922 | |
| 6923 | hlua_hc->hc = NULL; |
| 6924 | |
| 6925 | |
| 6926 | return 0; |
| 6927 | } |
| 6928 | |
| 6929 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 6930 | __LJMP static int hlua_httpclient_new(lua_State *L) |
| 6931 | { |
| 6932 | struct hlua_httpclient *hlua_hc; |
| 6933 | struct hlua *hlua; |
| 6934 | |
| 6935 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 6936 | hlua = hlua_gethlua(L); |
| 6937 | if (!hlua) |
| 6938 | return 0; |
| 6939 | |
| 6940 | /* Check stack size. */ |
| 6941 | if (!lua_checkstack(L, 3)) { |
| 6942 | hlua_pusherror(L, "httpclient: full stack"); |
| 6943 | goto err; |
| 6944 | } |
| 6945 | /* Create the object: obj[0] = userdata. */ |
| 6946 | lua_newtable(L); |
| 6947 | hlua_hc = MAY_LJMP(lua_newuserdata(L, sizeof(*hlua_hc))); |
| 6948 | lua_rawseti(L, -2, 0); |
| 6949 | memset(hlua_hc, 0, sizeof(*hlua_hc)); |
| 6950 | |
| 6951 | hlua_hc->hc = httpclient_new(hlua, 0, IST_NULL); |
| 6952 | if (!hlua_hc->hc) |
| 6953 | goto err; |
| 6954 | |
| 6955 | /* Pop a class stream metatable and affect it to the userdata. */ |
| 6956 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_httpclient_ref); |
| 6957 | lua_setmetatable(L, -2); |
| 6958 | |
| 6959 | return 1; |
| 6960 | |
| 6961 | err: |
| 6962 | WILL_LJMP(lua_error(L)); |
| 6963 | return 0; |
| 6964 | } |
| 6965 | |
| 6966 | |
| 6967 | /* |
| 6968 | * Callback of the httpclient, this callback wakes the lua task up, once the |
| 6969 | * httpclient receives some data |
| 6970 | * |
| 6971 | */ |
| 6972 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 6973 | static void hlua_httpclient_cb(struct httpclient *hc) |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 6974 | { |
| 6975 | struct hlua *hlua = hc->caller; |
| 6976 | |
| 6977 | if (!hlua || !hlua->task) |
| 6978 | return; |
| 6979 | |
| 6980 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 6981 | } |
| 6982 | |
| 6983 | /* |
William Lallemand | d7df73a | 2021-09-23 17:54:00 +0200 | [diff] [blame] | 6984 | * Fill the lua stack with headers from the httpclient response |
| 6985 | * This works the same way as the hlua_http_get_headers() function |
| 6986 | */ |
| 6987 | __LJMP static int hlua_httpclient_get_headers(lua_State *L, struct hlua_httpclient *hlua_hc) |
| 6988 | { |
| 6989 | struct http_hdr *hdr; |
| 6990 | |
| 6991 | lua_newtable(L); |
| 6992 | |
William Lallemand | ef574b2 | 2021-10-05 16:19:31 +0200 | [diff] [blame] | 6993 | for (hdr = hlua_hc->hc->res.hdrs; hdr && isttest(hdr->n); hdr++) { |
William Lallemand | d7df73a | 2021-09-23 17:54:00 +0200 | [diff] [blame] | 6994 | struct ist n, v; |
| 6995 | int len; |
| 6996 | |
| 6997 | n = hdr->n; |
| 6998 | v = hdr->v; |
| 6999 | |
| 7000 | /* Check for existing entry: |
| 7001 | * assume that the table is on the top of the stack, and |
| 7002 | * push the key in the stack, the function lua_gettable() |
| 7003 | * perform the lookup. |
| 7004 | */ |
| 7005 | |
| 7006 | lua_pushlstring(L, n.ptr, n.len); |
| 7007 | lua_gettable(L, -2); |
| 7008 | |
| 7009 | switch (lua_type(L, -1)) { |
| 7010 | case LUA_TNIL: |
| 7011 | /* Table not found, create it. */ |
| 7012 | lua_pop(L, 1); /* remove the nil value. */ |
| 7013 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 7014 | lua_newtable(L); /* create and push empty table. */ |
| 7015 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 7016 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 7017 | lua_rawset(L, -3); /* index new table with header name (pop the values). */ |
| 7018 | break; |
| 7019 | |
| 7020 | case LUA_TTABLE: |
| 7021 | /* Entry found: push the value in the table. */ |
| 7022 | len = lua_rawlen(L, -1); |
| 7023 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 7024 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 7025 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 7026 | break; |
| 7027 | |
| 7028 | default: |
| 7029 | /* Other cases are errors. */ |
| 7030 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 7031 | WILL_LJMP(lua_error(L)); |
| 7032 | } |
| 7033 | } |
| 7034 | return 1; |
| 7035 | } |
| 7036 | |
| 7037 | /* |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7038 | * Allocate and return an array of http_hdr ist extracted from the <headers> lua table |
| 7039 | * |
| 7040 | * Caller must free the result |
| 7041 | */ |
| 7042 | struct http_hdr *hlua_httpclient_table_to_hdrs(lua_State *L) |
| 7043 | { |
| 7044 | struct http_hdr hdrs[global.tune.max_http_hdr]; |
| 7045 | struct http_hdr *result = NULL; |
| 7046 | uint32_t hdr_num = 0; |
| 7047 | |
| 7048 | lua_pushnil(L); |
| 7049 | while (lua_next(L, -2) != 0) { |
| 7050 | struct ist name, value; |
| 7051 | const char *n, *v; |
| 7052 | size_t nlen, vlen; |
| 7053 | |
| 7054 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 7055 | /* Skip element if the key is not a string or if the value is not a table */ |
| 7056 | goto next_hdr; |
| 7057 | } |
| 7058 | |
| 7059 | n = lua_tolstring(L, -2, &nlen); |
| 7060 | name = ist2(n, nlen); |
| 7061 | |
| 7062 | /* Loop on header's values */ |
| 7063 | lua_pushnil(L); |
| 7064 | while (lua_next(L, -2)) { |
| 7065 | if (!lua_isstring(L, -1)) { |
| 7066 | /* Skip the value if it is not a string */ |
| 7067 | goto next_value; |
| 7068 | } |
| 7069 | |
| 7070 | v = lua_tolstring(L, -1, &vlen); |
| 7071 | value = ist2(v, vlen); |
| 7072 | name = ist2(n, nlen); |
| 7073 | |
| 7074 | hdrs[hdr_num].n = istdup(name); |
| 7075 | hdrs[hdr_num].v = istdup(value); |
| 7076 | |
| 7077 | hdr_num++; |
| 7078 | |
| 7079 | next_value: |
| 7080 | lua_pop(L, 1); |
| 7081 | } |
| 7082 | |
| 7083 | next_hdr: |
| 7084 | lua_pop(L, 1); |
| 7085 | |
| 7086 | } |
| 7087 | |
| 7088 | if (hdr_num) { |
| 7089 | /* alloc and copy the headers in the httpclient struct */ |
Tim Duesterhus | 16cc16d | 2021-11-06 15:14:45 +0100 | [diff] [blame] | 7090 | result = calloc((hdr_num + 1), sizeof(*result)); |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7091 | if (!result) |
| 7092 | goto skip_headers; |
| 7093 | memcpy(result, hdrs, sizeof(struct http_hdr) * (hdr_num + 1)); |
| 7094 | |
| 7095 | result[hdr_num].n = IST_NULL; |
| 7096 | result[hdr_num].v = IST_NULL; |
| 7097 | } |
| 7098 | |
| 7099 | skip_headers: |
| 7100 | lua_pop(L, 1); |
| 7101 | |
| 7102 | return result; |
| 7103 | } |
| 7104 | |
| 7105 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7106 | /* |
| 7107 | * For each yield, checks if there is some data in the httpclient and push them |
| 7108 | * in the lua buffer, once the httpclient finished its job, push the result on |
| 7109 | * the stack |
| 7110 | */ |
| 7111 | __LJMP static int hlua_httpclient_rcv_yield(lua_State *L, int status, lua_KContext ctx) |
| 7112 | { |
| 7113 | struct buffer *tr; |
| 7114 | int res; |
| 7115 | struct hlua *hlua = hlua_gethlua(L); |
| 7116 | struct hlua_httpclient *hlua_hc = hlua_checkhttpclient(L, 1); |
| 7117 | |
| 7118 | |
| 7119 | tr = get_trash_chunk(); |
| 7120 | |
| 7121 | res = httpclient_res_xfer(hlua_hc->hc, tr); |
| 7122 | luaL_addlstring(&hlua_hc->b, b_orig(tr), res); |
| 7123 | |
| 7124 | if (!httpclient_data(hlua_hc->hc) && httpclient_ended(hlua_hc->hc)) { |
| 7125 | |
| 7126 | luaL_pushresult(&hlua_hc->b); |
| 7127 | lua_settable(L, -3); |
| 7128 | |
| 7129 | lua_pushstring(L, "status"); |
| 7130 | lua_pushinteger(L, hlua_hc->hc->res.status); |
| 7131 | lua_settable(L, -3); |
| 7132 | |
| 7133 | |
| 7134 | lua_pushstring(L, "reason"); |
| 7135 | lua_pushlstring(L, hlua_hc->hc->res.reason.ptr, hlua_hc->hc->res.reason.len); |
| 7136 | lua_settable(L, -3); |
| 7137 | |
| 7138 | lua_pushstring(L, "headers"); |
| 7139 | hlua_httpclient_get_headers(L, hlua_hc); |
| 7140 | lua_settable(L, -3); |
| 7141 | |
| 7142 | return 1; |
| 7143 | } |
| 7144 | |
| 7145 | if (httpclient_data(hlua_hc->hc)) |
| 7146 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 7147 | |
| 7148 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_rcv_yield, TICK_ETERNITY, 0)); |
| 7149 | |
| 7150 | return 0; |
| 7151 | } |
| 7152 | |
| 7153 | /* |
| 7154 | * Call this when trying to stream a body during a request |
| 7155 | */ |
| 7156 | __LJMP static int hlua_httpclient_snd_yield(lua_State *L, int status, lua_KContext ctx) |
| 7157 | { |
| 7158 | struct hlua *hlua; |
| 7159 | struct hlua_httpclient *hlua_hc = hlua_checkhttpclient(L, 1); |
| 7160 | const char *body_str = NULL; |
| 7161 | int ret; |
| 7162 | int end = 0; |
| 7163 | size_t buf_len; |
| 7164 | size_t to_send = 0; |
| 7165 | |
| 7166 | hlua = hlua_gethlua(L); |
| 7167 | |
| 7168 | if (!hlua || !hlua->task) |
| 7169 | WILL_LJMP(luaL_error(L, "The 'get' function is only allowed in " |
| 7170 | "'frontend', 'backend' or 'task'")); |
| 7171 | |
| 7172 | ret = lua_getfield(L, -1, "body"); |
| 7173 | if (ret != LUA_TSTRING) |
| 7174 | goto rcv; |
| 7175 | |
| 7176 | body_str = lua_tolstring(L, -1, &buf_len); |
| 7177 | lua_pop(L, 1); |
| 7178 | |
| 7179 | to_send = MIN(buf_len - hlua_hc->sent, 1024); |
| 7180 | |
| 7181 | if ((hlua_hc->sent + to_send) >= buf_len) |
| 7182 | end = 1; |
| 7183 | |
| 7184 | /* the end flag is always set since we are using the whole remaining size */ |
| 7185 | hlua_hc->sent += httpclient_req_xfer(hlua_hc->hc, ist2(body_str + hlua_hc->sent, to_send), end); |
| 7186 | |
| 7187 | if (buf_len > hlua_hc->sent) { |
| 7188 | /* still need to process the buffer */ |
| 7189 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_snd_yield, TICK_ETERNITY, 0)); |
| 7190 | } else { |
| 7191 | goto rcv; |
| 7192 | /* we sent the whole request buffer we can recv */ |
| 7193 | } |
| 7194 | return 0; |
| 7195 | |
| 7196 | rcv: |
| 7197 | |
| 7198 | /* we return a "res" object */ |
| 7199 | lua_newtable(L); |
| 7200 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7201 | lua_pushstring(L, "body"); |
William Lallemand | d1187eb | 2021-11-02 10:40:06 +0100 | [diff] [blame] | 7202 | luaL_buffinit(L, &hlua_hc->b); |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7203 | |
William Lallemand | 933fe39 | 2021-11-04 09:45:58 +0100 | [diff] [blame] | 7204 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7205 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_rcv_yield, TICK_ETERNITY, 0)); |
| 7206 | |
| 7207 | return 1; |
| 7208 | } |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7209 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7210 | /* |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7211 | * Send an HTTP request and wait for a response |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7212 | */ |
| 7213 | |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7214 | __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] | 7215 | { |
| 7216 | struct hlua_httpclient *hlua_hc; |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7217 | struct http_hdr *hdrs = NULL; |
| 7218 | struct http_hdr *hdrs_i = NULL; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7219 | struct hlua *hlua; |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7220 | const char *url_str = NULL; |
William Lallemand | dec25c3 | 2021-10-25 19:48:37 +0200 | [diff] [blame] | 7221 | const char *body_str = NULL; |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7222 | size_t buf_len; |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7223 | int ret; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7224 | |
| 7225 | hlua = hlua_gethlua(L); |
| 7226 | |
| 7227 | if (!hlua || !hlua->task) |
| 7228 | WILL_LJMP(luaL_error(L, "The 'get' function is only allowed in " |
| 7229 | "'frontend', 'backend' or 'task'")); |
| 7230 | |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7231 | if (lua_gettop(L) != 2 || lua_type(L, -1) != LUA_TTABLE) |
| 7232 | WILL_LJMP(luaL_error(L, "'get' needs a table as argument")); |
| 7233 | |
| 7234 | ret = lua_getfield(L, -1, "url"); |
| 7235 | if (ret == LUA_TSTRING) { |
| 7236 | url_str = lua_tostring(L, -1); |
| 7237 | } |
| 7238 | lua_pop(L, 1); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7239 | |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7240 | ret = lua_getfield(L, -1, "headers"); |
| 7241 | if (ret == LUA_TTABLE) { |
| 7242 | hdrs = hlua_httpclient_table_to_hdrs(L); |
| 7243 | } |
| 7244 | lua_pop(L, 1); |
William Lallemand | 79416cb | 2021-09-24 14:51:44 +0200 | [diff] [blame] | 7245 | |
William Lallemand | dec25c3 | 2021-10-25 19:48:37 +0200 | [diff] [blame] | 7246 | ret = lua_getfield(L, -1, "body"); |
| 7247 | if (ret == LUA_TSTRING) { |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7248 | body_str = lua_tolstring(L, -1, &buf_len); |
William Lallemand | dec25c3 | 2021-10-25 19:48:37 +0200 | [diff] [blame] | 7249 | } |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7250 | |
William Lallemand | dec25c3 | 2021-10-25 19:48:37 +0200 | [diff] [blame] | 7251 | lua_pop(L, 1); |
| 7252 | |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7253 | if (!url_str) { |
| 7254 | WILL_LJMP(luaL_error(L, "'get' need a 'url' argument")); |
| 7255 | return 0; |
| 7256 | } |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7257 | |
| 7258 | hlua_hc = hlua_checkhttpclient(L, 1); |
| 7259 | |
| 7260 | hlua_hc->hc->req.url = istdup(ist(url_str)); |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7261 | hlua_hc->hc->req.meth = meth; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7262 | |
| 7263 | /* update the httpclient callbacks */ |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7264 | hlua_hc->hc->ops.res_stline = hlua_httpclient_cb; |
| 7265 | hlua_hc->hc->ops.res_headers = hlua_httpclient_cb; |
| 7266 | hlua_hc->hc->ops.res_payload = hlua_httpclient_cb; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7267 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7268 | /* a body is available, it will use the request callback */ |
| 7269 | if (body_str) { |
| 7270 | hlua_hc->hc->ops.req_payload = hlua_httpclient_cb; |
| 7271 | } |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7272 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7273 | ret = httpclient_req_gen(hlua_hc->hc, hlua_hc->hc->req.url, meth, hdrs, IST_NULL); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7274 | |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7275 | /* free the temporary headers array */ |
| 7276 | hdrs_i = hdrs; |
| 7277 | while (hdrs_i && isttest(hdrs_i->n)) { |
| 7278 | istfree(&hdrs_i->n); |
| 7279 | istfree(&hdrs_i->v); |
| 7280 | hdrs_i++; |
| 7281 | } |
| 7282 | ha_free(&hdrs); |
| 7283 | |
| 7284 | |
William Lallemand | 6137a9e | 2021-10-26 15:01:53 +0200 | [diff] [blame] | 7285 | if (ret != ERR_NONE) { |
| 7286 | WILL_LJMP(luaL_error(L, "Can't generate the HTTP request")); |
| 7287 | return 0; |
| 7288 | } |
| 7289 | |
William Lallemand | 6137a9e | 2021-10-26 15:01:53 +0200 | [diff] [blame] | 7290 | httpclient_start(hlua_hc->hc); |
| 7291 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7292 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_snd_yield, TICK_ETERNITY, 0)); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7293 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7294 | return 0; |
| 7295 | } |
| 7296 | |
| 7297 | /* |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7298 | * Sends an HTTP HEAD request and wait for a response |
| 7299 | * |
| 7300 | * httpclient:head(url, headers, payload) |
| 7301 | */ |
| 7302 | __LJMP static int hlua_httpclient_head(lua_State *L) |
| 7303 | { |
| 7304 | return hlua_httpclient_send(L, HTTP_METH_HEAD); |
| 7305 | } |
| 7306 | |
| 7307 | /* |
| 7308 | * Send an HTTP GET request and wait for a response |
| 7309 | * |
| 7310 | * httpclient:get(url, headers, payload) |
| 7311 | */ |
| 7312 | __LJMP static int hlua_httpclient_get(lua_State *L) |
| 7313 | { |
| 7314 | return hlua_httpclient_send(L, HTTP_METH_GET); |
| 7315 | |
| 7316 | } |
| 7317 | |
| 7318 | /* |
| 7319 | * Sends an HTTP PUT request and wait for a response |
| 7320 | * |
| 7321 | * httpclient:put(url, headers, payload) |
| 7322 | */ |
| 7323 | __LJMP static int hlua_httpclient_put(lua_State *L) |
| 7324 | { |
| 7325 | return hlua_httpclient_send(L, HTTP_METH_PUT); |
| 7326 | } |
| 7327 | |
| 7328 | /* |
| 7329 | * Send an HTTP POST request and wait for a response |
| 7330 | * |
| 7331 | * httpclient:post(url, headers, payload) |
| 7332 | */ |
| 7333 | __LJMP static int hlua_httpclient_post(lua_State *L) |
| 7334 | { |
| 7335 | return hlua_httpclient_send(L, HTTP_METH_POST); |
| 7336 | } |
| 7337 | |
| 7338 | |
| 7339 | /* |
| 7340 | * Sends an HTTP DELETE request and wait for a response |
| 7341 | * |
| 7342 | * httpclient:delete(url, headers, payload) |
| 7343 | */ |
| 7344 | __LJMP static int hlua_httpclient_delete(lua_State *L) |
| 7345 | { |
| 7346 | return hlua_httpclient_send(L, HTTP_METH_DELETE); |
| 7347 | } |
| 7348 | |
| 7349 | /* |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7350 | * |
| 7351 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7352 | * Class TXN |
| 7353 | * |
| 7354 | * |
| 7355 | */ |
| 7356 | |
| 7357 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7358 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7359 | */ |
| 7360 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 7361 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7362 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7363 | } |
| 7364 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7365 | __LJMP static int hlua_set_var(lua_State *L) |
| 7366 | { |
| 7367 | struct hlua_txn *htxn; |
| 7368 | const char *name; |
| 7369 | size_t len; |
| 7370 | struct sample smp; |
| 7371 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 7372 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 7373 | 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] | 7374 | |
| 7375 | /* It is useles to retrieve the stream, but this function |
| 7376 | * runs only in a stream context. |
| 7377 | */ |
| 7378 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7379 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 7380 | |
| 7381 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 7382 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7383 | hlua_lua2smp(L, 3, &smp); |
| 7384 | |
| 7385 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 7386 | 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] | 7387 | |
| 7388 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 7389 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 7390 | else |
| 7391 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 7392 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 7393 | return 1; |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7394 | } |
| 7395 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 7396 | __LJMP static int hlua_unset_var(lua_State *L) |
| 7397 | { |
| 7398 | struct hlua_txn *htxn; |
| 7399 | const char *name; |
| 7400 | size_t len; |
| 7401 | struct sample smp; |
| 7402 | |
| 7403 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 7404 | |
| 7405 | /* It is useles to retrieve the stream, but this function |
| 7406 | * runs only in a stream context. |
| 7407 | */ |
| 7408 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7409 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 7410 | |
| 7411 | /* Unset the variable. */ |
| 7412 | 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] | 7413 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 7414 | return 1; |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 7415 | } |
| 7416 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7417 | __LJMP static int hlua_get_var(lua_State *L) |
| 7418 | { |
| 7419 | struct hlua_txn *htxn; |
| 7420 | const char *name; |
| 7421 | size_t len; |
| 7422 | struct sample smp; |
| 7423 | |
| 7424 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 7425 | |
| 7426 | /* It is useles to retrieve the stream, but this function |
| 7427 | * runs only in a stream context. |
| 7428 | */ |
| 7429 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7430 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 7431 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 7432 | 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] | 7433 | if (!vars_get_by_name(name, len, &smp, NULL)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7434 | lua_pushnil(L); |
| 7435 | return 1; |
| 7436 | } |
| 7437 | |
| 7438 | return hlua_smp2lua(L, &smp); |
| 7439 | } |
| 7440 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7441 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7442 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7443 | struct hlua *hlua; |
| 7444 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7445 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 7446 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7447 | /* It is useles to retrieve the stream, but this function |
| 7448 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7449 | */ |
| 7450 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7451 | |
| 7452 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7453 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7454 | if (!hlua) |
| 7455 | return 0; |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7456 | |
| 7457 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 7458 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7459 | |
| 7460 | /* Get and store new value. */ |
| 7461 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 7462 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 7463 | |
| 7464 | return 0; |
| 7465 | } |
| 7466 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7467 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7468 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7469 | struct hlua *hlua; |
| 7470 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7471 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 7472 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7473 | /* It is useles to retrieve the stream, but this function |
| 7474 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7475 | */ |
| 7476 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7477 | |
| 7478 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7479 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7480 | if (!hlua) { |
| 7481 | lua_pushnil(L); |
| 7482 | return 1; |
| 7483 | } |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7484 | |
| 7485 | /* Push configuration index in the stack. */ |
| 7486 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 7487 | |
| 7488 | return 1; |
| 7489 | } |
| 7490 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7491 | /* Create stack entry containing a class TXN. This function |
| 7492 | * return 0 if the stack does not contains free slots, |
| 7493 | * otherwise it returns 1. |
| 7494 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 7495 | 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] | 7496 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7497 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7498 | |
| 7499 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 7500 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7501 | return 0; |
| 7502 | |
| 7503 | /* NOTE: The allocation never fails. The failure |
| 7504 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7505 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7506 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 7507 | /* Create the object: obj[0] = userdata. */ |
| 7508 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7509 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 7510 | lua_rawseti(L, -2, 0); |
| 7511 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7512 | htxn->s = s; |
| 7513 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 7514 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 7515 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7516 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7517 | /* Create the "f" field that contains a list of fetches. */ |
| 7518 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 7519 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7520 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7521 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7522 | |
| 7523 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 7524 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 7525 | 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] | 7526 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7527 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7528 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7529 | /* Create the "c" field that contains a list of converters. */ |
| 7530 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7531 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7532 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7533 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7534 | |
| 7535 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 7536 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 7537 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7538 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7539 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7540 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7541 | /* Create the "req" field that contains the request channel object. */ |
| 7542 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 7543 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7544 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7545 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7546 | |
| 7547 | /* Create the "res" field that contains the response channel object. */ |
| 7548 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 7549 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7550 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7551 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7552 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7553 | /* Creates the HTTP object is the current proxy allows http. */ |
| 7554 | lua_pushstring(L, "http"); |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 7555 | if (IS_HTX_STRM(s)) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7556 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7557 | return 0; |
| 7558 | } |
| 7559 | else |
| 7560 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7561 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7562 | |
Christopher Faulet | 78c3547 | 2020-02-26 17:14:08 +0100 | [diff] [blame] | 7563 | if ((htxn->flags & HLUA_TXN_CTX_MASK) == HLUA_TXN_FLT_CTX) { |
| 7564 | /* HTTPMessage object are created when a lua TXN is created from |
| 7565 | * a filter context only |
| 7566 | */ |
| 7567 | |
| 7568 | /* Creates the HTTP-Request object is the current proxy allows http. */ |
| 7569 | lua_pushstring(L, "http_req"); |
| 7570 | if (p->mode == PR_MODE_HTTP) { |
| 7571 | if (!hlua_http_msg_new(L, &s->txn->req)) |
| 7572 | return 0; |
| 7573 | } |
| 7574 | else |
| 7575 | lua_pushnil(L); |
| 7576 | lua_rawset(L, -3); |
| 7577 | |
| 7578 | /* Creates the HTTP-Response object is the current proxy allows http. */ |
| 7579 | lua_pushstring(L, "http_res"); |
| 7580 | if (p->mode == PR_MODE_HTTP) { |
| 7581 | if (!hlua_http_msg_new(L, &s->txn->rsp)) |
| 7582 | return 0; |
| 7583 | } |
| 7584 | else |
| 7585 | lua_pushnil(L); |
| 7586 | lua_rawset(L, -3); |
| 7587 | } |
| 7588 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7589 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 7590 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 7591 | lua_setmetatable(L, -2); |
| 7592 | |
| 7593 | return 1; |
| 7594 | } |
| 7595 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 7596 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 7597 | { |
| 7598 | const char *msg; |
| 7599 | struct hlua_txn *htxn; |
| 7600 | |
| 7601 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 7602 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7603 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7604 | |
| 7605 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 7606 | return 0; |
| 7607 | } |
| 7608 | |
| 7609 | __LJMP static int hlua_txn_log(lua_State *L) |
| 7610 | { |
| 7611 | int level; |
| 7612 | const char *msg; |
| 7613 | struct hlua_txn *htxn; |
| 7614 | |
| 7615 | MAY_LJMP(check_args(L, 3, "log")); |
| 7616 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7617 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7618 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 7619 | |
| 7620 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 7621 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 7622 | |
| 7623 | hlua_sendlog(htxn->s->be, level, msg); |
| 7624 | return 0; |
| 7625 | } |
| 7626 | |
| 7627 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 7628 | { |
| 7629 | const char *msg; |
| 7630 | struct hlua_txn *htxn; |
| 7631 | |
| 7632 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 7633 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7634 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7635 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 7636 | return 0; |
| 7637 | } |
| 7638 | |
| 7639 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 7640 | { |
| 7641 | const char *msg; |
| 7642 | struct hlua_txn *htxn; |
| 7643 | |
| 7644 | MAY_LJMP(check_args(L, 2, "Info")); |
| 7645 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7646 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7647 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 7648 | return 0; |
| 7649 | } |
| 7650 | |
| 7651 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 7652 | { |
| 7653 | const char *msg; |
| 7654 | struct hlua_txn *htxn; |
| 7655 | |
| 7656 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 7657 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7658 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7659 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 7660 | return 0; |
| 7661 | } |
| 7662 | |
| 7663 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 7664 | { |
| 7665 | const char *msg; |
| 7666 | struct hlua_txn *htxn; |
| 7667 | |
| 7668 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 7669 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7670 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7671 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 7672 | return 0; |
| 7673 | } |
| 7674 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7675 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 7676 | { |
| 7677 | struct hlua_txn *htxn; |
| 7678 | int ll; |
| 7679 | |
| 7680 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 7681 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7682 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7683 | |
| 7684 | if (ll < 0 || ll > 7) |
| 7685 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 7686 | |
| 7687 | htxn->s->logs.level = ll; |
| 7688 | return 0; |
| 7689 | } |
| 7690 | |
| 7691 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 7692 | { |
| 7693 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7694 | int tos; |
| 7695 | |
| 7696 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 7697 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7698 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7699 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 7700 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7701 | return 0; |
| 7702 | } |
| 7703 | |
| 7704 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 7705 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7706 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7707 | int mark; |
| 7708 | |
| 7709 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 7710 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7711 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7712 | |
Lukas Tribus | 579e3e3 | 2019-08-11 18:03:45 +0200 | [diff] [blame] | 7713 | conn_set_mark(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7714 | return 0; |
| 7715 | } |
| 7716 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 7717 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 7718 | { |
| 7719 | struct hlua_txn *htxn; |
| 7720 | |
| 7721 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 7722 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7723 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 7724 | return 0; |
| 7725 | } |
| 7726 | |
| 7727 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 7728 | { |
| 7729 | struct hlua_txn *htxn; |
| 7730 | |
| 7731 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 7732 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7733 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 7734 | return 0; |
| 7735 | } |
| 7736 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7737 | /* 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] | 7738 | * 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] | 7739 | * message and terminate the transaction. It returns 1 on success and 0 on |
| 7740 | * error. The Reply must be on top of the stack. |
| 7741 | */ |
| 7742 | __LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s) |
| 7743 | { |
| 7744 | struct htx *htx; |
| 7745 | struct htx_sl *sl; |
| 7746 | struct h1m h1m; |
| 7747 | const char *status, *reason, *body; |
| 7748 | size_t status_len, reason_len, body_len; |
| 7749 | int ret, code, flags; |
| 7750 | |
| 7751 | code = 200; |
| 7752 | status = "200"; |
| 7753 | status_len = 3; |
| 7754 | ret = lua_getfield(L, -1, "status"); |
| 7755 | if (ret == LUA_TNUMBER) { |
| 7756 | code = lua_tointeger(L, -1); |
| 7757 | status = lua_tolstring(L, -1, &status_len); |
| 7758 | } |
| 7759 | lua_pop(L, 1); |
| 7760 | |
| 7761 | reason = http_get_reason(code); |
| 7762 | reason_len = strlen(reason); |
| 7763 | ret = lua_getfield(L, -1, "reason"); |
| 7764 | if (ret == LUA_TSTRING) |
| 7765 | reason = lua_tolstring(L, -1, &reason_len); |
| 7766 | lua_pop(L, 1); |
| 7767 | |
| 7768 | body = NULL; |
| 7769 | body_len = 0; |
| 7770 | ret = lua_getfield(L, -1, "body"); |
| 7771 | if (ret == LUA_TSTRING) |
| 7772 | body = lua_tolstring(L, -1, &body_len); |
| 7773 | lua_pop(L, 1); |
| 7774 | |
| 7775 | /* Prepare the response before inserting the headers */ |
| 7776 | h1m_init_res(&h1m); |
| 7777 | htx = htx_from_buf(&s->res.buf); |
| 7778 | channel_htx_truncate(&s->res, htx); |
| 7779 | if (s->txn->req.flags & HTTP_MSGF_VER_11) { |
| 7780 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 7781 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), |
| 7782 | ist2(status, status_len), ist2(reason, reason_len)); |
| 7783 | } |
| 7784 | else { |
| 7785 | flags = HTX_SL_F_IS_RESP; |
| 7786 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), |
| 7787 | ist2(status, status_len), ist2(reason, reason_len)); |
| 7788 | } |
| 7789 | if (!sl) |
| 7790 | goto fail; |
| 7791 | sl->info.res.status = code; |
| 7792 | |
| 7793 | /* Push in the stack the "headers" entry. */ |
| 7794 | ret = lua_getfield(L, -1, "headers"); |
| 7795 | if (ret != LUA_TTABLE) |
| 7796 | goto skip_headers; |
| 7797 | |
| 7798 | lua_pushnil(L); |
| 7799 | while (lua_next(L, -2) != 0) { |
| 7800 | struct ist name, value; |
| 7801 | const char *n, *v; |
| 7802 | size_t nlen, vlen; |
| 7803 | |
| 7804 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 7805 | /* Skip element if the key is not a string or if the value is not a table */ |
| 7806 | goto next_hdr; |
| 7807 | } |
| 7808 | |
| 7809 | n = lua_tolstring(L, -2, &nlen); |
| 7810 | name = ist2(n, nlen); |
| 7811 | if (isteqi(name, ist("content-length"))) { |
| 7812 | /* Always skip content-length header. It will be added |
| 7813 | * later with the correct len |
| 7814 | */ |
| 7815 | goto next_hdr; |
| 7816 | } |
| 7817 | |
| 7818 | /* Loop on header's values */ |
| 7819 | lua_pushnil(L); |
| 7820 | while (lua_next(L, -2)) { |
| 7821 | if (!lua_isstring(L, -1)) { |
| 7822 | /* Skip the value if it is not a string */ |
| 7823 | goto next_value; |
| 7824 | } |
| 7825 | |
| 7826 | v = lua_tolstring(L, -1, &vlen); |
| 7827 | value = ist2(v, vlen); |
| 7828 | |
| 7829 | if (isteqi(name, ist("transfer-encoding"))) |
| 7830 | h1_parse_xfer_enc_header(&h1m, value); |
| 7831 | if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen))) |
| 7832 | goto fail; |
| 7833 | |
| 7834 | next_value: |
| 7835 | lua_pop(L, 1); |
| 7836 | } |
| 7837 | |
| 7838 | next_hdr: |
| 7839 | lua_pop(L, 1); |
| 7840 | } |
| 7841 | skip_headers: |
| 7842 | lua_pop(L, 1); |
| 7843 | |
| 7844 | /* Update h1m flags: CLEN is set if CHNK is not present */ |
| 7845 | if (!(h1m.flags & H1_MF_CHNK)) { |
| 7846 | const char *clen = ultoa(body_len); |
| 7847 | |
| 7848 | h1m.flags |= H1_MF_CLEN; |
| 7849 | if (!htx_add_header(htx, ist("content-length"), ist(clen))) |
| 7850 | goto fail; |
| 7851 | } |
| 7852 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 7853 | h1m.flags |= H1_MF_XFER_LEN; |
| 7854 | |
| 7855 | /* Update HTX start-line flags */ |
| 7856 | if (h1m.flags & H1_MF_XFER_ENC) |
| 7857 | flags |= HTX_SL_F_XFER_ENC; |
| 7858 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 7859 | flags |= HTX_SL_F_XFER_LEN; |
| 7860 | if (h1m.flags & H1_MF_CHNK) |
| 7861 | flags |= HTX_SL_F_CHNK; |
| 7862 | else if (h1m.flags & H1_MF_CLEN) |
| 7863 | flags |= HTX_SL_F_CLEN; |
| 7864 | if (h1m.body_len == 0) |
| 7865 | flags |= HTX_SL_F_BODYLESS; |
| 7866 | } |
| 7867 | sl->flags |= flags; |
| 7868 | |
| 7869 | |
| 7870 | if (!htx_add_endof(htx, HTX_BLK_EOH) || |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 7871 | (body_len && !htx_add_data_atonce(htx, ist2(body, body_len)))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7872 | goto fail; |
| 7873 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 7874 | htx->flags |= HTX_FL_EOM; |
| 7875 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7876 | /* Now, forward the response and terminate the transaction */ |
| 7877 | s->txn->status = code; |
| 7878 | htx_to_buf(htx, &s->res.buf); |
| 7879 | if (!http_forward_proxy_resp(s, 1)) |
| 7880 | goto fail; |
| 7881 | |
| 7882 | return 1; |
| 7883 | |
| 7884 | fail: |
| 7885 | channel_htx_truncate(&s->res, htx); |
| 7886 | return 0; |
| 7887 | } |
| 7888 | |
| 7889 | /* Terminate a transaction if called from a lua action. For TCP streams, |
| 7890 | * processing is just aborted. Nothing is returned to the client and all |
| 7891 | * arguments are ignored. For HTTP streams, if a reply is passed as argument, it |
| 7892 | * is forwarded to the client before terminating the transaction. On success, |
| 7893 | * the function exits with ACT_RET_DONE code. If an error occurred, it exits |
| 7894 | * with ACT_RET_ERR code. If this function is not called from a lua action, it |
| 7895 | * just exits without any processing. |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 7896 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 7897 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 7898 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 7899 | struct hlua_txn *htxn; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7900 | struct stream *s; |
| 7901 | int finst; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 7902 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 7903 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 7904 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7905 | /* If the flags NOTERM is set, we cannot terminate the session, so we |
| 7906 | * just end the execution of the current lua code. */ |
| 7907 | if (htxn->flags & HLUA_TXN_NOTERM) |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 7908 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 7909 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7910 | s = htxn->s; |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 7911 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7912 | struct channel *req = &s->req; |
| 7913 | struct channel *res = &s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 7914 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7915 | channel_auto_read(req); |
| 7916 | channel_abort(req); |
| 7917 | channel_auto_close(req); |
| 7918 | channel_erase(req); |
| 7919 | |
| 7920 | res->wex = tick_add_ifset(now_ms, res->wto); |
| 7921 | channel_auto_read(res); |
| 7922 | channel_auto_close(res); |
| 7923 | channel_shutr_now(res); |
| 7924 | |
| 7925 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D); |
| 7926 | goto done; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 7927 | } |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 7928 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7929 | if (lua_gettop(L) == 1 || !lua_istable(L, 2)) { |
| 7930 | /* No reply or invalid reply */ |
| 7931 | s->txn->status = 0; |
| 7932 | http_reply_and_close(s, 0, NULL); |
| 7933 | } |
| 7934 | else { |
| 7935 | /* Remove extra args to have the reply on top of the stack */ |
| 7936 | if (lua_gettop(L) > 2) |
| 7937 | lua_pop(L, lua_gettop(L) - 2); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 7938 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7939 | if (!hlua_txn_forward_reply(L, s)) { |
| 7940 | if (!(s->flags & SF_ERR_MASK)) |
| 7941 | s->flags |= SF_ERR_PRXCOND; |
| 7942 | lua_pushinteger(L, ACT_RET_ERR); |
| 7943 | WILL_LJMP(hlua_done(L)); |
| 7944 | return 0; /* Never reached */ |
| 7945 | } |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 7946 | } |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 7947 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7948 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H); |
| 7949 | if (htxn->dir == SMP_OPT_DIR_REQ) { |
| 7950 | /* let's log the request time */ |
| 7951 | s->logs.tv_request = now; |
| 7952 | 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] | 7953 | _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7954 | } |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 7955 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7956 | done: |
| 7957 | if (!(s->flags & SF_ERR_MASK)) |
| 7958 | s->flags |= SF_ERR_LOCAL; |
| 7959 | if (!(s->flags & SF_FINST_MASK)) |
| 7960 | s->flags |= finst; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 7961 | |
Christopher Faulet | e48d1dc | 2021-08-13 14:11:17 +0200 | [diff] [blame] | 7962 | if ((htxn->flags & HLUA_TXN_CTX_MASK) == HLUA_TXN_FLT_CTX) |
| 7963 | lua_pushinteger(L, -1); |
| 7964 | else |
| 7965 | lua_pushinteger(L, ACT_RET_ABRT); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 7966 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 7967 | return 0; |
| 7968 | } |
| 7969 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7970 | /* |
| 7971 | * |
| 7972 | * |
| 7973 | * Class REPLY |
| 7974 | * |
| 7975 | * |
| 7976 | */ |
| 7977 | |
| 7978 | /* Pushes the TXN reply onto the top of the stack. If the stask does not have a |
| 7979 | * free slots, the function fails and returns 0; |
| 7980 | */ |
| 7981 | static int hlua_txn_reply_new(lua_State *L) |
| 7982 | { |
| 7983 | struct hlua_txn *htxn; |
| 7984 | const char *reason, *body = NULL; |
| 7985 | int ret, status; |
| 7986 | |
| 7987 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 7988 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7989 | hlua_pusherror(L, "txn object is not an HTTP transaction."); |
| 7990 | WILL_LJMP(lua_error(L)); |
| 7991 | } |
| 7992 | |
| 7993 | /* Default value */ |
| 7994 | status = 200; |
| 7995 | reason = http_get_reason(status); |
| 7996 | |
| 7997 | if (lua_istable(L, 2)) { |
| 7998 | /* load status and reason from the table argument at index 2 */ |
| 7999 | ret = lua_getfield(L, 2, "status"); |
| 8000 | if (ret == LUA_TNIL) |
| 8001 | goto reason; |
| 8002 | else if (ret != LUA_TNUMBER) { |
| 8003 | /* invalid status: ignore the reason */ |
| 8004 | goto body; |
| 8005 | } |
| 8006 | status = lua_tointeger(L, -1); |
| 8007 | |
| 8008 | reason: |
| 8009 | lua_pop(L, 1); /* restore the stack: remove status */ |
| 8010 | ret = lua_getfield(L, 2, "reason"); |
| 8011 | if (ret == LUA_TSTRING) |
| 8012 | reason = lua_tostring(L, -1); |
| 8013 | |
| 8014 | body: |
| 8015 | lua_pop(L, 1); /* restore the stack: remove invalid status or reason */ |
| 8016 | ret = lua_getfield(L, 2, "body"); |
| 8017 | if (ret == LUA_TSTRING) |
| 8018 | body = lua_tostring(L, -1); |
| 8019 | lua_pop(L, 1); /* restore the stack: remove body */ |
| 8020 | } |
| 8021 | |
| 8022 | /* Create the Reply table */ |
| 8023 | lua_newtable(L); |
| 8024 | |
| 8025 | /* Add status element */ |
| 8026 | lua_pushstring(L, "status"); |
| 8027 | lua_pushinteger(L, status); |
| 8028 | lua_settable(L, -3); |
| 8029 | |
| 8030 | /* Add reason element */ |
| 8031 | reason = http_get_reason(status); |
| 8032 | lua_pushstring(L, "reason"); |
| 8033 | lua_pushstring(L, reason); |
| 8034 | lua_settable(L, -3); |
| 8035 | |
| 8036 | /* Add body element, nil if undefined */ |
| 8037 | lua_pushstring(L, "body"); |
| 8038 | if (body) |
| 8039 | lua_pushstring(L, body); |
| 8040 | else |
| 8041 | lua_pushnil(L); |
| 8042 | lua_settable(L, -3); |
| 8043 | |
| 8044 | /* Add headers element */ |
| 8045 | lua_pushstring(L, "headers"); |
| 8046 | lua_newtable(L); |
| 8047 | |
| 8048 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 8049 | if (lua_istable(L, 2)) { |
| 8050 | /* load headers from the table argument at index 2. If it is a table, copy it. */ |
| 8051 | ret = lua_getfield(L, 2, "headers"); |
| 8052 | if (ret == LUA_TTABLE) { |
| 8053 | /* stack: [ ... <headers:table>, <table> ] */ |
| 8054 | lua_pushnil(L); |
| 8055 | while (lua_next(L, -2) != 0) { |
| 8056 | /* stack: [ ... <headers:table>, <table>, k, v] */ |
| 8057 | if (!lua_isstring(L, -1) && !lua_istable(L, -1)) { |
| 8058 | /* invalid value type, skip it */ |
| 8059 | lua_pop(L, 1); |
| 8060 | continue; |
| 8061 | } |
| 8062 | |
| 8063 | |
| 8064 | /* Duplicate the key and swap it with the value. */ |
| 8065 | lua_pushvalue(L, -2); |
| 8066 | lua_insert(L, -2); |
| 8067 | /* stack: [ ... <headers:table>, <table>, k, k, v ] */ |
| 8068 | |
| 8069 | lua_newtable(L); |
| 8070 | lua_insert(L, -2); |
| 8071 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */ |
| 8072 | |
| 8073 | if (lua_isstring(L, -1)) { |
| 8074 | /* push the value in the inner table */ |
| 8075 | lua_rawseti(L, -2, 1); |
| 8076 | } |
| 8077 | else { /* table */ |
| 8078 | lua_pushnil(L); |
| 8079 | while (lua_next(L, -2) != 0) { |
| 8080 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */ |
| 8081 | if (!lua_isstring(L, -1)) { |
| 8082 | /* invalid value type, skip it*/ |
| 8083 | lua_pop(L, 1); |
| 8084 | continue; |
| 8085 | } |
| 8086 | /* push the value in the inner table */ |
| 8087 | lua_rawseti(L, -4, lua_rawlen(L, -4) + 1); |
| 8088 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */ |
| 8089 | } |
| 8090 | lua_pop(L, 1); |
| 8091 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */ |
| 8092 | } |
| 8093 | |
| 8094 | /* push (k,v) on the stack in the headers table: |
| 8095 | * stack: [ ... <headers:table>, <table>, k, k, v ] |
| 8096 | */ |
| 8097 | lua_settable(L, -5); |
| 8098 | /* stack: [ ... <headers:table>, <table>, k ] */ |
| 8099 | } |
| 8100 | } |
| 8101 | lua_pop(L, 1); |
| 8102 | } |
| 8103 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 8104 | lua_settable(L, -3); |
| 8105 | /* stack: [ txn, <Arg:table>, <Reply:table> ] */ |
| 8106 | |
| 8107 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 8108 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref); |
| 8109 | lua_setmetatable(L, -2); |
| 8110 | return 1; |
| 8111 | } |
| 8112 | |
| 8113 | /* Set the reply status code, and optionally the reason. If no reason is |
| 8114 | * provided, the default one corresponding to the status code is used. |
| 8115 | */ |
| 8116 | __LJMP static int hlua_txn_reply_set_status(lua_State *L) |
| 8117 | { |
| 8118 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 8119 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 8120 | |
| 8121 | /* First argument (self) must be a table */ |
| 8122 | luaL_checktype(L, 1, LUA_TTABLE); |
| 8123 | |
| 8124 | if (status < 100 || status > 599) { |
| 8125 | lua_pushboolean(L, 0); |
| 8126 | return 1; |
| 8127 | } |
| 8128 | if (!reason) |
| 8129 | reason = http_get_reason(status); |
| 8130 | |
| 8131 | lua_pushinteger(L, status); |
| 8132 | lua_setfield(L, 1, "status"); |
| 8133 | |
| 8134 | lua_pushstring(L, reason); |
| 8135 | lua_setfield(L, 1, "reason"); |
| 8136 | |
| 8137 | lua_pushboolean(L, 1); |
| 8138 | return 1; |
| 8139 | } |
| 8140 | |
| 8141 | /* Add a header into the reply object. Each header name is associated to an |
| 8142 | * array of values in the "headers" table. If the header name is not found, a |
| 8143 | * new entry is created. |
| 8144 | */ |
| 8145 | __LJMP static int hlua_txn_reply_add_header(lua_State *L) |
| 8146 | { |
| 8147 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8148 | const char *value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 8149 | int ret; |
| 8150 | |
| 8151 | /* First argument (self) must be a table */ |
| 8152 | luaL_checktype(L, 1, LUA_TTABLE); |
| 8153 | |
| 8154 | /* Push in the stack the "headers" entry. */ |
| 8155 | ret = lua_getfield(L, 1, "headers"); |
| 8156 | if (ret != LUA_TTABLE) { |
| 8157 | hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret)); |
| 8158 | WILL_LJMP(lua_error(L)); |
| 8159 | } |
| 8160 | |
| 8161 | /* check if the header is already registered. If not, register it. */ |
| 8162 | ret = lua_getfield(L, -1, name); |
| 8163 | if (ret == LUA_TNIL) { |
| 8164 | /* Entry not found. */ |
| 8165 | lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */ |
| 8166 | |
| 8167 | /* Insert the new header name in the array in the top of the stack. |
| 8168 | * It left the new array in the top of the stack. |
| 8169 | */ |
| 8170 | lua_newtable(L); |
| 8171 | lua_pushstring(L, name); |
| 8172 | lua_pushvalue(L, -2); |
| 8173 | lua_settable(L, -4); |
| 8174 | } |
| 8175 | else if (ret != LUA_TTABLE) { |
| 8176 | hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret)); |
| 8177 | WILL_LJMP(lua_error(L)); |
| 8178 | } |
| 8179 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8180 | /* Now the top of thestack is an array of values. We push |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8181 | * the header value as new entry. |
| 8182 | */ |
| 8183 | lua_pushstring(L, value); |
| 8184 | ret = lua_rawlen(L, -2); |
| 8185 | lua_rawseti(L, -2, ret + 1); |
| 8186 | |
| 8187 | lua_pushboolean(L, 1); |
| 8188 | return 1; |
| 8189 | } |
| 8190 | |
| 8191 | /* Remove all occurrences of a given header name. */ |
| 8192 | __LJMP static int hlua_txn_reply_del_header(lua_State *L) |
| 8193 | { |
| 8194 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8195 | int ret; |
| 8196 | |
| 8197 | /* First argument (self) must be a table */ |
| 8198 | luaL_checktype(L, 1, LUA_TTABLE); |
| 8199 | |
| 8200 | /* Push in the stack the "headers" entry. */ |
| 8201 | ret = lua_getfield(L, 1, "headers"); |
| 8202 | if (ret != LUA_TTABLE) { |
| 8203 | hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret)); |
| 8204 | WILL_LJMP(lua_error(L)); |
| 8205 | } |
| 8206 | |
| 8207 | lua_pushstring(L, name); |
| 8208 | lua_pushnil(L); |
| 8209 | lua_settable(L, -3); |
| 8210 | |
| 8211 | lua_pushboolean(L, 1); |
| 8212 | return 1; |
| 8213 | } |
| 8214 | |
| 8215 | /* Set the reply's body. Overwrite any existing entry. */ |
| 8216 | __LJMP static int hlua_txn_reply_set_body(lua_State *L) |
| 8217 | { |
| 8218 | const char *payload = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8219 | |
| 8220 | /* First argument (self) must be a table */ |
| 8221 | luaL_checktype(L, 1, LUA_TTABLE); |
| 8222 | |
| 8223 | lua_pushstring(L, payload); |
| 8224 | lua_setfield(L, 1, "body"); |
| 8225 | |
| 8226 | lua_pushboolean(L, 1); |
| 8227 | return 1; |
| 8228 | } |
| 8229 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 8230 | __LJMP static int hlua_log(lua_State *L) |
| 8231 | { |
| 8232 | int level; |
| 8233 | const char *msg; |
| 8234 | |
| 8235 | MAY_LJMP(check_args(L, 2, "log")); |
| 8236 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 8237 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8238 | |
| 8239 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 8240 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 8241 | |
| 8242 | hlua_sendlog(NULL, level, msg); |
| 8243 | return 0; |
| 8244 | } |
| 8245 | |
| 8246 | __LJMP static int hlua_log_debug(lua_State *L) |
| 8247 | { |
| 8248 | const char *msg; |
| 8249 | |
| 8250 | MAY_LJMP(check_args(L, 1, "debug")); |
| 8251 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8252 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 8253 | return 0; |
| 8254 | } |
| 8255 | |
| 8256 | __LJMP static int hlua_log_info(lua_State *L) |
| 8257 | { |
| 8258 | const char *msg; |
| 8259 | |
| 8260 | MAY_LJMP(check_args(L, 1, "info")); |
| 8261 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8262 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 8263 | return 0; |
| 8264 | } |
| 8265 | |
| 8266 | __LJMP static int hlua_log_warning(lua_State *L) |
| 8267 | { |
| 8268 | const char *msg; |
| 8269 | |
| 8270 | MAY_LJMP(check_args(L, 1, "warning")); |
| 8271 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8272 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 8273 | return 0; |
| 8274 | } |
| 8275 | |
| 8276 | __LJMP static int hlua_log_alert(lua_State *L) |
| 8277 | { |
| 8278 | const char *msg; |
| 8279 | |
| 8280 | MAY_LJMP(check_args(L, 1, "alert")); |
| 8281 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8282 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8283 | return 0; |
| 8284 | } |
| 8285 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8286 | __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] | 8287 | { |
| 8288 | int wakeup_ms = lua_tointeger(L, -1); |
Willy Tarreau | 12c0270 | 2021-09-30 16:12:31 +0200 | [diff] [blame] | 8289 | if (!tick_is_expired(wakeup_ms, now_ms)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8290 | 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] | 8291 | return 0; |
| 8292 | } |
| 8293 | |
| 8294 | __LJMP static int hlua_sleep(lua_State *L) |
| 8295 | { |
| 8296 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8297 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8298 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8299 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8300 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8301 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8302 | wakeup_ms = tick_add(now_ms, delay); |
| 8303 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8304 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8305 | 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] | 8306 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8307 | } |
| 8308 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8309 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8310 | { |
| 8311 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8312 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8313 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8314 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8315 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8316 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8317 | wakeup_ms = tick_add(now_ms, delay); |
| 8318 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8319 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8320 | 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] | 8321 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8322 | } |
| 8323 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8324 | /* This functionis an LUA binding. it permits to give back |
| 8325 | * the hand at the HAProxy scheduler. It is used when the |
| 8326 | * LUA processing consumes a lot of time. |
| 8327 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8328 | __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] | 8329 | { |
| 8330 | return 0; |
| 8331 | } |
| 8332 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8333 | __LJMP static int hlua_yield(lua_State *L) |
| 8334 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8335 | 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] | 8336 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8337 | } |
| 8338 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8339 | /* This function change the nice of the currently executed |
| 8340 | * task. It is used set low or high priority at the current |
| 8341 | * task. |
| 8342 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 8343 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8344 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8345 | struct hlua *hlua; |
| 8346 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8347 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8348 | MAY_LJMP(check_args(L, 1, "set_nice")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8349 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8350 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8351 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 8352 | hlua = hlua_gethlua(L); |
| 8353 | |
| 8354 | /* If the task is not set, I'm in a start mode. */ |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8355 | if (!hlua || !hlua->task) |
| 8356 | return 0; |
| 8357 | |
| 8358 | if (nice < -1024) |
| 8359 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8360 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8361 | nice = 1024; |
| 8362 | |
| 8363 | hlua->task->nice = nice; |
| 8364 | return 0; |
| 8365 | } |
| 8366 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8367 | /* 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] | 8368 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 8369 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 8370 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8371 | * |
| 8372 | * Task wrapper are longjmp safe because the only one Lua code |
| 8373 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8374 | */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 8375 | 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] | 8376 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 8377 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8378 | enum hlua_exec status; |
| 8379 | |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 8380 | if (task->thread_mask == MAX_THREADS_MASK) |
| 8381 | task_set_affinity(task, tid_bit); |
| 8382 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8383 | /* If it is the first call to the task, we must initialize the |
| 8384 | * execution timeouts. |
| 8385 | */ |
| 8386 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 8387 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8388 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8389 | /* Execute the Lua code. */ |
| 8390 | status = hlua_ctx_resume(hlua, 1); |
| 8391 | |
| 8392 | switch (status) { |
| 8393 | /* finished or yield */ |
| 8394 | case HLUA_E_OK: |
| 8395 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 8396 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 8397 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8398 | break; |
| 8399 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 8400 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 8401 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 8402 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8403 | break; |
| 8404 | |
| 8405 | /* finished with error. */ |
| 8406 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8407 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8408 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 8409 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 8410 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8411 | break; |
| 8412 | |
| 8413 | case HLUA_E_ERR: |
| 8414 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8415 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8416 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 8417 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 8418 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8419 | break; |
| 8420 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 8421 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8422 | } |
| 8423 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8424 | /* This function is an LUA binding that register LUA function to be |
| 8425 | * executed after the HAProxy configuration parsing and before the |
| 8426 | * HAProxy scheduler starts. This function expect only one LUA |
| 8427 | * argument that is a function. This function returns nothing, but |
| 8428 | * throws if an error is encountered. |
| 8429 | */ |
| 8430 | __LJMP static int hlua_register_init(lua_State *L) |
| 8431 | { |
| 8432 | struct hlua_init_function *init; |
| 8433 | int ref; |
| 8434 | |
| 8435 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 8436 | |
| 8437 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 8438 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8439 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8440 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8441 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8442 | |
| 8443 | init->function_ref = ref; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 8444 | LIST_APPEND(&hlua_init_functions[hlua_state_id], &init->l); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8445 | return 0; |
| 8446 | } |
| 8447 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8448 | /* This functio is an LUA binding. It permits to register a task |
| 8449 | * executed in parallel of the main HAroxy activity. The task is |
| 8450 | * created and it is set in the HAProxy scheduler. It can be called |
| 8451 | * from the "init" section, "post init" or during the runtime. |
| 8452 | * |
| 8453 | * Lua prototype: |
| 8454 | * |
| 8455 | * <none> core.register_task(<function>) |
| 8456 | */ |
| 8457 | static int hlua_register_task(lua_State *L) |
| 8458 | { |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8459 | struct hlua *hlua = NULL; |
| 8460 | struct task *task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8461 | int ref; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8462 | int state_id; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8463 | |
| 8464 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 8465 | |
| 8466 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 8467 | |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 8468 | /* Get the reference state. If the reference is NULL, L is the master |
| 8469 | * state, otherwise hlua->T is. |
| 8470 | */ |
| 8471 | hlua = hlua_gethlua(L); |
| 8472 | if (hlua) |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8473 | /* we are in runtime processing */ |
| 8474 | state_id = hlua->state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 8475 | else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8476 | /* we are in initialization mode */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8477 | state_id = hlua_state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 8478 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8479 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8480 | if (!hlua) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8481 | goto alloc_error; |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8482 | HLUA_INIT(hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8483 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8484 | /* We are in the common lua state, execute the task anywhere, |
| 8485 | * otherwise, inherit the current thread identifier |
| 8486 | */ |
| 8487 | if (state_id == 0) |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 8488 | task = task_new_anywhere(); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8489 | else |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 8490 | task = task_new_here(); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 8491 | if (!task) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8492 | goto alloc_error; |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 8493 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8494 | task->context = hlua; |
| 8495 | task->process = hlua_process_task; |
| 8496 | |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8497 | if (!hlua_ctx_init(hlua, state_id, task, 1)) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8498 | goto alloc_error; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8499 | |
| 8500 | /* Restore the function in the stack. */ |
| 8501 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 8502 | hlua->nargs = 0; |
| 8503 | |
| 8504 | /* Schedule task. */ |
Willy Tarreau | e3957f8 | 2021-09-30 16:17:37 +0200 | [diff] [blame] | 8505 | task_wakeup(task, TASK_WOKEN_INIT); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8506 | |
| 8507 | return 0; |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8508 | |
| 8509 | alloc_error: |
| 8510 | task_destroy(task); |
| 8511 | hlua_ctx_destroy(hlua); |
| 8512 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 8513 | return 0; /* Never reached */ |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8514 | } |
| 8515 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8516 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 8517 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 8518 | * resume converters. |
| 8519 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8520 | 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] | 8521 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 8522 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8523 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8524 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8525 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8526 | if (!stream) |
| 8527 | return 0; |
| 8528 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8529 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8530 | * Lua context can be not initialized. This behavior |
| 8531 | * permits to save performances because a systematic |
| 8532 | * Lua initialization cause 5% performances loss. |
| 8533 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8534 | if (!stream->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8535 | struct hlua *hlua; |
| 8536 | |
| 8537 | hlua = pool_alloc(pool_head_hlua); |
| 8538 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8539 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 8540 | return 0; |
| 8541 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8542 | HLUA_INIT(hlua); |
| 8543 | stream->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8544 | 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] | 8545 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 8546 | return 0; |
| 8547 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8548 | } |
| 8549 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8550 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8551 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8552 | |
| 8553 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8554 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8555 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 8556 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8557 | else |
| 8558 | error = "critical error"; |
| 8559 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8560 | return 0; |
| 8561 | } |
| 8562 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8563 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8564 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8565 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8566 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8567 | return 0; |
| 8568 | } |
| 8569 | |
| 8570 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8571 | 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] | 8572 | |
| 8573 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8574 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8575 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8576 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8577 | return 0; |
| 8578 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8579 | hlua_smp2lua(stream->hlua->T, smp); |
| 8580 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8581 | |
| 8582 | /* push keywords in the stack. */ |
| 8583 | if (arg_p) { |
| 8584 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8585 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8586 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8587 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8588 | return 0; |
| 8589 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8590 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 8591 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8592 | } |
| 8593 | } |
| 8594 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8595 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8596 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8597 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8598 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8599 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8600 | } |
| 8601 | |
| 8602 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8603 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8604 | /* finished. */ |
| 8605 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 8606 | /* If the stack is empty, the function fails. */ |
| 8607 | if (lua_gettop(stream->hlua->T) <= 0) |
| 8608 | return 0; |
| 8609 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8610 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8611 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 8612 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8613 | return 1; |
| 8614 | |
| 8615 | /* yield. */ |
| 8616 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8617 | 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] | 8618 | return 0; |
| 8619 | |
| 8620 | /* finished with error. */ |
| 8621 | case HLUA_E_ERRMSG: |
| 8622 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8623 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8624 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 8625 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8626 | return 0; |
| 8627 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8628 | case HLUA_E_ETMOUT: |
| 8629 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 8630 | return 0; |
| 8631 | |
| 8632 | case HLUA_E_NOMEM: |
| 8633 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 8634 | return 0; |
| 8635 | |
| 8636 | case HLUA_E_YIELD: |
| 8637 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 8638 | return 0; |
| 8639 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8640 | case HLUA_E_ERR: |
| 8641 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8642 | 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] | 8643 | /* fall through */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8644 | |
| 8645 | default: |
| 8646 | return 0; |
| 8647 | } |
| 8648 | } |
| 8649 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8650 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 8651 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8652 | * resume sample-fetches. This function will be called by the sample |
| 8653 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8654 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8655 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 8656 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8657 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 8658 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8659 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8660 | const char *error; |
Christopher Faulet | 8c9e6bb | 2021-08-06 16:29:41 +0200 | [diff] [blame] | 8661 | unsigned int hflags = HLUA_TXN_NOTERM | HLUA_TXN_SMP_CTX; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8662 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8663 | if (!stream) |
| 8664 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 8665 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8666 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8667 | * Lua context can be not initialized. This behavior |
| 8668 | * permits to save performances because a systematic |
| 8669 | * Lua initialization cause 5% performances loss. |
| 8670 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8671 | if (!stream->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8672 | struct hlua *hlua; |
| 8673 | |
| 8674 | hlua = pool_alloc(pool_head_hlua); |
| 8675 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8676 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 8677 | return 0; |
| 8678 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8679 | hlua->T = NULL; |
| 8680 | stream->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8681 | 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] | 8682 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 8683 | return 0; |
| 8684 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8685 | } |
| 8686 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8687 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8688 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8689 | |
| 8690 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8691 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8692 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 8693 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8694 | else |
| 8695 | error = "critical error"; |
| 8696 | 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] | 8697 | return 0; |
| 8698 | } |
| 8699 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8700 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8701 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8702 | 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] | 8703 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8704 | return 0; |
| 8705 | } |
| 8706 | |
| 8707 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8708 | 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] | 8709 | |
| 8710 | /* push arguments in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 8711 | 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] | 8712 | 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] | 8713 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8714 | return 0; |
| 8715 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8716 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8717 | |
| 8718 | /* push keywords in the stack. */ |
| 8719 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 8720 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8721 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8722 | 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] | 8723 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8724 | return 0; |
| 8725 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8726 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 8727 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8728 | } |
| 8729 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8730 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8731 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8732 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8733 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8734 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8735 | } |
| 8736 | |
| 8737 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8738 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8739 | /* finished. */ |
| 8740 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 8741 | /* If the stack is empty, the function fails. */ |
| 8742 | if (lua_gettop(stream->hlua->T) <= 0) |
| 8743 | return 0; |
| 8744 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8745 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8746 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 8747 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8748 | |
| 8749 | /* Set the end of execution flag. */ |
| 8750 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 8751 | return 1; |
| 8752 | |
| 8753 | /* yield. */ |
| 8754 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8755 | 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] | 8756 | return 0; |
| 8757 | |
| 8758 | /* finished with error. */ |
| 8759 | case HLUA_E_ERRMSG: |
| 8760 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8761 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8762 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 8763 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8764 | return 0; |
| 8765 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8766 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8767 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 8768 | return 0; |
| 8769 | |
| 8770 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8771 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 8772 | return 0; |
| 8773 | |
| 8774 | case HLUA_E_YIELD: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8775 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 8776 | return 0; |
| 8777 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8778 | case HLUA_E_ERR: |
| 8779 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8780 | 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] | 8781 | /* fall through */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8782 | |
| 8783 | default: |
| 8784 | return 0; |
| 8785 | } |
| 8786 | } |
| 8787 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8788 | /* This function is an LUA binding used for registering |
| 8789 | * "sample-conv" functions. It expects a converter name used |
| 8790 | * in the haproxy configuration file, and an LUA function. |
| 8791 | */ |
| 8792 | __LJMP static int hlua_register_converters(lua_State *L) |
| 8793 | { |
| 8794 | struct sample_conv_kw_list *sck; |
| 8795 | const char *name; |
| 8796 | int ref; |
| 8797 | int len; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8798 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8799 | struct sample_conv *sc; |
| 8800 | struct buffer *trash; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8801 | |
| 8802 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 8803 | |
| 8804 | /* First argument : converter name. */ |
| 8805 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8806 | |
| 8807 | /* Second argument : lua function. */ |
| 8808 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 8809 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8810 | /* Check if the converter is already registered */ |
| 8811 | trash = get_trash_chunk(); |
| 8812 | chunk_printf(trash, "lua.%s", name); |
| 8813 | sc = find_sample_conv(trash->area, trash->data); |
| 8814 | if (sc != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8815 | fcn = sc->private; |
| 8816 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 8817 | ha_warning("Trying to register converter 'lua.%s' more than once. " |
| 8818 | "This will become a hard error in version 2.5.\n", name); |
| 8819 | } |
| 8820 | fcn->function_ref[hlua_state_id] = ref; |
| 8821 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8822 | } |
| 8823 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8824 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8825 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8826 | if (!sck) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8827 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 8828 | fcn = new_hlua_function(); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8829 | if (!fcn) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8830 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8831 | |
| 8832 | /* Fill fcn. */ |
| 8833 | fcn->name = strdup(name); |
| 8834 | if (!fcn->name) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8835 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8836 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8837 | |
| 8838 | /* List head */ |
| 8839 | sck->list.n = sck->list.p = NULL; |
| 8840 | |
| 8841 | /* converter keyword. */ |
| 8842 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8843 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8844 | if (!sck->kw[0].kw) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8845 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8846 | |
| 8847 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 8848 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 8849 | 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] | 8850 | sck->kw[0].val_args = NULL; |
| 8851 | sck->kw[0].in_type = SMP_T_STR; |
| 8852 | sck->kw[0].out_type = SMP_T_STR; |
| 8853 | sck->kw[0].private = fcn; |
| 8854 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8855 | /* Register this new converter */ |
| 8856 | sample_register_convs(sck); |
| 8857 | |
| 8858 | return 0; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8859 | |
| 8860 | alloc_error: |
| 8861 | release_hlua_function(fcn); |
| 8862 | ha_free(&sck); |
| 8863 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 8864 | return 0; /* Never reached */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8865 | } |
| 8866 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8867 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8868 | * "sample-fetch" functions. It expects a converter name used |
| 8869 | * in the haproxy configuration file, and an LUA function. |
| 8870 | */ |
| 8871 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 8872 | { |
| 8873 | const char *name; |
| 8874 | int ref; |
| 8875 | int len; |
| 8876 | struct sample_fetch_kw_list *sfk; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8877 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8878 | struct sample_fetch *sf; |
| 8879 | struct buffer *trash; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8880 | |
| 8881 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 8882 | |
| 8883 | /* First argument : sample-fetch name. */ |
| 8884 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8885 | |
| 8886 | /* Second argument : lua function. */ |
| 8887 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 8888 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8889 | /* Check if the sample-fetch is already registered */ |
| 8890 | trash = get_trash_chunk(); |
| 8891 | chunk_printf(trash, "lua.%s", name); |
| 8892 | sf = find_sample_fetch(trash->area, trash->data); |
| 8893 | if (sf != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8894 | fcn = sf->private; |
| 8895 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 8896 | ha_warning("Trying to register sample-fetch 'lua.%s' more than once. " |
| 8897 | "This will become a hard error in version 2.5.\n", name); |
| 8898 | } |
| 8899 | fcn->function_ref[hlua_state_id] = ref; |
| 8900 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8901 | } |
| 8902 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8903 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8904 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8905 | if (!sfk) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8906 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 8907 | fcn = new_hlua_function(); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8908 | if (!fcn) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8909 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8910 | |
| 8911 | /* Fill fcn. */ |
| 8912 | fcn->name = strdup(name); |
| 8913 | if (!fcn->name) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8914 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8915 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8916 | |
| 8917 | /* List head */ |
| 8918 | sfk->list.n = sfk->list.p = NULL; |
| 8919 | |
| 8920 | /* sample-fetch keyword. */ |
| 8921 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8922 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8923 | if (!sfk->kw[0].kw) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8924 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8925 | |
| 8926 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 8927 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 8928 | 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] | 8929 | sfk->kw[0].val_args = NULL; |
| 8930 | sfk->kw[0].out_type = SMP_T_STR; |
| 8931 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 8932 | sfk->kw[0].val = 0; |
| 8933 | sfk->kw[0].private = fcn; |
| 8934 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8935 | /* Register this new fetch. */ |
| 8936 | sample_register_fetches(sfk); |
| 8937 | |
| 8938 | return 0; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8939 | |
| 8940 | alloc_error: |
| 8941 | release_hlua_function(fcn); |
| 8942 | ha_free(&sfk); |
| 8943 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 8944 | return 0; /* Never reached */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8945 | } |
| 8946 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 8947 | /* This function is a lua binding to set the wake_time. |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8948 | */ |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 8949 | __LJMP static int hlua_set_wake_time(lua_State *L) |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8950 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8951 | struct hlua *hlua; |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8952 | unsigned int delay; |
| 8953 | unsigned int wakeup_ms; |
| 8954 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8955 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 8956 | hlua = hlua_gethlua(L); |
| 8957 | if (!hlua) { |
| 8958 | return 0; |
| 8959 | } |
| 8960 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8961 | MAY_LJMP(check_args(L, 1, "wake_time")); |
| 8962 | |
| 8963 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 8964 | wakeup_ms = tick_add(now_ms, delay); |
| 8965 | hlua->wake_time = wakeup_ms; |
| 8966 | return 0; |
| 8967 | } |
| 8968 | |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8969 | /* This function is a wrapper to execute each LUA function declared as an action |
| 8970 | * wrapper during the initialisation period. This function may return any |
| 8971 | * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is |
| 8972 | * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the |
| 8973 | * return value is the first element on the stack. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8974 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 8975 | 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] | 8976 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8977 | { |
| 8978 | char **arg; |
Christopher Faulet | 8c9e6bb | 2021-08-06 16:29:41 +0200 | [diff] [blame] | 8979 | unsigned int hflags = HLUA_TXN_ACT_CTX; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8980 | int dir, act_ret = ACT_RET_CONT; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8981 | const char *error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 8982 | |
| 8983 | switch (rule->from) { |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 8984 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 8985 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 8986 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 8987 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 8988 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8989 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 8990 | goto end; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 8991 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 8992 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8993 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8994 | * Lua context can be not initialized. This behavior |
| 8995 | * permits to save performances because a systematic |
| 8996 | * Lua initialization cause 5% performances loss. |
| 8997 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8998 | if (!s->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8999 | struct hlua *hlua; |
| 9000 | |
| 9001 | hlua = pool_alloc(pool_head_hlua); |
| 9002 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9003 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9004 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9005 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9006 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 9007 | HLUA_INIT(hlua); |
| 9008 | s->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9009 | 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] | 9010 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9011 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9012 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9013 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 9014 | } |
| 9015 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9016 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9017 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9018 | |
| 9019 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9020 | if (!SET_SAFE_LJMP(s->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9021 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 9022 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9023 | else |
| 9024 | error = "critical error"; |
| 9025 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9026 | rule->arg.hlua_rule->fcn->name, error); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9027 | goto end; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9028 | } |
| 9029 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9030 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9031 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9032 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9033 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9034 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9035 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9036 | } |
| 9037 | |
| 9038 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9039 | 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] | 9040 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 9041 | /* Create and and push object stream in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 9042 | if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9043 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9044 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9045 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9046 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9047 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9048 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9049 | |
| 9050 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9051 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9052 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9053 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9054 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9055 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9056 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9057 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9058 | lua_pushstring(s->hlua->T, *arg); |
| 9059 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9060 | } |
| 9061 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9062 | /* Now the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9063 | RESET_SAFE_LJMP(s->hlua); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9064 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 9065 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9066 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9067 | } |
| 9068 | |
| 9069 | /* Execute the function. */ |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 9070 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9071 | /* finished. */ |
| 9072 | case HLUA_E_OK: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9073 | /* Catch the return value */ |
| 9074 | if (lua_gettop(s->hlua->T) > 0) |
| 9075 | act_ret = lua_tointeger(s->hlua->T, -1); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9076 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9077 | /* Set timeout in the required channel. */ |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 9078 | if (act_ret == ACT_RET_YIELD) { |
| 9079 | if (flags & ACT_OPT_FINAL) |
| 9080 | goto err_yield; |
| 9081 | |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 9082 | if (dir == SMP_OPT_DIR_REQ) |
| 9083 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 9084 | s->hlua->wake_time); |
| 9085 | else |
| 9086 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 9087 | s->hlua->wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9088 | } |
| 9089 | goto end; |
| 9090 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9091 | /* yield. */ |
| 9092 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 9093 | /* Set timeout in the required channel. */ |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 9094 | if (dir == SMP_OPT_DIR_REQ) |
| 9095 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 9096 | s->hlua->wake_time); |
| 9097 | else |
| 9098 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 9099 | s->hlua->wake_time); |
| 9100 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9101 | /* Some actions can be wake up when a "write" event |
| 9102 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9103 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9104 | */ |
Christopher Faulet | 51fa358 | 2019-07-26 14:54:52 +0200 | [diff] [blame] | 9105 | if (HLUA_IS_WAKERESWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9106 | s->res.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9107 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9108 | s->req.flags |= CF_WAKE_WRITE; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9109 | act_ret = ACT_RET_YIELD; |
| 9110 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9111 | |
| 9112 | /* finished with error. */ |
| 9113 | case HLUA_E_ERRMSG: |
| 9114 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9115 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9116 | rule->arg.hlua_rule->fcn->name, lua_tostring(s->hlua->T, -1)); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9117 | lua_pop(s->hlua->T, 1); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9118 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9119 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9120 | case HLUA_E_ETMOUT: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9121 | 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] | 9122 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9123 | |
| 9124 | case HLUA_E_NOMEM: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9125 | 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] | 9126 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9127 | |
| 9128 | case HLUA_E_YIELD: |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 9129 | err_yield: |
| 9130 | act_ret = ACT_RET_CONT; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9131 | 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] | 9132 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9133 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9134 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9135 | case HLUA_E_ERR: |
| 9136 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9137 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9138 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9139 | |
| 9140 | default: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9141 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9142 | } |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9143 | |
| 9144 | end: |
Christopher Faulet | 2361fd9 | 2020-07-30 10:40:58 +0200 | [diff] [blame] | 9145 | if (act_ret != ACT_RET_YIELD && s->hlua) |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 9146 | s->hlua->wake_time = TICK_ETERNITY; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9147 | return act_ret; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9148 | } |
| 9149 | |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 9150 | 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] | 9151 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 9152 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9153 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9154 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 9155 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 9156 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9157 | } |
| 9158 | |
| 9159 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 9160 | { |
| 9161 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9162 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9163 | struct task *task; |
| 9164 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9165 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9166 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 9167 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9168 | if (!hlua) { |
| 9169 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9170 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9171 | return 0; |
| 9172 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9173 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9174 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9175 | ctx->ctx.hlua_apptcp.flags = 0; |
| 9176 | |
| 9177 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 9178 | task = task_new_here(); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9179 | if (!task) { |
| 9180 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9181 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9182 | return 0; |
| 9183 | } |
| 9184 | task->nice = 0; |
| 9185 | task->context = ctx; |
| 9186 | task->process = hlua_applet_wakeup; |
| 9187 | ctx->ctx.hlua_apptcp.task = task; |
| 9188 | |
| 9189 | /* In the execution wrappers linked with a stream, the |
| 9190 | * Lua context can be not initialized. This behavior |
| 9191 | * permits to save performances because a systematic |
| 9192 | * Lua initialization cause 5% performances loss. |
| 9193 | */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9194 | 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] | 9195 | 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] | 9196 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9197 | return 0; |
| 9198 | } |
| 9199 | |
| 9200 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 9201 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9202 | |
| 9203 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9204 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9205 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 9206 | error = lua_tostring(hlua->T, -1); |
| 9207 | else |
| 9208 | error = "critical error"; |
| 9209 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9210 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9211 | return 0; |
| 9212 | } |
| 9213 | |
| 9214 | /* Check stack available size. */ |
| 9215 | if (!lua_checkstack(hlua->T, 1)) { |
| 9216 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9217 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9218 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9219 | return 0; |
| 9220 | } |
| 9221 | |
| 9222 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9223 | 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] | 9224 | |
| 9225 | /* Create and and push object stream in the stack. */ |
| 9226 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 9227 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9228 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9229 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9230 | return 0; |
| 9231 | } |
| 9232 | hlua->nargs = 1; |
| 9233 | |
| 9234 | /* push keywords in the stack. */ |
| 9235 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 9236 | if (!lua_checkstack(hlua->T, 1)) { |
| 9237 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9238 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9239 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9240 | return 0; |
| 9241 | } |
| 9242 | lua_pushstring(hlua->T, *arg); |
| 9243 | hlua->nargs++; |
| 9244 | } |
| 9245 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9246 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9247 | |
| 9248 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 9249 | si_cant_get(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 9250 | si_rx_endp_more(si); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9251 | |
| 9252 | return 1; |
| 9253 | } |
| 9254 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 9255 | void hlua_applet_tcp_fct(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9256 | { |
| 9257 | struct stream_interface *si = ctx->owner; |
| 9258 | struct stream *strm = si_strm(si); |
| 9259 | struct channel *res = si_ic(si); |
| 9260 | struct act_rule *rule = ctx->rule; |
| 9261 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9262 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9263 | |
| 9264 | /* The applet execution is already done. */ |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 9265 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) { |
| 9266 | /* eat the whole request */ |
| 9267 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9268 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 9269 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9270 | |
| 9271 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 9272 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 9273 | return; |
| 9274 | |
| 9275 | /* Execute the function. */ |
| 9276 | switch (hlua_ctx_resume(hlua, 1)) { |
| 9277 | /* finished. */ |
| 9278 | case HLUA_E_OK: |
| 9279 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 9280 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9281 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 9282 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9283 | res->flags |= CF_READ_NULL; |
| 9284 | si_shutr(si); |
| 9285 | return; |
| 9286 | |
| 9287 | /* yield. */ |
| 9288 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 9289 | if (hlua->wake_time != TICK_ETERNITY) |
| 9290 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9291 | return; |
| 9292 | |
| 9293 | /* finished with error. */ |
| 9294 | case HLUA_E_ERRMSG: |
| 9295 | /* Display log. */ |
| 9296 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9297 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9298 | lua_pop(hlua->T, 1); |
| 9299 | goto error; |
| 9300 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9301 | case HLUA_E_ETMOUT: |
| 9302 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9303 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9304 | goto error; |
| 9305 | |
| 9306 | case HLUA_E_NOMEM: |
| 9307 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9308 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9309 | goto error; |
| 9310 | |
| 9311 | case HLUA_E_YIELD: /* unexpected */ |
| 9312 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9313 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9314 | goto error; |
| 9315 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9316 | case HLUA_E_ERR: |
| 9317 | /* Display log. */ |
| 9318 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9319 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9320 | goto error; |
| 9321 | |
| 9322 | default: |
| 9323 | goto error; |
| 9324 | } |
| 9325 | |
| 9326 | error: |
| 9327 | |
| 9328 | /* For all other cases, just close the stream. */ |
| 9329 | si_shutw(si); |
| 9330 | si_shutr(si); |
| 9331 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 9332 | } |
| 9333 | |
| 9334 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 9335 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 9336 | task_destroy(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9337 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9338 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9339 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9340 | } |
| 9341 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9342 | /* The function returns 1 if the initialisation is complete, 0 if |
| 9343 | * an errors occurs and -1 if more data are required for initializing |
| 9344 | * the applet. |
| 9345 | */ |
| 9346 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 9347 | { |
| 9348 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9349 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9350 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9351 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9352 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9353 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9354 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9355 | txn = strm->txn; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 9356 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9357 | if (!hlua) { |
| 9358 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9359 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9360 | return 0; |
| 9361 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9362 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9363 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9364 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 9365 | ctx->ctx.hlua_apphttp.flags = 0; |
| 9366 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 9367 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 9368 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 9369 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9370 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 9371 | task = task_new_here(); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9372 | if (!task) { |
| 9373 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9374 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9375 | return 0; |
| 9376 | } |
| 9377 | task->nice = 0; |
| 9378 | task->context = ctx; |
| 9379 | task->process = hlua_applet_wakeup; |
| 9380 | ctx->ctx.hlua_apphttp.task = task; |
| 9381 | |
| 9382 | /* In the execution wrappers linked with a stream, the |
| 9383 | * Lua context can be not initialized. This behavior |
| 9384 | * permits to save performances because a systematic |
| 9385 | * Lua initialization cause 5% performances loss. |
| 9386 | */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9387 | 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] | 9388 | 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] | 9389 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9390 | return 0; |
| 9391 | } |
| 9392 | |
| 9393 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 9394 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9395 | |
| 9396 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9397 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9398 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 9399 | error = lua_tostring(hlua->T, -1); |
| 9400 | else |
| 9401 | error = "critical error"; |
| 9402 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9403 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9404 | return 0; |
| 9405 | } |
| 9406 | |
| 9407 | /* Check stack available size. */ |
| 9408 | if (!lua_checkstack(hlua->T, 1)) { |
| 9409 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9410 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9411 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9412 | return 0; |
| 9413 | } |
| 9414 | |
| 9415 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9416 | 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] | 9417 | |
| 9418 | /* Create and and push object stream in the stack. */ |
| 9419 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 9420 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9421 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9422 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9423 | return 0; |
| 9424 | } |
| 9425 | hlua->nargs = 1; |
| 9426 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9427 | /* push keywords in the stack. */ |
| 9428 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 9429 | if (!lua_checkstack(hlua->T, 1)) { |
| 9430 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9431 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9432 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9433 | return 0; |
| 9434 | } |
| 9435 | lua_pushstring(hlua->T, *arg); |
| 9436 | hlua->nargs++; |
| 9437 | } |
| 9438 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9439 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9440 | |
| 9441 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 9442 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9443 | |
| 9444 | return 1; |
| 9445 | } |
| 9446 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 9447 | void hlua_applet_http_fct(struct appctx *ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9448 | { |
| 9449 | struct stream_interface *si = ctx->owner; |
| 9450 | struct stream *strm = si_strm(si); |
| 9451 | struct channel *req = si_oc(si); |
| 9452 | struct channel *res = si_ic(si); |
| 9453 | struct act_rule *rule = ctx->rule; |
| 9454 | struct proxy *px = strm->be; |
| 9455 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
| 9456 | struct htx *req_htx, *res_htx; |
| 9457 | |
| 9458 | res_htx = htx_from_buf(&res->buf); |
| 9459 | |
| 9460 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 9461 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 9462 | goto out; |
| 9463 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9464 | /* Check if the input buffer is available. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9465 | if (!b_size(&res->buf)) { |
| 9466 | si_rx_room_blk(si); |
| 9467 | goto out; |
| 9468 | } |
| 9469 | /* check that the output is not closed */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9470 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9471 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 9472 | |
| 9473 | /* Set the currently running flag. */ |
| 9474 | if (!HLUA_IS_RUNNING(hlua) && |
| 9475 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
Christopher Faulet | bd878d2 | 2021-04-28 10:50:21 +0200 | [diff] [blame] | 9476 | if (!co_data(req)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9477 | si_cant_get(si); |
| 9478 | goto out; |
| 9479 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9480 | } |
| 9481 | |
| 9482 | /* Executes The applet if it is not done. */ |
| 9483 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 9484 | |
| 9485 | /* Execute the function. */ |
| 9486 | switch (hlua_ctx_resume(hlua, 1)) { |
| 9487 | /* finished. */ |
| 9488 | case HLUA_E_OK: |
| 9489 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 9490 | break; |
| 9491 | |
| 9492 | /* yield. */ |
| 9493 | case HLUA_E_AGAIN: |
| 9494 | if (hlua->wake_time != TICK_ETERNITY) |
| 9495 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9496 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9497 | |
| 9498 | /* finished with error. */ |
| 9499 | case HLUA_E_ERRMSG: |
| 9500 | /* Display log. */ |
| 9501 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9502 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9503 | lua_pop(hlua->T, 1); |
| 9504 | goto error; |
| 9505 | |
| 9506 | case HLUA_E_ETMOUT: |
| 9507 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9508 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9509 | goto error; |
| 9510 | |
| 9511 | case HLUA_E_NOMEM: |
| 9512 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9513 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9514 | goto error; |
| 9515 | |
| 9516 | case HLUA_E_YIELD: /* unexpected */ |
| 9517 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9518 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9519 | goto error; |
| 9520 | |
| 9521 | case HLUA_E_ERR: |
| 9522 | /* Display log. */ |
| 9523 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9524 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9525 | goto error; |
| 9526 | |
| 9527 | default: |
| 9528 | goto error; |
| 9529 | } |
| 9530 | } |
| 9531 | |
| 9532 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9533 | if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT) |
| 9534 | goto done; |
| 9535 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9536 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) |
| 9537 | goto error; |
| 9538 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 9539 | /* no more data are expected. Don't add TLR because mux-h1 will take care of it */ |
| 9540 | res_htx->flags |= HTX_FL_EOM; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9541 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 9542 | ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9543 | } |
| 9544 | |
| 9545 | done: |
| 9546 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9547 | if (!(res->flags & CF_SHUTR)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9548 | res->flags |= CF_READ_NULL; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9549 | si_shutr(si); |
| 9550 | } |
| 9551 | |
| 9552 | /* eat the whole request */ |
| 9553 | if (co_data(req)) { |
| 9554 | req_htx = htx_from_buf(&req->buf); |
| 9555 | co_htx_skip(req, req_htx, co_data(req)); |
| 9556 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9557 | } |
| 9558 | } |
| 9559 | |
| 9560 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9561 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9562 | return; |
| 9563 | |
| 9564 | error: |
| 9565 | |
| 9566 | /* If we are in HTTP mode, and we are not send any |
| 9567 | * data, return a 500 server error in best effort: |
| 9568 | * if there is no room available in the buffer, |
| 9569 | * just close the connection. |
| 9570 | */ |
| 9571 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 9572 | struct buffer *err = &http_err_chunks[HTTP_ERR_500]; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9573 | |
| 9574 | channel_erase(res); |
| 9575 | res->buf.data = b_data(err); |
| 9576 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 9577 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 9578 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9579 | } |
| 9580 | if (!(strm->flags & SF_ERR_MASK)) |
| 9581 | strm->flags |= SF_ERR_RESOURCE; |
| 9582 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 9583 | goto done; |
| 9584 | } |
| 9585 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9586 | static void hlua_applet_http_release(struct appctx *ctx) |
| 9587 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 9588 | task_destroy(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9589 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9590 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9591 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9592 | } |
| 9593 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9594 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9595 | * success case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9596 | * |
| 9597 | * This function can fail with an abort() due to an Lua critical error. |
| 9598 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 9599 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9600 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9601 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 9602 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9603 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9604 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9605 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9606 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9607 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9608 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9609 | if (!rule->arg.hlua_rule) { |
| 9610 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9611 | goto error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9612 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9613 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9614 | /* Memory for arguments. */ |
Tim Duesterhus | e52b6e5 | 2020-09-12 20:26:43 +0200 | [diff] [blame] | 9615 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, |
| 9616 | sizeof(*rule->arg.hlua_rule->args)); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9617 | if (!rule->arg.hlua_rule->args) { |
| 9618 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9619 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9620 | } |
| 9621 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9622 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9623 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9624 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9625 | /* Expect some arguments */ |
| 9626 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 9627 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9628 | memprintf(err, "expect %d arguments", fcn->nargs); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9629 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9630 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 9631 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9632 | if (!rule->arg.hlua_rule->args[i]) { |
| 9633 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9634 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9635 | } |
| 9636 | (*cur_arg)++; |
| 9637 | } |
| 9638 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9639 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 9640 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9641 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 9642 | return ACT_RET_PRS_OK; |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9643 | |
| 9644 | error: |
| 9645 | if (rule->arg.hlua_rule) { |
| 9646 | if (rule->arg.hlua_rule->args) { |
| 9647 | for (i = 0; i < fcn->nargs; i++) |
| 9648 | ha_free(&rule->arg.hlua_rule->args[i]); |
| 9649 | ha_free(&rule->arg.hlua_rule->args); |
| 9650 | } |
| 9651 | ha_free(&rule->arg.hlua_rule); |
| 9652 | } |
| 9653 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9654 | } |
| 9655 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9656 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 9657 | struct act_rule *rule, char **err) |
| 9658 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9659 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9660 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 9661 | /* HTTP applets are forbidden in tcp-request rules. |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9662 | * HTTP applet request requires everything initialized by |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 9663 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9664 | * The applet will be immediately initialized, but its before |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 9665 | * the call of this analyzer. |
| 9666 | */ |
| 9667 | if (rule->from != ACT_F_HTTP_REQ) { |
| 9668 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 9669 | return ACT_RET_PRS_ERR; |
| 9670 | } |
| 9671 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9672 | /* Memory for the rule. */ |
| 9673 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 9674 | if (!rule->arg.hlua_rule) { |
| 9675 | memprintf(err, "out of memory error"); |
| 9676 | return ACT_RET_PRS_ERR; |
| 9677 | } |
| 9678 | |
| 9679 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9680 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9681 | |
| 9682 | /* TODO: later accept arguments. */ |
| 9683 | rule->arg.hlua_rule->args = NULL; |
| 9684 | |
| 9685 | /* Add applet pointer in the rule. */ |
| 9686 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 9687 | rule->applet.name = fcn->name; |
| 9688 | rule->applet.init = hlua_applet_http_init; |
| 9689 | rule->applet.fct = hlua_applet_http_fct; |
| 9690 | rule->applet.release = hlua_applet_http_release; |
| 9691 | rule->applet.timeout = hlua_timeout_applet; |
| 9692 | |
| 9693 | return ACT_RET_PRS_OK; |
| 9694 | } |
| 9695 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9696 | /* This function is an LUA binding used for registering |
| 9697 | * "sample-conv" functions. It expects a converter name used |
| 9698 | * in the haproxy configuration file, and an LUA function. |
| 9699 | */ |
| 9700 | __LJMP static int hlua_register_action(lua_State *L) |
| 9701 | { |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9702 | struct action_kw_list *akl = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9703 | const char *name; |
| 9704 | int ref; |
| 9705 | int len; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9706 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9707 | int nargs; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9708 | struct buffer *trash; |
| 9709 | struct action_kw *akw; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9710 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9711 | /* Initialise the number of expected arguments at 0. */ |
| 9712 | nargs = 0; |
| 9713 | |
| 9714 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 9715 | 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] | 9716 | |
| 9717 | /* First argument : converter name. */ |
| 9718 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 9719 | |
| 9720 | /* Second argument : environment. */ |
| 9721 | if (lua_type(L, 2) != LUA_TTABLE) |
| 9722 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 9723 | |
| 9724 | /* Third argument : lua function. */ |
| 9725 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 9726 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9727 | /* 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] | 9728 | if (lua_gettop(L) >= 4) |
| 9729 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 9730 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9731 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9732 | lua_pushnil(L); |
| 9733 | while (lua_next(L, 2) != 0) { |
| 9734 | if (lua_type(L, -1) != LUA_TSTRING) |
| 9735 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 9736 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9737 | /* Check if action exists */ |
| 9738 | trash = get_trash_chunk(); |
| 9739 | chunk_printf(trash, "lua.%s", name); |
| 9740 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) { |
| 9741 | akw = tcp_req_cont_action(trash->area); |
| 9742 | } else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) { |
| 9743 | akw = tcp_res_cont_action(trash->area); |
| 9744 | } else if (strcmp(lua_tostring(L, -1), "http-req") == 0) { |
| 9745 | akw = action_http_req_custom(trash->area); |
| 9746 | } else if (strcmp(lua_tostring(L, -1), "http-res") == 0) { |
| 9747 | akw = action_http_res_custom(trash->area); |
| 9748 | } else { |
| 9749 | akw = NULL; |
| 9750 | } |
| 9751 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9752 | fcn = akw->private; |
| 9753 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 9754 | ha_warning("Trying to register action 'lua.%s' more than once. " |
| 9755 | "This will become a hard error in version 2.5.\n", name); |
| 9756 | } |
| 9757 | fcn->function_ref[hlua_state_id] = ref; |
| 9758 | |
| 9759 | /* pop the environment string. */ |
| 9760 | lua_pop(L, 1); |
| 9761 | continue; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9762 | } |
| 9763 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9764 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 9765 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9766 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9767 | if (!akl) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9768 | goto alloc_error;; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 9769 | fcn = new_hlua_function(); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9770 | if (!fcn) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9771 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9772 | |
| 9773 | /* Fill fcn. */ |
| 9774 | fcn->name = strdup(name); |
| 9775 | if (!fcn->name) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9776 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9777 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9778 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 9779 | /* Set the expected number of arguments. */ |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9780 | fcn->nargs = nargs; |
| 9781 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9782 | /* List head */ |
| 9783 | akl->list.n = akl->list.p = NULL; |
| 9784 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9785 | /* action keyword. */ |
| 9786 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9787 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9788 | if (!akl->kw[0].kw) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9789 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9790 | |
| 9791 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 9792 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 9793 | akl->kw[0].flags = 0; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9794 | akl->kw[0].private = fcn; |
| 9795 | akl->kw[0].parse = action_register_lua; |
| 9796 | |
| 9797 | /* select the action registering point. */ |
| 9798 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 9799 | tcp_req_cont_keywords_register(akl); |
| 9800 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 9801 | tcp_res_cont_keywords_register(akl); |
| 9802 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 9803 | http_req_keywords_register(akl); |
| 9804 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 9805 | http_res_keywords_register(akl); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9806 | else { |
| 9807 | release_hlua_function(fcn); |
| 9808 | if (akl) |
| 9809 | ha_free((char **)&(akl->kw[0].kw)); |
| 9810 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 9811 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9812 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 9813 | "are expected.", lua_tostring(L, -1))); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9814 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9815 | |
| 9816 | /* pop the environment string. */ |
| 9817 | lua_pop(L, 1); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9818 | |
| 9819 | /* reset for next loop */ |
| 9820 | akl = NULL; |
| 9821 | fcn = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9822 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9823 | return ACT_RET_PRS_OK; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9824 | |
| 9825 | alloc_error: |
| 9826 | release_hlua_function(fcn); |
| 9827 | ha_free(&akl); |
| 9828 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 9829 | return 0; /* Never reached */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9830 | } |
| 9831 | |
| 9832 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 9833 | struct act_rule *rule, char **err) |
| 9834 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9835 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9836 | |
Christopher Faulet | 280f85b | 2019-07-15 15:02:04 +0200 | [diff] [blame] | 9837 | if (px->mode == PR_MODE_HTTP) { |
| 9838 | memprintf(err, "Lua TCP services cannot be used on HTTP proxies"); |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 9839 | return ACT_RET_PRS_ERR; |
| 9840 | } |
| 9841 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9842 | /* Memory for the rule. */ |
| 9843 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 9844 | if (!rule->arg.hlua_rule) { |
| 9845 | memprintf(err, "out of memory error"); |
| 9846 | return ACT_RET_PRS_ERR; |
| 9847 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9848 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9849 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9850 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9851 | |
| 9852 | /* TODO: later accept arguments. */ |
| 9853 | rule->arg.hlua_rule->args = NULL; |
| 9854 | |
| 9855 | /* Add applet pointer in the rule. */ |
| 9856 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 9857 | rule->applet.name = fcn->name; |
| 9858 | rule->applet.init = hlua_applet_tcp_init; |
| 9859 | rule->applet.fct = hlua_applet_tcp_fct; |
| 9860 | rule->applet.release = hlua_applet_tcp_release; |
| 9861 | rule->applet.timeout = hlua_timeout_applet; |
| 9862 | |
| 9863 | return 0; |
| 9864 | } |
| 9865 | |
| 9866 | /* This function is an LUA binding used for registering |
| 9867 | * "sample-conv" functions. It expects a converter name used |
| 9868 | * in the haproxy configuration file, and an LUA function. |
| 9869 | */ |
| 9870 | __LJMP static int hlua_register_service(lua_State *L) |
| 9871 | { |
| 9872 | struct action_kw_list *akl; |
| 9873 | const char *name; |
| 9874 | const char *env; |
| 9875 | int ref; |
| 9876 | int len; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9877 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9878 | struct buffer *trash; |
| 9879 | struct action_kw *akw; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9880 | |
| 9881 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 9882 | |
| 9883 | /* First argument : converter name. */ |
| 9884 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 9885 | |
| 9886 | /* Second argument : environment. */ |
| 9887 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 9888 | |
| 9889 | /* Third argument : lua function. */ |
| 9890 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 9891 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9892 | /* Check for service already registered */ |
| 9893 | trash = get_trash_chunk(); |
| 9894 | chunk_printf(trash, "lua.%s", name); |
| 9895 | akw = service_find(trash->area); |
| 9896 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9897 | fcn = akw->private; |
| 9898 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 9899 | ha_warning("Trying to register service 'lua.%s' more than once. " |
| 9900 | "This will become a hard error in version 2.5.\n", name); |
| 9901 | } |
| 9902 | fcn->function_ref[hlua_state_id] = ref; |
| 9903 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9904 | } |
| 9905 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9906 | /* Allocate and fill the sample fetch keyword struct. */ |
| 9907 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 9908 | if (!akl) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9909 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 9910 | fcn = new_hlua_function(); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9911 | if (!fcn) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9912 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9913 | |
| 9914 | /* Fill fcn. */ |
| 9915 | len = strlen("<lua.>") + strlen(name) + 1; |
| 9916 | fcn->name = calloc(1, len); |
| 9917 | if (!fcn->name) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9918 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9919 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9920 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9921 | |
| 9922 | /* List head */ |
| 9923 | akl->list.n = akl->list.p = NULL; |
| 9924 | |
| 9925 | /* converter keyword. */ |
| 9926 | len = strlen("lua.") + strlen(name) + 1; |
| 9927 | akl->kw[0].kw = calloc(1, len); |
| 9928 | if (!akl->kw[0].kw) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9929 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9930 | |
| 9931 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 9932 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 9933 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9934 | if (strcmp(env, "tcp") == 0) |
| 9935 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9936 | else if (strcmp(env, "http") == 0) |
| 9937 | akl->kw[0].parse = action_register_service_http; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9938 | else { |
| 9939 | release_hlua_function(fcn); |
| 9940 | if (akl) |
| 9941 | ha_free((char **)&(akl->kw[0].kw)); |
| 9942 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 9943 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 9944 | "'tcp' or 'http' are expected.", env)); |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9945 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9946 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 9947 | akl->kw[0].flags = 0; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9948 | akl->kw[0].private = fcn; |
| 9949 | |
| 9950 | /* End of array. */ |
| 9951 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 9952 | |
| 9953 | /* Register this new converter */ |
| 9954 | service_keywords_register(akl); |
| 9955 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9956 | return 0; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 9957 | |
| 9958 | alloc_error: |
| 9959 | release_hlua_function(fcn); |
| 9960 | ha_free(&akl); |
| 9961 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 9962 | return 0; /* Never reached */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9963 | } |
| 9964 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9965 | /* This function initialises Lua cli handler. It copies the |
| 9966 | * arguments in the Lua stack and create channel IO objects. |
| 9967 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 9968 | 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] | 9969 | { |
| 9970 | struct hlua *hlua; |
| 9971 | struct hlua_function *fcn; |
| 9972 | int i; |
| 9973 | const char *error; |
| 9974 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9975 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9976 | appctx->ctx.hlua_cli.fcn = private; |
| 9977 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 9978 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9979 | if (!hlua) { |
| 9980 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 9981 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9982 | } |
| 9983 | HLUA_INIT(hlua); |
| 9984 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9985 | |
| 9986 | /* Create task used by signal to wakeup applets. |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 9987 | * 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] | 9988 | * applet_http. It is absolutely compatible. |
| 9989 | */ |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 9990 | appctx->ctx.hlua_cli.task = task_new_here(); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9991 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 9992 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 9993 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 9994 | } |
| 9995 | appctx->ctx.hlua_cli.task->nice = 0; |
| 9996 | appctx->ctx.hlua_cli.task->context = appctx; |
| 9997 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 9998 | |
| 9999 | /* Initialises the Lua context */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 10000 | 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] | 10001 | 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] | 10002 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10003 | } |
| 10004 | |
| 10005 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10006 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10007 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 10008 | error = lua_tostring(hlua->T, -1); |
| 10009 | else |
| 10010 | error = "critical error"; |
| 10011 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 10012 | goto error; |
| 10013 | } |
| 10014 | |
| 10015 | /* Check stack available size. */ |
| 10016 | if (!lua_checkstack(hlua->T, 2)) { |
| 10017 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 10018 | goto error; |
| 10019 | } |
| 10020 | |
| 10021 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 10022 | 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] | 10023 | |
| 10024 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 10025 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10026 | */ |
| 10027 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 10028 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 10029 | goto error; |
| 10030 | } |
| 10031 | hlua->nargs = 1; |
| 10032 | |
| 10033 | /* push keywords in the stack. */ |
| 10034 | for (i = 0; *args[i]; i++) { |
| 10035 | /* Check stack available size. */ |
| 10036 | if (!lua_checkstack(hlua->T, 1)) { |
| 10037 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 10038 | goto error; |
| 10039 | } |
| 10040 | lua_pushstring(hlua->T, args[i]); |
| 10041 | hlua->nargs++; |
| 10042 | } |
| 10043 | |
| 10044 | /* We must initialize the execution timeouts. */ |
| 10045 | hlua->max_time = hlua_timeout_session; |
| 10046 | |
| 10047 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10048 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10049 | |
| 10050 | /* It's ok */ |
| 10051 | return 0; |
| 10052 | |
| 10053 | /* It's not ok. */ |
| 10054 | error: |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10055 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10056 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 10057 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10058 | return 1; |
| 10059 | } |
| 10060 | |
| 10061 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 10062 | { |
| 10063 | struct hlua *hlua; |
| 10064 | struct stream_interface *si; |
| 10065 | struct hlua_function *fcn; |
| 10066 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10067 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10068 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 10069 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10070 | |
| 10071 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 10072 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 10073 | return 1; |
| 10074 | |
| 10075 | /* Execute the function. */ |
| 10076 | switch (hlua_ctx_resume(hlua, 1)) { |
| 10077 | |
| 10078 | /* finished. */ |
| 10079 | case HLUA_E_OK: |
| 10080 | return 1; |
| 10081 | |
| 10082 | /* yield. */ |
| 10083 | case HLUA_E_AGAIN: |
| 10084 | /* We want write. */ |
| 10085 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 10086 | si_rx_room_blk(si); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10087 | /* Set the timeout. */ |
| 10088 | if (hlua->wake_time != TICK_ETERNITY) |
| 10089 | task_schedule(hlua->task, hlua->wake_time); |
| 10090 | return 0; |
| 10091 | |
| 10092 | /* finished with error. */ |
| 10093 | case HLUA_E_ERRMSG: |
| 10094 | /* Display log. */ |
| 10095 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 10096 | fcn->name, lua_tostring(hlua->T, -1)); |
| 10097 | lua_pop(hlua->T, 1); |
| 10098 | return 1; |
| 10099 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 10100 | case HLUA_E_ETMOUT: |
| 10101 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 10102 | fcn->name); |
| 10103 | return 1; |
| 10104 | |
| 10105 | case HLUA_E_NOMEM: |
| 10106 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 10107 | fcn->name); |
| 10108 | return 1; |
| 10109 | |
| 10110 | case HLUA_E_YIELD: /* unexpected */ |
| 10111 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 10112 | fcn->name); |
| 10113 | return 1; |
| 10114 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10115 | case HLUA_E_ERR: |
| 10116 | /* Display log. */ |
| 10117 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 10118 | fcn->name); |
| 10119 | return 1; |
| 10120 | |
| 10121 | default: |
| 10122 | return 1; |
| 10123 | } |
| 10124 | |
| 10125 | return 1; |
| 10126 | } |
| 10127 | |
| 10128 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 10129 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10130 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10131 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10132 | } |
| 10133 | |
| 10134 | /* This function is an LUA binding used for registering |
| 10135 | * new keywords in the cli. It expects a list of keywords |
| 10136 | * which are the "path". It is limited to 5 keywords. A |
| 10137 | * description of the command, a function to be executed |
| 10138 | * for the parsing and a function for io handlers. |
| 10139 | */ |
| 10140 | __LJMP static int hlua_register_cli(lua_State *L) |
| 10141 | { |
| 10142 | struct cli_kw_list *cli_kws; |
| 10143 | const char *message; |
| 10144 | int ref_io; |
| 10145 | int len; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10146 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10147 | int index; |
| 10148 | int i; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10149 | struct buffer *trash; |
| 10150 | const char *kw[5]; |
| 10151 | struct cli_kw *cli_kw; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10152 | const char *errmsg; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10153 | |
| 10154 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 10155 | |
| 10156 | /* First argument : an array of maximum 5 keywords. */ |
| 10157 | if (!lua_istable(L, 1)) |
| 10158 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 10159 | |
| 10160 | /* Second argument : string with contextual message. */ |
| 10161 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 10162 | |
| 10163 | /* Third and fourth argument : lua function. */ |
| 10164 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 10165 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10166 | /* Check for CLI service already registered */ |
| 10167 | trash = get_trash_chunk(); |
| 10168 | index = 0; |
| 10169 | lua_pushnil(L); |
| 10170 | memset(kw, 0, sizeof(kw)); |
| 10171 | while (lua_next(L, 1) != 0) { |
| 10172 | if (index >= CLI_PREFIX_KW_NB) |
| 10173 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 10174 | if (lua_type(L, -1) != LUA_TSTRING) |
| 10175 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 10176 | kw[index] = lua_tostring(L, -1); |
| 10177 | if (index == 0) |
| 10178 | chunk_printf(trash, "%s", kw[index]); |
| 10179 | else |
| 10180 | chunk_appendf(trash, " %s", kw[index]); |
| 10181 | index++; |
| 10182 | lua_pop(L, 1); |
| 10183 | } |
| 10184 | cli_kw = cli_find_kw_exact((char **)kw); |
| 10185 | if (cli_kw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10186 | fcn = cli_kw->private; |
| 10187 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 10188 | ha_warning("Trying to register CLI keyword 'lua.%s' more than once. " |
| 10189 | "This will become a hard error in version 2.5.\n", trash->area); |
| 10190 | } |
| 10191 | fcn->function_ref[hlua_state_id] = ref_io; |
| 10192 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10193 | } |
| 10194 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10195 | /* Allocate and fill the sample fetch keyword struct. */ |
| 10196 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10197 | if (!cli_kws) { |
| 10198 | errmsg = "Lua out of memory error."; |
| 10199 | goto error; |
| 10200 | } |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 10201 | fcn = new_hlua_function(); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10202 | if (!fcn) { |
| 10203 | errmsg = "Lua out of memory error."; |
| 10204 | goto error; |
| 10205 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10206 | |
| 10207 | /* Fill path. */ |
| 10208 | index = 0; |
| 10209 | lua_pushnil(L); |
| 10210 | while(lua_next(L, 1) != 0) { |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10211 | if (index >= 5) { |
| 10212 | errmsg = "1st argument must be a table with a maximum of 5 entries"; |
| 10213 | goto error; |
| 10214 | } |
| 10215 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 10216 | errmsg = "1st argument must be a table filled with strings"; |
| 10217 | goto error; |
| 10218 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10219 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10220 | if (!cli_kws->kw[0].str_kw[index]) { |
| 10221 | errmsg = "Lua out of memory error."; |
| 10222 | goto error; |
| 10223 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10224 | index++; |
| 10225 | lua_pop(L, 1); |
| 10226 | } |
| 10227 | |
| 10228 | /* Copy help message. */ |
| 10229 | cli_kws->kw[0].usage = strdup(message); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10230 | if (!cli_kws->kw[0].usage) { |
| 10231 | errmsg = "Lua out of memory error."; |
| 10232 | goto error; |
| 10233 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10234 | |
| 10235 | /* Fill fcn io handler. */ |
| 10236 | len = strlen("<lua.cli>") + 1; |
| 10237 | for (i = 0; i < index; i++) |
| 10238 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 10239 | fcn->name = calloc(1, len); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10240 | if (!fcn->name) { |
| 10241 | errmsg = "Lua out of memory error."; |
| 10242 | goto error; |
| 10243 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10244 | strncat((char *)fcn->name, "<lua.cli", len); |
| 10245 | for (i = 0; i < index; i++) { |
| 10246 | strncat((char *)fcn->name, ".", len); |
| 10247 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 10248 | } |
| 10249 | strncat((char *)fcn->name, ">", len); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 10250 | fcn->function_ref[hlua_state_id] = ref_io; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10251 | |
| 10252 | /* Fill last entries. */ |
| 10253 | cli_kws->kw[0].private = fcn; |
| 10254 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 10255 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 10256 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 10257 | |
| 10258 | /* Register this new converter */ |
| 10259 | cli_register_kw(cli_kws); |
| 10260 | |
| 10261 | return 0; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10262 | |
| 10263 | error: |
| 10264 | release_hlua_function(fcn); |
| 10265 | if (cli_kws) { |
| 10266 | for (i = 0; i < index; i++) |
| 10267 | ha_free((char **)&(cli_kws->kw[0].str_kw[i])); |
| 10268 | ha_free((char **)&(cli_kws->kw[0].usage)); |
| 10269 | } |
| 10270 | ha_free(&cli_kws); |
| 10271 | WILL_LJMP(luaL_error(L, errmsg)); |
| 10272 | return 0; /* Never reached */ |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10273 | } |
| 10274 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10275 | static int hlua_filter_init_per_thread(struct proxy *px, struct flt_conf *fconf) |
| 10276 | { |
| 10277 | struct hlua_flt_config *conf = fconf->conf; |
| 10278 | lua_State *L; |
| 10279 | int error, pos, state_id, flt_ref; |
| 10280 | |
| 10281 | state_id = reg_flt_to_stack_id(conf->reg); |
| 10282 | L = hlua_states[state_id]; |
| 10283 | pos = lua_gettop(L); |
| 10284 | |
| 10285 | /* The filter parsing function */ |
| 10286 | lua_rawgeti(L, LUA_REGISTRYINDEX, conf->reg->fun_ref[state_id]); |
| 10287 | |
| 10288 | /* Push the filter class on the stack and resolve all callbacks */ |
| 10289 | lua_rawgeti(L, LUA_REGISTRYINDEX, conf->reg->flt_ref[state_id]); |
| 10290 | |
| 10291 | /* Duplicate the filter class so each filter will have its own copy */ |
| 10292 | lua_newtable(L); |
| 10293 | lua_pushnil(L); |
| 10294 | |
| 10295 | while (lua_next(L, pos+2)) { |
| 10296 | lua_pushvalue(L, -2); |
| 10297 | lua_insert(L, -2); |
| 10298 | lua_settable(L, -4); |
| 10299 | } |
| 10300 | flt_ref = luaL_ref(L, LUA_REGISTRYINDEX); |
| 10301 | |
| 10302 | /* Remove the original lua filter class from the stack */ |
| 10303 | lua_pop(L, 1); |
| 10304 | |
| 10305 | /* Push the copy on the stack */ |
| 10306 | lua_rawgeti(L, LUA_REGISTRYINDEX, flt_ref); |
| 10307 | |
| 10308 | /* extra args are pushed in a table */ |
| 10309 | lua_newtable(L); |
| 10310 | for (pos = 0; conf->args[pos]; pos++) { |
| 10311 | /* Check stack available size. */ |
| 10312 | if (!lua_checkstack(L, 1)) { |
| 10313 | ha_alert("Lua filter '%s' : Lua error : full stack.", conf->reg->name); |
| 10314 | goto error; |
| 10315 | } |
| 10316 | lua_pushstring(L, conf->args[pos]); |
| 10317 | lua_rawseti(L, -2, lua_rawlen(L, -2) + 1); |
| 10318 | } |
| 10319 | |
| 10320 | error = lua_pcall(L, 2, LUA_MULTRET, 0); |
| 10321 | switch (error) { |
| 10322 | case LUA_OK: |
| 10323 | /* replace the filter ref */ |
| 10324 | conf->ref[state_id] = flt_ref; |
| 10325 | break; |
| 10326 | case LUA_ERRRUN: |
| 10327 | ha_alert("Lua filter '%s' : runtime error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 10328 | goto error; |
| 10329 | case LUA_ERRMEM: |
| 10330 | ha_alert("Lua filter '%s' : out of memory error", conf->reg->name); |
| 10331 | goto error; |
| 10332 | case LUA_ERRERR: |
| 10333 | ha_alert("Lua filter '%s' : message handler error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 10334 | goto error; |
| 10335 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
| 10336 | case LUA_ERRGCMM: |
| 10337 | ha_alert("Lua filter '%s' : garbage collector error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 10338 | goto error; |
| 10339 | #endif |
| 10340 | default: |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 10341 | 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] | 10342 | goto error; |
| 10343 | } |
| 10344 | |
| 10345 | lua_settop(L, 0); |
| 10346 | return 0; |
| 10347 | |
| 10348 | error: |
| 10349 | lua_settop(L, 0); |
| 10350 | return -1; |
| 10351 | } |
| 10352 | |
| 10353 | static void hlua_filter_deinit_per_thread(struct proxy *px, struct flt_conf *fconf) |
| 10354 | { |
| 10355 | struct hlua_flt_config *conf = fconf->conf; |
| 10356 | lua_State *L; |
| 10357 | int state_id; |
| 10358 | |
| 10359 | if (!conf) |
| 10360 | return; |
| 10361 | |
| 10362 | state_id = reg_flt_to_stack_id(conf->reg); |
| 10363 | L = hlua_states[state_id]; |
| 10364 | luaL_unref(L, LUA_REGISTRYINDEX, conf->ref[state_id]); |
| 10365 | } |
| 10366 | |
| 10367 | static int hlua_filter_init(struct proxy *px, struct flt_conf *fconf) |
| 10368 | { |
| 10369 | struct hlua_flt_config *conf = fconf->conf; |
| 10370 | int state_id = reg_flt_to_stack_id(conf->reg); |
| 10371 | |
| 10372 | /* Rely on per-thread init for global scripts */ |
| 10373 | if (!state_id) |
| 10374 | return hlua_filter_init_per_thread(px, fconf); |
| 10375 | return 0; |
| 10376 | } |
| 10377 | |
| 10378 | static void hlua_filter_deinit(struct proxy *px, struct flt_conf *fconf) |
| 10379 | { |
| 10380 | |
| 10381 | if (fconf->conf) { |
| 10382 | struct hlua_flt_config *conf = fconf->conf; |
| 10383 | int state_id = reg_flt_to_stack_id(conf->reg); |
| 10384 | int pos; |
| 10385 | |
| 10386 | /* Rely on per-thread deinit for global scripts */ |
| 10387 | if (!state_id) |
| 10388 | hlua_filter_deinit_per_thread(px, fconf); |
| 10389 | |
| 10390 | for (pos = 0; conf->args[pos]; pos++) |
| 10391 | free(conf->args[pos]); |
| 10392 | free(conf->args); |
| 10393 | } |
| 10394 | ha_free(&fconf->conf); |
| 10395 | ha_free((char **)&fconf->id); |
| 10396 | ha_free(&fconf->ops); |
| 10397 | } |
| 10398 | |
| 10399 | static int hlua_filter_new(struct stream *s, struct filter *filter) |
| 10400 | { |
| 10401 | struct hlua_flt_config *conf = FLT_CONF(filter); |
| 10402 | struct hlua_flt_ctx *flt_ctx = NULL; |
| 10403 | int ret = 1; |
| 10404 | |
| 10405 | /* In the execution wrappers linked with a stream, the |
| 10406 | * Lua context can be not initialized. This behavior |
| 10407 | * permits to save performances because a systematic |
| 10408 | * Lua initialization cause 5% performances loss. |
| 10409 | */ |
| 10410 | if (!s->hlua) { |
| 10411 | struct hlua *hlua; |
| 10412 | |
| 10413 | hlua = pool_alloc(pool_head_hlua); |
| 10414 | if (!hlua) { |
| 10415 | SEND_ERR(s->be, "Lua filter '%s': can't initialize Lua context.\n", |
| 10416 | conf->reg->name); |
| 10417 | ret = 0; |
| 10418 | goto end; |
| 10419 | } |
| 10420 | HLUA_INIT(hlua); |
| 10421 | s->hlua = hlua; |
| 10422 | if (!hlua_ctx_init(s->hlua, reg_flt_to_stack_id(conf->reg), s->task, 0)) { |
| 10423 | SEND_ERR(s->be, "Lua filter '%s': can't initialize Lua context.\n", |
| 10424 | conf->reg->name); |
| 10425 | ret = 0; |
| 10426 | goto end; |
| 10427 | } |
| 10428 | } |
| 10429 | |
| 10430 | flt_ctx = pool_zalloc(pool_head_hlua_flt_ctx); |
| 10431 | if (!flt_ctx) { |
| 10432 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 10433 | conf->reg->name); |
| 10434 | ret = 0; |
| 10435 | goto end; |
| 10436 | } |
| 10437 | flt_ctx->hlua[0] = pool_alloc(pool_head_hlua); |
| 10438 | flt_ctx->hlua[1] = pool_alloc(pool_head_hlua); |
| 10439 | if (!flt_ctx->hlua[0] || !flt_ctx->hlua[1]) { |
| 10440 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 10441 | conf->reg->name); |
| 10442 | ret = 0; |
| 10443 | goto end; |
| 10444 | } |
| 10445 | HLUA_INIT(flt_ctx->hlua[0]); |
| 10446 | HLUA_INIT(flt_ctx->hlua[1]); |
| 10447 | if (!hlua_ctx_init(flt_ctx->hlua[0], reg_flt_to_stack_id(conf->reg), s->task, 0) || |
| 10448 | !hlua_ctx_init(flt_ctx->hlua[1], reg_flt_to_stack_id(conf->reg), s->task, 0)) { |
| 10449 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 10450 | conf->reg->name); |
| 10451 | ret = 0; |
| 10452 | goto end; |
| 10453 | } |
| 10454 | |
| 10455 | if (!HLUA_IS_RUNNING(s->hlua)) { |
| 10456 | /* The following Lua calls can fail. */ |
| 10457 | if (!SET_SAFE_LJMP(s->hlua)) { |
| 10458 | const char *error; |
| 10459 | |
| 10460 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 10461 | error = lua_tostring(s->hlua->T, -1); |
| 10462 | else |
| 10463 | error = "critical error"; |
| 10464 | SEND_ERR(s->be, "Lua filter '%s': %s.\n", conf->reg->name, error); |
| 10465 | ret = 0; |
| 10466 | goto end; |
| 10467 | } |
| 10468 | |
| 10469 | /* Check stack size. */ |
| 10470 | if (!lua_checkstack(s->hlua->T, 1)) { |
| 10471 | 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] | 10472 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10473 | ret = 0; |
| 10474 | goto end; |
| 10475 | } |
| 10476 | |
| 10477 | lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, conf->ref[s->hlua->state_id]); |
| 10478 | if (lua_getfield(s->hlua->T, -1, "new") != LUA_TFUNCTION) { |
| 10479 | SEND_ERR(s->be, "Lua filter '%s': 'new' field is not a function.\n", |
| 10480 | conf->reg->name); |
| 10481 | RESET_SAFE_LJMP(s->hlua); |
| 10482 | ret = 0; |
| 10483 | goto end; |
| 10484 | } |
| 10485 | lua_insert(s->hlua->T, -2); |
| 10486 | |
| 10487 | /* Push the copy on the stack */ |
| 10488 | s->hlua->nargs = 1; |
| 10489 | |
| 10490 | /* We must initialize the execution timeouts. */ |
| 10491 | s->hlua->max_time = hlua_timeout_session; |
| 10492 | |
| 10493 | /* At this point the execution is safe. */ |
| 10494 | RESET_SAFE_LJMP(s->hlua); |
| 10495 | } |
| 10496 | |
| 10497 | switch (hlua_ctx_resume(s->hlua, 0)) { |
| 10498 | case HLUA_E_OK: |
| 10499 | /* Nothing returned or not a table, ignore the filter for current stream */ |
| 10500 | if (!lua_gettop(s->hlua->T) || !lua_istable(s->hlua->T, 1)) { |
| 10501 | ret = 0; |
| 10502 | goto end; |
| 10503 | } |
| 10504 | |
| 10505 | /* Attached the filter pointer to the ctx */ |
| 10506 | lua_pushstring(s->hlua->T, "__filter"); |
| 10507 | lua_pushlightuserdata(s->hlua->T, filter); |
| 10508 | lua_settable(s->hlua->T, -3); |
| 10509 | |
| 10510 | /* Save a ref on the filter ctx */ |
| 10511 | lua_pushvalue(s->hlua->T, 1); |
| 10512 | flt_ctx->ref = luaL_ref(s->hlua->T, LUA_REGISTRYINDEX); |
| 10513 | filter->ctx = flt_ctx; |
| 10514 | break; |
| 10515 | case HLUA_E_ERRMSG: |
| 10516 | SEND_ERR(s->be, "Lua filter '%s' : %s.\n", conf->reg->name, lua_tostring(s->hlua->T, -1)); |
| 10517 | ret = -1; |
| 10518 | goto end; |
| 10519 | case HLUA_E_ETMOUT: |
| 10520 | SEND_ERR(s->be, "Lua filter '%s' : 'new' execution timeout.\n", conf->reg->name); |
| 10521 | ret = 0; |
| 10522 | goto end; |
| 10523 | case HLUA_E_NOMEM: |
| 10524 | SEND_ERR(s->be, "Lua filter '%s' : out of memory error.\n", conf->reg->name); |
| 10525 | ret = 0; |
| 10526 | goto end; |
| 10527 | case HLUA_E_AGAIN: |
| 10528 | case HLUA_E_YIELD: |
| 10529 | SEND_ERR(s->be, "Lua filter '%s': yield functions like core.tcp() or core.sleep()" |
| 10530 | " are not allowed from 'new' function.\n", conf->reg->name); |
| 10531 | ret = 0; |
| 10532 | goto end; |
| 10533 | case HLUA_E_ERR: |
| 10534 | SEND_ERR(s->be, "Lua filter '%s': 'new' returns an unknown error.\n", conf->reg->name); |
| 10535 | ret = 0; |
| 10536 | goto end; |
| 10537 | default: |
| 10538 | ret = 0; |
| 10539 | goto end; |
| 10540 | } |
| 10541 | |
| 10542 | end: |
| 10543 | if (s->hlua) |
| 10544 | lua_settop(s->hlua->T, 0); |
| 10545 | if (ret <= 0) { |
| 10546 | if (flt_ctx) { |
| 10547 | hlua_ctx_destroy(flt_ctx->hlua[0]); |
| 10548 | hlua_ctx_destroy(flt_ctx->hlua[1]); |
| 10549 | pool_free(pool_head_hlua_flt_ctx, flt_ctx); |
| 10550 | } |
| 10551 | } |
| 10552 | return ret; |
| 10553 | } |
| 10554 | |
| 10555 | static void hlua_filter_delete(struct stream *s, struct filter *filter) |
| 10556 | { |
| 10557 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10558 | |
| 10559 | luaL_unref(s->hlua->T, LUA_REGISTRYINDEX, flt_ctx->ref); |
| 10560 | hlua_ctx_destroy(flt_ctx->hlua[0]); |
| 10561 | hlua_ctx_destroy(flt_ctx->hlua[1]); |
| 10562 | pool_free(pool_head_hlua_flt_ctx, flt_ctx); |
| 10563 | filter->ctx = NULL; |
| 10564 | } |
| 10565 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 10566 | static int hlua_filter_from_payload(struct filter *filter) |
| 10567 | { |
| 10568 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10569 | |
| 10570 | return (flt_ctx && !!(flt_ctx->flags & HLUA_FLT_CTX_FL_PAYLOAD)); |
| 10571 | } |
| 10572 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10573 | static int hlua_filter_callback(struct stream *s, struct filter *filter, const char *fun, |
| 10574 | int dir, unsigned int flags) |
| 10575 | { |
| 10576 | struct hlua *flt_hlua; |
| 10577 | struct hlua_flt_config *conf = FLT_CONF(filter); |
| 10578 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10579 | unsigned int hflags = HLUA_TXN_FLT_CTX; |
| 10580 | int ret = 1; |
| 10581 | |
| 10582 | flt_hlua = flt_ctx->hlua[(dir == SMP_OPT_DIR_REQ ? 0 : 1)]; |
| 10583 | if (!flt_hlua) |
| 10584 | goto end; |
| 10585 | |
| 10586 | if (!HLUA_IS_RUNNING(flt_hlua)) { |
| 10587 | int extra_idx = lua_gettop(flt_hlua->T); |
| 10588 | |
| 10589 | /* The following Lua calls can fail. */ |
| 10590 | if (!SET_SAFE_LJMP(flt_hlua)) { |
| 10591 | const char *error; |
| 10592 | |
| 10593 | if (lua_type(flt_hlua->T, -1) == LUA_TSTRING) |
| 10594 | error = lua_tostring(flt_hlua->T, -1); |
| 10595 | else |
| 10596 | error = "critical error"; |
| 10597 | SEND_ERR(s->be, "Lua filter '%s': %s.\n", conf->reg->name, error); |
| 10598 | goto end; |
| 10599 | } |
| 10600 | |
| 10601 | /* Check stack size. */ |
| 10602 | if (!lua_checkstack(flt_hlua->T, 3)) { |
| 10603 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 10604 | RESET_SAFE_LJMP(flt_hlua); |
| 10605 | goto end; |
| 10606 | } |
| 10607 | |
| 10608 | lua_rawgeti(flt_hlua->T, LUA_REGISTRYINDEX, flt_ctx->ref); |
| 10609 | if (lua_getfield(flt_hlua->T, -1, fun) != LUA_TFUNCTION) { |
| 10610 | RESET_SAFE_LJMP(flt_hlua); |
| 10611 | goto end; |
| 10612 | } |
| 10613 | lua_insert(flt_hlua->T, -2); |
| 10614 | |
| 10615 | if (!hlua_txn_new(flt_hlua->T, s, s->be, dir, hflags)) { |
| 10616 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 10617 | RESET_SAFE_LJMP(flt_hlua); |
| 10618 | goto end; |
| 10619 | } |
| 10620 | flt_hlua->nargs = 2; |
| 10621 | |
| 10622 | if (flags & HLUA_FLT_CB_ARG_CHN) { |
| 10623 | if (dir == SMP_OPT_DIR_REQ) |
| 10624 | lua_getfield(flt_hlua->T, -1, "req"); |
| 10625 | else |
| 10626 | lua_getfield(flt_hlua->T, -1, "res"); |
| 10627 | if (lua_type(flt_hlua->T, -1) == LUA_TTABLE) { |
| 10628 | lua_pushstring(flt_hlua->T, "__filter"); |
| 10629 | lua_pushlightuserdata(flt_hlua->T, filter); |
| 10630 | lua_settable(flt_hlua->T, -3); |
| 10631 | } |
| 10632 | flt_hlua->nargs++; |
| 10633 | } |
| 10634 | else if (flags & HLUA_FLT_CB_ARG_HTTP_MSG) { |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 10635 | if (dir == SMP_OPT_DIR_REQ) |
| 10636 | lua_getfield(flt_hlua->T, -1, "http_req"); |
| 10637 | else |
| 10638 | lua_getfield(flt_hlua->T, -1, "http_res"); |
| 10639 | if (lua_type(flt_hlua->T, -1) == LUA_TTABLE) { |
| 10640 | lua_pushstring(flt_hlua->T, "__filter"); |
| 10641 | lua_pushlightuserdata(flt_hlua->T, filter); |
| 10642 | lua_settable(flt_hlua->T, -3); |
| 10643 | } |
| 10644 | flt_hlua->nargs++; |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10645 | } |
| 10646 | |
| 10647 | /* Check stack size. */ |
| 10648 | if (!lua_checkstack(flt_hlua->T, 1)) { |
| 10649 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 10650 | RESET_SAFE_LJMP(flt_hlua); |
| 10651 | goto end; |
| 10652 | } |
| 10653 | |
| 10654 | while (extra_idx--) { |
| 10655 | lua_pushvalue(flt_hlua->T, 1); |
| 10656 | lua_remove(flt_hlua->T, 1); |
| 10657 | flt_hlua->nargs++; |
| 10658 | } |
| 10659 | |
| 10660 | /* We must initialize the execution timeouts. */ |
| 10661 | flt_hlua->max_time = hlua_timeout_session; |
| 10662 | |
| 10663 | /* At this point the execution is safe. */ |
| 10664 | RESET_SAFE_LJMP(flt_hlua); |
| 10665 | } |
| 10666 | |
| 10667 | switch (hlua_ctx_resume(flt_hlua, !(flags & HLUA_FLT_CB_FINAL))) { |
| 10668 | case HLUA_E_OK: |
| 10669 | /* Catch the return value if it required */ |
| 10670 | if ((flags & HLUA_FLT_CB_RETVAL) && lua_gettop(flt_hlua->T) > 0) { |
| 10671 | ret = lua_tointeger(flt_hlua->T, -1); |
| 10672 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 10673 | } |
| 10674 | |
| 10675 | /* Set timeout in the required channel. */ |
| 10676 | if (flt_hlua->wake_time != TICK_ETERNITY) { |
| 10677 | if (dir == SMP_OPT_DIR_REQ) |
| 10678 | s->req.analyse_exp = flt_hlua->wake_time; |
| 10679 | else |
| 10680 | s->res.analyse_exp = flt_hlua->wake_time; |
| 10681 | } |
| 10682 | break; |
| 10683 | case HLUA_E_AGAIN: |
| 10684 | /* Set timeout in the required channel. */ |
| 10685 | if (flt_hlua->wake_time != TICK_ETERNITY) { |
| 10686 | if (dir == SMP_OPT_DIR_REQ) |
| 10687 | s->req.analyse_exp = flt_hlua->wake_time; |
| 10688 | else |
| 10689 | s->res.analyse_exp = flt_hlua->wake_time; |
| 10690 | } |
| 10691 | /* Some actions can be wake up when a "write" event |
| 10692 | * is detected on a response channel. This is useful |
| 10693 | * only for actions targeted on the requests. |
| 10694 | */ |
| 10695 | if (HLUA_IS_WAKERESWR(flt_hlua)) |
| 10696 | s->res.flags |= CF_WAKE_WRITE; |
| 10697 | if (HLUA_IS_WAKEREQWR(flt_hlua)) |
| 10698 | s->req.flags |= CF_WAKE_WRITE; |
| 10699 | ret = 0; |
| 10700 | goto end; |
| 10701 | case HLUA_E_ERRMSG: |
| 10702 | SEND_ERR(s->be, "Lua filter '%s' : %s.\n", conf->reg->name, lua_tostring(flt_hlua->T, -1)); |
| 10703 | ret = -1; |
| 10704 | goto end; |
| 10705 | case HLUA_E_ETMOUT: |
| 10706 | SEND_ERR(s->be, "Lua filter '%s' : '%s' callback execution timeout.\n", conf->reg->name, fun); |
| 10707 | goto end; |
| 10708 | case HLUA_E_NOMEM: |
| 10709 | SEND_ERR(s->be, "Lua filter '%s' : out of memory error.\n", conf->reg->name); |
| 10710 | goto end; |
| 10711 | case HLUA_E_YIELD: |
| 10712 | SEND_ERR(s->be, "Lua filter '%s': yield functions like core.tcp() or core.sleep()" |
| 10713 | " are not allowed from '%s' callback.\n", conf->reg->name, fun); |
| 10714 | goto end; |
| 10715 | case HLUA_E_ERR: |
| 10716 | SEND_ERR(s->be, "Lua filter '%s': '%s' returns an unknown error.\n", conf->reg->name, fun); |
| 10717 | goto end; |
| 10718 | default: |
| 10719 | goto end; |
| 10720 | } |
| 10721 | |
| 10722 | |
| 10723 | end: |
| 10724 | return ret; |
| 10725 | } |
| 10726 | |
| 10727 | static int hlua_filter_start_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 10728 | { |
| 10729 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10730 | |
| 10731 | flt_ctx->flags = 0; |
| 10732 | return hlua_filter_callback(s, filter, "start_analyze", |
| 10733 | (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 10734 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_CHN)); |
| 10735 | } |
| 10736 | |
| 10737 | static int hlua_filter_end_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 10738 | { |
| 10739 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10740 | |
| 10741 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 10742 | return hlua_filter_callback(s, filter, "end_analyze", |
| 10743 | (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 10744 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_CHN)); |
| 10745 | } |
| 10746 | |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 10747 | static int hlua_filter_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg) |
| 10748 | { |
| 10749 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10750 | |
| 10751 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 10752 | return hlua_filter_callback(s, filter, "http_headers", |
| 10753 | (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 10754 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 10755 | } |
| 10756 | |
| 10757 | static int hlua_filter_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg, |
| 10758 | unsigned int offset, unsigned int len) |
| 10759 | { |
| 10760 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10761 | struct hlua *flt_hlua; |
| 10762 | int dir = (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES); |
| 10763 | int idx = (dir == SMP_OPT_DIR_REQ ? 0 : 1); |
| 10764 | int ret; |
| 10765 | |
| 10766 | flt_hlua = flt_ctx->hlua[idx]; |
| 10767 | flt_ctx->cur_off[idx] = offset; |
| 10768 | flt_ctx->cur_len[idx] = len; |
| 10769 | flt_ctx->flags |= HLUA_FLT_CTX_FL_PAYLOAD; |
| 10770 | ret = hlua_filter_callback(s, filter, "http_payload", dir, (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 10771 | if (ret != -1) { |
| 10772 | ret = flt_ctx->cur_len[idx]; |
| 10773 | if (lua_gettop(flt_hlua->T) > 0) { |
| 10774 | ret = lua_tointeger(flt_hlua->T, -1); |
| 10775 | if (ret > flt_ctx->cur_len[idx]) |
| 10776 | ret = flt_ctx->cur_len[idx]; |
| 10777 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 10778 | } |
| 10779 | } |
| 10780 | return ret; |
| 10781 | } |
| 10782 | |
| 10783 | static int hlua_filter_http_end(struct stream *s, struct filter *filter, struct http_msg *msg) |
| 10784 | { |
| 10785 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10786 | |
| 10787 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 10788 | return hlua_filter_callback(s, filter, "http_end", |
| 10789 | (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 10790 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 10791 | } |
| 10792 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10793 | static int hlua_filter_tcp_payload(struct stream *s, struct filter *filter, struct channel *chn, |
| 10794 | unsigned int offset, unsigned int len) |
| 10795 | { |
| 10796 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10797 | struct hlua *flt_hlua; |
| 10798 | int dir = (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES); |
| 10799 | int idx = (dir == SMP_OPT_DIR_REQ ? 0 : 1); |
| 10800 | int ret; |
| 10801 | |
| 10802 | flt_hlua = flt_ctx->hlua[idx]; |
| 10803 | flt_ctx->cur_off[idx] = offset; |
| 10804 | flt_ctx->cur_len[idx] = len; |
| 10805 | flt_ctx->flags |= HLUA_FLT_CTX_FL_PAYLOAD; |
| 10806 | ret = hlua_filter_callback(s, filter, "tcp_payload", dir, (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_ARG_CHN)); |
| 10807 | if (ret != -1) { |
| 10808 | ret = flt_ctx->cur_len[idx]; |
| 10809 | if (lua_gettop(flt_hlua->T) > 0) { |
| 10810 | ret = lua_tointeger(flt_hlua->T, -1); |
| 10811 | if (ret > flt_ctx->cur_len[idx]) |
| 10812 | ret = flt_ctx->cur_len[idx]; |
| 10813 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 10814 | } |
| 10815 | } |
| 10816 | return ret; |
| 10817 | } |
| 10818 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10819 | static int hlua_filter_parse_fct(char **args, int *cur_arg, struct proxy *px, |
| 10820 | struct flt_conf *fconf, char **err, void *private) |
| 10821 | { |
| 10822 | struct hlua_reg_filter *reg_flt = private; |
| 10823 | lua_State *L; |
| 10824 | struct hlua_flt_config *conf = NULL; |
| 10825 | const char *flt_id = NULL; |
| 10826 | int state_id, pos, flt_flags = 0; |
| 10827 | struct flt_ops *hlua_flt_ops = NULL; |
| 10828 | |
| 10829 | state_id = reg_flt_to_stack_id(reg_flt); |
| 10830 | L = hlua_states[state_id]; |
| 10831 | |
| 10832 | /* Initialize the filter ops with default callbacks */ |
| 10833 | hlua_flt_ops = calloc(1, sizeof(*hlua_flt_ops)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10834 | if (!hlua_flt_ops) |
| 10835 | goto error; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10836 | hlua_flt_ops->init = hlua_filter_init; |
| 10837 | hlua_flt_ops->deinit = hlua_filter_deinit; |
| 10838 | if (state_id) { |
| 10839 | /* Set per-thread callback if script is loaded per-thread */ |
| 10840 | hlua_flt_ops->init_per_thread = hlua_filter_init_per_thread; |
| 10841 | hlua_flt_ops->deinit_per_thread = hlua_filter_deinit_per_thread; |
| 10842 | } |
| 10843 | hlua_flt_ops->attach = hlua_filter_new; |
| 10844 | hlua_flt_ops->detach = hlua_filter_delete; |
| 10845 | |
| 10846 | /* Push the filter class on the stack and resolve all callbacks */ |
| 10847 | lua_rawgeti(L, LUA_REGISTRYINDEX, reg_flt->flt_ref[state_id]); |
| 10848 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10849 | if (lua_getfield(L, -1, "start_analyze") == LUA_TFUNCTION) |
| 10850 | hlua_flt_ops->channel_start_analyze = hlua_filter_start_analyze; |
| 10851 | lua_pop(L, 1); |
| 10852 | if (lua_getfield(L, -1, "end_analyze") == LUA_TFUNCTION) |
| 10853 | hlua_flt_ops->channel_end_analyze = hlua_filter_end_analyze; |
| 10854 | lua_pop(L, 1); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 10855 | if (lua_getfield(L, -1, "http_headers") == LUA_TFUNCTION) |
| 10856 | hlua_flt_ops->http_headers = hlua_filter_http_headers; |
| 10857 | lua_pop(L, 1); |
| 10858 | if (lua_getfield(L, -1, "http_payload") == LUA_TFUNCTION) |
| 10859 | hlua_flt_ops->http_payload = hlua_filter_http_payload; |
| 10860 | lua_pop(L, 1); |
| 10861 | if (lua_getfield(L, -1, "http_end") == LUA_TFUNCTION) |
| 10862 | hlua_flt_ops->http_end = hlua_filter_http_end; |
| 10863 | lua_pop(L, 1); |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10864 | if (lua_getfield(L, -1, "tcp_payload") == LUA_TFUNCTION) |
| 10865 | hlua_flt_ops->tcp_payload = hlua_filter_tcp_payload; |
| 10866 | lua_pop(L, 1); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10867 | |
| 10868 | /* Get id and flags of the filter class */ |
| 10869 | if (lua_getfield(L, -1, "id") == LUA_TSTRING) |
| 10870 | flt_id = lua_tostring(L, -1); |
| 10871 | lua_pop(L, 1); |
| 10872 | if (lua_getfield(L, -1, "flags") == LUA_TNUMBER) |
| 10873 | flt_flags = lua_tointeger(L, -1); |
| 10874 | lua_pop(L, 1); |
| 10875 | |
| 10876 | /* Create the filter config */ |
| 10877 | conf = calloc(1, sizeof(*conf)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10878 | if (!conf) |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10879 | goto error; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10880 | conf->reg = reg_flt; |
| 10881 | |
| 10882 | /* duplicate args */ |
| 10883 | for (pos = 0; *args[*cur_arg + 1 + pos]; pos++); |
| 10884 | conf->args = calloc(pos + 1, sizeof(*conf->args)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10885 | if (!conf->args) |
| 10886 | goto error; |
| 10887 | for (pos = 0; *args[*cur_arg + 1 + pos]; pos++) { |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10888 | conf->args[pos] = strdup(args[*cur_arg + 1 + pos]); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10889 | if (!conf->args[pos]) |
| 10890 | goto error; |
| 10891 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10892 | conf->args[pos] = NULL; |
| 10893 | *cur_arg += pos + 1; |
| 10894 | |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10895 | if (flt_id) { |
| 10896 | fconf->id = strdup(flt_id); |
| 10897 | if (!fconf->id) |
| 10898 | goto error; |
| 10899 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10900 | fconf->flags = flt_flags; |
| 10901 | fconf->conf = conf; |
| 10902 | fconf->ops = hlua_flt_ops; |
| 10903 | |
| 10904 | lua_settop(L, 0); |
| 10905 | return 0; |
| 10906 | |
| 10907 | error: |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10908 | 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] | 10909 | free(hlua_flt_ops); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10910 | if (conf && conf->args) { |
| 10911 | for (pos = 0; conf->args[pos]; pos++) |
| 10912 | free(conf->args[pos]); |
| 10913 | free(conf->args); |
| 10914 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10915 | free(conf); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10916 | free((char *)fconf->id); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10917 | lua_settop(L, 0); |
| 10918 | return -1; |
| 10919 | } |
| 10920 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10921 | __LJMP static int hlua_register_data_filter(lua_State *L) |
| 10922 | { |
| 10923 | struct filter *filter; |
| 10924 | struct channel *chn; |
| 10925 | |
| 10926 | MAY_LJMP(check_args(L, 2, "register_data_filter")); |
| 10927 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
| 10928 | chn = MAY_LJMP(hlua_checkchannel(L, 2)); |
| 10929 | |
| 10930 | lua_getfield(L, 1, "__filter"); |
| 10931 | MAY_LJMP(luaL_checktype(L, -1, LUA_TLIGHTUSERDATA)); |
| 10932 | filter = lua_touserdata (L, -1); |
| 10933 | lua_pop(L, 1); |
| 10934 | |
| 10935 | register_data_filter(chn_strm(chn), chn, filter); |
| 10936 | return 1; |
| 10937 | } |
| 10938 | |
| 10939 | __LJMP static int hlua_unregister_data_filter(lua_State *L) |
| 10940 | { |
| 10941 | struct filter *filter; |
| 10942 | struct channel *chn; |
| 10943 | |
| 10944 | MAY_LJMP(check_args(L, 2, "unregister_data_filter")); |
| 10945 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
| 10946 | chn = MAY_LJMP(hlua_checkchannel(L, 2)); |
| 10947 | |
| 10948 | lua_getfield(L, 1, "__filter"); |
| 10949 | MAY_LJMP(luaL_checktype(L, -1, LUA_TLIGHTUSERDATA)); |
| 10950 | filter = lua_touserdata (L, -1); |
| 10951 | lua_pop(L, 1); |
| 10952 | |
| 10953 | unregister_data_filter(chn_strm(chn), chn, filter); |
| 10954 | return 1; |
| 10955 | } |
| 10956 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10957 | /* 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] | 10958 | * 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] | 10959 | * parse configuration arguments. |
| 10960 | */ |
| 10961 | __LJMP static int hlua_register_filter(lua_State *L) |
| 10962 | { |
| 10963 | struct buffer *trash; |
| 10964 | struct flt_kw_list *fkl; |
| 10965 | struct flt_kw *fkw; |
| 10966 | const char *name; |
| 10967 | struct hlua_reg_filter *reg_flt= NULL; |
| 10968 | int flt_ref, fun_ref; |
| 10969 | int len; |
| 10970 | |
| 10971 | MAY_LJMP(check_args(L, 3, "register_filter")); |
| 10972 | |
| 10973 | /* First argument : filter name. */ |
| 10974 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 10975 | |
| 10976 | /* Second argument : The filter class */ |
| 10977 | flt_ref = MAY_LJMP(hlua_checktable(L, 2)); |
| 10978 | |
| 10979 | /* Third argument : lua function. */ |
| 10980 | fun_ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 10981 | |
| 10982 | trash = get_trash_chunk(); |
| 10983 | chunk_printf(trash, "lua.%s", name); |
| 10984 | fkw = flt_find_kw(trash->area); |
| 10985 | if (fkw != NULL) { |
| 10986 | reg_flt = fkw->private; |
| 10987 | if (reg_flt->flt_ref[hlua_state_id] != -1 || reg_flt->fun_ref[hlua_state_id] != -1) { |
| 10988 | ha_warning("Trying to register filter 'lua.%s' more than once. " |
| 10989 | "This will become a hard error in version 2.5.\n", name); |
| 10990 | } |
| 10991 | reg_flt->flt_ref[hlua_state_id] = flt_ref; |
| 10992 | reg_flt->fun_ref[hlua_state_id] = fun_ref; |
| 10993 | return 0; |
| 10994 | } |
| 10995 | |
| 10996 | fkl = calloc(1, sizeof(*fkl) + sizeof(struct flt_kw) * 2); |
| 10997 | if (!fkl) |
| 10998 | goto alloc_error; |
| 10999 | fkl->scope = "HLUA"; |
| 11000 | |
| 11001 | reg_flt = new_hlua_reg_filter(name); |
| 11002 | if (!reg_flt) |
| 11003 | goto alloc_error; |
| 11004 | |
| 11005 | reg_flt->flt_ref[hlua_state_id] = flt_ref; |
| 11006 | reg_flt->fun_ref[hlua_state_id] = fun_ref; |
| 11007 | |
| 11008 | /* The filter keyword */ |
| 11009 | len = strlen("lua.") + strlen(name) + 1; |
| 11010 | fkl->kw[0].kw = calloc(1, len); |
| 11011 | if (!fkl->kw[0].kw) |
| 11012 | goto alloc_error; |
| 11013 | |
| 11014 | snprintf((char *)fkl->kw[0].kw, len, "lua.%s", name); |
| 11015 | |
| 11016 | fkl->kw[0].parse = hlua_filter_parse_fct; |
| 11017 | fkl->kw[0].private = reg_flt; |
| 11018 | memset(&fkl->kw[1], 0, sizeof(*fkl->kw)); |
| 11019 | |
| 11020 | /* Register this new filter */ |
| 11021 | flt_register_keywords(fkl); |
| 11022 | |
| 11023 | return 0; |
| 11024 | |
| 11025 | alloc_error: |
| 11026 | release_hlua_reg_filter(reg_flt); |
| 11027 | ha_free(&fkl); |
| 11028 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 11029 | return 0; /* Never reached */ |
| 11030 | } |
| 11031 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11032 | 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] | 11033 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11034 | char **err, unsigned int *timeout) |
| 11035 | { |
| 11036 | const char *error; |
| 11037 | |
| 11038 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 11039 | if (error == PARSE_TIME_OVER) { |
| 11040 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 11041 | args[1], args[0]); |
| 11042 | return -1; |
| 11043 | } |
| 11044 | else if (error == PARSE_TIME_UNDER) { |
| 11045 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 11046 | args[1], args[0]); |
| 11047 | return -1; |
| 11048 | } |
| 11049 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11050 | memprintf(err, "%s: invalid timeout", args[0]); |
| 11051 | return -1; |
| 11052 | } |
| 11053 | return 0; |
| 11054 | } |
| 11055 | |
| 11056 | 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] | 11057 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11058 | char **err) |
| 11059 | { |
| 11060 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 11061 | file, line, err, &hlua_timeout_session); |
| 11062 | } |
| 11063 | |
| 11064 | 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] | 11065 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11066 | char **err) |
| 11067 | { |
| 11068 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 11069 | file, line, err, &hlua_timeout_task); |
| 11070 | } |
| 11071 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11072 | 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] | 11073 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11074 | char **err) |
| 11075 | { |
| 11076 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 11077 | file, line, err, &hlua_timeout_applet); |
| 11078 | } |
| 11079 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 11080 | 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] | 11081 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 11082 | char **err) |
| 11083 | { |
| 11084 | char *error; |
| 11085 | |
| 11086 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 11087 | if (*error != '\0') { |
| 11088 | memprintf(err, "%s: invalid number", args[0]); |
| 11089 | return -1; |
| 11090 | } |
| 11091 | return 0; |
| 11092 | } |
| 11093 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11094 | 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] | 11095 | const struct proxy *defpx, const char *file, int line, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11096 | char **err) |
| 11097 | { |
| 11098 | char *error; |
| 11099 | |
| 11100 | if (*(args[1]) == 0) { |
| 11101 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 11102 | return -1; |
| 11103 | } |
| 11104 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 11105 | if (*error != '\0') { |
| 11106 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 11107 | return -1; |
| 11108 | } |
| 11109 | return 0; |
| 11110 | } |
| 11111 | |
| 11112 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11113 | /* This function is called by the main configuration key "lua-load". It loads and |
| 11114 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 11115 | * the main lua entry point. |
| 11116 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 11117 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 11118 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11119 | * |
| 11120 | * In some error case, LUA set an error message in top of the stack. This function |
| 11121 | * 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] | 11122 | * |
| 11123 | * This function can fail with an abort() due to an Lua critical error. |
| 11124 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 11125 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11126 | */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11127 | static int hlua_load_state(char *filename, lua_State *L, char **err) |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11128 | { |
| 11129 | int error; |
| 11130 | |
| 11131 | /* Just load and compile the file. */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11132 | error = luaL_loadfile(L, filename); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11133 | if (error) { |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11134 | memprintf(err, "error in Lua file '%s': %s", filename, lua_tostring(L, -1)); |
| 11135 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11136 | return -1; |
| 11137 | } |
| 11138 | |
| 11139 | /* If no syntax error where detected, execute the code. */ |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11140 | error = lua_pcall(L, 0, LUA_MULTRET, 0); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11141 | switch (error) { |
| 11142 | case LUA_OK: |
| 11143 | break; |
| 11144 | case LUA_ERRRUN: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11145 | memprintf(err, "Lua runtime error: %s\n", lua_tostring(L, -1)); |
| 11146 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11147 | return -1; |
| 11148 | case LUA_ERRMEM: |
Thierry Fournier | de6145f | 2020-11-29 00:55:53 +0100 | [diff] [blame] | 11149 | memprintf(err, "Lua out of memory error\n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11150 | return -1; |
| 11151 | case LUA_ERRERR: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11152 | memprintf(err, "Lua message handler error: %s\n", lua_tostring(L, -1)); |
| 11153 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11154 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 11155 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11156 | case LUA_ERRGCMM: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11157 | memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(L, -1)); |
| 11158 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11159 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 11160 | #endif |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11161 | default: |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11162 | memprintf(err, "Lua unknown error: %s\n", lua_tostring(L, -1)); |
| 11163 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11164 | return -1; |
| 11165 | } |
| 11166 | |
| 11167 | return 0; |
| 11168 | } |
| 11169 | |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11170 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 11171 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11172 | char **err) |
| 11173 | { |
| 11174 | if (*(args[1]) == 0) { |
| 11175 | memprintf(err, "'%s' expects a file name as parameter.\n", args[0]); |
| 11176 | return -1; |
| 11177 | } |
| 11178 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11179 | /* loading for global state */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 11180 | hlua_state_id = 0; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11181 | ha_set_thread(NULL); |
Thierry Fournier | afc63e2 | 2020-11-28 17:06:51 +0100 | [diff] [blame] | 11182 | return hlua_load_state(args[1], hlua_states[0], err); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11183 | } |
| 11184 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11185 | 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] | 11186 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11187 | char **err) |
| 11188 | { |
| 11189 | int len; |
| 11190 | |
| 11191 | if (*(args[1]) == 0) { |
| 11192 | memprintf(err, "'%s' expects a file as parameter.\n", args[0]); |
| 11193 | return -1; |
| 11194 | } |
| 11195 | |
| 11196 | if (per_thread_load == NULL) { |
| 11197 | /* allocate the first entry large enough to store the final NULL */ |
| 11198 | per_thread_load = calloc(1, sizeof(*per_thread_load)); |
| 11199 | if (per_thread_load == NULL) { |
| 11200 | memprintf(err, "out of memory error"); |
| 11201 | return -1; |
| 11202 | } |
| 11203 | } |
| 11204 | |
| 11205 | /* count used entries */ |
| 11206 | for (len = 0; per_thread_load[len] != NULL; len++) |
| 11207 | ; |
| 11208 | |
| 11209 | per_thread_load = realloc(per_thread_load, (len + 2) * sizeof(*per_thread_load)); |
| 11210 | if (per_thread_load == NULL) { |
| 11211 | memprintf(err, "out of memory error"); |
| 11212 | return -1; |
| 11213 | } |
| 11214 | |
| 11215 | per_thread_load[len] = strdup(args[1]); |
| 11216 | per_thread_load[len + 1] = NULL; |
| 11217 | |
| 11218 | if (per_thread_load[len] == NULL) { |
| 11219 | memprintf(err, "out of memory error"); |
| 11220 | return -1; |
| 11221 | } |
| 11222 | |
| 11223 | /* loading for thread 1 only */ |
| 11224 | hlua_state_id = 1; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11225 | ha_set_thread(NULL); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11226 | return hlua_load_state(args[1], hlua_states[1], err); |
| 11227 | } |
| 11228 | |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 11229 | /* Prepend the given <path> followed by a semicolon to the `package.<type>` variable |
| 11230 | * in the given <ctx>. |
| 11231 | */ |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 11232 | static int hlua_prepend_path(lua_State *L, char *type, char *path) |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 11233 | { |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 11234 | lua_getglobal(L, "package"); /* push package variable */ |
| 11235 | lua_pushstring(L, path); /* push given path */ |
| 11236 | lua_pushstring(L, ";"); /* push semicolon */ |
| 11237 | lua_getfield(L, -3, type); /* push old path */ |
| 11238 | lua_concat(L, 3); /* concatenate to new path */ |
| 11239 | lua_setfield(L, -2, type); /* store new path */ |
| 11240 | lua_pop(L, 1); /* pop package variable */ |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 11241 | |
| 11242 | return 0; |
| 11243 | } |
| 11244 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11245 | 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] | 11246 | const struct proxy *defpx, const char *file, int line, |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11247 | char **err) |
| 11248 | { |
| 11249 | char *path; |
| 11250 | char *type = "path"; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11251 | struct prepend_path *p = NULL; |
Bertrand Jacquin | 7fbc770 | 2021-11-24 21:16:06 +0000 | [diff] [blame] | 11252 | size_t i; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11253 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11254 | if (too_many_args(2, args, err, NULL)) { |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11255 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11256 | } |
| 11257 | |
| 11258 | if (!(*args[1])) { |
| 11259 | memprintf(err, "'%s' expects to receive a <path> as argument", args[0]); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11260 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11261 | } |
| 11262 | path = args[1]; |
| 11263 | |
| 11264 | if (*args[2]) { |
| 11265 | if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) { |
| 11266 | 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] | 11267 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11268 | } |
| 11269 | type = args[2]; |
| 11270 | } |
| 11271 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11272 | p = calloc(1, sizeof(*p)); |
| 11273 | if (p == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 11274 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11275 | goto err; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11276 | } |
| 11277 | p->path = strdup(path); |
| 11278 | if (p->path == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 11279 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11280 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11281 | } |
| 11282 | p->type = strdup(type); |
| 11283 | if (p->type == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 11284 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11285 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11286 | } |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 11287 | LIST_APPEND(&prepend_path_list, &p->l); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11288 | |
Tim Duesterhus | 9e5e586 | 2021-10-11 18:51:08 +0200 | [diff] [blame] | 11289 | /* Handle the global state and the per-thread state for the first |
| 11290 | * thread. The remaining threads will be initialized based on |
| 11291 | * prepend_path_list. |
| 11292 | */ |
Bertrand Jacquin | 7fbc770 | 2021-11-24 21:16:06 +0000 | [diff] [blame] | 11293 | for (i = 0; i < 2; i++) { |
Tim Duesterhus | 9e5e586 | 2021-10-11 18:51:08 +0200 | [diff] [blame] | 11294 | lua_State *L = hlua_states[i]; |
| 11295 | const char *error; |
| 11296 | |
| 11297 | if (setjmp(safe_ljmp_env) != 0) { |
| 11298 | lua_atpanic(L, hlua_panic_safe); |
| 11299 | if (lua_type(L, -1) == LUA_TSTRING) |
| 11300 | error = lua_tostring(L, -1); |
| 11301 | else |
| 11302 | error = "critical error"; |
| 11303 | fprintf(stderr, "lua-prepend-path: %s.\n", error); |
| 11304 | exit(1); |
| 11305 | } else { |
| 11306 | lua_atpanic(L, hlua_panic_ljmp); |
| 11307 | } |
| 11308 | |
| 11309 | hlua_prepend_path(L, type, path); |
| 11310 | |
| 11311 | lua_atpanic(L, hlua_panic_safe); |
| 11312 | } |
| 11313 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11314 | return 0; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11315 | |
| 11316 | err2: |
| 11317 | free(p->type); |
| 11318 | free(p->path); |
| 11319 | err: |
| 11320 | free(p); |
| 11321 | return -1; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11322 | } |
| 11323 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11324 | /* configuration keywords declaration */ |
| 11325 | static struct cfg_kw_list cfg_kws = {{ },{ |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11326 | { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11327 | { CFG_GLOBAL, "lua-load", hlua_load }, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11328 | { CFG_GLOBAL, "lua-load-per-thread", hlua_load_per_thread }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11329 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 11330 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 11331 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 11332 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11333 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11334 | { 0, NULL, NULL }, |
| 11335 | }}; |
| 11336 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 11337 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 11338 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 11339 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 11340 | /* This function can fail with an abort() due to an Lua critical error. |
| 11341 | * We are in the initialisation process of HAProxy, this abort() is |
| 11342 | * tolerated. |
| 11343 | */ |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11344 | int hlua_post_init_state(lua_State *L) |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11345 | { |
| 11346 | struct hlua_init_function *init; |
| 11347 | const char *msg; |
| 11348 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 11349 | const char *error; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11350 | const char *kind; |
| 11351 | const char *trace; |
| 11352 | int return_status = 1; |
| 11353 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 11354 | int nres; |
| 11355 | #endif |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11356 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11357 | /* disable memory limit checks if limit is not set */ |
| 11358 | if (!hlua_global_allocator.limit) |
| 11359 | hlua_global_allocator.limit = ~hlua_global_allocator.limit; |
| 11360 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 11361 | /* Call post initialisation function in safe environment. */ |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11362 | if (setjmp(safe_ljmp_env) != 0) { |
| 11363 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11364 | if (lua_type(L, -1) == LUA_TSTRING) |
| 11365 | error = lua_tostring(L, -1); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 11366 | else |
| 11367 | error = "critical error"; |
| 11368 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 11369 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11370 | } else { |
| 11371 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 11372 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 11373 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11374 | hlua_fcn_post_init(L); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 11375 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 11376 | list_for_each_entry(init, &hlua_init_functions[hlua_state_id], l) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11377 | lua_rawgeti(L, LUA_REGISTRYINDEX, init->function_ref); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11378 | |
| 11379 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11380 | ret = lua_resume(L, L, 0, &nres); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11381 | #else |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11382 | ret = lua_resume(L, L, 0); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11383 | #endif |
| 11384 | kind = NULL; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11385 | switch (ret) { |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11386 | |
| 11387 | case LUA_OK: |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11388 | lua_pop(L, -1); |
Thierry Fournier | 13d08b7 | 2020-11-28 11:02:58 +0100 | [diff] [blame] | 11389 | break; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11390 | |
| 11391 | case LUA_ERRERR: |
| 11392 | kind = "message handler error"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 11393 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11394 | case LUA_ERRRUN: |
| 11395 | if (!kind) |
| 11396 | kind = "runtime error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11397 | msg = lua_tostring(L, -1); |
| 11398 | lua_settop(L, 0); /* Empty the stack. */ |
| 11399 | lua_pop(L, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 11400 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11401 | if (msg) |
| 11402 | ha_alert("Lua init: %s: '%s' from %s\n", kind, msg, trace); |
| 11403 | else |
| 11404 | ha_alert("Lua init: unknown %s from %s\n", kind, trace); |
| 11405 | return_status = 0; |
| 11406 | break; |
| 11407 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11408 | default: |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11409 | /* Unknown error */ |
| 11410 | kind = "Unknown error"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 11411 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11412 | case LUA_YIELD: |
| 11413 | /* yield is not configured at this step, this state doesn't happen */ |
| 11414 | if (!kind) |
| 11415 | kind = "yield not allowed"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 11416 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11417 | case LUA_ERRMEM: |
| 11418 | if (!kind) |
| 11419 | kind = "out of memory error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11420 | lua_settop(L, 0); |
| 11421 | lua_pop(L, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 11422 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11423 | ha_alert("Lua init: %s: %s\n", kind, trace); |
| 11424 | return_status = 0; |
| 11425 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11426 | } |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11427 | if (!return_status) |
| 11428 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11429 | } |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11430 | |
| 11431 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11432 | return return_status; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11433 | } |
| 11434 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11435 | int hlua_post_init() |
| 11436 | { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11437 | int ret; |
| 11438 | int i; |
| 11439 | int errors; |
| 11440 | char *err = NULL; |
| 11441 | struct hlua_function *fcn; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11442 | struct hlua_reg_filter *reg_flt; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11443 | |
Willy Tarreau | b131049 | 2021-08-30 09:35:18 +0200 | [diff] [blame] | 11444 | #if defined(USE_OPENSSL) |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11445 | /* Initialize SSL server. */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 11446 | if (socket_ssl->xprt->prepare_srv) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11447 | int saved_used_backed = global.ssl_used_backend; |
| 11448 | // don't affect maxconn automatic computation |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 11449 | socket_ssl->xprt->prepare_srv(socket_ssl); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11450 | global.ssl_used_backend = saved_used_backed; |
| 11451 | } |
| 11452 | #endif |
| 11453 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 11454 | /* Perform post init of common thread */ |
| 11455 | hlua_state_id = 0; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11456 | ha_set_thread(&ha_thread_info[0]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11457 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 11458 | if (ret == 0) |
| 11459 | return 0; |
| 11460 | |
| 11461 | /* init remaining lua states and load files */ |
| 11462 | for (hlua_state_id = 2; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 11463 | |
| 11464 | /* set thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11465 | ha_set_thread(&ha_thread_info[hlua_state_id - 1]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11466 | |
| 11467 | /* Init lua state */ |
| 11468 | hlua_states[hlua_state_id] = hlua_init_state(hlua_state_id); |
| 11469 | |
| 11470 | /* Load lua files */ |
| 11471 | for (i = 0; per_thread_load && per_thread_load[i]; i++) { |
| 11472 | ret = hlua_load_state(per_thread_load[i], hlua_states[hlua_state_id], &err); |
| 11473 | if (ret != 0) { |
| 11474 | ha_alert("Lua init: %s\n", err); |
| 11475 | return 0; |
| 11476 | } |
| 11477 | } |
| 11478 | } |
| 11479 | |
| 11480 | /* Reset thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11481 | ha_set_thread(NULL); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11482 | |
| 11483 | /* Execute post init for all states */ |
| 11484 | for (hlua_state_id = 1; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 11485 | |
| 11486 | /* set thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11487 | ha_set_thread(&ha_thread_info[hlua_state_id - 1]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11488 | |
| 11489 | /* run post init */ |
| 11490 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 11491 | if (ret == 0) |
| 11492 | return 0; |
| 11493 | } |
| 11494 | |
| 11495 | /* Reset thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11496 | ha_set_thread(NULL); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11497 | |
| 11498 | /* control functions registering. Each function must have: |
| 11499 | * - only the function_ref[0] set positive and all other to -1 |
| 11500 | * - only the function_ref[0] set to -1 and all other positive |
| 11501 | * This ensure a same reference is not used both in shared |
| 11502 | * lua state and thread dedicated lua state. Note: is the case |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 11503 | * reach, the shared state is priority, but the bug will be |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11504 | * complicated to found for the end user. |
| 11505 | */ |
| 11506 | errors = 0; |
| 11507 | list_for_each_entry(fcn, &referenced_functions, l) { |
| 11508 | ret = 0; |
| 11509 | for (i = 1; i < global.nbthread + 1; i++) { |
| 11510 | if (fcn->function_ref[i] == -1) |
| 11511 | ret--; |
| 11512 | else |
| 11513 | ret++; |
| 11514 | } |
| 11515 | if (abs(ret) != global.nbthread) { |
| 11516 | ha_alert("Lua function '%s' is not referenced in all thread. " |
| 11517 | "Expect function in all thread or in none thread.\n", fcn->name); |
| 11518 | errors++; |
| 11519 | continue; |
| 11520 | } |
| 11521 | |
| 11522 | if ((fcn->function_ref[0] == -1) == (ret < 0)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 11523 | ha_alert("Lua function '%s' is referenced both ins shared Lua context (through lua-load) " |
| 11524 | "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] | 11525 | "exclusive.\n", fcn->name); |
| 11526 | errors++; |
| 11527 | } |
| 11528 | } |
| 11529 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11530 | /* Do the same with registered filters */ |
| 11531 | list_for_each_entry(reg_flt, &referenced_filters, l) { |
| 11532 | ret = 0; |
| 11533 | for (i = 1; i < global.nbthread + 1; i++) { |
| 11534 | if (reg_flt->flt_ref[i] == -1) |
| 11535 | ret--; |
| 11536 | else |
| 11537 | ret++; |
| 11538 | } |
| 11539 | if (abs(ret) != global.nbthread) { |
| 11540 | ha_alert("Lua filter '%s' is not referenced in all thread. " |
| 11541 | "Expect function in all thread or in none thread.\n", reg_flt->name); |
| 11542 | errors++; |
| 11543 | continue; |
| 11544 | } |
| 11545 | |
| 11546 | if ((reg_flt->flt_ref[0] == -1) == (ret < 0)) { |
| 11547 | ha_alert("Lua filter '%s' is referenced both ins shared Lua context (through lua-load) " |
| 11548 | "and per-thread Lua context (through lua-load-per-thread). these two context " |
| 11549 | "exclusive.\n", fcn->name); |
| 11550 | errors++; |
| 11551 | } |
| 11552 | } |
| 11553 | |
| 11554 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11555 | if (errors > 0) |
| 11556 | return 0; |
| 11557 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 11558 | /* 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] | 11559 | * -1 in order to have probably a segfault if someone use it |
| 11560 | */ |
| 11561 | hlua_state_id = -1; |
| 11562 | |
| 11563 | return 1; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11564 | } |
| 11565 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11566 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 11567 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 11568 | * 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] | 11569 | * allocation. <nsize> is the requested new size. A new allocation is |
| 11570 | * indicated by <ptr> being NULL. A free is indicated by <nsize> being |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11571 | * zero. This one verifies that the limits are respected but is optimized |
| 11572 | * 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] | 11573 | * |
| 11574 | * Warning: while this API ressembles glibc's realloc() a lot, glibc surpasses |
| 11575 | * POSIX by making realloc(ptr,0) an effective free(), but others do not do |
| 11576 | * that and will simply allocate zero as if it were the result of malloc(0), |
| 11577 | * so mapping this onto realloc() will lead to memory leaks on non-glibc |
| 11578 | * systems. |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11579 | */ |
| 11580 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 11581 | { |
| 11582 | struct hlua_mem_allocator *zone = ud; |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11583 | size_t limit, old, new; |
| 11584 | |
| 11585 | /* a limit of ~0 means unlimited and boot complete, so there's no need |
| 11586 | * for accounting anymore. |
| 11587 | */ |
Willy Tarreau | a5efdff | 2021-10-22 16:00:02 +0200 | [diff] [blame] | 11588 | if (likely(~zone->limit == 0)) { |
| 11589 | if (!nsize) |
| 11590 | ha_free(&ptr); |
| 11591 | else |
| 11592 | ptr = realloc(ptr, nsize); |
| 11593 | return ptr; |
| 11594 | } |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11595 | |
Willy Tarreau | d36c7fa | 2020-12-02 12:26:29 +0100 | [diff] [blame] | 11596 | if (!ptr) |
| 11597 | osize = 0; |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11598 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11599 | /* enforce strict limits across all threads */ |
| 11600 | limit = zone->limit; |
| 11601 | old = _HA_ATOMIC_LOAD(&zone->allocated); |
| 11602 | do { |
| 11603 | new = old + nsize - osize; |
| 11604 | if (unlikely(nsize && limit && new > limit)) |
| 11605 | return NULL; |
| 11606 | } while (!_HA_ATOMIC_CAS(&zone->allocated, &old, new)); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11607 | |
Willy Tarreau | a5efdff | 2021-10-22 16:00:02 +0200 | [diff] [blame] | 11608 | if (!nsize) |
| 11609 | ha_free(&ptr); |
| 11610 | else |
| 11611 | ptr = realloc(ptr, nsize); |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11612 | |
| 11613 | if (unlikely(!ptr && nsize)) // failed |
| 11614 | _HA_ATOMIC_SUB(&zone->allocated, nsize - osize); |
| 11615 | |
| 11616 | __ha_barrier_atomic_store(); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11617 | return ptr; |
| 11618 | } |
| 11619 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 11620 | /* 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] | 11621 | * We are in the initialisation process of HAProxy, this abort() is |
| 11622 | * tolerated. |
| 11623 | */ |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 11624 | lua_State *hlua_init_state(int thread_num) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 11625 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11626 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 11627 | int idx; |
| 11628 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 11629 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 11630 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 11631 | const char *error_msg; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 11632 | void **context; |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11633 | lua_State *L; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11634 | struct prepend_path *pp; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11635 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 11636 | /* Init main lua stack. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11637 | L = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 11638 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 11639 | /* Initialise Lua context to NULL */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11640 | context = lua_getextraspace(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 11641 | *context = NULL; |
| 11642 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 11643 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 11644 | * the Lua function can fail with an abort. We are in the initialisation |
| 11645 | * process of HAProxy, this abort() is tolerated. |
| 11646 | */ |
| 11647 | |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11648 | /* Call post initialisation function in safe environment. */ |
| 11649 | if (setjmp(safe_ljmp_env) != 0) { |
| 11650 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11651 | if (lua_type(L, -1) == LUA_TSTRING) |
| 11652 | error_msg = lua_tostring(L, -1); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 11653 | else |
| 11654 | error_msg = "critical error"; |
| 11655 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
| 11656 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11657 | } else { |
| 11658 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 11659 | } |
| 11660 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 11661 | /* Initialise lua. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11662 | luaL_openlibs(L); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 11663 | #define HLUA_PREPEND_PATH_TOSTRING1(x) #x |
| 11664 | #define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x) |
| 11665 | #ifdef HLUA_PREPEND_PATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11666 | hlua_prepend_path(L, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 11667 | #endif |
| 11668 | #ifdef HLUA_PREPEND_CPATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11669 | hlua_prepend_path(L, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 11670 | #endif |
| 11671 | #undef HLUA_PREPEND_PATH_TOSTRING |
| 11672 | #undef HLUA_PREPEND_PATH_TOSTRING1 |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11673 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11674 | /* Apply configured prepend path */ |
| 11675 | list_for_each_entry(pp, &prepend_path_list, l) |
| 11676 | hlua_prepend_path(L, pp->type, pp->path); |
| 11677 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11678 | /* |
| 11679 | * |
| 11680 | * Create "core" object. |
| 11681 | * |
| 11682 | */ |
| 11683 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 11684 | /* This table entry is the object "core" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11685 | lua_newtable(L); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11686 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 11687 | /* set the thread id */ |
| 11688 | hlua_class_const_int(L, "thread", thread_num); |
| 11689 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11690 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 11691 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11692 | hlua_class_const_int(L, log_levels[i], i); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11693 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11694 | /* Register special functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11695 | hlua_class_function(L, "register_init", hlua_register_init); |
| 11696 | hlua_class_function(L, "register_task", hlua_register_task); |
| 11697 | hlua_class_function(L, "register_fetches", hlua_register_fetches); |
| 11698 | hlua_class_function(L, "register_converters", hlua_register_converters); |
| 11699 | hlua_class_function(L, "register_action", hlua_register_action); |
| 11700 | hlua_class_function(L, "register_service", hlua_register_service); |
| 11701 | hlua_class_function(L, "register_cli", hlua_register_cli); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11702 | hlua_class_function(L, "register_filter", hlua_register_filter); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11703 | hlua_class_function(L, "yield", hlua_yield); |
| 11704 | hlua_class_function(L, "set_nice", hlua_set_nice); |
| 11705 | hlua_class_function(L, "sleep", hlua_sleep); |
| 11706 | hlua_class_function(L, "msleep", hlua_msleep); |
| 11707 | hlua_class_function(L, "add_acl", hlua_add_acl); |
| 11708 | hlua_class_function(L, "del_acl", hlua_del_acl); |
| 11709 | hlua_class_function(L, "set_map", hlua_set_map); |
| 11710 | hlua_class_function(L, "del_map", hlua_del_map); |
| 11711 | hlua_class_function(L, "tcp", hlua_socket_new); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 11712 | hlua_class_function(L, "httpclient", hlua_httpclient_new); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11713 | hlua_class_function(L, "log", hlua_log); |
| 11714 | hlua_class_function(L, "Debug", hlua_log_debug); |
| 11715 | hlua_class_function(L, "Info", hlua_log_info); |
| 11716 | hlua_class_function(L, "Warning", hlua_log_warning); |
| 11717 | hlua_class_function(L, "Alert", hlua_log_alert); |
| 11718 | hlua_class_function(L, "done", hlua_done); |
| 11719 | hlua_fcn_reg_core_fcn(L); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11720 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11721 | lua_setglobal(L, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 11722 | |
| 11723 | /* |
| 11724 | * |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 11725 | * Create "act" object. |
| 11726 | * |
| 11727 | */ |
| 11728 | |
| 11729 | /* This table entry is the object "act" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11730 | lua_newtable(L); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 11731 | |
| 11732 | /* push action return constants */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11733 | hlua_class_const_int(L, "CONTINUE", ACT_RET_CONT); |
| 11734 | hlua_class_const_int(L, "STOP", ACT_RET_STOP); |
| 11735 | hlua_class_const_int(L, "YIELD", ACT_RET_YIELD); |
| 11736 | hlua_class_const_int(L, "ERROR", ACT_RET_ERR); |
| 11737 | hlua_class_const_int(L, "DONE", ACT_RET_DONE); |
| 11738 | hlua_class_const_int(L, "DENY", ACT_RET_DENY); |
| 11739 | hlua_class_const_int(L, "ABORT", ACT_RET_ABRT); |
| 11740 | hlua_class_const_int(L, "INVALID", ACT_RET_INV); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 11741 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11742 | hlua_class_function(L, "wake_time", hlua_set_wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 11743 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11744 | lua_setglobal(L, "act"); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 11745 | |
| 11746 | /* |
| 11747 | * |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11748 | * Create "Filter" object. |
| 11749 | * |
| 11750 | */ |
| 11751 | |
| 11752 | /* This table entry is the object "filter" base. */ |
| 11753 | lua_newtable(L); |
| 11754 | |
| 11755 | /* push flags and constants */ |
| 11756 | hlua_class_const_int(L, "CONTINUE", 1); |
| 11757 | hlua_class_const_int(L, "WAIT", 0); |
| 11758 | hlua_class_const_int(L, "ERROR", -1); |
| 11759 | |
| 11760 | hlua_class_const_int(L, "FLT_CFG_FL_HTX", FLT_CFG_FL_HTX); |
| 11761 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 11762 | hlua_class_function(L, "wake_time", hlua_set_wake_time); |
| 11763 | hlua_class_function(L, "register_data_filter", hlua_register_data_filter); |
| 11764 | hlua_class_function(L, "unregister_data_filter", hlua_unregister_data_filter); |
| 11765 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11766 | lua_setglobal(L, "filter"); |
| 11767 | |
| 11768 | /* |
| 11769 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11770 | * Register class Map |
| 11771 | * |
| 11772 | */ |
| 11773 | |
| 11774 | /* This table entry is the object "Map" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11775 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11776 | |
| 11777 | /* register pattern types. */ |
| 11778 | for (i=0; i<PAT_MATCH_NUM; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11779 | hlua_class_const_int(L, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 11780 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 11781 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11782 | hlua_class_const_int(L, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 11783 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11784 | |
| 11785 | /* register constructor. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11786 | hlua_class_function(L, "new", hlua_map_new); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11787 | |
| 11788 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11789 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11790 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 11791 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11792 | lua_pushstring(L, "__index"); |
| 11793 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11794 | |
| 11795 | /* Register . */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11796 | hlua_class_function(L, "lookup", hlua_map_lookup); |
| 11797 | hlua_class_function(L, "slookup", hlua_map_slookup); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11798 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11799 | lua_rawset(L, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11800 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 11801 | /* Register previous table in the registry with reference and named entry. |
| 11802 | * The function hlua_register_metatable() pops the stack, so we |
| 11803 | * previously create a copy of the table. |
| 11804 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11805 | lua_pushvalue(L, -1); /* Copy the -1 entry and push it on the stack. */ |
| 11806 | class_map_ref = hlua_register_metatable(L, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11807 | |
| 11808 | /* Assign the metatable to the mai Map object. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11809 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11810 | |
| 11811 | /* Set a name to the table. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11812 | lua_setglobal(L, "Map"); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 11813 | |
| 11814 | /* |
| 11815 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 11816 | * Register class Channel |
| 11817 | * |
| 11818 | */ |
| 11819 | |
| 11820 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11821 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 11822 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 11823 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11824 | lua_pushstring(L, "__index"); |
| 11825 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 11826 | |
| 11827 | /* Register . */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 11828 | hlua_class_function(L, "data", hlua_channel_get_data); |
| 11829 | hlua_class_function(L, "line", hlua_channel_get_line); |
| 11830 | hlua_class_function(L, "set", hlua_channel_set_data); |
| 11831 | hlua_class_function(L, "remove", hlua_channel_del_data); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11832 | hlua_class_function(L, "append", hlua_channel_append); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 11833 | hlua_class_function(L, "prepend", hlua_channel_prepend); |
| 11834 | hlua_class_function(L, "insert", hlua_channel_insert_data); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11835 | hlua_class_function(L, "send", hlua_channel_send); |
| 11836 | hlua_class_function(L, "forward", hlua_channel_forward); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 11837 | hlua_class_function(L, "input", hlua_channel_get_in_len); |
| 11838 | hlua_class_function(L, "output", hlua_channel_get_out_len); |
| 11839 | hlua_class_function(L, "may_recv", hlua_channel_may_recv); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11840 | hlua_class_function(L, "is_full", hlua_channel_is_full); |
| 11841 | hlua_class_function(L, "is_resp", hlua_channel_is_resp); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 11842 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 11843 | /* Deprecated API */ |
| 11844 | hlua_class_function(L, "get", hlua_channel_get); |
| 11845 | hlua_class_function(L, "dup", hlua_channel_dup); |
| 11846 | hlua_class_function(L, "getline", hlua_channel_getline); |
| 11847 | hlua_class_function(L, "get_in_len", hlua_channel_get_in_len); |
| 11848 | hlua_class_function(L, "get_out_len", hlua_channel_get_out_len); |
| 11849 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11850 | lua_rawset(L, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 11851 | |
| 11852 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11853 | class_channel_ref = hlua_register_metatable(L, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 11854 | |
| 11855 | /* |
| 11856 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 11857 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 11858 | * |
| 11859 | */ |
| 11860 | |
| 11861 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11862 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 11863 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 11864 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11865 | lua_pushstring(L, "__index"); |
| 11866 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 11867 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 11868 | /* Browse existing fetches and create the associated |
| 11869 | * object method. |
| 11870 | */ |
| 11871 | sf = NULL; |
| 11872 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 11873 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 11874 | * by an underscore. |
| 11875 | */ |
Willy Tarreau | 5ef9656 | 2021-08-26 16:48:53 +0200 | [diff] [blame] | 11876 | strlcpy2(trash.area, sf->kw, trash.size); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 11877 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 11878 | if (*p == '.' || *p == '-' || *p == '+') |
| 11879 | *p = '_'; |
| 11880 | |
| 11881 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11882 | lua_pushstring(L, trash.area); |
| 11883 | lua_pushlightuserdata(L, sf); |
| 11884 | lua_pushcclosure(L, hlua_run_sample_fetch, 1); |
| 11885 | lua_rawset(L, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 11886 | } |
| 11887 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11888 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 11889 | |
| 11890 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11891 | class_fetches_ref = hlua_register_metatable(L, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 11892 | |
| 11893 | /* |
| 11894 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 11895 | * Register class Converters |
| 11896 | * |
| 11897 | */ |
| 11898 | |
| 11899 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11900 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 11901 | |
| 11902 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11903 | lua_pushstring(L, "__index"); |
| 11904 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 11905 | |
| 11906 | /* Browse existing converters and create the associated |
| 11907 | * object method. |
| 11908 | */ |
| 11909 | sc = NULL; |
| 11910 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 11911 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 11912 | * by an underscore. |
| 11913 | */ |
Willy Tarreau | 5ef9656 | 2021-08-26 16:48:53 +0200 | [diff] [blame] | 11914 | strlcpy2(trash.area, sc->kw, trash.size); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 11915 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 11916 | if (*p == '.' || *p == '-' || *p == '+') |
| 11917 | *p = '_'; |
| 11918 | |
| 11919 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11920 | lua_pushstring(L, trash.area); |
| 11921 | lua_pushlightuserdata(L, sc); |
| 11922 | lua_pushcclosure(L, hlua_run_sample_conv, 1); |
| 11923 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 11924 | } |
| 11925 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11926 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 11927 | |
| 11928 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11929 | class_converters_ref = hlua_register_metatable(L, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 11930 | |
| 11931 | /* |
| 11932 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 11933 | * Register class HTTP |
| 11934 | * |
| 11935 | */ |
| 11936 | |
| 11937 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11938 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 11939 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 11940 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11941 | lua_pushstring(L, "__index"); |
| 11942 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 11943 | |
| 11944 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11945 | hlua_class_function(L, "req_get_headers",hlua_http_req_get_headers); |
| 11946 | hlua_class_function(L, "req_del_header", hlua_http_req_del_hdr); |
| 11947 | hlua_class_function(L, "req_rep_header", hlua_http_req_rep_hdr); |
| 11948 | hlua_class_function(L, "req_rep_value", hlua_http_req_rep_val); |
| 11949 | hlua_class_function(L, "req_add_header", hlua_http_req_add_hdr); |
| 11950 | hlua_class_function(L, "req_set_header", hlua_http_req_set_hdr); |
| 11951 | hlua_class_function(L, "req_set_method", hlua_http_req_set_meth); |
| 11952 | hlua_class_function(L, "req_set_path", hlua_http_req_set_path); |
| 11953 | hlua_class_function(L, "req_set_query", hlua_http_req_set_query); |
| 11954 | hlua_class_function(L, "req_set_uri", hlua_http_req_set_uri); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 11955 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11956 | hlua_class_function(L, "res_get_headers",hlua_http_res_get_headers); |
| 11957 | hlua_class_function(L, "res_del_header", hlua_http_res_del_hdr); |
| 11958 | hlua_class_function(L, "res_rep_header", hlua_http_res_rep_hdr); |
| 11959 | hlua_class_function(L, "res_rep_value", hlua_http_res_rep_val); |
| 11960 | hlua_class_function(L, "res_add_header", hlua_http_res_add_hdr); |
| 11961 | hlua_class_function(L, "res_set_header", hlua_http_res_set_hdr); |
| 11962 | hlua_class_function(L, "res_set_status", hlua_http_res_set_status); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 11963 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11964 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 11965 | |
| 11966 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11967 | class_http_ref = hlua_register_metatable(L, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 11968 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 11969 | /* |
| 11970 | * |
| 11971 | * Register class HTTPMessage |
| 11972 | * |
| 11973 | */ |
| 11974 | |
| 11975 | /* Create and fill the metatable. */ |
| 11976 | lua_newtable(L); |
| 11977 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 11978 | /* Create and fill the __index entry. */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 11979 | lua_pushstring(L, "__index"); |
| 11980 | lua_newtable(L); |
| 11981 | |
| 11982 | /* Register Lua functions. */ |
| 11983 | hlua_class_function(L, "is_resp", hlua_http_msg_is_resp); |
| 11984 | hlua_class_function(L, "get_stline", hlua_http_msg_get_stline); |
| 11985 | hlua_class_function(L, "get_headers", hlua_http_msg_get_headers); |
| 11986 | hlua_class_function(L, "del_header", hlua_http_msg_del_hdr); |
| 11987 | hlua_class_function(L, "rep_header", hlua_http_msg_rep_hdr); |
| 11988 | hlua_class_function(L, "rep_value", hlua_http_msg_rep_val); |
| 11989 | hlua_class_function(L, "add_header", hlua_http_msg_add_hdr); |
| 11990 | hlua_class_function(L, "set_header", hlua_http_msg_set_hdr); |
| 11991 | hlua_class_function(L, "set_method", hlua_http_msg_set_meth); |
| 11992 | hlua_class_function(L, "set_path", hlua_http_msg_set_path); |
| 11993 | hlua_class_function(L, "set_query", hlua_http_msg_set_query); |
| 11994 | hlua_class_function(L, "set_uri", hlua_http_msg_set_uri); |
| 11995 | hlua_class_function(L, "set_status", hlua_http_msg_set_status); |
| 11996 | hlua_class_function(L, "is_full", hlua_http_msg_is_full); |
| 11997 | hlua_class_function(L, "may_recv", hlua_http_msg_may_recv); |
| 11998 | hlua_class_function(L, "eom", hlua_http_msg_is_eom); |
| 11999 | hlua_class_function(L, "input", hlua_http_msg_get_in_len); |
| 12000 | hlua_class_function(L, "output", hlua_http_msg_get_out_len); |
| 12001 | |
| 12002 | hlua_class_function(L, "body", hlua_http_msg_get_body); |
| 12003 | hlua_class_function(L, "set", hlua_http_msg_set_data); |
| 12004 | hlua_class_function(L, "remove", hlua_http_msg_del_data); |
| 12005 | hlua_class_function(L, "append", hlua_http_msg_append); |
| 12006 | hlua_class_function(L, "prepend", hlua_http_msg_prepend); |
| 12007 | hlua_class_function(L, "insert", hlua_http_msg_insert_data); |
| 12008 | hlua_class_function(L, "set_eom", hlua_http_msg_set_eom); |
| 12009 | hlua_class_function(L, "unset_eom", hlua_http_msg_unset_eom); |
| 12010 | |
| 12011 | hlua_class_function(L, "send", hlua_http_msg_send); |
| 12012 | hlua_class_function(L, "forward", hlua_http_msg_forward); |
| 12013 | |
| 12014 | lua_rawset(L, -3); |
| 12015 | |
| 12016 | /* Register previous table in the registry with reference and named entry. */ |
| 12017 | class_http_msg_ref = hlua_register_metatable(L, CLASS_HTTP_MSG); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 12018 | |
| 12019 | /* |
| 12020 | * |
| 12021 | * Register class HTTPClient |
| 12022 | * |
| 12023 | */ |
| 12024 | |
| 12025 | /* Create and fill the metatable. */ |
| 12026 | lua_newtable(L); |
| 12027 | lua_pushstring(L, "__index"); |
| 12028 | lua_newtable(L); |
| 12029 | hlua_class_function(L, "get", hlua_httpclient_get); |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 12030 | hlua_class_function(L, "head", hlua_httpclient_head); |
| 12031 | hlua_class_function(L, "put", hlua_httpclient_put); |
| 12032 | hlua_class_function(L, "post", hlua_httpclient_post); |
| 12033 | hlua_class_function(L, "delete", hlua_httpclient_delete); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 12034 | lua_settable(L, -3); /* Sets the __index entry. */ |
William Lallemand | f77f1de | 2021-09-28 19:10:38 +0200 | [diff] [blame] | 12035 | /* Register the garbage collector entry. */ |
| 12036 | lua_pushstring(L, "__gc"); |
| 12037 | lua_pushcclosure(L, hlua_httpclient_gc, 0); |
| 12038 | lua_settable(L, -3); /* Push the last 2 entries in the table at index -3 */ |
| 12039 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 12040 | |
| 12041 | |
| 12042 | class_httpclient_ref = hlua_register_metatable(L, CLASS_HTTPCLIENT); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12043 | /* |
| 12044 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12045 | * Register class AppletTCP |
| 12046 | * |
| 12047 | */ |
| 12048 | |
| 12049 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12050 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12051 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12052 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12053 | lua_pushstring(L, "__index"); |
| 12054 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12055 | |
| 12056 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12057 | hlua_class_function(L, "getline", hlua_applet_tcp_getline); |
| 12058 | hlua_class_function(L, "receive", hlua_applet_tcp_recv); |
| 12059 | hlua_class_function(L, "send", hlua_applet_tcp_send); |
| 12060 | hlua_class_function(L, "set_priv", hlua_applet_tcp_set_priv); |
| 12061 | hlua_class_function(L, "get_priv", hlua_applet_tcp_get_priv); |
| 12062 | hlua_class_function(L, "set_var", hlua_applet_tcp_set_var); |
| 12063 | hlua_class_function(L, "unset_var", hlua_applet_tcp_unset_var); |
| 12064 | hlua_class_function(L, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12065 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12066 | lua_settable(L, -3); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12067 | |
| 12068 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12069 | class_applet_tcp_ref = hlua_register_metatable(L, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12070 | |
| 12071 | /* |
| 12072 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12073 | * Register class AppletHTTP |
| 12074 | * |
| 12075 | */ |
| 12076 | |
| 12077 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12078 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12079 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12080 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12081 | lua_pushstring(L, "__index"); |
| 12082 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12083 | |
| 12084 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12085 | hlua_class_function(L, "set_priv", hlua_applet_http_set_priv); |
| 12086 | hlua_class_function(L, "get_priv", hlua_applet_http_get_priv); |
| 12087 | hlua_class_function(L, "set_var", hlua_applet_http_set_var); |
| 12088 | hlua_class_function(L, "unset_var", hlua_applet_http_unset_var); |
| 12089 | hlua_class_function(L, "get_var", hlua_applet_http_get_var); |
| 12090 | hlua_class_function(L, "getline", hlua_applet_http_getline); |
| 12091 | hlua_class_function(L, "receive", hlua_applet_http_recv); |
| 12092 | hlua_class_function(L, "send", hlua_applet_http_send); |
| 12093 | hlua_class_function(L, "add_header", hlua_applet_http_addheader); |
| 12094 | hlua_class_function(L, "set_status", hlua_applet_http_status); |
| 12095 | hlua_class_function(L, "start_response", hlua_applet_http_start_response); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12096 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12097 | lua_settable(L, -3); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12098 | |
| 12099 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12100 | class_applet_http_ref = hlua_register_metatable(L, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12101 | |
| 12102 | /* |
| 12103 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12104 | * Register class TXN |
| 12105 | * |
| 12106 | */ |
| 12107 | |
| 12108 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12109 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12110 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12111 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12112 | lua_pushstring(L, "__index"); |
| 12113 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12114 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 12115 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12116 | hlua_class_function(L, "set_priv", hlua_set_priv); |
| 12117 | hlua_class_function(L, "get_priv", hlua_get_priv); |
| 12118 | hlua_class_function(L, "set_var", hlua_set_var); |
| 12119 | hlua_class_function(L, "unset_var", hlua_unset_var); |
| 12120 | hlua_class_function(L, "get_var", hlua_get_var); |
| 12121 | hlua_class_function(L, "done", hlua_txn_done); |
| 12122 | hlua_class_function(L, "reply", hlua_txn_reply_new); |
| 12123 | hlua_class_function(L, "set_loglevel", hlua_txn_set_loglevel); |
| 12124 | hlua_class_function(L, "set_tos", hlua_txn_set_tos); |
| 12125 | hlua_class_function(L, "set_mark", hlua_txn_set_mark); |
| 12126 | hlua_class_function(L, "set_priority_class", hlua_txn_set_priority_class); |
| 12127 | hlua_class_function(L, "set_priority_offset", hlua_txn_set_priority_offset); |
| 12128 | hlua_class_function(L, "deflog", hlua_txn_deflog); |
| 12129 | hlua_class_function(L, "log", hlua_txn_log); |
| 12130 | hlua_class_function(L, "Debug", hlua_txn_log_debug); |
| 12131 | hlua_class_function(L, "Info", hlua_txn_log_info); |
| 12132 | hlua_class_function(L, "Warning", hlua_txn_log_warning); |
| 12133 | hlua_class_function(L, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 12134 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12135 | lua_rawset(L, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 12136 | |
| 12137 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12138 | class_txn_ref = hlua_register_metatable(L, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12139 | |
| 12140 | /* |
| 12141 | * |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 12142 | * Register class reply |
| 12143 | * |
| 12144 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12145 | lua_newtable(L); |
| 12146 | lua_pushstring(L, "__index"); |
| 12147 | lua_newtable(L); |
| 12148 | hlua_class_function(L, "set_status", hlua_txn_reply_set_status); |
| 12149 | hlua_class_function(L, "add_header", hlua_txn_reply_add_header); |
| 12150 | hlua_class_function(L, "del_header", hlua_txn_reply_del_header); |
| 12151 | hlua_class_function(L, "set_body", hlua_txn_reply_set_body); |
| 12152 | lua_settable(L, -3); /* Sets the __index entry. */ |
| 12153 | class_txn_reply_ref = luaL_ref(L, LUA_REGISTRYINDEX); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 12154 | |
| 12155 | |
| 12156 | /* |
| 12157 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12158 | * Register class Socket |
| 12159 | * |
| 12160 | */ |
| 12161 | |
| 12162 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12163 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12164 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12165 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12166 | lua_pushstring(L, "__index"); |
| 12167 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12168 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 12169 | #ifdef USE_OPENSSL |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12170 | hlua_class_function(L, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 12171 | #endif |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12172 | hlua_class_function(L, "connect", hlua_socket_connect); |
| 12173 | hlua_class_function(L, "send", hlua_socket_send); |
| 12174 | hlua_class_function(L, "receive", hlua_socket_receive); |
| 12175 | hlua_class_function(L, "close", hlua_socket_close); |
| 12176 | hlua_class_function(L, "getpeername", hlua_socket_getpeername); |
| 12177 | hlua_class_function(L, "getsockname", hlua_socket_getsockname); |
| 12178 | hlua_class_function(L, "setoption", hlua_socket_setoption); |
| 12179 | hlua_class_function(L, "settimeout", hlua_socket_settimeout); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12180 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12181 | 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] | 12182 | |
| 12183 | /* Register the garbage collector entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12184 | lua_pushstring(L, "__gc"); |
| 12185 | lua_pushcclosure(L, hlua_socket_gc, 0); |
| 12186 | 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] | 12187 | |
| 12188 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12189 | class_socket_ref = hlua_register_metatable(L, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12190 | |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 12191 | lua_atpanic(L, hlua_panic_safe); |
| 12192 | |
| 12193 | return L; |
| 12194 | } |
| 12195 | |
| 12196 | void hlua_init(void) { |
| 12197 | int i; |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 12198 | char *errmsg; |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 12199 | #ifdef USE_OPENSSL |
| 12200 | struct srv_kw *kw; |
| 12201 | int tmp_error; |
| 12202 | char *error; |
| 12203 | char *args[] = { /* SSL client configuration. */ |
| 12204 | "ssl", |
| 12205 | "verify", |
| 12206 | "none", |
| 12207 | NULL |
| 12208 | }; |
| 12209 | #endif |
| 12210 | |
| 12211 | /* Init post init function list head */ |
| 12212 | for (i = 0; i < MAX_THREADS + 1; i++) |
| 12213 | LIST_INIT(&hlua_init_functions[i]); |
| 12214 | |
| 12215 | /* Init state for common/shared lua parts */ |
| 12216 | hlua_state_id = 0; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 12217 | ha_set_thread(NULL); |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 12218 | hlua_states[0] = hlua_init_state(0); |
| 12219 | |
| 12220 | /* Init state 1 for thread 0. We have at least one thread. */ |
| 12221 | hlua_state_id = 1; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 12222 | ha_set_thread(NULL); |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 12223 | hlua_states[1] = hlua_init_state(1); |
| 12224 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12225 | /* Proxy and server configuration initialisation. */ |
William Lallemand | 6bb77b9 | 2021-07-28 15:48:16 +0200 | [diff] [blame] | 12226 | 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] | 12227 | if (!socket_proxy) { |
| 12228 | fprintf(stderr, "Lua init: %s\n", errmsg); |
| 12229 | exit(1); |
| 12230 | } |
| 12231 | proxy_preset_defaults(socket_proxy); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12232 | |
| 12233 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12234 | socket_tcp = new_server(socket_proxy); |
| 12235 | if (!socket_tcp) { |
| 12236 | fprintf(stderr, "Lua init: failed to allocate tcp server socket\n"); |
| 12237 | exit(1); |
| 12238 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12239 | |
| 12240 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12241 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12242 | socket_ssl = new_server(socket_proxy); |
| 12243 | if (!socket_ssl) { |
| 12244 | fprintf(stderr, "Lua init: failed to allocate ssl server socket\n"); |
| 12245 | exit(1); |
| 12246 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12247 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12248 | socket_ssl->use_ssl = 1; |
| 12249 | socket_ssl->xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12250 | |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 12251 | for (i = 0; args[i] != NULL; i++) { |
| 12252 | 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] | 12253 | /* |
| 12254 | * |
| 12255 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 12256 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12257 | * features like client certificates and ssl_verify. |
| 12258 | * |
| 12259 | */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12260 | tmp_error = kw->parse(args, &i, socket_proxy, socket_ssl, &error); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12261 | if (tmp_error != 0) { |
| 12262 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 12263 | abort(); /* This must be never arrives because the command line |
| 12264 | not editable by the user. */ |
| 12265 | } |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 12266 | i += kw->skip; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12267 | } |
| 12268 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12269 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 12270 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 12271 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 12272 | |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 12273 | static void hlua_deinit() |
| 12274 | { |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 12275 | int thr; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12276 | struct hlua_reg_filter *reg_flt, *reg_flt_bck; |
| 12277 | |
| 12278 | list_for_each_entry_safe(reg_flt, reg_flt_bck, &referenced_filters, l) |
| 12279 | release_hlua_reg_filter(reg_flt); |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 12280 | |
| 12281 | for (thr = 0; thr < MAX_THREADS+1; thr++) { |
| 12282 | if (hlua_states[thr]) |
| 12283 | lua_close(hlua_states[thr]); |
| 12284 | } |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 12285 | |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 12286 | srv_drop(socket_tcp); |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 12287 | |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 12288 | #ifdef USE_OPENSSL |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 12289 | srv_drop(socket_ssl); |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 12290 | #endif |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 12291 | |
| 12292 | free_proxy(socket_proxy); |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 12293 | } |
| 12294 | |
| 12295 | REGISTER_POST_DEINIT(hlua_deinit); |
| 12296 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 12297 | static void hlua_register_build_options(void) |
| 12298 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 12299 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 12300 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 12301 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 12302 | hap_register_build_opts(ptr, 1); |
| 12303 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 12304 | |
| 12305 | INITCALL0(STG_REGISTER, hlua_register_build_options); |